Wemos goes offline

I’m using a Wemos D1 mini Pro to monitor the water level in a water tank using a float which just acts as a switch with the normal being pin low and everything works as intended except the device goes offline often sometimes for seconds and sometimes days. I setup another device that is less than 15’ from my router and it does the same thing and they are on different wifi networks. Any suggestions? Also is there a way for the program to see that it went offline and send the current status when it reconnects if the switch is high?

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.

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

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

#define SENSOR_PIN D5 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define CONNECT_LED D6

int previousState = 0;
int currentState = 1;
int sensorValue = digitalRead(D5);
void setup()


  digitalWrite(CONNECT_LED, HIGH);
// Cayenne.begin(username, password, clientID);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  while(WiFi.status() != WL_CONNECTED)
    delay (500);
//    Serial.print();
    Serial.print("Connected, IP address: ");
    digitalWrite(CONNECT_LED, LOW);


void loop()
currentState = digitalRead(SENSOR_PIN);
delay (2000);
// Cayenne.virtualWrite(0, millis());

if (currentState != previousState) {
Cayenne.virtualWrite(VIRTUAL_CHANNEL, currentState);

previousState = currentState;


can you connect your device to your pc and check the serial monitor output when the disconnect occur. If you want to check if disconnect occur then you can use this code:

  onlineStatus = true;
    digitalWrite(LED_PIN1, HIGH);

  onlineStatus = false;
    digitalWrite(LED_PIN1, LOW);


It disconnects randomly and normally takes hours to disconnect I have added sending the signal strenght to Cayenne so I can get a graph to see what happens.

When I try you program I get a compile error "‘onlineStatus’ was not declared in this scope

you can delete it from the code. I had just given you an example. you will have to modify the code accordingly.

I was trying to find more info about Cayenne with Arduino and ESP8266 but when clicking on the links for the github from the docs I get 404 error

you will get everything here https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino

I should have said I did use google to find it but the documentation links are bad.
I have added an up time counter so I can more easily find out if its locking up or just losing connection. So now I’ll just wait with it connected to the computer with a serial terminal open and hopefully it doesn’t wait too many hours before I lose connection

Thank you for all of your help, my only programming experiance was from programming in Basic and that was years ago

It disconnected after 33 minutes and the wifi signal was still good so it is something with the device or program I have tried several brands so don’t know what to do for that.
Terminal info
2064878] Disconnected
[2064878] Connecting to mqtt.mydevices.com:1883
[2065230] Connected

I did order an ESP32 to try

Is there a way to reset the device if it looses connection?

I am now using a ESP32 and it went offline then back on after 4 hours

Wifi -72 Uptime 4
Wifi -73 Uptime 4
[15439281] Disconnected
23:27:01.415 -> [15439281] Connecting to mqtt.mydevices.com:1883
[15439997] Connected
Wifi -76 Uptime 4
Wifi -72Uptime 4

It has disconnected several times this morning
Terminal data
[1353061] Disconnected
09:07:04.719 -> [1353061] Connecting to mqtt.mydevices.com:1883
[1357062] Network connect failed
[1362062] Network connect failed
[1373997] Connected

[1599544] Disconnected
09:11:11.220 -> [1599544] Connecting to mqtt.mydevices.com:1883
[1600904] Connected

[2657070] Disconnected
09:28:48.796 -> [2657070] Connecting to mqtt.mydevices.com:1883
[2657585] Connected

It is not losing the network just connection with the server.

There are many factors that can cause the disconnect and I can’t tell what is at your end.

  onlineStatus = true;
    digitalWrite(LED_PIN1, HIGH);
  onlineStatus = false;
    digitalWrite(LED_PIN1, LOW);

I see you have delay (2000); in there. Try taking that delay down to about 100. Long delays can cause issues with disconnects.

1 Like

I had the disconnects before the delay was added to the program and they happen on two completely different networks. I setup a Raspberry Pi with just the default setup thinking it would solve the problem and it disconnects randomly also and it has never been at the same time as the Wemos board.
I just added a watchdog timer to the Wemos and turned off notifications for on/offline.

check the logs when the disconnect occur tail -f /var/log/myDevices/cayenne.log

The Pi doesn’t go off line as often it went offline 5 times yesterday and hasn’t went offline today the Esp32 has gone offline 40 times today alone it came right back online all but one time which took about an hour to come back online.

Are you using your MQTT credentials more than once? That will surely cause disconnects like you are seeing.


Really hard to find the cause of this issue. but lets do some testing:
Add this basic code without any sensor code https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/blob/master/examples/Connections/ESP8266/ESP8266.ino and see if disconnect the same way.