Arduino Online/Offline Override

not in front of the right PC at the moment - do a text search of the library files for “blynkondisconnect” (from memory) to find the right file and uncomment as required. Also uncomment CAYENNE_DISCONNECTED in the KEYWORDS file
If you get stuck let me know and i’ll post again on monday

ok - found it - file is BlynkProtocol.h

do a find and remove the comments on ALL the lines like this: //BlynkOnDisconnected();
There are 5 or six or so from memory

I made the functions you mentioned and edited the library.
But when I put my device offline, it just keeps reconnecting (which is good…) but does not seem to go though my local instructions…
Here’s the code I used. Also, the " CAYENNE_DISCONNECTED" does not turn orange color unlike " CAYENNE_CONNECTED" (I did un-commented it on the library as you mentioned.

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneEthernet.h>

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "o6vy3e061m";
boolean onlineStatus=false;
void setup()
{
	Serial.begin(9600);
	Cayenne.begin(token);
}
   CAYENNE_CONNECTED(){onlineStatus=true;}
   CAYENNE_DISCONNECTED(){onlineStatus=false;}
void loop()
{
   if (onlineStatus==true)
   {Cayenne.run();
   Serial.print("Online");} 
   else{ 
    digitalWrite(23,digitalRead(22));
    digitalWrite(25,digitalRead(24));
    digitalWrite(27,digitalRead(26));
    digitalWrite(29,digitalRead(28));
    digitalWrite(31,digitalRead(30));
    digitalWrite(33,digitalRead(32));
    digitalWrite(35,digitalRead(34));
    digitalWrite(37,digitalRead(36));
    digitalWrite(39,digitalRead(38));
    digitalWrite(41,digitalRead(40));
    digitalWrite(43,digitalRead(42));
    digitalWrite(45,digitalRead(44));
    digitalWrite(47,digitalRead(46));
    digitalWrite(49,digitalRead(48));
    digitalWrite(51,digitalRead(50));
    digitalWrite(53,digitalRead(52));
    Serial.print("Offline");        //Test if the program enters else..
   }   
} 

Serial.print(“Offline”)-does not appear on my serial monitor
I know there are a LOT MORE efficient ways to do this than my codes, but again, I am not really good at this, so the simpler the better, hehe

You need to uncomment it in the KEYWORDS file as well to get it to go orange.

As for the rest…i havent tested or explored what was discussed any further. I only use it turn switch a LED to show locally the status of the device. It will take some testing to work out best way to do it.
Scrap all the digitalwrites and get the abolute basics running first as expected (ie serial output of online/offline or switching a led based on status) and work from there

Here’s my update… I uncommented the KEYWORDS file and got the disconnect function to turn yellow, then i turned everything to basics. I was able to make it do EXACTLY as i want if i used the manual example (the one where i manually set a static IP). Here is the working code… It prints “offline” when the net is unplugged then “online” when it is connected.

#define CAYENNE_DEBUG           
#define CAYENNE_PRINT Serial       
#include <CayenneEthernet.h>        
#include <CayenneEthernetW5500.h> 
#include <CayenneEthernetW5200.h> 

char token[] = "xxxxxxxxxx";
boolean onlineStatus=false;
byte arduino_mac[] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX }; //actual values replaced, just to be safe when i post it...
IPAddress arduino_ip(xxx, xxx, x, xx);
IPAddress dns_ip(x, x, x, x);
IPAddress gateway_ip(xxx, xxx, x, x);
IPAddress subnet_mask(xxx, xxx, xxx, x);

void setup()
{
	Serial.begin(9600);
	Cayenne.begin(token, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
}
CAYENNE_CONNECTED(){onlineStatus=true;}
CAYENNE_DISCONNECTED(){onlineStatus=false;}
void loop()
{
	Cayenne.run();
  if (onlineStatus==true){
        Serial.print("ONLINE ");}
  else{
        Serial.print("Local ");}
  }

But when i tried it for the regular ethernet example (i used the required libraries etc as the default example), it hangs on “getting IP” when network is unplugged… Any work around for that??? Code below if it helps:

#define CAYENNE_DEBUG       
#define CAYENNE_PRINT Serial
#include <CayenneEthernet.h>

char token[] = "xxxxxxxxxx";
boolean onlineStatus=false;
void setup()
{	Serial.begin(9600);
	Cayenne.begin(token);
}
   CAYENNE_CONNECTED(){onlineStatus=true;}
   CAYENNE_DISCONNECTED(){onlineStatus=false;}
void loop()
{
  Cayenne.run();
  if (onlineStatus==true){
    Serial.print("Online ");}
  else{
    Serial.print("Off ");}
} 

This one hangs on “getting IP” when network is unplugged, while if I used static IP it works fine… but i think i would need the non-static ip route… Any suggestions??? ^____^

Can you get the basic cayenne ethernet example to run?

yep, using the cayenne ethernet example, no issues, but if i use it without the network plugged in, it stay at “getting IP”…

should drop to DHCP Failed! after a while thats what mine does

When i get some time i’ll have more of a look - you may need to modify time out behaviours etc

1 Like

ALAS!!! You mean the BlynkEthernet.h right? I read a thread ago about this, and applying that would be making a line with the word “fatal” into a comment and adding two line of codes under it.

//BLYNK_FATAL("DHCP Failed!");
BLYNK_LOG("DHCP failed. check your network connection!");
BLYNK_LOG("bypassing blynk...");
return;

Just uploaded it and it tries to connect, after a few seconds, it goes to my local commands and prints out “Off” as i wanted it to! WOOHOO!!! I think this is it! Will try and apply my actual instructions to my project and update my progress!
Thank you for your patience with me! I think this one is solved!!! :innocent:

—side note, i do not know how/why those lines of code worked, i just followed the instructions from the thread i have read. and i am just glad that they do. hehe

2 Likes

Well done - I hadn’t had time to look as I’m chasing other bugs at the moment!

1 Like

no problem! you helped me A LOT!!! Seriously, thanks! :smile:

1 Like

This is good stuff! I hadn’t finished my battery powered DHT11 monitor because of the possibility of it draining the batteries if the Cayenne server was down. This definitely helps, but I think I’m going to change direction and use MQTT when it is released and forgo any cayenne functions.

1 Like

Hey! I have build a aquarium controller wit arduino, I have 8 relay and temp. sensors. Some relay control lights and heaters. The lights are on schedule and the heaters are trigger by temp. Everything is made with cayenne, now my worry is when the net or server is going down they will stop working. Can you help with your code for local and how to combine with cayenne code? I"m very new to this coding!

It has been a long time since i made that, But if I remember correctly…

1)Search for a file within the arduino: BlynkProtocol.h and edit it.

do a find and remove the comments on ALL the lines like this:
//BlynkOnDisconnected();
There are 5 or six or so from memory

2)With the edited version saved, you can now use:

CAYENNE_CONNECTED(){
//Do Stuff for Online here
}

}
CAYENNE_DISCONNECTED(){
//Do Stuff for Offline here
}

Hope this helps :innocent:

Thanks! I will give it a try

Let me edit that… I think it goes more like this:

1)Search for a file within the arduino library (cayenne folder i
think…): BlynkProtocol.h
and edit it.

do a find and remove the comments on ALL the lines like this:
//BlynkOnDisconnected();
There are 5 or six or so from memory

2)With the edited version saved, you can now use:

//create a global boolean variable
bool varStatus=false;

CAYENNE_CONNECTED(){
varStatus=true;
}

}
CAYENNE_DISCONNECTED(){
varStatus=flase;
}

Loop (){
//use an if-else test for varStatus

if(varStatus==true){
//Do online stuff here}
else{
//Do offline stuff here}
}

////DO NOT TRUST MY SYNTAX!!! it’s been long since i programmed. I might
miss a syntax or two, but i hope you get the idea here…

:+1: I will try this next week, will see how it goes! When you say edit , you say to only uncomment or I have to do something else?

Uncomment then Save the BlynkProtocol.h
Alright, Good luck man

1 Like