#define BLYNK_TEMPLATE_ID "TMPL3fF62WpXb"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
// Define the LED pin, button pin, and switch pin
#define LED_PIN 4
#define BUTTON_PIN 1
#define SWITCH_PIN 3
// Define the Blynk virtual pins for the button, switch, and uptime
#define BUTTON_VPIN V0;
#define SWITCH_VPIN V1;
#define UPTIME_VPIN V2;
// Create a Blynk object
Blynk blynk;
// Create a button widget and a switch widget
Button button(BUTTON_VPIN);
Switch switchWidget(SWITCH_VPIN);
// Setup the button widget and switch widget
void setup() {
// Initialize Blynk
blynk.begin("YOUR_AUTH_TOKEN", "YOUR_WIFI_SSID", "YOUR_WIFI_PASSWORD");
// Set the LED pin to output mode
pinMode(LED_PIN, OUTPUT);
// Set the button pin to input mode
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Set the switch pin to input mode
pinMode(SWITCH_PIN, INPUT_PULLUP);
// Attach the button widget to the button pin
button.attach(buttonPressed);
// Attach the switch widget to the switch pin
switchWidget.attach(switchChanged);
}
// Loop function
void loop() {
// Run Blynk
blynk.run();
// Check if the button is pressed
if (digitalRead(BUTTON_PIN) == LOW) {
// Toggle the LED state
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}
// Check if the switch is on
if (digitalRead(SWITCH_PIN) == LOW) {
// Turn on the LED
digitalWrite(LED_PIN, HIGH);
} else {
// Turn off the LED
digitalWrite(LED_PIN, LOW);
}
// Send the uptime to Blynk
blynk.virtualWrite(UPTIME_VPIN, millis() / 1000);
}
// Button pressed callback function
void buttonPressed() {
// Toggle the LED state
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}
// Switch changed callback function
void switchChanged() {
// If the switch is on, turn on the LED. Otherwise, turn off the LED.
digitalWrite(LED_PIN, digitalRead(SWITCH_PIN) == LOW);
}