#define BLYNK_TEMPLATE_ID "x"
#define BLYNK_DEVICE_NAME "Shed Light Relays"
#define BLYNK_AUTH_TOKEN "x"
#define BLYNK_PRINT Serial
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SPI.h>
#include <WidgetRTC.h> //blynk time server
WidgetRTC rtc;
char auth [] = BLYNK_AUTH_TOKEN;
int extlight;
int lightcheck;
int shed1check;
int shed2check;
BlynkTimer timer;
BLYNK_CONNECTED()
{
// Synchronize time on connection
rtc.begin();
Blynk.syncVirtual(V0); //both lights
Blynk.syncVirtual(V1); //shed1 lights
Blynk.syncVirtual(V2); //shed2 lights
}
byte mac[] = {0xAX, 0x61, 0x0X, 0xAE, 0xXX, 0xXX}; //specific to the new ethernet shield
IPAddress ip ( 10, 20, 10, 18);
IPAddress myDns(1, 1, 1, 1);
IPAddress gateway(10, 20, 10, 1);
IPAddress subnet(255, 255, 248, 0);
////////////////////////////////////////
void setup()
{
Serial.begin(9600);
Ethernet.init(10);
Ethernet.begin(mac, ip, myDns, gateway, subnet);
// Blynk.begin(auth, "blynk.cloud", 8080);
Blynk.config(auth, "blynk.cloud", 8080); // Initialise the Blynk connection settings
Blynk.connect();
pinMode(3, OUTPUT); //relay 1
pinMode(5, OUTPUT); //relay 2
pinMode(6, INPUT_PULLUP ); //shed1 light switch
pinMode(9, INPUT_PULLUP ); //shed2 light switch
timer.setTimeout(3600000L, [] () {} ); // dummy/sacrificial Function timers with ID 0 cannot be turned on/off
timer.setInterval(500, Timer); // Miliseconds to seconds, every 1/2 second run function which counts time and potentially turns the light off
}
////////////////////////////////////////
void loop()
{
Blynk.run();
timer.run();
}
////////////////////////////////////////
BLYNK_WRITE(0)//Lights on via Blynk
{ if ( param.asInt() == 1 )
{
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
extlight = 0;
lightcheck = 1;
}
else
{
digitalWrite(3, LOW);
digitalWrite(5, LOW);
extlight = 0;
lightcheck = 0;
}
}
////////////////////////////////////////
void Timer()
{
Serial.println("Entered Timer"); //just here to test with no ethernet connection
//Checking to turn off light switches
if (lightcheck == 1)
{
extlight ++;
if(extlight >= 600) //if 5 minutes has passed turn the light off. (600 because timer runs every 1/2 second)
{
digitalWrite(3, LOW);
digitalWrite(5, LOW);
Blynk.virtualWrite(0, LOW); //V0
extlight = 0;
lightcheck =0;
}
}
if (digitalRead(6) == LOW) //Shed1 Switch, goes low when switch is pressed //ON
{
digitalWrite(3, HIGH); //Shed1 Relay
shed1check = 1;
Blynk.virtualWrite(V1, HIGH);
}
if (digitalRead(6) == HIGH && shed1check == 1) //Shed1 Switch //OFF
{
digitalWrite(3, LOW); //Shed1 Relay
shed1check = 0;
Blynk.virtualWrite(V1, LOW);
}
///////////////////
if (digitalRead(9) == LOW) //Shed2 Switch, goes low when switch is pressed //ON
{
digitalWrite(5, HIGH); // Shed2 Relay
shed2check = 1;
Blynk.virtualWrite(V2, HIGH);
}
if (digitalRead(9) == HIGH && shed2check == 1) //Shed2 Switch //OFF
{
digitalWrite(5, LOW); //Shed2 Relay
shed2check = 0;
Blynk.virtualWrite(V2, LOW);
}
}
Blynk gets to:
22:53:58.045 → [5356751] Connecting to blynk.cloud:8080