It would seem that you have an internet connection that is / should be always on (when not experiencing issues); possibly consider an option like the following:
Send a command to the Arduino and have it reply with a status (Ping…Pong!)
The code below will turn on the ElectroValve, wait 150,000ms and then turn it off, reporting to Cayenne each action.
In your Arduino definitions:
#define TriggerChannel 1 // 1 should equal your actual pin
#define ElectroValvePin 4 // 4 should equal your actual pin
//Set Cayenne VirtualChannel numbers to friendly names (can be anything).
#define CayenneOnOffDisplay 2 // change to your channel - Indicator Off/On
#define CayenneGraphDisplay 3 // optional, reports channel status as "voltage" so you can show a graph in project
In your Arduino setup:
Long cycleTime = 150000;
pinMode(ElectroValvePin, OUTPUT);
In your Arduino loop code:
CAYENNE_IN(TriggerChannel)
{
//Turn on the Electro Valve / Pump
digitalWrite(ElectroValvePin, HIGH);
// Optional - to simulate switch / start time
delay(1000);
/* Read the actual value from the ElectroValvePin and send an indicating
signal back to cayenne on CayenneOnOffDisplay indicating the pump is turned on.
*/
Cayenne.virtualWrite(CayenneOnOffDisplay, digitalRead(ElectroValvePin),"digital_actuator","d");
// Same as above but can be displayed as a graph over time in Cayenne...
Cayenne.virtualWrite(CayenneGraphDisplay, digitalRead(ElectroValvePin), "voltage","v");
// leave the pump on until the cycle timer expires
delay(cycleTime);
digitalWrite(ElectroValvePin, HIGH); //Turn off the Electro Valve / Pump
// report to Cayenne the actual state of the output
Cayenne.virtualWrite(CayenneOnOffDisplay, digitalRead(ElectroValvePin),"digital_actuator","d");
// Same as above but can be displayed as a graph over time in Cayenne...
Cayenne.virtualWrite(CayenneGraphDisplay, digitalRead(ElectroValvePin), "voltage","v");
}
Notes:
1: The first time the Arduino reports using this code it will create the appropriate widget, at which point you can “plus” it into your project permanently.
2: If the original widget doesn’t offer a graph you can delete the widget in cayenne, and change ‘digital_actuator’ to ‘digital_sensor’ and that widget will offer a data graph.
3: If you prefer the graph and its ability to see history, you only need 1 Cayenne VirtualChannel, and can remove all lines containing CayenneOnOffDisplay
Let me know if this works and what you think?!?
This is my first coding suggestion, new IoT programmer.