Last active
December 12, 2022 18:15
-
-
Save sucheendradas/0de6bdcc8fe97707bd0d53ab8ea50488 to your computer and use it in GitHub Desktop.
Blynk 2.0 working with esp8266 IOT Board (Arduino IDE 2.0)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 "TMPLB_PpB7qe" // template ID Not Template ID | |
#define BLYNK_DEVICE_NAME "esp8266" //Here Device_Name not Template Name | |
#define BLYNK_AUTH_TOKEN "n5x628GDTFMk0hNa-69lhHK94hPSYX_a" | |
// 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[] = "Sucheendradas"; // "WIFI NAME"; | |
char pass[] = "55555555"; // "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 16 //D0 // Look at esp8266's Board , and Look Pin diagram, Here We Using GPIO Number | |
#define GREEN_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 V0 // DataStream Virtual pin for Red LED's Widget Switch | |
#define VPIN_SWITCH_2 V1 // 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 | |
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_CONNECTED() { | |
// Request the latest state from the server | |
if (fetch_blynk_state){ | |
Blynk.syncVirtual(VPIN_SWITCH_1); | |
Blynk.syncVirtual(VPIN_SWITCH_2); | |
} | |
} | |
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); | |
} | |
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 | |
//During Starting all LED are Turned OFF | |
digitalWrite(RED_LED_Pin, LOW); | |
digitalWrite(GREEN_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); | |
} | |
} | |
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! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment