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

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?.

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.

2 Likes

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

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

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.

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.

3 Likes

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.

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.

i will post it here once i code it.

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 ?

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

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); 
  }
}

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

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.

will the script execute if there is no connection?

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.

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

Hi… I am using Node-MCU and cayenne iot to monitor the liquid level . When liquid level low automatically filling liquid through the Solonoid valve .But problem is sometimes cayenne iot and node-MCU goes to offline mode. Why is that?

check your serial monitor for the cause. Add #define CAYENNE_DEBUG to get more detail.

ok. thank u very much. i will try it