簡體   English   中英

使用速度傳感器時 ESP8266 上的軟 wdt 復位

[英]Soft wdt reset on ESP8266 when using Speed Sensor

我在我的個人項目中創建一個 esp8266 模塊,它可以從車輛讀取多個傳感器,並且可以通過 mqtt 發送數據。 所以我寫了代碼,我嘗試讀取速度傳感器,它正在重置 wdt 並再次重置 esp8266

所以這是我的代碼

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  lcd.init();
  lcd.backlight();
  pinMode(speedSensor,INPUT_PULLUP);

  // attempt to connect to WiFi network:
  Serial.print("Attempting to connect to WPA SSID: ");
  Serial.println(ssid);
  
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    lcd.setCursor(0,0);
    lcd.print("Connecting to");
    lcd.setCursor(0,1);
    lcd.print("the network");
    Serial.print(".");
    delay(5000);
  }
  lcd.clear();

  Serial.println("You're connected to the network");
  Serial.println();

  lcd.setCursor(0,0);
  lcd.print("Connected to");
  lcd.setCursor(0,1);
  lcd.print("the network");
  delay(2000);
  lcd.clear();

  // attempt to connect to the MQTT broker:
  Serial.print("Attempting to connect to the MQTT broker: ");
  Serial.println(broker);

  if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1);
  }

  Serial.println("You're connected to the MQTT broker!");
  Serial.println();

  // subscribe to the topic and set callback function
  mqttClient.onMessage(SwitchMotor);
  mqttClient.subscribe(topicSwitchMotor);

  lcd.setCursor(0,0);
  lcd.print("Connected to");
  lcd.setCursor(0,1);
  lcd.print("the MQTT broker");
  delay(2000);
  lcd.clear();
  
  lcd.setCursor(0,0);
  lcd.print(" Fuel :");
  lcd.setCursor(0,1);
  lcd.print(" RPM  :");

  SPI.begin();
  rfid.PCD_Init();
  Serial.println("I am waiting for card...");
  pinMode(pinRelay, OUTPUT);
  digitalWrite(pinRelay, HIGH);
}

void loop() {

  mqttClient.poll(); // check for incoming messages

  FuelSensor(); // Fuel Sensor
  SpeedMotor(); // Speed Sensor
  StartMotor(); // Start Motor with RFID
}

void SpeedMotor(){
  start_time=millis();
  end_time=start_time+1000;
  
  while(millis()<end_time){
    yield();
      if(digitalRead(speedSensor)){
          steps=steps+1; 
          while(digitalRead(speedSensor));
      }
  }

  // calculate the speed
  temp=steps-steps_old;
  steps_old=steps;
  rps=(temp/20);
  rpm=(rps*60);
  lcd.setCursor(9,1);
  lcd.print(rpm);
  lcd.print("   ");

  // publish the message
  mqttClient.beginMessage(topicSpeedRpm);
  mqttClient.print(rpm);
  mqttClient.endMessage();

  mqttClient.beginMessage(topicSpeedRps);
  mqttClient.print(rps);
  mqttClient.endMessage();
}

這是來自串行監視器的 output

Attempting to connect to WPA SSID: cieciecie
...You're connected to the network

Attempting to connect to the MQTT broker: xx.xxx.xxx.xx
You're connected to the MQTT broker!

I am waiting for card...

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Soft WDT reset

>>>stack>>>

ctx: cont
sp: 3ffffde0 end: 3fffffc0 offset: 01a0
3fffff80:  3fffdad0 3ffee7bc 3ffee7c0 40201143  
3fffff90:  3fffdad0 00000000 3ffeeaa8 402016e9  
3fffffa0:  3fffdad0 00000000 3ffeeaa8 40205d5c  
3fffffb0:  feefeffe feefeffe 3ffe8600 40100e61  
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------
⸮⸮Attempting to connect to WPA SSID: cieciecie
...You're connected to the network

Attempting to connect to the MQTT broker: xx.xxx.xxx.xx
You're connected to the MQTT broker!

I am waiting for card...

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Soft WDT reset

它會一次又一次地軟 wdt 重置

我已經嘗試過 Stack ESP execption 解碼器,它給出了這個結果ESP Exception 解碼器結果

結果在 speedMotor() function 處提到了第 152 行

void SpeedMotor(){
  start_time=millis();
  end_time=start_time+1000;
  
  while(millis()<end_time){
    yield(); // already use yield(); on while() loop
      if(digitalRead(speedSensor)){ // line 152
          steps=steps+1; 
          while(digitalRead(speedSensor));
      }
  }

  // calculate the speed
  temp=steps-steps_old;
  steps_old=steps;
  rps=(temp/20);
  rpm=(rps*60);
  lcd.setCursor(9,1);
  lcd.print(rpm);
  lcd.print("   ");

  // publish the message
  mqttClient.beginMessage(topicSpeedRpm);
  mqttClient.print(rpm);
  mqttClient.endMessage();

  mqttClient.beginMessage(topicSpeedRps);
  mqttClient.print(rps);
  mqttClient.endMessage();
}

我也已經從 arduino 論壇https://forum.arduino.cc/t/solved-esp8266-millis-crash/636543嘗試過,它說嘗試使用yield(); while()循環中,但它仍然給出了軟 wdt rest

我想清楚地回答如何使用yield(); function 正確所以沒有更多的軟 wdt 再次重置

根據評論,問題出在這部分代碼中:

 while(millis()<end_time){
    yield(); // already use yield(); on while() loop
      if(digitalRead(speedSensor)){ // line 152
          steps=steps+1; 
          while(digitalRead(speedSensor));
      }
  }

問題是,如果digitalRead(speedSensor)返回 true ,則循環運行直到digitalRead(speedSensor)返回false 這可能會運行足夠長的時間來觸發看門狗。

while(digitalRead(speedSensor));

解決這個問題的一種方法是在循環內yield ,即:

while(digitalRead(speedSensor)) {
     yield(); 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM