How-to reboot ESP8266 hw by using Terminal widget?

Hello all!
I could’t find solution for this by browsing thru forum and few Google search.

Question: How-to reboot ESP8266 hw by using Terminal widget?

System in use:
Blynk app: Android
HW: ESP8266-12
Firmware in HW: Blynk “Push data” example. https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=GettingStarted%2FPushData

System status:
At the moment system is up and running, ESP hw is “online (Heartbeat is coming)” and sending temp/hum data to Blynk app. Data can be seen on e.g.Terminal widget.

Combine the code in the Terminal widget example with the ESP8266 Core functions for resetting the ESP.

I’ve done this and it works wonderfully. I look for terminal input being “reboot” and then look at a succeeding password string. If that matches, I execute ESP.reset();

/**************************************************************************************
  TERMINAL INTERACTION SECTION
  Attached to Virtual Pin V9
  Commands supported are help, info, time, upime, wifi, ver, ip, name, reboot, erase
  Commands to change labels of widgets like buttons and timers are also supported.
  Command format is sn "string" for switches/timers.
**************************************************************************************/

BLYNK_WRITE(V9)
{
  int j = 0;
  char command_txt[30];
  char original_txt[40];

  //convert keyed in text to lower case
  strcpy(termtxt, param.asStr());
  for (int i = 0; termtxt[i]; i++) {
    termtxt[i] = tolower(termtxt[i]);
  }

  //Extract command and arguments and trimming the strings
  strcpy(original_txt, termtxt);
  String orig = String(original_txt);
  orig.trim();
  Serial.print("Trimmed original: ");
  Serial.println(orig);
  int spacepos = orig.indexOf(" ");
  String arg = orig.substring(spacepos);
  arg.trim();
  Serial.print("Argument: ");
  Serial.println(arg);

  sscanf(termtxt, "%s", command_txt);
  strcpy(termtxt, command_txt);
  Serial.println(command_txt);
  
  if (String("help") == termtxt) {
    terminal.println("\nCommands are not case sensitive");
    terminal.println("Supported commands: help, info, clear, time, uptime, wifi, ip, name, ver");
    terminal.println("Can change switch/timer names eg. s1 name1");
    j = 1;
    strcpy(prevcmd, termtxt);
    terminal.flush();
  }
  if (String("info") == termtxt)  {
    terminal.print(device_type);
    terminal.println();
    j = 1;
    strcpy(prevcmd, termtxt);
    terminal.flush();
  }
  if (String("time") == termtxt)  {
    terminal.print("\nTime: ");
    rawtime = now();
    ts = localtime ( &rawtime );
    strftime (time_stamp, 80, "%a %I:%M%p %d %b %G.", ts);
    terminal.println(time_stamp);
    Serial.println(time_stamp);
    j = 1;
    strcpy(prevcmd, termtxt);
    terminal.flush();
  }
  if (String("uptime") == termtxt)  {
    int x, days, hours, minutes, months = 0;
    x = (millis() / 1000);
    Serial.println(x);
    days = x / 60 / 60 / 24;
    hours = (x / 60 / 60) % 24;
    minutes = (x / 60) % 60;
    terminal.print("\nUp since ");
    terminal.println(firstconnect_ts);
    terminal.print("Uptime: " );
    terminal.print(days);
    terminal.print("d:");
    terminal.print(hours);
    terminal.print("h:");
    terminal.print(minutes);
    terminal.println("m");
    terminal.flush();    
    j = 1;
    strcpy(prevcmd, termtxt);
    update_uptime();
  }
  if (String("wifi") == termtxt) {
    terminal.print("\nSSID: ");
    terminal.print(WiFi.SSID());
    terminal.print("\nSignal: ");
    terminal.print(WiFi.RSSI());
    terminal.println("dB");
    terminal.flush();
    j = 1;
    strcpy(prevcmd, termtxt);
 
  }
  if (String("ip") == termtxt) {
    terminal.print("\nName: ");
    terminal.println(WiFi.hostname());
    terminal.print("Device ip: ");
    terminal.println(WiFi.localIP());
    j = 1;
    strcpy(prevcmd, termtxt);
    terminal.flush();
  }
  if (String("name") == termtxt)  {
    terminal.print("\nName: ");
    terminal.println(WiFi.hostname());
    j = 1;
    strcpy(prevcmd, termtxt);
    terminal.flush();
  }
  if (String("clear") == termtxt)  {
    for (int i = 0; i <= 50; i++)
    {
      terminal.println("");     // "clear screen" in app.
    }
    j = 1;
    strcpy(prevcmd, termtxt);
    terminal.flush();
  }

  //Till the reboot commands, the next set of if statements will be used to 
  //widget labels to suit the customer

  if (String("s1") == termtxt) {
    if (arg != "") {
      Blynk.setProperty(V1, "label", arg);
      Blynk.setProperty(V11, "label", (arg + " timer"));
      terminal.println("Changed name for s1");
    }
    else {
      terminal.println("No name given");
    }
    j=1;
    strcpy(prevcmd, termtxt);
    terminal.flush();
  }

  if (String("s2") == termtxt) {
    if (arg != "") {
      Blynk.setProperty(V2, "label", arg);
      Blynk.setProperty(V12, "label", (arg + " timer"));
      terminal.println("Changed name for s2");
    }
    else {
      terminal.println("No name given");
    }
    j=1;
    strcpy(prevcmd, termtxt);
    terminal.flush();
  }
  
  if (String("s3") == termtxt) {
    if (arg != "") {
      Blynk.setProperty(V3, "label", arg);
      Blynk.setProperty(V13, "label", (arg + " timer"));
      terminal.println("Changed name for s3");
    }
    else {
      terminal.println("No name given");
    }
    j=1;
    strcpy(prevcmd, termtxt);
    terminal.flush();
  }
  
  if (String("s4") == termtxt) {
    if (arg != "") {
      Blynk.setProperty(V4, "label", arg);
      Blynk.setProperty(V14, "label", (arg + " timer"));
      terminal.println("Changed name for s4");
    }
    else {
      terminal.println("No name given");
    }
    j=1;
    strcpy(prevcmd, termtxt);
    terminal.flush();
  }

// end of label definition section
  
  if (String("reboot") == termtxt)  {
    terminal.println("\npassword:");
    j = 1;
    strcpy(prevcmd, termtxt);
    terminal.flush();
  }
  if (String("erase") == termtxt)  {
    terminal.println("\npassword:");
    j = 1;
    strcpy(prevcmd, termtxt);
    terminal.flush();
  }
  if ((String("mohan") == termtxt) && (String("reboot") == prevcmd))  {
    ESP.reset();
  }
  if ((String("maple") == termtxt) && (String("erase") == prevcmd))  {
    wifiManager.resetSettings();
    ESP.reset();
  }
  if (String("ver") == termtxt)  {
    terminal.print("\nVer: ");
    terminal.println(firmware_ver_date);
    j = 1;
    strcpy(prevcmd, termtxt);
    terminal.flush();
  } else {
    if (!j) {
      terminal.println("\nInvalid command");
      terminal.flush();
    }
  }
  Serial.print("prevcmd: ");
  Serial.println(prevcmd);
  terminal.flush();

  //exit point of BLYNK_WRITE(V9) Terminal section
}
5 Likes

good job !!
:wink:

Make sure you read this :slight_smile:

Significant difference. Thank you of reminder @Fettkeewl.

ESP.reset don’t work well, sometime yes, sometime no
no more esp.restart
better reset with rset pin
:wink:

That’s normally down to user error.
Reset will only work reliably if you manually reset the ESP after each local flash.

it’s what I said Costas lol !
:joy::joy::joy::joy:
I know that we have to reset after each local flash.
there is a documentation about this issue.
so better use rst PIN

1 Like

nice terminal template, @mohan_sundaram.

Thank you. Feels good receiving such feedback being a newbie with Blynk and programming.

On a dev board that I had, ESP.reset() and ESP.restart() both resulted in boot mode(1,7) and stopped going further. I did some digging around, pulled GPIO 0 and 2 high and then gave the ESP.restart() command. Started working fine after that going to boot mode (3,7). I’m sure gurus here already know this but did not seem to find this in any conversation. Thus, documented this.

What is MDB0 on a ESP board and how does one set that? Need to set this to LOW apart from the earlier two for restart to be perfect, from what I read.

Which dev boards are you using?
The popular WeMos D1 Mini never fails to reboot via code as long as you manually reset it after every local flash.

This is a noname board. Will buy Wemos in future. My other boards with 4 relays boot just fine whether it is ESP.reset or ESP.restart.

Edit: The dev board says LoLin

my nodemcu is a LoLin V3
it works fine with ESPreset and ESPrestart.