#include <Arduino.h>
#include <CayenneESP8266Shield.h>
#define SERIAL_BUFFER_SIZE 256
#define SERIAL_TX_BUFFER_SIZE 256
#define SERIAL_RX_BUFFER_SIZE 256
char token[] = "token";
char ssid[] = "wifi";
char password[] = "wifipassword";
#define EspSerial Serial
ESP8266 wifi(EspSerial);
const unsigned long interval = 120000; // milliseconds to check moisture
unsigned long time1 = 0;
unsigned long onlineTime;
unsigned long prevTime;
byte disco = 0;
void setup()
{
EspSerial.begin(9600);
delay(10);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(13, OUTPUT);
Cayenne.begin(token, wifi, ssid, password);
}
void loop()
{
Cayenne.run();
if (millis() - time1 > interval)
{
runMoist();
time1 = millis();
}
}
void runMoist()
{
int x = 2; // pwr pin
int y = 0; // data
int z = 1; // channel
for (byte i = 0; i <= 6; i++)
{
checkMoist(x, y, z);
x++;
y++;
z++;
if (y == 4)
{
y = 6;
}
}
}
void checkMoist(byte pwrPin, byte dataPin, byte channel)
{
digitalWrite(pwrPin, HIGH);
delay(300);
float plant = ( 1024 - analogRead(dataPin) );
byte plantPer = float(plant / 800) * 100; // ~800 is top range for my sensors
digitalWrite(pwrPin, LOW);
Cayenne.virtualWrite(channel, plantPer);
}
CAYENNE_CONNECTED()
{
prevTime = millis();
digitalWrite(13, HIGH);
}
CAYENNE_DISCONNECTED()
{
disco++;
digitalWrite(13, LOW);
}
CAYENNE_OUT(V23)
{
Cayenne.virtualWrite(V23, disco);
}
CAYENNE_OUT(V24)
{
float time2 = ( millis() / 1000 ) / 60;
Cayenne.virtualWrite(V24, time2);
}
CAYENNE_OUT(V25)
{
onlineTime = ( ( millis() - prevTime ) / 1000 ) / 60;
Cayenne.virtualWrite(V25, onlineTime);
}
Comments