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