How could i compare this two strings

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);}

}

I’ve fixed the formatting of your code. You need to use three backticks at the start and end of your code. Three backticks look like this:
```

You cannot have Blynk.virtualWrites in your void loop. Read this:

You need to be using the BLYNK_WRITE(V15) callback to capture the value from your text input widget on V15 and do the string comparison.

Pete.

1 Like

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(V15, hour(), minute());/

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.

Pete.

1 Like

Sorry Pete so that would be BLYNK_WRITE (hour (), minute()); ? without the virtual pin and later specify it on the blynk widget right?

Okay, let me try to explain again.

BLYNK_WRITE(VPin) does not write data to the app.

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.
1 Like

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

I don’t think that syntax will work.

Pete.

1 Like

Read the comments for more clarity

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
  }

}
2 Likes

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
  }}

}
}

sorry why is it that says that its not declare CurrentTIme when it is as a string at the beginning?,sorry im new on this .

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.

Pete.

1 Like

Pete
if i leave the void clock display this way (

void clockDisplay()
{
   currentTime = (hour()) + ":" + minute() + ":" + second();
   currentDate = (day()) + " " + month()  + " " + year();
}

without the string it says invalid operands of types ‘const char*’ and ‘const char [2]’ to binary ‘operator+’

How have you declared your variables?

Pete.

1 Like

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

You need this after your credentials:

String currentTime;

This declares the variable currentTime as a global variable of type String.

As I said earlier, you should google ‘Variable Scope’.

You also need to have your clockDisplay function looking like this:

void clockDisplay()
{
  currentTime = String(hour()) + ":" + minute() + ":" + second();
  currentDate = String(day()) + " " + month()  + " " + year();
}

The String() commands that are in there are converting char variable types to Strings. This is different to String without the brackets.

Pete.

2 Likes

THANK YOU IT WORKED FINE!!!

1 Like