/*************************************************************
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 "TMPL21qgBEpSz"
#define BLYNK_TEMPLATE_NAME "IoT LED"
#define BLYNK_AUTH_TOKEN "MrmNT3ptzG2D4hu4MlDiiY4__xzYowuQ"
/* 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[] = "";
BlynkTimer timer; // BLYNK TIMER INITIALIZATION...
/* ======= PUSH BUTTON DEFINITION ======= */
#define BUTTON_PIN 13 // GIOP21 pin connected to button
// Variables will change:
int lastState = HIGH; // the previous state from the input pin
int currentState; // the current reading from the input pin
int virtual_State1 = 1; // Initial state of Blynk Virtual Pin
int virtual_State2;
/* ---------------------------------------*/
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0) // This function sends data from Blynk to the Device
{
// Set incoming value from pin V0 to a variable
int value = param.asInt();
/* === THE DEVICE SENDS DATA TO BLYNK with (Blynk.virtualWrite) === */
// Update state with data from the device...
//Blynk.virtualWrite(V1, value); // This state data for V1 (Switch Value)
Serial.println(value);
Serial.println("The button is pressed");
// For the LED
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
delay(500);
}
// 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);
}
/* ============================ SETUP CODE ============================ */
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
/* ======= PUSH BUTTON SETUP CODE ======= */
// initialize the pushbutton pin as an pull-down input
// the pull-up input pin will be LOW when the switch is open and HIGH when the switch is closed.
pinMode(BUTTON_PIN, INPUT_PULLDOWN);
// Set another Pin as an OUTPUT so that it can be used by the Blynk button
pinMode(12, OUTPUT);
// Set another Pin as an OUTPUT so that it can be used by the Blynk button
pinMode(5, OUTPUT);
}
/* =========================== LOOP CODE ============================== */
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!
/* ========= PUSH BUTTON CODE ========= */
// For the Push-Button
// read the state of the switch/button:
int buttonState = digitalRead(BUTTON_PIN);
// Update state with data from the device...
// Blynk.virtualWrite(V0, buttonState);
// print out the button's state
Serial.println(buttonState);
// A state check
if (buttonState == 1){
Serial.println("The button is pressed");
// Update state with data from the device...
//Blynk.virtualWrite(V1, buttonState);
digitalWrite(12, HIGH);
}
else{
digitalWrite(12, LOW);
}
}