i have a code for turning on led in through blynk i have to show how much time the led is turned on for a day and display it in blynk platform
#define BLYNK_TEMPLATE_NAME "led blink"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// Your Wi-Fi credentials
char ssid[] = "YourWiFiSSID";
char password[ ] = "YourWiFiPassword";
// Blynk authentication token
char auth[] = "YourBlynkAuthToken";
// Pin for the LED
const int ledPin = 13; // You can use any GPIO pin
void setup() {
Serial.begin(9600);
// Check if Wi-Fi is connected
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
}
Blynk.begin(auth, ssid, password);
pinMode(ledPin, OUTPUT);
}
void loop() {
Blynk.run();
}
// Blynk function to handle button widget events
BLYNK_WRITE(V0) {
int value = param.asInt(); // Get the value from the Button widget
digitalWrite(ledPin, value); // Turn the LED on/off based on the widget value
}
please give me correct code where i can monitor the duration led in turned on
The way I would approach this would be to set a flag whenever the LED is on.
I’d then use a routine, called once every second by BlynkTimer, that checks if the flag is set to true or false.
If the flag is true then you update the “on count” by one. Your “on count” then shows the cumulative value of how many seconds the LED has been on for.
You’ll need a way to zero the results at midnight, and I guess an Automation may be the simplest way to do this.