Arduino Uno disconnects when operating the relay or valve sometimes


#1

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:-

[1240269] Connecting to arduino.mydevices.com:8442
[1255288] Connecting to arduino.mydevices.com:8442
[1270308] Connecting to arduino.mydevices.com:8442
[1285327] Connecting to arduino.mydevices.com:8442
[1300347] Connecting to arduino.mydevices.com:8442

And then after some seconds it is connected again or sometimes I need to power it off then on.

Anyone can help?!

Thanks
Rassim


#2

@rassim803 welcome to cayenne community.
can you post the code you are using.


#3

Thanks @shramiksalgaonkar 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);
}
##################

#4

is it fine with you to move to MQTT as it is more stable.


#5

I don’t have much info about MQTT, as I am new to Arduino world nearly one month.


#6

have a look at this https://mydevices.com/cayenne/docs/bring-your-own-thing-api/#cayenne-mqtt-api-using-arduino-mqtt


#7

Thanks @shramiksalgaonkar , I will check and give you a feedback but will this be helpful?!! Do you think so?!!


#8

Thanks @shramiksalgaonkar, I added my device using MQTT and seems to be stable connection.

But when I tried to connect relay, I figured out that the output voltage from the pin is 0.38v when it is high and not 5v as required.

Also if you can help how to use relays, virtual pins so I can insert my own formula for calculation s.

Thanks


#9

@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.


#10

add a custom output widget on your dashboard with channel 1.

 CAYENNE_IN(1)
{
 int currentValue = getValue.asInt(); 
if (currentValue == 1) 
{
 digitalWrite(4,HIGH); 
}
 else
 { 
digitalWrite(4,LOW);
 }
}

#11

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.

Is this possible?

[126860] Heartbeat timeout
This should print first, then wait 5 seconds
[129863] Connecting to arduino.mydevices.com:8442
This should print second, then wait 5 seconds
[134864] Connecting to arduino.mydevices.com:8442
This should print first, then wait 5 seconds
[139865] Connecting to arduino.mydevices.com:8442
This should print second, then wait 5 seconds
[144866] Connecting to arduino.mydevices.com:8442
This should print first, then wait 5 seconds
[149867] Connecting to arduino.mydevices.com:8442
\

And what to do in such cases as the device may be in remote location and have no easy access to it.

Thanks @rsiegel


#12

Thanks @shramiksalgaonkar 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.

Appreciate your feedback.


#13

@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


#14

Also Please check the below image, it got disconnected and trying to connect for almost two hours but not!!!

Could you help please @shramiksalgaonkar @rsiegel


Thanks for help


#15

And it is not resolved yet @rsiegel why it is marked as resolved>???


#16

Could you post your code again as it currently stands? That might help me find where it’s potentially timing out.


#17

Thanks @rsiegel please find it.Timer added to the New Full Code With Fuel Guage.txt (2.9 KB)


#18

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?


#19

Hello @adam

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:

  1. In the Cayenne Dashboard add a new Valve Switch widget.
  2. Select a virtual channel number for the widget.
  3. Set the VIRTUAL_CHANNEL value below to virtual channel you selected.
  4. Set up your valve schematic and attach it to a digital pin.
  5. Set the ACTUATOR_PIN value below to the digital pin number you selected.
  6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  7. Compile and upload this sketch.
  8. 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.

void setup()
{
Serial.begin(9600);
pinMode(ACTUATOR_PIN, OUTPUT);
Cayenne.begin(username, password, clientID);
}

void loop()
{
Cayenne.loop();
}

// 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);
}`


#20

HI All, @shramiksalgaonkar @adam @rsiegel

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.

Thanks
Rassim