How to stop Cayenne.run() running continuously?

I want to combine Cayenne copy paste code (only to upload data) with my own sketch. It is working very good if there is good connection with the internet, but if there is no connection it is trying to connect on internet forever so my main code will never run. If it has not found an IP it will keep trying forever, if it has IP loop will run on every time out (20sec). How to run cayenne every (say) 30 minutes and if there is no connection bypass it?

void setup () {
  Serial.begin(9600);
  Cayenne.begin(token);
  setup_buttons();
  start_rtc();
  sensors.begin();  // see if the card is present and can be initialized:
  initialize_SD();
  pointer_file();

}

void loop () {
  Cayenne.run();


  sensors.requestTemperatures(); // Send the command to get temperatures
  for (int i = 0; i < 2; i++) {
    Serial.print("Temperature for the device ");
    Serial.print(i);
    Serial.print(" is ");
    tempC[i] = sensors.getTempCByIndex(i);
    Serial.print(tempC[i]);
    Serial.print("\n");
  }

  check_time_and_write_on_SD(tempC);

}

Hi @dpiralis
I read your code.
Your code expresses the sensor data written to the SD card when the timer time is over.
But did not see the code to upload the sensor data to the Caynne cloud.
This should not require the use of Caynne cloud functionality.

It was a part of my code. If you mean you want to see cayenne out code here it is. Cayenne is working, but if there is no connection it is delaying my code for 20 sec if it has already IP(connection time out) and forever if it is searching for DHCP (Getting IP… DHCP Failed!)

In this case my code is not working or it is missworking.

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(V1)
{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Pin.
  Cayenne.celsiusWrite(V1, sensors.getTempCByIndex(0));
  Serial.println("Temp updated");
  // To send the temperature in Fahrenheit use the corresponding code below.
  //Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}

CAYENNE_OUT(V0)
{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Pin.
  Cayenne.celsiusWrite(V0, sensors.getTempCByIndex(1));
  Serial.println("Temp updated");
  // To send the temperature in Fahrenheit use the corresponding code below.
  //Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}

Hi @dpiralis
Thank you for your explanation.
Your distress should be the underlying network connection is unstable.
Caused the device and Caynne Cloud can not communicate smoothly.
Need to solve the underlying communication.
What kind of underlying network ethernet or wifi you use ?
I am using ESP8266 wifi to set up communication.

I use Ethernet module (not shield) for Arduino MEGA. I want my sketch run without long delays what cayenne is causing when there is no connection. So I want to upload data to cloud every say 1 hour(1 accepted delay every one hour max).Libraries that are used are:

#define CAYENNE_DEBUG        
#define CAYENNE_PRINT Serial  
#include <CayenneDefines.h>
#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>
#include <CayenneEthernetClient.h>
#define VIRTUAL_PIN V1
#define VIRTUAL_PIN V0

To stop trying finding DHCP forever I changed BLYNK_FATAL("DHCP Failed!") to BLYNK_LOG("DHCP Failed!") on BlynkEthernet.h

// DHCP with domain
int begin( const char* auth,
            const char* domain = BLYNK_DEFAULT_DOMAIN,
            uint16_t port      = BLYNK_DEFAULT_PORT,
            const byte mac[]   = _blynkEthernetMac)
{	
    Base::begin(auth);
    BLYNK_LOG("Getting IP...HI1"); 
       if (!Ethernet.begin((byte*)mac)) { 
            BLYNK_FATAL("DHCP Failed!"); 
    } 
    // give the Ethernet shield a second to initialize:
    ::delay(1000);
		this->conn.begin(domain, port); 
		IPAddress myip = Ethernet.localIP(); 
		BLYNK_LOG("My IP: %d.%d.%d.%d", myip[0], myip[1], myip[2], myip[3]);
	   return 0;

}

After library editing for my module I finally got this. But how to change the timeout of Ethernet.begin((byte)mac)?*
Here is the code to run cayenne.run every one hour and stop it when all data are uploaded.

const unsigned long doNotUploadTime = 1UL * 1000UL* 60UL *60UL; //1 hour
bool dataIsUploaded = false;
unsigned long uploadedTime;

...

void setup() {
  ...
  Cayenne.begin(token); // After library editing is has a timeout of 1min, I don't how to change it.
  ...
}

void loop() {

  ...

  unsigned long passedTime = millis() - uploadedTime;
  if (passedTime > doNotUploadTime) {
    dataIsUploaded = false;
    check_if_uploaded[0] = 0; // for V0
    check_if_uploaded[1] = 0; // for V1
  }


  if (!dataIsUploaded and !check_if_uploaded[0] and !check_if_uploaded[1]) {
    cayanae_run();
    dataIsUploaded = true;
    uploadedTime = millis();
  }

  ... // your code

}


void cayanae_run(void )
{
  if (!dataIsWritted) {

    while (1) // Add time out if you want
    {
      //Add here: if too much time passed call Cayenne.begin(token);
      Cayenne.run();
      if (check_if_uploaded[0] == 1 and check_if_uploaded[1] == 1) {
        break;
      }
    }
  }

}

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(V1)
{
  Cayenne.celsiusWrite(V1, random(-5, 35));
  Serial.write("Temp updated");
  check_if_uploaded[1] = 1;
}

CAYENNE_OUT(V0)
{
  Cayenne.celsiusWrite(V0, random(-5, 35));
  Serial.write("Temp updated");
  check_if_uploaded[0] = 1;
}
1 Like

does exist a command, like Cayenne.end (); (don’t try this it’s an example lol) ?
or how to stop cayenne run ?

This was answered in the Cayenne Slack channel, so just reposting here in case anyone else was reading this.

* 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
1 Like

thanks @bestes i’ll try this.