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_DEBUG
#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 VIRTUAL_CHANNEL 1
#define CONNECT_LED D6
int previousState = 0;
int currentState = 1;
int sensorValue = digitalRead(D5);
void setup()
{
pinMode(SENSOR_PIN, INPUT_PULLUP);
pinMode(CONNECT_LED, OUTPUT);
digitalWrite(CONNECT_LED, HIGH);
Serial.begin(115200);
// Cayenne.begin(username, password, clientID);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
Serial.print("Connecting");
while(WiFi.status() != WL_CONNECTED)
{
delay (500);
Serial.print(".");
}
// Serial.print();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(CONNECT_LED, LOW);
}
void loop()
{
Cayenne.loop();
Serial.print(WiFi.RSSI());
Serial.println(sensorValue);
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:
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
"
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
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.
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.
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.