Whenever i input something from blynk terminal, esp8266 reboots

Hi. I’m building an automated irrigation system for rice field.
whenever i input something from blynk terminal, esp8266 reboots. Sometimes it work, but most of the time it shows this in serial monitor.

rst cause:2, boot mode:(3,6) 
17:23:26.404 -> 
17:23:26.404 -> load 0x4010f000, len 3424, room 16 
17:23:26.404 -> tail 0 
17:23:26.404 -> chksum 0x2e 
17:23:26.404 -> load 0x3fff20b8, len 40, room 8 
17:23:26.404 -> tail 0 
17:23:26.404 -> chksum 0x2b 
17:23:26.404 -> csum 0x2b 
17:23:26.404 -> v00076700 
17:23:26.404 -> ~ld

The code to handle the terminal input

BLYNK_WRITE_DEFAULT() {
  String command = param.asStr();

  if (command.startsWith("s1c")) {
    bs1c = command.substring(4).toFloat();
    Blynk.virtualWrite(V6, "s1c is changed");
  } else if (command.startsWith("s2c")) {
    bs2c = command.substring(4).toFloat();
    Blynk.virtualWrite(V6, "s2c is changed");
  } else if (command.startsWith("s3c")) {
    bs3c = command.substring(4).toFloat();
    Blynk.virtualWrite(V6, "s3c is changed");
  } else if (command.startsWith("s4c")) {
    bs4c = command.substring(4).toFloat();
    Blynk.virtualWrite(V6, "s4c is changed");
  } else if (command.startsWith("gl")) {
    bgroundLevel = command.substring(3).toInt();
    Blynk.virtualWrite(V6, "groundLevel is changed");
  } else if (command.startsWith("m1l")) {
    bm1low = command.substring(4).toFloat();
    Blynk.virtualWrite(V6, "mode 1 low threshold is changed");
  } else if (command.startsWith("m1h")) {
    bm1high = command.substring(4).toFloat();
    Blynk.virtualWrite(V6, "mode 1 high threshold is changed");
  } else if (command.startsWith("m2t")) {
    bm2t = command.substring(4).toFloat();
    Blynk.virtualWrite(V6, "mode 2 threshold is changed");
  } else if (command.startsWith("prefc")) {
    preferences.begin("irrigation", false);
    preferences.clear();
    preferences.end();
    Blynk.virtualWrite(V6, "pref is cleared");
  } else if (command.equals("reboot")) {
    Blynk.virtualWrite(V6, "Restarting ESP8266...");
    //delay(1000);
    ESP.restart();
  } else if (command.startsWith("idate")) {
    String newDate = command.substring(6);
    if (isValidDateFormat(newDate)) {
      binitialDate = newDate;
      GetDateTime(initialDate);
      savePreferences1();
      Blynk.virtualWrite(V6, "initialDate is changed");
    } else {
      Blynk.virtualWrite(V6, "Invalid date format. Please use dd/mm/yyyy.");
    }
  } else if (command.startsWith("lidate")) {
    String newDate2 = command.substring(7);
    if (isValidDateFormat(newDate2)) {
      int liday = newDate2.substring(0, 2).toInt();
      int limonth = newDate2.substring(3, 5).toInt()-1;
      int liyear = newDate2.substring(6).toInt()-1900;

      Serial.print("Last irrigation day: ");
      Serial.println(liday);
      Serial.print("Last irrigation month: ");
      Serial.println(limonth);
      Serial.print("Last irrigation year: ");
      Serial.println(liyear);

      preferences.begin("irrigation", false);
      preferences.putInt("lastIrriagationDay", liday);
      preferences.putInt("lastIrriagationMon", limonth);
      preferences.putInt("lastIrriagationYear", liyear);
      preferences.end();

      Blynk.virtualWrite(V6, "last irrigation Date is changed");
    } else {
      Blynk.virtualWrite(V6, "Invalid date format. Please use dd/mm/yyyy.");
    }
  } else {
    // Handle commands received from the Blynk Terminal widget
    // Add your custom commands handling here
    Blynk.virtualWrite(V6, "Not a command: " + command);
  }

  savePreferences1();
}

When it fails, it doesn’t even show the value changed confirmation text in the terminal, showing that the error happens before that.
the full code is available here if anyone is interested Code - PasteCode.dev

You might want to redact your Google key from that code.

Pete.

Looking at your full code there’s lots of weird and wonderful stuff happening there, including having three instances of BlynkTimer when only one is needed, use of NTP instead of Blynk RTC, using millis comparison and manually managing WDT, and the unnecessary use of ‘BLYNK_WRITE_DEFAULT()’ to mention a few.

I’d suggest adding some serial print statements that show your free heap memory, because it wouldn’t surprise me if it was a low memory issue causing the issue.

Pete.

Thank you, Pete. I’ve edited the link.

About the WDT, I was trying to figure out if manually handling the watchdog timer fixes the issue or not. Well, it doesn’t change anything.

About the NTP, i need the current time at the location the machine is stationed. And after any kind of power outage, i would still need the current time. Based on the initial transplanted date and current date, I’d set some conditions on irrigation. Is there any easier approach to this?

About 3 timers, one is for updating data to Google sheets, one is for blynk and the other is my main program loop. And i might set 3 different intervals for these three. Should i manage this without using 3 timers?

Thank you very much. During this project, your comments on the community helped me.

Yes, using Blynk’s RTC functionality, as I said earlier….

This approach reduces the number of libraries that are needed, freeing-up memory.

Each BlynkTimer object can handle up to 16 timers. You’re using three, which is using more memory than needed…

Pete.

Dear Pete, i used RTC clock and a single timer. Here is the new code

Still, when i input anything, even not a command, the esp reboots. Now it shows this

03:21:34.386 -> Stack smashing detected.
03:21:34.386 -> 
03:21:34.386 -> Exception (5):
03:21:34.419 -> epc1=0x4010016f epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

Thanks

I have no idea where you found the “RTC fix” code from, but it’s not the correct way to use RTC with Blynk IoT.
The correct method is shown in the link I provided.

Pete.

okay. i’m looking into it. Sorry for my messy code. I’m new here. Trying to learn. Thanks again
Found the RTC code in blynk library example

I gave you a direct link to the RTC documentation. Why not use that?

Pete.

I have a question, after i sync time with my esp RTC, how do i access it? I kinda need the current date in string dd/mm/yyyy format for further calculations. Thanks.

You can do it 8n a very similar way to how you were doing it in your original code.
The Blynk RTC method gives you the current time in Unix format, in just the same way that NTP does.

Pete.