// Definitions
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "TMPL614f4SA7v"
#define BLYNK_TEMPLATE_NAME "smart home" //Here Device_Name not Template Name
#define BLYNK_AUTH_TOKEN "yA-Iq2Zb_fGBBMO8Kn62tWQEh9ohKLnx"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
//hardWare Specifics
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
char ssid[] = "WIFI@YXY STORE"; // "WIFI NAME";
char pass[] = "password$"; // "WIFI PASSWORD"; // Set password to "" for open networks.
bool fetch_blynk_state = true; //true or false
#define wifiLed LED_BUILTIN //D0int
int wifiFlag = 0;
// (NodeMcu OutPuts)
// define the GPIO connected with lED and Relays
#define RED_LED_Pin 15 //D0 // Look at esp8266's Board , and Look Pin diagram, Here We Using GPIO Number
#define GREEN_LED_Pin 4 //D1 // Look at esp8266's Board , and Look Pin diagram, Here We Using GPIO Number
#define BLUE_LED_Pin 5 //D1 // Look at esp8266's Board , and Look Pin diagram, Here We Using GPIO Number
// the virtual pins
#define VPIN_SWITCH_1 V15 // DataStream Virtual pin for Red LED's Widget Switch
#define VPIN_SWITCH_2 V4 // DataStream Virtual pin for Green LED's Widget Switch
#define VPIN_SWITCH_3 V5 // DataStream Virtual pin for Green LED's Widget Switch
bool RED_LED_State = LOW; //Define integer to remember the toggle state for RED LED
bool GREEN_LED_State = LOW; //Define integer to remember the toggle state for GREEN LED
bool BLUE_LED_State = LOW; //Define integer to remember the toggle state for GREEN LED
BlynkTimer timer;
BLYNK_WRITE(VPIN_SWITCH_1) // Executes when the value of virtual pin 0 changes
{
RED_LED_State = param.asInt();
if( RED_LED_State == 1){ digitalWrite(RED_LED_Pin,HIGH); delay(10);}
if( RED_LED_State == 0){ digitalWrite(RED_LED_Pin,LOW ); delay(10);}
// digitalWrite(RED_LED_Pin, RED_LED_State);
}
BLYNK_WRITE(VPIN_SWITCH_2) // Executes when the value of virtual pin 0 changes
{
GREEN_LED_State = param.asInt();
digitalWrite(GREEN_LED_Pin, GREEN_LED_State);
}
BLYNK_WRITE(VPIN_SWITCH_3) // Executes when the value of virtual pin 0 changes
{
BLUE_LED_State = param.asInt();
digitalWrite(BLUE_LED_Pin, BLUE_LED_State);
}
BLYNK_CONNECTED() {
// Request the latest state from the server
if (fetch_blynk_state){
Blynk.syncVirtual(VPIN_SWITCH_1);
Blynk.syncVirtual(VPIN_SWITCH_2);
Blynk.syncVirtual(VPIN_SWITCH_3);
}
}
void checkBlynkStatus() { // called every 2 seconds by SimpleTimer
bool isconnected = Blynk.connected();
if (isconnected == false) {
Serial.println("Blynk Not Connected");
digitalWrite(wifiLed, HIGH);
}
if (isconnected == true) {
if (!fetch_blynk_state){
Blynk.virtualWrite(VPIN_SWITCH_1, RED_LED_State );
Blynk.virtualWrite(VPIN_SWITCH_2, GREEN_LED_State);
Blynk.virtualWrite(VPIN_SWITCH_3, BLUE_LED_State);
}
wifiFlag = 0;
digitalWrite(wifiLed, LOW);
//Serial.println("Blynk Connected");
}
}
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(wifiLed, OUTPUT);
pinMode(RED_LED_Pin, OUTPUT); // Initialise digital pin 5 as an output pin
pinMode(GREEN_LED_Pin, OUTPUT); // Initialise digital pin 4 as an output pin
pinMode(BLUE_LED_Pin, OUTPUT);
//During Starting all LED are Turned OFF
digitalWrite(RED_LED_Pin, LOW);
digitalWrite(GREEN_LED_Pin, LOW);
digitalWrite(BLUE_LED_Pin, LOW);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
//Setup a function to be called every second
timer.setInterval(2000L, checkBlynkStatus); // check if Blynk server is connected every 2 seconds
delay(1000);
if (!fetch_blynk_state){
Blynk.virtualWrite(VPIN_SWITCH_1, RED_LED_State );
Blynk.virtualWrite(VPIN_SWITCH_2, GREEN_LED_State);
Blynk.virtualWrite(VPIN_SWITCH_3, BLUE_LED_State);
}
}
void loop()
{
Blynk.run();
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
}
Post a Comment