Hi,
I have an Arduino mega and w5100 ethernet shield. I would like to use CAYENNE_CONNECTED to determine if my Arduino is connected or not. I have written some simple code to toggle on/off an LED if connected/disconnected.
The trouble is… when I have turned the internet off, the LED still turns on every 2 or 3 seconds (for about 1 sec) - I presume when trying to connect. I was hoping to carry out some processes if not connected but the method wont work.
DO you have any ideas?
Craig
in your sketch could you add this define (before setup) ? :
#define CAYENNE_DEBUG
I use it with my “Cayenne-MQTT-ESP8266” librarie, and very usefull to see whats happen in serial monitor, with Cayenne.
I use the exact same thing your trying to do. You need to call CAYENNE_CONNECTED and CAYENNE_DISCONNECTED and put your digital write for you LED under those functions. However, to get the disconnected part to work you need to edit your cayenne library.
Cayenne library → Blynk → BlynkProtocal.h
There will be about 8 or 9 functions named “BlynkOnDisconnected();” that are commented out. Uncomment all of these and now when the system gets disconnected it calls that function in your code. Here is a example of what I use for testing my boards:
in header to hold values:
unsigned long onlineTime;
unsigned long prevTime;
byte disco = 0;
after loop or wherever in your sketch as long as its not under another function or loop:
CAYENNE_CONNECTED() //set LED on if connected
{
prevTime = millis();
digitalWrite(LED_BUILTIN, HIGH);
}
CAYENNE_DISCONNECTED() //set LED off if disconnected
{
digitalWrite(LED_BUILTIN, LOW);
disco++;
}
CAYENNE_OUT(V23) // counts disconnects
{
Cayenne.virtualWrite(V23, disco);
}
CAYENNE_OUT(V24) // tracks system power on time in minutes
{
float time2 = (mills() / 1000) / 60;
Cayenne.virtualWrite(V24, time2);
}
CAYENNE_OUT(V25) // tracks online time in minutes
{
onlineTime = ((mills() - prevTime) / 1000) / 60;
Cayenne.virtualWrite(V25, onlineTime);
}
Ok… so I have just had some more time to look at this. I uncommented the debug messages and the serial output is as follows:
When connected:
And that seems all OK.
When I disconnect the ethernet cable:
And the problem lies in that whenever the line
Connecting to arduino.mydevices.com:8442 appears… the LED turns ON then turns OFF again when connection fails.
Any ideas?
My code is a simple one to check this as follows:
#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 = “n50v11ls5g”;
boolean onlineStatus = false;
const int TestPin = 13;
void setup()
{
Serial.begin(9600);
Cayenne.begin(token);
pinMode (TestPin, OUTPUT);
}
void loop()
{
Cayenne.run();
if (onlineStatus == true){
digitalWrite(TestPin, HIGH);
}
if (onlineStatus == false){
digitalWrite(TestPin, LOW);
}
}
CAYENNE_CONNECTED() {
onlineStatus = true;
}
CAYENNE_DISCONNECTED() {
onlineStatus = false;
}
Hmm, It shouldn’t turn on the LED until its connected, not when its trying to connect. Your code looks good. I tried your code on my mega & 5100 ethernet shield and it didn’t flash the light when I unplugged the ethernet. Maybe try this library and replace your current Cayenne library. This is is already setup for disconnect/reconnect. See if this makes a difference.