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

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