Xinyuan-LilyGO / T-Display-S3

MIT License
731 stars 173 forks source link

How to properly setup a watchdog for the board LilyGo Soldered pin S3, no touch screen? #209

Closed igoralves1 closed 3 months ago

igoralves1 commented 6 months ago

I have a project for watering plants using a LilyGo Soldered pin S3 board no touch screen.

The board should stay on forever. Every hr it should turn a water pump on. Every minute it sends a mqtt message to a public broker.hivemq.com:1883 broker. The project works well.

The problem is that every some Hrs the board suddenly stops working. It does not turn on the water pump and it does not send the mqtt message.

Doing a google I found that this is a recurring problems with device boards, specially ESP32. There is a solution for that which is called watchdog . This is a kind of application that reboot the processor in each x time (seconds, minutes or hrs).

I found this solution for ESP32 which is not working , maybe because it is not for ESP32-S3-> https://iotassistant.io/esp32/enable-hardware-watchdog-timer-esp32-arduino-ide/

This is the code that I have

#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include "WiFi.h"
#include <PubSubClient.h>
#include <esp_task_wdt.h>

const char* ssid = "Frontier5664";
const char* password = "0546758114";
const char* mqttServer = "broker.hivemq.com";
const int mqttPort = 1883;

#define HIVEMQ_IOT_PUBLISH_TOPIC   "esp32/pub"
#define HIVEMQ_IOT_SUBSCRIBE_TOPIC "esp32/sub"

//3 seconds WDT
#define WDT_TIMEOUT 3

WiFiClient espClient;
PubSubClient client(espClient);

void connectHIVEMQ(){
  Serial.println("connectHIVEMQ Fn");

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  Serial.println("Connecting to Wi-Fi");

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  // Connect to the MQTT broker on the AWS endpoint we defined earlier
  client.setServer(mqttServer, mqttPort);

  // Create a message handler
  client.setCallback(messageHandler);

  Serial.println("Connecting to HIVEMQ IOT");

  if (!client.connected())
  {
    Serial.println("HIVEMQ Timeout!");
    return;
  }

  // Subscribe to a topic
  client.subscribe(HIVEMQ_IOT_SUBSCRIBE_TOPIC);

  Serial.println("HIVEMQ Connected!");
}

void publishMessage(char* status, int seconds){
  StaticJsonDocument<200> doc;
  doc["status"] = status;
  doc["time(s)"] = seconds;
  char jsonBuffer[512];
  serializeJson(doc, jsonBuffer);
  client.publish(HIVEMQ_IOT_PUBLISH_TOPIC, jsonBuffer);
}

void messageHandler(char* topic, byte* payload, unsigned int length){
  Serial.print("incoming: ");
  Serial.println(topic);

  StaticJsonDocument<200> doc;
  deserializeJson(doc, payload);
  const char* message = doc["message"];
  Serial.println(message);
}

const int relay4 = 16;

void setup() {
  Serial.begin(115200);
  Serial.println("Setup()");

  esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
  esp_task_wdt_add(NULL); //add current thread to WDT watch

  connectHIVEMQ();
  pinMode(relay4, OUTPUT);
}

int j = 0;
int last = millis();

void loop() {

  // resetting WDT every 2s, 5 times only
  if (millis() - last >= 2000 && j < 5) {
      Serial.println("Resetting WDT...");
      publishMessage("Resetting WDT...", j); 
      esp_task_wdt_reset();
      last = millis();
      j++;
      if (j == 5) {
        Serial.println("Stopping WDT reset. CPU should reboot in 3s");
        publishMessage("Stopping WDT reset. CPU should reboot in 3s", j);
      }
  }

  Serial.println("Desliga o motor");
  digitalWrite(relay4, HIGH);
  publishMessage("Motor Desligado", 0);  
  client.loop();
  for(int i = 1; i<=3600; i++){ //Set i<=28800 in PROD i<=10 in DEV
    Serial.println(i);
    if ( (i%60)  == 0 ){
      Serial.println("+ 1 Min");  
      publishMessage("+ 1 Min", i);
      client.loop();      
    }
    client.loop(); 
    delay(1);
  }

  Serial.println("Liga o motor");
  digitalWrite(relay4, LOW);
  for(int i = 1; i<=15; i++){
    Serial.println(i);

    publishMessage("Motor Ligado", i);  
    client.loop();    

    delay(1000);
  }

  Serial.println("....");
}

And this is the output that I am getting

ELF file SHA256: 18b5035848021cdb

Rebooting...
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0xc (RTC_SW_CPU_RST),boot:0xa (SPI_FAST_FLASH_BOOT)
Saved PC:0x40376e80
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbd8
load:0x403cc700,len:0x2a80
entry 0x403c98d0

That means the watchdog code is breaking the process.

Does someone knows what is the issue here? How to fix that?

github-actions[bot] commented 3 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 3 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.