Arduino Online/Offline Override

i feel your pain…

1 Like

Hi, is there such a “status” or a “flag” that I can detect to inform me if i am online or not? (quoted words cause I am not sure if they are called like that, hahaha). Something that would tell me my device is not connected so i can wrap my whole local commands in a huge “if” statement?

CAYENNE_CONNECTED() & CAYENNE_DISCONNECTED()

Although you need to go and make some changes in the background Blynk libraries to get the disconnected one working as its been commented out.

Use them to set a flag in your code and act on it - im just using them to turn a status LED on and off while i debug a problem

1 Like

just messing around

define a boolean first:

boolean onlineStatus = false;

then in your code insert these:

CAYENNE_CONNECTED(){
onlineStatus = true;
}

CAYENNE_DISCONNECTED(){
onlineStatus = false;
}

Then in your main loop act on the true/false status - simples!
Well maybe not but hope that helps :slight_smile:

2 Likes

But my prev post stands about having to edit the libaries to get CAYENNE_DISCONNECTED working

1 Like

Yes!!! I think that just might do it!!! I will try it and hopefully it will yield success!

Are those lines/commands/function/library located within “arduino/libraries/cayenne/…ethernet*” (sorry not in front of the computer right now). It is the cayenne ethernet library that i call from the example code right (#include cayenne ethernet something something$? Hoping for the best! Will post my petty endeavor later. Thanks tim2!

Oh yeah! I appreciate the detailed explanation! Excited to try it later! Tnx again

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