Hi All,
And thanks for reviewing and trying to help.
I have arduino uno attached with ethernet shield w1500, connected to the cayenne web dashboard, I am using the digital ports to trigger relay to operate some device.
But sometimes when trying to run the relay on, the arduino got disconnected and trying to connect again:-
Thanks @shramik_salgaonkar for your comment:- the below is the code, but the relay was added from the dashboard not by using the code:-
##########################
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneEthernet.h>
char Fuel;
char Litres;
// Virtual Pin of the widget (from 0 to 25).
#define TempVirtual V0
#define FuelVirtual V25
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "xxxxxxxxxx";
void setup()
{
Serial.begin(9600);
Cayenne.begin(token);
}
void loop()
{
Cayenne.run();
//delay(4000);
}
//
// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(TempVirtual)
{
float inp=A0; // This represent the actual port in the Board that you need to connect to.
float val=0;
float val1=0;
//Serial.println(analogRead(inp));
val1=analogRead(inp);
val=val1/1024;
//Serial.println(val);
val=val*5000*0.0277;
//Serial.print("Temp is: ");
//Serial.print(val);
//Serial.println(" C");
// Read data from the sensor and send it to the virtual channel here.
// You can write data using virtualWrite or other Cayenne write functions.
// For example, to send a temperature in Celsius you can use the following:
Cayenne.virtualWrite(TempVirtual, val , TEMPERATURE, CELSIUS);
}
// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(FuelVirtual)
{
float Finp=A5; // This represent the actual port in the Board that you need to connect to.
float Fval=0;
Fval=analogRead(Finp);
if (Fval<=444){
Fval=0;
}
else{
Fval=Fval/55.88;
}
//Serial.print("Fuel lrvel is: ");
// Serial.println(Fval);
//Serial.println(" Litre");
// Read data from the sensor and send it to the virtual channel here.
// You can write data using virtualWrite or other Cayenne write functions.
// For example, to send a temperature in Celsius you can use the following:
Cayenne.virtualWrite(FuelVirtual, Fval , TEMPERATURE, CELSIUS);
}
##################
@rassim803 Just wanted to add that our original Arduino connectivity (the non-MQTT one you were originally using) is sensitive to delay() statements, so I’d advise avoiding them or keeping them to a bare minimum if you want to use this connectivity. Glad you have a more stable connection with MQTT however, it’s where we envision the future of the Cayenne product.
Have a look at this post where I show how you can use SimpleTimer to replace delay() statements in your code, if they are necessary.
Thanks, I added the timer and seems working fine, but once happened that the network was disconnected for some seconds but the arduino couldn’t connect to the cayenne for long time.
Thanks @shramik_salgaonkar I followed the advice from @rsiegel and came back to normal code as it is easier and I became somehow familiar to it, also I guess there is difficulty in adding virtual pins and making my own calculations!!!.
Thanks for the help and if you have it for the virtual pins, please share it.
@rsiegel also please check the below case:-
This should print first, then wait 5 seconds
This should print second, then wait 5 seconds
This should print first, then wait 5 seconds
[1032415] Login timeout
[1032586] Connecting to arduino.mydevices.com:8442
This should print second, then wait 5 seconds
This should print first, then wait 5 seconds
…
…
…
…
…
…
[1292079] Connecting to arduino.mydevices.com:8442
…
…
…
…
[1324840] Connecting to arduino.mydevices.com:8442
This should print second, then wait 5 seconds
This should print first, then wait 5 seconds
[1357152] Connecting to arduino.mydevices.com:8442
This should print second, then wait 5 seconds
I’m not seeing anything obviously wrong in the code you posted. Honestly it would probably be best to go with the MQTT connection option. Can you post your code for that so we can make some suggestions on calculations?
Thanks for you, Please see the below MQTT code that I used, I put both the virtual and actual pins as 13 to exclude any misunderstanding from my side to the issue:-
It doesn’t work to trigger the pin 13 to HIGH:-
`/*
Cayenne Valve Switch Example
This sketch shows how to set up a Valve Switch with Cayenne. The Arduino cannot
drive a valve because it does not output the needed current. As a result, in order
to make this example work, various electronic components are necessary to connect
the valve. To keep it simple, you will need an external power source, transistor (eg. TIP120),
diode (eg. 1N4001), and a 1k ohm resistor. Alternatively you could also use a relay switch to
connect the valve.
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
Steps:
In the Cayenne Dashboard add a new Valve Switch widget.
Select a virtual channel number for the widget.
Set the VIRTUAL_CHANNEL value below to virtual channel you selected.
Set up your valve schematic and attach it to a digital pin.
Set the ACTUATOR_PIN value below to the digital pin number you selected.
Set the Cayenne authentication info to match the authentication info from the Dashboard.
Compile and upload this sketch.
Once the Arduino connects to the Dashboard you can use the widget button to open and close the valve.
*/
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space #include <CayenneMQTTEthernet.h>
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “----";
char password[] = "";
char clientID[] = "----**”;
#define VIRTUAL_CHANNEL 13 #define ACTUATOR_PIN 13 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN, value);
}`
Hello, I solved my issue by adding the function Ethernet.Maintain() to the beginning of the loop which will always try to get the connection back as soon as it is disconnected.
I tried it and tried many scenarios and seems that the issue is fixed.
Sometimes it gets disconnected but reconnects soon and fast.