Online/offline code

Hi guys,

I am trying to segregate my code into ‘if online’ and ‘if online’ in cayenne - ideally i want my code to execute and at the end of my code for my uploads to happen.

In search i found a thread which suggests editing libraries for blinkprotocol.h and using the cayenne_connect and cayenne_disconnect - what i wasnt sure of is how this would stop a failure to connect in the loop when cayenne tries to reconnect constantly.

Any guidance would be appreciated

Cheers

Hi, It’s not very clear , you want to do something when Offline and another when Online ?

Yes, I’d like to time out the code for connection to cayenne when my network goes down.better to use an interrupt?

On Arduino ? RPI ?

oo, sorry - yeah thats a big miss. Yeah, on arduino, on a mega.

i use this with ESP8266 (Arduino IDE) try if working with Mega ?

// This function will run every time the Cayenne connection is established.
CAYENNE_CONNECTED()
{
  CAYENNE_LOG("Connection established");
  Serial.println("\n++ Cayenne Connected ! ++"); // can be deleted
 }
CAYENNE_DISCONNECTED()
{
  CAYENNE_LOG("Connection finished");
  Serial.println("\n--  Cayenne Disconnected ! --");    //  can be deleted
//<==== HERE i do something when Cayenne is disconnected  
}
3 Likes

Hey man, that code worked - i can sense when cayenne is connected or not now.

What id like to know now is, cayenne tries to reconnect and keeps failing - Whats my best way to make it stop trying after a certain amount of trys and go back to my offline code? a while loop, or? kinda a hard thing to search for…

digging into the code of the CayenneMQTTClient library we can find:

* Disconnect from the Cayenne server.
* @param[in] client The client object
* @return success code
*/
int CayenneMQTTDisconnect(CayenneMQTTClient* client)
{
    return MQTTDisconnect(&client->mqttClient);
}

https://github.com/myDevicesIoT/Cayenne-MQTT-ESP/blob/bb9e93597605c7aa89883ae6c5d27cc0f81edac0/src/CayenneMQTTClient/CayenneMQTTClient.c

OR in Cayenne-MQTT-Arduino Cayenne-MQTT-Arduino/MQTTClient.h at master · myDevicesIoT/Cayenne-MQTT-Arduino · GitHub

/** MQTT Disconnect - send an MQTT disconnect packet and close the connection
 *  @param client - the client object to use
 *  @return success code
 */
DLLExport int MQTTDisconnect(MQTTClient* client);

BUT how to turn it into a command line in the Arduino IDE to cut Off Cayenne Connection ?
@shramik_salgaonkar @adam The kings of programming :wink: do you have an idea ?

@SuperNinja no idea on this. will try to see if i can get anything.

Tagging @jburhenn, I won’t be able to look at it today.

The CayenneMQTTDisconnect call is only used internally to handle disconnecting/reconnecting when it detects a connection failure. So to use it manually you’d probably need to modify CayenneArduinoMQTTClient.h to add some kind of disconnect() wrapper function.

If you only want to have it try to connect a certain number of times you’d probably need to modify the connect() function in CayenneArduinoMQTTClient.h so it exits the connection loop after a certain number of times. You could maybe specify it by adding a parameter, e.g. void connect(int attempts).

ok i found this in

	void begin(Client& client, const char* username, const char* password, const char* clientID, int chunkSize = 0) {
		NetworkInit(&_network, &client, chunkSize);
		CayenneMQTTClientInit(&_mqttClient, &_network, username, password, clientID, CayenneMessageArrived);
		connect(5);//<---------------------- HERE : try to connect 5x to Cayenne
	}

	/**
	* Connects to Cayenne
	*/
	void connect(int attempts) {//<---------------------- HERE : number of connecting
		CAYENNE_LOG("Connecting to %s:%d", CAYENNE_DOMAIN, CAYENNE_PORT);
		int error = MQTT_FAILURE;
		do {
			if (!NetworkConnect(&_network, CAYENNE_DOMAIN, CAYENNE_PORT)) {
				CAYENNE_LOG("Network connect failed");
				delay(1000);
                --attemps; //<------------------ Here: add decounter 

			}
			else if ((error = CayenneMQTTConnect(&_mqttClient)) != MQTT_SUCCESS) {
				CAYENNE_LOG("MQTT connect failed, error %d", error);
				NetworkDisconnect(&_network);
				delay(1000);
                --attemps; //<------------------ Here: add decounter 

			}
		}
            if (attemps==0) ????????? //<------------------ Here: how to exit loop ?


		while (error != MQTT_SUCCESS);
        attemps=5;//<------------------ Here: cayenne connected : RESET "bad Connection counter" 
		CAYENNE_LOG("Connected");
		CayenneConnected();
		CayenneMQTTSubscribe(&_mqttClient, NULL, COMMAND_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
		CayenneMQTTSubscribe(&_mqttClient, NULL, CONFIG_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
#ifdef DIGITAL_AND_ANALOG_SUPPORT
		CayenneMQTTSubscribe(&_mqttClient, NULL, DIGITAL_COMMAND_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
		CayenneMQTTSubscribe(&_mqttClient, NULL, DIGITAL_CONFIG_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
		CayenneMQTTSubscribe(&_mqttClient, NULL, ANALOG_COMMAND_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
		CayenneMQTTSubscribe(&_mqttClient, NULL, ANALOG_CONFIG_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
#endif
		publishDeviceInfo();
	}

@jburhenn

  1. does my adds are correct ( i couldn’t test it) ?
  2. if (attemps==0) doSomething //<------------------ Here: how to exit loop ?
1 Like
  1. Yeah, I think that should work, though you might want to set a default number of attempts (I set it to 5 in the code below).
  2. You just need to return to exit the loop, though the if statement needs to be inside the do-while loop.

Here’s some example code:

	void connect(int attempts = 5) {
		CAYENNE_LOG("Connecting to %s:%d", CAYENNE_DOMAIN, CAYENNE_PORT);
		int error = MQTT_FAILURE;
		do {
			if (!NetworkConnect(&_network, CAYENNE_DOMAIN, CAYENNE_PORT)) {
				CAYENNE_LOG("Network connect failed");
				delay(1000);
				attempts--;
			}
			else if ((error = CayenneMQTTConnect(&_mqttClient)) != MQTT_SUCCESS) {
				CAYENNE_LOG("MQTT connect failed, error %d", error);
				NetworkDisconnect(&_network);
				delay(1000);
				attempts--;
			}
			if (attempts == 0)
				return;
		}
		while (error != MQTT_SUCCESS);

		CAYENNE_LOG("Connected");
		CayenneConnected();
		CayenneMQTTSubscribe(&_mqttClient, NULL, COMMAND_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
#ifdef DIGITAL_AND_ANALOG_SUPPORT
		CayenneMQTTSubscribe(&_mqttClient, NULL, DIGITAL_COMMAND_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
		CayenneMQTTSubscribe(&_mqttClient, NULL, DIGITAL_CONFIG_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
		CayenneMQTTSubscribe(&_mqttClient, NULL, ANALOG_COMMAND_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
		CayenneMQTTSubscribe(&_mqttClient, NULL, ANALOG_CONFIG_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
#endif
		publishDeviceInfo();
	}
3 Likes

Thanks @jburhenn , I think that should do the trick.

@brendon.walters : find and modifie the CayenneArduinoMQTTClient.h On the libraries\Cayenne-MQTT-ESP8266-master\src\folder by the code jusr above.

:v:

1 Like