setProperty "abuse"

I was trying to display a “string of time” using this snippet:

  String currentTime = currentHour + ":" + currentMinute;
  
  Blynk.setProperty(V14, "label", currentTime);

But my label displays al sorts of weird stuff, like stuff from Terminal widget, or half names like “BEL” from LABEL. I’m pretty sure this is not the intended use of the setProperty function, but I thought I’d mention it. It would be cool if I could have the Label bet the date or time though. :slight_smile:

Should’t it be

String currentTime = String(currentHour) + ":" + currentMinute;

?

For nice formatting, yes of course, bur that isn’t the point. It doesn’t get displayed in the labeled value widget. It displays really weird, totally unrelated stuff, like things I write to my terminal widget at vpin 127.

Edit

I al ready put in the current(hour) stufd before since I use that somewhere else too.

Edit more

It now also starts printing stuff from serial output …

void displayTime()
{
  // Make into globals, could be usefull anywhere
  int currentMinute = minute();
  int currentHour = hour();

  Blynk.virtualWrite(V16, currentHour);
  Blynk.virtualWrite(V17, currentHour);

  if(currentMinute < 10)
  {
    String currentMinute = "0" + minute();
  }

  if(currentHour < 10)
  {
    String currentHour = "0" + hour();
  }

  String currentTime = currentHour + ":" + currentMinute;
  
  Blynk.setProperty(V14, "label", currentTime);

This is the stary from the function. Nothing fancy as far as I can see

Sure. This is C. That’s mean you can easily shoot in the foot :slight_smile:. Most probably those concatenation is wrong or there is buffer overflow in setProperty. @vshymanskyy should know better.

Should be

String currentMinute = String("0") + minute();

Fixed hat, also String’ed the minute() and hour() but alas, same weird stuff happening. Sometimes it says “:”, “LABEL” or “ABEL”. Very random

int currentMinute
...
String currentMinute

I think problem is this.

Hmm, you could be onto something. The IDE doesn’t mind either way, but I’ll try to put it in separate, correctly initialized strings and see if that helps because everything is messed up now, much more disconnects, unstability etc. And it has been running for months, so it must be something in the code I guess.

I also added all the new (awesome) widgets, but maybe the Mega has some problems with keep up, lol.

void displayTime()
{
  int currentMinute = minute();
  int currentHour = hour();

  String currentMinuteStr, currentHourStr;

  Blynk.virtualWrite(V16, currentHour);
  Blynk.virtualWrite(V17, currentMinute);

  if(currentMinute < 10)
  {
    String currentMinuteStr = String("0") + String(minute());
  }

  if(currentHour < 10)
  {
    String currentHourStr = String("0") + String(hour());
  }

  String currentTimeStr = currentHourStr + ":" + currentMinuteStr;

  Blynk.virtualWrite(V14, currentTimeStr);
  Blynk.setProperty(V14, "label", currentTimeStr);

This is what I got now. I added Value displays at V16 and 17 and they display the correct hour/minute after RTC has synced, but the label only display the “:”. Must be something with the formatting, but I have no idea what, you guys got any idea?

should be

  if(currentMinute < 10)
  {
    currentMinuteStr = String("0") + String(minute());
  }
1 Like

As always I go to fast, thank :slight_smile:

Weird that the IDE doesn’t mention this re-declaration though, but ah well, I’ll put this on the list for tonight :slight_smile:

U r welcome!

Unfortunately, my label still consists of ": ". With this code:

void displayTime()
{
  int currentMinute = minute();
  int currentHour = hour();

  String currentMinuteStr, currentHourStr;

  Blynk.virtualWrite(V16, currentHour);
  Blynk.virtualWrite(V17, currentMinute);

  if(currentMinute < 10)
  {
    currentMinuteStr = String("0") + String(minute());
  }

  if(currentHour < 10)
  {
    currentHourStr = String("0") + String(hour());
  }

  String currentTimeStr = currentHourStr + ":" + currentMinuteStr;

  Blynk.virtualWrite(V14, currentTimeStr);
  Blynk.setProperty(V14, "label", currentTimeStr);

Any other stuff I forgot in this code? I just want to display the time as the label for a labeled value widget, I don’t know why, but it seems to be handy for some sort of things.

Test this code:

void clockvalue() // Digital clock display of the time
{

 int gmthour = hour();
  if (gmthour == 24){
     gmthour = 0;
  }
  String displayhour =   String(gmthour, DEC);
  int hourdigits = displayhour.length();
  if(hourdigits == 1){
    displayhour = "0" + displayhour;
  }
  String displayminute = String(minute(), DEC);
  int minutedigits = displayminute.length();  
  if(minutedigits == 1){
    displayminute = "0" + displayminute;
  }  
  String displaysecond = String(second(), DEC);
  int seconddigits = displaysecond.length();  
  if(seconddigits == 1){
    displaysecond = "0" + displaysecond;
  }
  String displaycurrenttime = displayhour + ":" + displayminute;
  Blynk.virtualWrite(V1, displaycurrenttime); //Display clock using V1
  Blynk.setProperty(V1, "label", displaycurrenttime);
} 

1 Like

Thanx, somehow that did the trick! Got it like this now, for future reference (I integrated the .lenght() in the if, for short code):

  int currentMinute = minute();
  int currentHour = hour();

  String currentMinuteStr = String(currentMinute, DEC);
  String currentHourStr = String(currentHour, DEC);

  Blynk.virtualWrite(V16, currentHour);
  Blynk.virtualWrite(V17, currentMinute);

  if(currentMinuteStr.length() == 1)
  {
    currentMinuteStr = 0 + currentMinuteStr;
  }

  if(currentHourStr.length() == 1)
  {
    currentHourStr = 0 + currentHourStr;
  }
  
  String currentTimeStr = currentHourStr + ":" + currentMinuteStr;

  Blynk.virtualWrite(V14, currentTimeStr);
  Blynk.setProperty(V14, "label", currentTimeStr);

-edit-

Ok Lichtsignaal. moving your topic to Solved now …

Thanks to @Costas, this piece of code come from him.

In that case thanks also go to @Costas :wink: