ESP32 - Periodic autoreset possible ? (Solved #1)

I have a simple code that posts my cabin temperature to the Blynk app. All working fine. But then suddenly for no obvious reason the ESP32 hangs and stops posting. A simple reset gets it going again. I am using the Adafruit ESP32 Feather board. Not very lucky when I tried to make it Sleep between posts ... you can see that part of the code commented out. In fact in a case like this where you post to the Blynk cloud, I am not sure how to implement Deep Sleep.

So the next best is to ensure the ESP32 resets itself once every hour or so. Any ideas to make this code work more reliably ??

#include "RunningAverage.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#define ADC_Ch3 39

int adcRawBits = 0;
int adcMeanBits = 0;
int raSampleSize = 50;
double DegC ; 
unsigned long adcRdIntvl = 10;
unsigned long adcRdIntvl_ms = millis();
unsigned long adcDispIntvl = 500;
unsigned long adcDispIntvl_ms = millis();
byte thirteenLED  = 13;
bool ledState;

// Use below if you want to enter deep sleep. Not working though !! 
//#define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */
//#define TIME_TO_SLEEP  10        /* Time ESP32 will go to sleep (in seconds) */

RunningAverage ra_ChA0(raSampleSize);

char auth[] = "1234";
char ssid[] = "ssid";
char pass[] = "password";

// Use Virtual pin 0 for temperature value
#define ambientTemp V0
BlynkTimer timer;

//#################################

void setup()
{
  Serial.begin(115200);
  delay(1000);
  Serial.println("ESP32 LM35 Read and Post to Blynk.");

  Blynk.begin(auth, ssid, pass);

  pinMode( thirteenLED, OUTPUT);
  digitalWrite(thirteenLED, HIGH);
  delay(1000);
  digitalWrite(thirteenLED, LOW);

  // Setup a function to be called once every 10 second
  timer.setInterval(10000L, myTimerEvent);
}

//#################################

void loop()
{
  readAdcPin();                   // Executes once every 10ms
  showAdcValue();                 // Executes once every 500ms
  Blynk.run();                    // Posts the value to cloud on demand.
  timer.run();                    // Fire the timer event at the set interval 
}

//#################################

void readAdcPin()
{
  if (millis() - adcRdIntvl_ms > adcRdIntvl)
  {
    adcRdIntvl_ms = millis();
    adcRawBits = analogRead(39);
    ra_ChA0.addValue(adcRawBits);
    adcMeanBits = ra_ChA0.getAverage();
  }
}

//*********************************

void showAdcValue()
{
  if (millis() - adcDispIntvl_ms > adcDispIntvl)
  {
    adcDispIntvl_ms = millis();
    Serial.println(adcMeanBits);
    DegC = getVoltage(adcMeanBits)*100;
    Serial.println(DegC, 2); 
    if (ledState ) ledState = LOW;
    else ledState = HIGH;
    digitalWrite( thirteenLED, ledState);      // I am alive !!
  }
}

//*********************************

double getVoltage(int count)
{
  double reading = count;          // Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
  if(reading < 1 || reading > 4095) return 0;
  return -0.000000000000016 * pow(reading,4) + 0.000000000118171 * pow(reading,3)- 0.000000301211691 * pow(reading,2)+ 0.001109019271794 * reading + 0.034143524634089;
} 

//*********************************

// This function tells ESP32 to write
// the Temperature value to V5 pin

void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(ambientTemp, DegC);
  digitalWrite(thirteenLED, LOW);
  //esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  //esp_deep_sleep_start();
}

You can use a watchdog timer for that purpose - arduino-esp32/WatchdogTimer.ino at master · espressif/arduino-esp32 · GitHub

Another solution - buy an outlet mechanical or electronic timer for your ESP power supply.

If you want to reset the system you can just use

    esp_restart();
1 Like

alesam:
You can use a watchdog timer for that purpose - arduino-esp32/libraries/ESP32/examples/Timer/WatchdogTimer/WatchdogTimer.ino at master · espressif/arduino-esp32 · GitHub

Another solution - buy an outlet mechanical or electronic timer for your ESP power supply.

Both solutions are practical with the Watch Dog being more elegant. The second one is simple and suitable for many such devices which need to remain on power for ever but have to be reset …

Thanks

@Whandall

The esp_restart() is a good option to use.

And while on this landed on this YouTube link which nicely explains the WDT method. Good one . Except that it does not mention what the maximum time that is possible to enter as the time before the dog bites.