How can i end the {cayenne.loop()} if there is no connection?

#1

hi there iam using wemos d1 to connect to cayenne and i want to control it with bluetooth when there is no internet connection but the problem is that cayenne.loop will be repeated and i cant connect with bluetooth.
how can i end the cayenne loop if there is no connection?.

#2

there is no easy way to this unless you can modify the Connect() code from the library. we are having this feature on cayenne roadmap.

1 Like
#3

and how can i do that?
as i have seen some people have used CAYENNE_CONNECTED()
wont it work?

#4

sorry but i have not tried out anything on this. Let me check with the developer and try to find a solution for the same.

1 Like
#5

thank you for your help i will be waiting for your solution please inform me as soon as you get any thing on this topic.

Without Internet device reset
Offline- nothing works- Solution (kind of)
ESP + Wemos D1 get freezes when wifi connection lost
Show connected
Independent Switch
Offline working
#6

Here is a basic way to work a offline code whenever there is a connection loss.

CAYENNE_CONNECTED() {
  Serial.println("connected.......");
}

CAYENNE_DISCONNECTED() {
  Serial.println("disconnected......");
  while (true)
  {
    Serial.println("blocking");
    digitalWrite(LED_BUILTIN, LOW);   
    delay(1000);                      
    digitalWrite(LED_BUILTIN, HIGH);  
    delay(2000);
  }
}

Whenever there is a connection loss it stays in the while loop and runs the code. one thing to note is that you will need to make this while loop false by some kind of external method, maybe be an external switch or another Arduino communicating with it. You have to figure it out. Also if you try to connect again by breaking the while loop and there is no internet connection then it will forever stay in loop looking for connection.

2 Likes
#7

thank you for the help.
I don’t have my arduino at this moment so i will try it when i have it.

i have one question tho :

if i tried to use the interrupts by applying a counter(in the structure of the interrupts) that counts the pwm on one of the pins and after some time(1000 count maybe) if the pwm didn’t stop on that pin the interrupt will work and will go to some line after the cayenne loop.

(this way will apply some delay on the program but will make me able to check for internet connection in every cycle of the program)

will this way work?

please share your opinion,much appreciated.

#8

I did not get what you are trying to do. can you share the code for what you mean for the above and provide more details.

#9

i will post it here once i code it.

#10

A very simple question,
Can i toggle ON/OFF an LED by using
CAYENNE_DISCONNECTED() & CAYENNE_CONNECTED() to simply function as a status LED for assuring that my IOT device is successfully connected ?

#11

You can do it, but you have to figure out some way to check there is a internet connection? As if you exit the CAYENNE_DISCONNECTED() and there is no internet connection then the code will loop in

#12

Hi,
This is an old post but here is what I tried and it works for me.

CAYENNE_DISCONNECTED() {
  Serial.println("Connection lost");
  bool disconnected = true;
  while (disconnected)
  {
    if( WiFi.status() == WL_CONNECTED ){
      Serial.println("Wifi is back...");
      disconnected = false;
    } else {
      Serial.println("No wifi...");
    }
    delay(2000); 
  }
}
#13

the above method is best to handle wifi connection and not for handling internet connection.

#14

I’m new to Cayenne and really like the stability of the connection from it. For disconnection detection & reset i use a script from another user i found here (also how i discovered Cayenne), stanislas.guiot.sams

The script makes use of a sensor, actuator and triggers to write and read values from the device to the Cayenne server. if connection is ok, the values are equal. If values (adjustable timers) aren’t equal, it means that the Arduino couldn’t read the value from the Cayenne server, then the script resets my Arduino.

#15

will the script execute if there is no connection?

#16

Just tested it with pulling the main internet cable to the router, device doesn’t reset so the i guess Cayenne is trying to connect continuously without running the loop code (and thus the reset code).

I tested this (modified) code with Virtuino Android app (no cloud server) and that worked fine, but gave problems when the app went to sleepmode (normal behavior) the Arduino got a reset. SO i thought with a normal cloud server like Cayenne this script should work fine.

Back to the drawingboard i guess… Although Cayenne connection seems pretty stable, so i won’t need the reset code after all. With the Virtuino app the Arduino+shield disconnected a lot and a reset was needed to get it connected again.

#17

you can do a test using the Serial monitor, add #define CAYENNE_DEBUG and check whats happening. the current cayenne MQTT library continuously loops in to check if internet is available and it does not execute other code. A solution was the one i posted above, but that also require some improvement to let the arduino know that the internet connection is available.
We are working on fixing this issue.

1 Like