/*************************************************************
This is a simple demo of sending and receiving some data.
Be sure to check out other examples!
*************************************************************/
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL3bNX_yrpE"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "0eFcVicdp4FoZFgMgpcXhRXhnl5fhsT_"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
const char* ntpServer = "in.pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 3600;
int autom=0;
int hour=15;
int time_min=18;
int time_max=6;
int red =0;
int green=0;
int yellow =0;
int a=0;
String on="ON";
String off="OFF";
String fuse="FUSED";
int fuse1;
BlynkTimer timer;
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
// Set incoming value from pin V0 to a variable
autom = param.asInt();
// Update state
Blynk.virtualWrite(V1, autom);
}
BLYNK_WRITE(V4)
{
// Set incoming value from pin V0 to a variable
time_min = param.asInt();
// Update state
//Blynk.virtualWrite(V1, autom);
}
BLYNK_WRITE(V6)
{
// Set incoming value from pin V0 to a variable
time_max = param.asInt();
// Update state
//Blynk.virtualWrite(V1, autom);
}
BLYNK_WRITE(V7)
{
// Set incoming value from pin V0 to a variable
red = param.asInt();
// Update state
//Blynk.virtualWrite(V1, autom);
}
BLYNK_WRITE(V8)
{
// Set incoming value from pin V0 to a variable
green = param.asInt();
// Update state
//Blynk.virtualWrite(V1, autom);
}
BLYNK_WRITE(V9)
{
// Set incoming value from pin V0 to a variable
yellow = param.asInt();
// Update state
//Blynk.virtualWrite(V1, autom);
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
// Change Web Link Button message to "Congratulations!"
Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
Blynk.setProperty(V3, "onImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}
// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V2, millis() / 1000);
}
void printLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
Serial.print("Day of week: ");
Serial.println(&timeinfo, "%A");
Serial.print("Month: ");
Serial.println(&timeinfo, "%B");
Serial.print("Day of Month: ");
Serial.println(&timeinfo, "%d");
Serial.print("Year: ");
Serial.println(&timeinfo, "%Y");
Serial.print("Hour: ");
Serial.println(&timeinfo, "%H");
//hour=timeinfo.tm_hour;
}
void streetlight1(){
digitalWrite(13, HIGH);
//Serial.println(analogRead(32));
if (analogRead(32)<4000){
Blynk.virtualWrite(V10, fuse);
Blynk.logEvent("streetlight_fused","Red Streetlight is fused");
}
else{
Blynk.virtualWrite(V10, on);
}
}
void streetlight1_off(){
digitalWrite(13, LOW);
Blynk.virtualWrite(V10, off);
}
void streetlight2(){
digitalWrite(14, HIGH);
if (analogRead(34)<4000){
Blynk.virtualWrite(V11, fuse);
Blynk.logEvent("streetlight_fused","Green Streetlight is fused");
}
else{
Blynk.virtualWrite(V11, on);
}
}
void streetlight2_off(){
digitalWrite(14, LOW);
Blynk.virtualWrite(V11, off);
}
void streetlight3(){
digitalWrite(12, HIGH);
if (analogRead(35)<4000){
Blynk.virtualWrite(V12, fuse);
Blynk.logEvent("streetlight_fused","Yellow Streetlight is fused");
}
else{
Blynk.virtualWrite(V12, on);
}
}
void streetlight3_off(){
digitalWrite(12, LOW);
Blynk.virtualWrite(V12, off);
}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(1000L, myTimerEvent);
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(14, OUTPUT);
pinMode(34, INPUT);
pinMode(35, INPUT);
pinMode(32, INPUT);
}
void timing(){
if((time_min<hour)||(time_max>hour)){
a=1;
//Serial.println("night.....");
}
else{
a=0;
}
}
void loop()
{
timing();
Blynk.run();
timer.run();
if((red==1)||(a==1)){
streetlight1();
}
else{
streetlight1_off();
}
if((green==1)||(a==1)){
streetlight2();
}
else{
streetlight2_off();
}
if((yellow==1)||(a==1)){
streetlight3();
}
else{
streetlight3_off();
}
delay(200);
}