Code is below.
include <CayenneMQTTEthernet.h>
#define VIRTUAL_CHANNEL_1 3
#define VIRTUAL_CHANNEL_2 4
#define ACTUATOR_PIN_5 5
#define ACTUATOR_PIN_6 6
const int AnalogPin = 1;
float Waterlevel, WaterDistance, distance, level;
long AnalogVolt;
float Waterfull = 30;
float Waterempty = 140;
unsigned int lastMillis;
boolean currentValue, getValue;
char username = “”;
char password = “”;
char clientID = “”;
byte mac = { 0x90, 0xA2, 0xDA, 0x0D, 0x68, 0x9E };
IPAddress ip(192, 168, 1, 175);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
EthernetClient client;
void setup()
{
//Serial.begin(9600);
delay(10);
digitalWrite(ACTUATOR_PIN_5, HIGH);
digitalWrite(ACTUATOR_PIN_6, HIGH);
pinMode(ACTUATOR_PIN_5, OUTPUT);
pinMode(ACTUATOR_PIN_6, OUTPUT);
Cayenne.begin(username, password, clientID);
Ethernet.begin(mac, ip);
}
void loop()
{
Cayenne.loop();
if (millis() - lastMillis > 5000) {
lastMillis = millis();
readMaxbotix();
Waterlevel = map(WaterDistance, Waterfull, Waterempty, 100, 0);
Cayenne.virtualWrite(0, millis());
Cayenne.virtualWrite(1, Waterlevel, “tl”, “null”);
Cayenne.virtualWrite(2, WaterDistance, TYPE_PROXIMITY, UNIT_CENTIMETER);
}
}
void readMaxbotix(){
AnalogVolt = analogRead(AnalogPin);
distance = AnalogVolt * 5;
WaterDistance = distance/10;
}
CAYENNE_IN(VIRTUAL_CHANNEL_1)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1
// assuming you wire your relay as normally open
if (currentValue == 0) {
digitalWrite(ACTUATOR_PIN_5, HIGH);
} else {
digitalWrite(ACTUATOR_PIN_5, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_2)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1
// assuming you wire your relay as normally open
if (currentValue == 0) {
digitalWrite(ACTUATOR_PIN_6, HIGH);
} else {
digitalWrite(ACTUATOR_PIN_6, LOW);
}
}