Why watchdog not working with MySensors?



  • Hello,

    I faced with the same issue like https://forum.mysensors.org/topic/9946/watchdog-not-watchdogging/1
    MySensor ver 2.3.1
    Optiboot ver 8.0
    atmega328, 8MHz
    I have tested two codes on the one board and the result is bellow.
    This test code works well. Watchdog is working and Atmega is rebooting.

    #include <Arduino.h>
    #include <avr/wdt.h>
      
    #define STATUS_LED_PIN        A2
    
    void setup() {
      wdt_disable();
      Serial.begin(9600);
      Serial.println("Setup..");
      pinMode(STATUS_LED_PIN, OUTPUT);
      digitalWrite(STATUS_LED_PIN, 0);
      
      Serial.println("Wait 5 sec..");
      delay(5000);
      wdt_enable (WDTO_8S);
      Serial.println("Watchdog enabled.");
    }
    
    int timer = 0;
    
    void loop(){
      if(!(millis()%1000)){
        timer++;
        Serial.println(timer);
        digitalWrite(STATUS_LED_PIN, digitalRead(STATUS_LED_PIN)==1?0:1); delay(1);
      }
      //wdt_reset();
    }
    

    But this code is not working. Watchdog is not working.

    #include <Arduino.h>
    #include <avr/wdt.h>
      
    // RFM95
      #define MY_RADIO_RFM95
      //#define MY_DEBUG_VERBOSE_RFM95
      #define MY_RFM95_FREQUENCY (RFM95_868MHZ)
      #define MY_RFM95_MODEM_CONFIGRUATION RFM95_BW125CR45SF128
    
      #define MY_NODE_ID 10
      #define MY_PARENT_NODE_ID 0
      #define MY_PARENT_NODE_IS_STATIC
    
    #include <MySensors.h>
    
    #define STATUS_LED_PIN        A2
    
    void setup() {
      wdt_disable();
      Serial.begin(9600);
      Serial.println("Setup..");
      pinMode(STATUS_LED_PIN, OUTPUT);
      digitalWrite(STATUS_LED_PIN, 0);
      
      Serial.println("Wait 5 sec..");
      delay(5000);
      wdt_enable (WDTO_8S);
      Serial.println("Watchdog enabled.");
    }
    
    int timer = 0;
    
    void loop(){
      if(!(millis()%1000)){
        timer++;
        Serial.println(timer);
        digitalWrite(STATUS_LED_PIN, digitalRead(STATUS_LED_PIN)==1?0:1); delay(1);
      }
      //wdt_reset();
    }
    

    What I am doing wrong?


  • Mod

    @dmytro-zadvornov MySensors will automatically reset the watchdog every time loop is exited.



  • @mfalkvidd Thx for you answer.
    If I understand you right MySensor has implicit call wdt_reset()?
    And in this case this new code will reboot.

    #include <Arduino.h>
    #include <avr/wdt.h>
      
    // RFM95
      #define MY_RADIO_RFM95
      //#define MY_DEBUG_VERBOSE_RFM95
      #define MY_RFM95_FREQUENCY (RFM95_868MHZ)
      #define MY_RFM95_MODEM_CONFIGRUATION RFM95_BW125CR45SF128
    
      #define MY_NODE_ID 10
      #define MY_PARENT_NODE_ID 0
      #define MY_PARENT_NODE_IS_STATIC
    
    #include <MySensors.h>
    
    #define STATUS_LED_PIN        A2
    
    void setup() {
      wdt_disable();
      Serial.begin(9600);
      Serial.println("Setup..");
      pinMode(STATUS_LED_PIN, OUTPUT);
      digitalWrite(STATUS_LED_PIN, 0);
      
      Serial.println("Wait 5 sec..");
      delay(5000);
      wdt_enable (WDTO_8S);
      Serial.println("Watchdog enabled.");
    }
    
    int timer = 0;
    
    void loop(){
      if(!(millis()%1000)){
        timer++;
        Serial.println(timer);
        digitalWrite(STATUS_LED_PIN, digitalRead(STATUS_LED_PIN)==1?0:1); delay(1);
      }
      delay(20000);
      //wdt_reset();
    }
    

    I will try it later


  • Mod

    @dmytro-zadvornov delay will also reset the watchdog



  • Hi,
    I have tried last code, and it works - the watchdog reboot MC. As expected.

    @mfalkvidd Why you think delay() should reset the watchdog?


  • Mod

    @dmytro-zadvornov thanks for reporting back.

    Sorry for my mistake. I thought most platforms called yield() inside delay(), and yield() usually resets the watchdog. Example https://github.com/arduino/ArduinoCore-avr/blob/b7c607663fecc232e598f2c0acf419ceb0b7078c/cores/arduino/wiring.c#L106
    But since your test shows that the watchdog is tripped, the watchdog is not reset inside delay().

    https://github.com/arduino/ArduinoCore-avr/blob/b7c607663fecc232e598f2c0acf419ceb0b7078c/cores/arduino/Arduino.h#L38 shows that avr does not reset watchdog in yield().


Log in to reply
 

Suggested Topics

  • 1
  • 1
  • 2
  • 2
  • 10
  • 2

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts