0

这是我的代码,它在距离大于 150 时实际发送 blynk 通知。但它的输出非常奇怪。它一次又一次地运行设置功能,而不是只运行一次。由于此代码,esp8266 设备上线和下线,然后再次上线。你能告诉我为什么它一次又一次地运行设置函数以及为什么它打印堆栈......<<在输出而不是serial.print()函数。我不知道为什么?我是布林克的新手。

int trigPin = D4;
int echoPin = D5;
int led=D6;
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>
char ssid[]="EVO-Charji-0A7E";
char pass[]="Kv4hFDEA";

int count=0;


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "1CfQ53wXPctk_gnrk_-ItbTWSgycPHq5";

BlynkTimer timer;

void notifyUptime(){

  long uptime = millis() / 60000L;
  //millis() is a fuction which returns the number of seconds the time has elapsed after the start of the device
  Serial.println("Running for 5 times");
  long duration, distance;
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration=pulseIn(echoPin, HIGH);
  distance =(duration/2)/29.1;
  Serial.print(distance);
  Serial.println("CM");
  delay(10);

 if((distance<=150)) 
  {digitalWrite(led,LOW);
  Serial.println("enter");
    count=0;   unsigned long startTime=millis();
      unsigned long endTime=startTime;
      while((endTime-startTime) <=60000){
        if (distance>150){
          Blynk.notify("  Danger  \nGerms Alert!!!");
           Serial.println("leave");
          digitalWrite(led,HIGH);
          delay(1000);
          digitalWrite(led,LOW);
          delay(1000);
          digitalWrite(led,HIGH);
          count=count+1;
          break;
          }
        endTime=millis();
        }
        if (count==0){
          Blynk.notify("Successfully Cleaned the Hands.");
           Serial.println("success");
          digitalWrite(led,HIGH);
          }        
}
   else if(distance>150)
 {
  Serial.println("No man");  
   }
  //Blynk.notify(String("Running for ") + uptime + " minutes.");
  //   only one notification per 15 seconds is allowed. 
}
void setup(){
Serial.begin(9600);
WiFi.mode(WIFI_OFF);
  delay(1000);
  // This mode allows NodeMCU to connect any WiFi directly.
  WiFi.mode(WIFI_STA);        
  // Connect NodeMCU to your WiFi.
  WiFi.begin(ssid, pass);
    pinMode(trigPin, OUTPUT);
   pinMode(led,OUTPUT);
  pinMode(echoPin, INPUT);

  Serial.print("\n\n");
  Serial.print("Try to connect to WiFi. Please wait! ");
  Serial.print("\n\n");
  // Halt the code until connected to WiFi.
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("*");
  }

  // If connection is successful, turn control led(if used) on and write WiFi SSID to serial monitor along with assigned IPAddress.
  Serial.print("\n\n");
  Serial.print("-------------------------------------");
  Serial.print("\n\n");
  Serial.print("Connection is successful!");
  Serial.print("\n\n");
  Serial.print("Connected WiFi SSID : ");
  Serial.print(ssid);
  Serial.print("\n\n");
  Serial.println("Connected IPAddress : ");
  Serial.println(WiFi.localIP());
  Serial.print("\n\n"); 
Blynk.begin(auth, ssid, pass);

Serial.println("Device statred");
//Blynk.notify("Device started");
   // Setup a function to be called every minute
  timer.setInterval(10000L,notifyUptime);
  }

void loop(){
  Blynk.run();
    timer.run();
    }

代码的输出如下所示:

Try to connect to WiFi. Please wait! 

**********

-------------------------------------

Connection is successful!

Connected WiFi SSID : EVO-Charji-0A7E

Connected IPAddress : 
192.168.1.108


[6243] Connecting to EVO-Charji-0A7E
[6283] Connected to WiFi
[6310] IP: 192.168.1.108
[6337] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on NodeMCU

[6492] Connecting to blynk-cloud.com:80
[7572] Ready (ping: 482ms).
Device statred
Running for 5 times
0CM
enter

Soft WDT reset

>>>stack>>>

ctx: cont
sp: 3ffffd40 end: 3fffffc0 offset: 01b0
3ffffef0:  00000000 00000000 00418937 00000000  
3fffff00:  402039e8 3ffeec9c 40100259 4bc6a7f0  
3fffff10:  00000000 3ffeec9c 3ffe886e 40203cc9  
3fffff20:  0056faef 00000005 3ffeec9c 40203d90  
3fffff30:  3ffee9c8 00000000 3ffeec9c 00000000  
3fffff40:  000044f1 3ffeec0c 3ffeec9c 402010c2  
3fffff50:  3ffee9c8 00000000 3ffee9c8 4020394c  
3fffff60:  00001dd7 00002710 000044e7 00000000  
3fffff70:  00000001 00000002 3ffeec9c 40202085  
3fffff80:  00000000 00000000 00000001 3ffeedc8  
3fffff90:  3fffdad0 00000000 3ffeed88 402020c1  
3fffffa0:  3fffdad0 00000000 3ffeed88 40204668  
3fffffb0:  feefeffe feefeffe 3ffe8528 40100cad  
<<<stack<<<
H!⸮ԅp⸮D⸮⸮⸮

Try to connect to WiFi. Please wait! 

**********

-------------------------------------

Connection is successful!

Connected WiFi SSID : EVO-Charji-0A7E

Connected IPAddress : 
192.168.1.108


[6244] Connecting to EVO-Charji-0A7E
[6284] Connected to WiFi
[6311] IP: 192.168.1.108
[6338] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on NodeMCU

[6494] Connecting to blynk-cloud.com:80
[7381] Ready (ping: 478ms).
Device statred

.......它会一次又一次地继续打印。任何帮助将不胜感激。

4

1 回答 1

0

我现在有点晚了,但我希望你还能读到这篇文章。

您的问题很简单,您尝试多次连接到 WiFi。设置函数应如下所示:

void setup() {
  Serial.begin(9600);

  pinMode(trigPin, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(echoPin, INPUT);


  Blynk.begin(auth, ssid, pass);

  Serial.println("Device statred");
  //Blynk.notify("Device started");
  // Setup a function to be called every minute
  timer.setInterval(10000L, notifyUptime);
}
于 2020-04-15T09:26:38.227 回答