Hi , im trying to turn on a led if the input text i write on blynk its the same as the number of hours and minute of the day .
example : its 12:16 , if i introduce “1216” the led would turn on and im not making it work properly, the virtual 15 allready gives me the time as i need “1216” ( all together ) im failing in the comparision i think,
the pin i used for the input text its v16
i add the code just in case someone notes the error
thank you
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include "TimeLib.h"
#include <WidgetRTC.h>
//#include <Servo.h>
#include <Wire.h>
#include <SPI.h>
String hourminute = hour(), minute ();
int LED = D2 ;
char auth[] = " "; //tokken
char ssid[] = " ";
char pass[] = " ";
BlynkTimer timer;
WidgetRTC rtc;
// Digital clock display of the time
void clockDisplay()
{
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(day()) + " " + month() + " " + year();
}
BLYNK_CONNECTED() {
// Synchronize time on connection
rtc.begin();
Blynk.syncVirtual(V8);
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
timer.setInterval(10000L, clockDisplay);
pinMode (LED, OUTPUT);
}
void loop()
{ Serial.print (hour() + ":" + minute());
Blynk.run();
timer.run();
Blynk.virtualWrite(V15, hour(), minute());// here i can see the display with the minutes and the time all together ( 12:36 ex: 1236)
if (String (hourminute () == param.asStr(V16)) ; ) // here i meant to say if what is written on V16 ( mi input text widget its the same as the hourminute (!236) turn on a led
{ LED (HIGH);}
}
Hi Pete, thank you for your answer, ive changed blynk.virtualWrite for BLYNK_WRITE but now gives me this error.
“BLYNK_WRITE” passed 3 arguments, but takes just 1
BLYNK_WRITE(VPin) is a callback function that is called automatically when the value of the widget attached to the VPin changes.
You don’t call it, or pass any values to it, it gets called automatically.
When you type the numbers in the text input widget and press Send then the BLYNK_WRITE(V16) will be triggered and you can retrieve the value that was sent from the widget using param.asStr command then do the string comparison.
BLYNK_WRITE(VPin) is a function used within your code and triggered automatically when the app is writing data into your device.
If you use a Text Input widget attached to V16 then the function BLYNK_WRITE(V16) will be triggered when the user presses the ‘Send’ button in the widget.
If you wanted to print the data that the user has entered in the text widget top the serial monitor then the code would look like this:
BLYNK_WRITE(V16)
{
String User_Text = param.asStr();
Serial.print("the user just entered ");
Serial.println(User_Text);
}
Pete.
ok i think i get that but i would like to compare the “user_text” to v15 (where is the hours and minutes together) … so the condition would be
if (String (hourminute () == User_text(V16)) {do something} correct?; ) //
thanks again
BLYNK_WRITE(V16) // This function is called whenever state or data changes happen to the widget on V16
{
String User_Text = param.asStr(); // This takes the state or data as a string and applies it to the variable User_Text
Serial.print("the user just entered ");
Serial.println(User_Text);
// This might be able to compare the two strings and run the required code as applicable... My syntax might be off... test it and see.
if (currentTime == User_Text) // compare with previously acquired global variable
{
// do something
} else {
// do nothing
}
}
gunner, thank you very just one more doubt, why now it says "currentTime was not declare, when it is at the beginning of the code
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include "TimeLib.h"
#include <WidgetRTC.h>
#include <Wire.h>
#include <SPI.h>
//String hourminute = hour(), minute ();
int LED = D2 ;
char auth[] = ""; //tokken
char ssid[] = "";
char pass[] = ";
BlynkTimer timer;
WidgetRTC rtc;
// Digital clock display of the time
void clockDisplay()
{
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(day()) + " " + month() + " " + year();
}
BLYNK_CONNECTED() {
// Synchronize time on connection
rtc.begin();
Blynk.syncVirtual(V8);
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
timer.setInterval(10000L, clockDisplay);
pinMode (LED, OUTPUT);
}
void loop()
{ Serial.print (hour() + ":" + minute());
Blynk.run();
timer.run();
}
BLYNK_WRITE(V16) // This function is called whenever state or data changes happen to the widget on V16
{currentTime = hour() + minute();
}
{ String User_Text = param.asStr(); // This takes the state or data as a string and applies it to the variable User_Text
Serial.print("the user just entered ");
Serial.println(User_Text);
// This might be able to compare the two strings and run the required code as applicable... My syntax might be off... test it and see.
if (currentTime == User_Text) // compare with previously acquired global variable
{ digitalWrite (LED,HIGH);
// do something
} else {
// do nothing
}}
}
}
I fixed your code formatting (again). If you can’t find the backtick character on your keyboard then simply copy the three backticks I posted for you earlier.
The reason that you’re getting this error message is that the variable currentTime is declared within the function clockDisplay(). This makes it local to that function, so it can’t be referenced elsewhere within your code.
You should google ‘Variable Scope’ to learn more about this.
The solution is to declare the variable at the top of your code (preferably just after you declare your auth, ssid and pass variables), then when you use that variable within the clockDisplay() function, remove the ‘string’ prefix, as this would have the effect of limiting the value you assign to it to that function.
well ive put String currentTime = String(hour()) + “:” + minute() + “:” + second(); after my credentials as youve said nothing more, could you help me correcting the code? i could learn for noticing were i failed