NodeMCU Restart possible?

Hi
I need to be able to restart my nodemcu as my internet link drops connection, is there anyway to detect the connection is actually lost to the mydevices servers, my wifi connection stays up but the connection to the external world drops and if i restart the nodemcu, all good… I see in the debug window that COnnection OK comes up every few seconds, but I need to actually chk the mydevices servers are on line

tia

Sure, there’s a thread here that discusses it.

Hi Adam
Thanks, it still doesnt work, in the serial window, I can see [16184] Connection ok , but the I pulled the link on the WAN side of the router, so its still connected to my Wireless AP, but not to mqtt.mydevices.com, I really need something to verify if the servers are online

Tx

Ah, you’re using MQTT. It should still be the same functions, CAYENNE_CONNECTED() and CAYENNE_DISCONNECTED(). I’m pretty sure @vapor83 has done this with MQTT, maybe he can help?

1 Like

Ill await his response , hopefully

Hey! sorry for the delay, there is a way to restart any esp for whatever reason. CAYENNE_CONNECTED() is called upon successful connection to the MQTT server, and CAYENNE_DISCONNECTED() is called whenever the “heartbeat” times out, usually because of latency. I believe it defaults to 10 seconds. When I’m testing my apps I put a disconnect count, online time, and system uptime on my dashboard to see if its having issues.

As far as the restarting you just need to add a library that has all the esp functions in it. The library is called just ESP8266Wifi.h and you can download it here. You really only need ESP8266Wifi library to get the reboot working. Here is a example code using the connected/disconnect and reboot functions for ya. Let me know if you have anymore questions :slight_smile:

// #define CAYENNE_DEBUG Serial
// #define CAYENNE_PRINT Serial

#include <CayenneMQTTESP8266.h>
#include "ESP8266WiFi.h"


char ssid[] = "wifi";
char wifiPassword[] = "pass";

char username[] = "";
char password[] = "";
char clientID[] = "";


bool online = false;

unsigned long onlineTime;
unsigned long prevTime;
unsigned long time4;

int disco = 0;

byte restartChip = 0;




void setup()
{
  pinMode(BUILTIN_LED, OUTPUT);

  // Serial.begin(9600);
  
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);

  time4 = millis();

}


void loop()
{
  Cayenne.loop();
  
  if (millis() - time4 > 20000)  // used to send data to dashboard every 20 seconds
  {
     execute();
  }


  if (online == true)  // blink builtin LED if online
  {
    digitalWrite(BUILTIN_LED, LOW);
    delay(10);
    digitalWrite(BUILTIN_LED, HIGH);
  }
  else
  {
    digitalWrite(BUILTIN_LED, HIGH);  //turn off LED if offline
  }
}




void execute()
{
  time4 = millis();

  Cayenne.virtualWrite(23, disco, "counter", "null");

  int time2 = (millis() / 1000) / 60;
  Cayenne.virtualWrite(24, time2, "counter", "null");

  onlineTime = ((millis() - prevTime) / 1000) / 60;
  Cayenne.virtualWrite(25, onlineTime, "counter", "null");
}




CAYENNE_CONNECTED()
{
  online = true;
  prevTime = millis();
}

CAYENNE_DISCONNECTED()
{
  online = false;
  disco++;
}




CAYENNE_IN(1)
{
  restartChip = getValue.asInt();

  delay(1500);

  if (restartChip == 1)  // if button is pressed
  {
Cayenne.virtualWrite(1, 0, "digital_sensor", "d");        // put button back to off on dashboard
Cayenne.loop();          //updates the above virtualWrite to the dashboard
delay(1500);              //wait for data to transmit just encase
ESP.restart();            //triggers a restart
  }
}
4 Likes

Here is a pic of one of my dashboards:

3 Likes

awesome, Ill try when I get home, thanks so much for your support

1 Like

Worked like a charm, thanks again for the awesome support @vapor83 and @adam

2 Likes

thank`s for code , but i dont know how to add the restart button.

on you cayenne dashboard go to: add new —> devices/widgets —> actuators —> digital output. fill in all the details and select the appropriate channel number which you have added in the code.