#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
#define BLYNK_TEMPLATE_ID "TMPL9eSiQdOu"
#define BLYNK_DEVICE_NAME "LIGHT"
//#define BLYNK_AUTH_TOKEN "QzmdFgtXRvl5OuNCoIZ5eUXqfpCjFhMS"
#define RelayPin1 26 //D26
//#define RelayPin2 27 //D27
#define SwitchPin1 32 //D32
//#define SwitchPin2 33 //D33
#define wifiLed 25 //D25
#define VPIN_BUTTON_1 V1
//#define VPIN_BUTTON_2 V2
int toggleState_1 = 1; //Define integer to remember the toggle state for relay 1
//int toggleState_2 = 1; //Define integer to remember the toggle state for relay 2
int wifiFlag = 0;
#define AUTH "QzmdFgtXRvl5OuNCoIZ5eUXqfpCjFhMS"
// You should get Auth Token in the Blynk App.
#define WIFI_SSID "123123" //Enter Wifi Name
#define WIFI_PASS "sami1234" //Enter wifi Password
void relayOnOff(int relay){
switch(relay){
case 1:
if(toggleState_1 == 1){
digitalWrite(RelayPin1, LOW); // turn on relay 1
toggleState_1 = 0;
Serial.println("Device1 ON");
}
else{
digitalWrite(RelayPin1, HIGH); // turn off relay 1
toggleState_1 = 1;
Serial.println("Device1 OFF");
}
delay(100);
break;
// case 2:
// if(toggleState_2 == 1){
// digitalWrite(RelayPin2, LOW); // turn on relay 2
// toggleState_2 = 0;
// Serial.println("Device2 ON");
// else{
// digitalWrite(RelayPin2, HIGH); // turn off relay 2
// toggleState_2 = 1;
// }
// Serial.println("Device2 OFF");
// }
// delay(100);
// break;
default : break;
}
}
void with_internet(){
//Manual Switch Control
if (digitalRead(SwitchPin1) == LOW){
delay(200);
relayOnOff(1);
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1); // Update Button Widget
}
//else if (digitalRead(SwitchPin2) == LOW){
///delay(200);
//relayOnOff(2);
// Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2); // Update Button Widget
//}
}
void without_internet(){
//Manual Switch Control
if (digitalRead(SwitchPin1) == LOW){
delay(200);
relayOnOff(1);
}
// else if (digitalRead(SwitchPin2) == LOW){
//delay(200);
//relayOnOff(2);
// }
}
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(VPIN_BUTTON_1);
//Blynk.syncVirtual(VPIN_BUTTON_2);
}
// When App button is pushed - switch the state
BLYNK_WRITE(VPIN_BUTTON_1) {
toggleState_1 = param.asInt();
digitalWrite(RelayPin1, toggleState_1);
}
//BLYNK_WRITE(VPIN_BUTTON_2) {
//toggleState_2 = param.asInt();
//digitalWrite(RelayPin2, toggleState_2);
//}
void checkBlynkStatus() { // called every 3 seconds by SimpleTimer
bool isconnected = Blynk.connected();
if (isconnected == false) {
wifiFlag = 1;
digitalWrite(wifiLed, LOW); //Turn off WiFi LED
}
if (isconnected == true) {
wifiFlag = 0;
digitalWrite(wifiLed, HIGH); //Turn on WiFi LED
}
}
void setup()
{
Serial.begin(9600);
pinMode(RelayPin1, OUTPUT);
//pinMode(RelayPin2, OUTPUT);
pinMode(wifiLed, OUTPUT);
pinMode(SwitchPin1, INPUT_PULLUP);
// pinMode(SwitchPin2, INPUT_PULLUP);
//During Starting all Relays should TURN OFF
digitalWrite(RelayPin1, toggleState_1);
// digitalWrite(RelayPin2, toggleState_2);
WiFi.begin(WIFI_SSID, WIFI_PASS);
timer.setInterval(3000L, checkBlynkStatus);
// check if Blynk server is connected every 3 seconds
Blynk.config(AUTH);
}
void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
Serial.println("WiFi Not Connected");
}
else
{
Serial.println("WiFi Connected");
Blynk.run();
}
timer.run(); // Initiates SimpleTimer
if (wifiFlag == 0)
with_internet();
else
without_internet();
}
@sadia04 please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
Pete.
done
These should be the first two lines of your sketch, otherwise the compiler will assume that it’s a Legacy sketch and try to connect to the Legacy servers.
There are lots of other issues with your sketch, but moving these two lines will fix your connection issues.
Pete.
okay Thank you.
I would like to request you to kindly explain other mistakes, so that I can correct them as well. Your help will be much appreciated.
Is this your sketch, or one written by someone else that you’ve adopted and adapted?
Pete.
I have copied it from a website, basically I want to connect a simple led to a esp32 using blynk. I want to do that to further use IFTTT for google voice assistant control
Which website?
Pete.