I have set up an ESP8266 to control a heater for some plants in my garden. The logic is, that the heater should switch on, once the temperature drops below 0°C and switches off again, once the temperature goes above 5°C. As the ESP8266 is physically located in the garden, the WiFi connection is not totally reliable, sometimes it fails for some time. That’s why I kept the control logic for switching on and off in the microcontroller code instead of using an event trigger by Cayenne. Cayenne is used to display temperature and status information and to control some other, less critical things (addtitional sensors and switches to turn garden lights on/off). The problem that I have is, that once the WiFi connection gets lost, it seems that the ESP8266 hangs in the “Cayenne.loop()” function and effectively does nothing else anymore. This means, that the heating function will not work during off-line. Is there a way around this?
Would using interrrupts be a good idea or does anybody have another suggestion? Here is my code (only an extract with the relevant parts):
void loop() {
Cayenne.loop(); // System hangs here when WiFi connection is lost!
sensor_Palme.requestTemperatures();
p=sensor_Palme.getTempCByIndex(0);
Cayenne.virtualWrite(8, p); //Display Temperature
if (p<0) {h=0;} // heater on
if (p>5) {h=1;} // heater off
digitalWrite (D5, h);
Cayenne.virtualWrite(10, h); // Display heater status
}
void loop() {
if (onlineStatus==true)
{Cayenne.loop();
Serial.print(“Online”);}
else{ Serial.println(“Offline”); }
}
When I run this, the system permanently prints “Online”, even when the connection gets lost. Means “onlineStatus” is never set to false. In the post, something is mentioned about “editing a Keyword file”. I have no clue what that means. Do you have concrete information, what file to edit and what needs to be changed in that file? Maybe that is the problem, because the Arduino IDE highlights the “CAYENNE_CONNECTED” text, while it does not hightlight the “CAYENNE_DISCONNECTED” text, which seems strange to me. A short explanation of these “CAYENNE_CONNECTED” and “CAYENNE_DISCONNECTED” would also help. Where and when is the code executed, that would set the onlineStatus to true or false?
search for a file “BlynkProtocol.h” in arduino folder. open and remove the comments on ALL the lines like this: //BlynkOnDisconnected(); There are 5 or six or so from memory and save it.
I did edit the “BlynkProtocl.h” and removed the comment slashes in the 6 lines where BlynkOnDisconnected() was found. No change. Continues to give me “online” message, once the WiFi is off, I get no status “offline”. Here is the output I see, when I switch off WiFi:
No - I need to take that back. It does not work reliably. Sometimes it works, sometimes it does not. I guess it only works, if the WiFi is lost, while the ESP8266 executes outside of the Cayenne.loop. If WiFi is lost, while ESP8266 executes already inside the Cayenne.loop it still hangs. Also using ticker and interrupt does not work, because after executing the ISR routine, the control is simply returned to the Cayenne.loop, and the system continues to hang…Any other ideas?
Tried this already. Does not work either, as the “Cayenne.begin” routine, which is executed during setup is also written in a way, that it hangs up indefinitely, if no WiFi is present. Besides that, I found that the ESP8266 does not reset reliably neither with ESP.restart() nor with ESP.reset() - there are several discussions of this problem in various forums.
I examined the Cayenne libraries. In the CayenneArduinoMQTTClient.h, there is a function “connect()”, which contains a do/while loop, that looks like the one causing the problem:
do {/ attempt to connect; delay(1000); /}
while (error != MQTT_SUCCESS);
it looks to me, that if I could replace this with an equivalent for-loop, which counts up to 10 executions, this could work as some timeout, if after 10 seconds there is still no connection possible. I just have not figured out, how to jump out of the Cayenne.loop function in that case. Would be grateful, if anybody has an idea here - unfortunately I am not very skilled in programming…
I will take a look at it and see what I can come up with. I modified the blynk connection method to do that over here so it should be possible.
Yes, I have seen a few people posting about this. Might be better to just put the ESP is deep sleep mode (also in the link above)
This must be an MQTT issues since I don’t remember seeing that behavior in the blynk connection method. Again, I’ll take a look and see if I can get it working.
I really need to update the battery powered ESP project page so maybe I’ll take some time to update it to the officially supported MQTT connection method while I’m testing these things.
Hello community. Im from Russia. At first sorry for my english.
Now i working at my project - smart fan with remote control on esp8266 node mcu 12e. Fan relay controls at main (loop) of sketch.
I planned to use cayenne for remote monitoring.
But faced with problem:
When internet - is ok, everything is ok. But when internet connection is lost, all sketch is freezing.
After many trying i understand that freezed cayenne.loop.
I try to use enable flag ( ping 8.8.8.8) - is a little better. But sometimes freze still.
Is anybody knows how solve this problem?
Is anybody knows alternative method to sent information to cayenne server.
Because problem with looping in checking internet connection in cayenne loop.
I have solved this with an Interrupt Service Routine (ISR), which is executed every 2 seconds. If the system is hung up in the Cayenne loop, a watchdog counter is incremented each time the ISR is called. After the counter reaches 4 (means the system hangs in the Cayenne loop for 8 seconds), key variables are written to the EEPROM and the ESP8266 executes a reset by software. If the internet still is not available after restart, at least the setup code gets executed, before the system again hangs in the “cayenne.begin” loop and 8 seconds later another reset occurs. This game is continued until the internet comes back and normal execution resumes. The problem with the hung up Cayenne loop is, that it never recovers, even when the internet connection comes back. This ISR solution solves that problem. Credit for that solution goes to Andreas Spiess (#33 Internet of Things with ESP8266 #5: Watchdogs, Timers & Stability - YouTube) Here are the relevant code pieces:
Well, as you say, @shramik_salgaonkar, it makes no sense to run an IOT project without internet connection. However, often there are situations in which the WiFi connection fails for just a few minutes. And there the solution has 2 benefits:
Even though the internet hangs, the ESP8266 can execute some critical code in the first part of the init-routine (before the cayenne.begin statement). In my case reading a temperature value and taking action to keep that temperature, even though there is no internet connection. Of course I cannot manipulate the temperature target through the Cayenne interface nor monitor the actual temperature, but at least the value is kept within the target range during the internet downtime.
And this is the more important part: Usually the WiFi connection just fails for a few minutes - either there is a router problem or the signal becomes weak for some reason. However, once the internet connection is lost, the cayenne.loop will hang indefinitely - even after the WiFi connection recovers. The only way to get the program back running is to physically press the reset button on the ESP8266 - not a good solution. First, it can potentially take a long time, until the user notices the hangup, second some important variables might get lost and third the ESP8266 might not be easy to reach.
Thank you very much for you answer. But i dint understand. This method only for restarting. But how control ( read sensor and control relay) in period from 0-8 sec. it is not imposible ind this method?
I didnt understand…
Ok i have lost_internet_key_var , why i can not put cayenne.begin() in IF(lost_internet_key_var =1) statement?
And i worried about session writing Eeprom memory? After 2 seconds? Lifetime esp will be not more 1 year… am i right?