BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Problem with Blynk.virtualWrite and Time Input Widget

Hi Everyone,

This is my first post, so forgive me if I get anything wrong.

I am working on a scheduler for a thermostat project, and have run into a problem using Blynk.virtualWrite to write a time value back to the Time Input Widget on pins V16, V19, and V22. I am using the same the same code to write back to Value displays on pins V15, V18, and V21, and it works fine. I’ve looked at and tried everything I can find on the Internet, and for the life of me, I can’t seem to make it work. By the way, I did try the “Update Time Input State on UI” example sketch with the same board and UI, and it works fine - which is even more confusing!

I am using a nodeMCU board and iOS UI.

If anyone can shed some light on this it would be much appreciated. Thanks in advance!

Code

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <TimeLib.h>
//#include <WidgetRTC.h>
//#include "DHT.h"

char auth[] = "";
char ssid[] = "";
char pass[] = "";

//DHT dht1(D3, DHT22); //outdoor sensor
//DHT dht2(D4, DHT22); //indoor sensor (main)
//DHT dht3(D5, DHT22); //sleep sensor (sleep)

//BlynkTimer timer;
//WidgetRTC rtc;

// Widgit Setup
WidgetLED wifiLED(V0);
WidgetLED fanLED(V1);
WidgetLED coolLED(V2);
WidgetLED heatLED(V3);

//Variables
int schDayFlag; //1 = Monday...7 = Sunday
long schTimeAway [7]; //1 = Monday...7 = Sunday
long schTimeHome [7]; //1 = Monday...7 = Sunday
long schTimeSleep [7]; //1 = Monday...7 = Sunday
int schTempAway [7]; //1 = Monday...7 = Sunday
int schTempHome [7]; //1 = Monday...7 = Sunday
int schTempSleep [7]; //1 = Monday...7 = Sunday

//Functions
void SchRefreshFN();

BLYNK_WRITE(V14){  //Capture a day index flag
   schDayFlag = (param.asInt()); //capture the index value for day selected in menu
   SchRefreshFN(); //refresh schedule display on UI
}

BLYNK_WRITE(V16){ //Time Input Widget: Save new start time for Away Setting, refresh UI
    schTimeAway[schDayFlag] = param[0].asLong();
    SchRefreshFN();
}

BLYNK_WRITE(V19){ //Time Input Widget: Save new start time for Away Setting, refresh UI
    schTimeHome[schDayFlag] = param[0].asLong();
    SchRefreshFN();
}

BLYNK_WRITE(V22){  //Time Input Widget: Save new start time for Away Setting, refresh UI
    schTimeSleep[schDayFlag] = param[0].asLong();
    SchRefreshFN();
}

BLYNK_WRITE(V17){ //set temp for Away, refresh UI
switch (param.asInt())
  case 1:{
  schTempAway[schDayFlag]++;
  SchRefreshFN();
  break;
  
  case -1:
  schTempAway[schDayFlag]--;
  SchRefreshFN();
  break;
  }
}

BLYNK_WRITE(V20){ //set temp for Home, refresh UI
switch (param.asInt())
  case 1:{
  schTempHome[schDayFlag]++;
  SchRefreshFN();
  break;
  
  case -1:
  schTempHome[schDayFlag]--;
  SchRefreshFN();
  break;
  }
}

BLYNK_WRITE(V23){ //set temp for Sleep, refresh UI
switch (param.asInt())
  case 1:{
  schTempSleep[schDayFlag]++;
  SchRefreshFN();
  break;
  
  case -1:
  schTempSleep[schDayFlag]--;
  SchRefreshFN();
  break;
  }
}

void SchRefreshFN(){ //, refresh UI
   switch (schDayFlag)
    case 1:{
      Blynk.virtualWrite(V15, schTempAway[1]);
      Blynk.virtualWrite(V18, schTempHome[1]);
      Blynk.virtualWrite(V21, schTempSleep[1]);
      Blynk.virtualWrite(V16, schTimeAway[1]);
      Blynk.virtualWrite(V19, schTimeHome[1]);
      Blynk.virtualWrite(V22, schTimeSleep[1]);
      break;

    case 2:  
      Blynk.virtualWrite(V15, schTempAway[2]);
      Blynk.virtualWrite(V18, schTempHome[2]);
      Blynk.virtualWrite(V21, schTempSleep[2]);
      Blynk.virtualWrite(V16, schTimeAway[2]);
      Blynk.virtualWrite(V19, schTimeHome[2]);
      Blynk.virtualWrite(V22, schTimeSleep[2]);
      break;

    case 3:
      Blynk.virtualWrite(V15, schTempAway[3]);
      Blynk.virtualWrite(V18, schTempHome[3]);
      Blynk.virtualWrite(V21, schTempSleep[3]);
      Blynk.virtualWrite(V16, schTimeAway[3]);
      Blynk.virtualWrite(V19, schTimeHome[3]);
      Blynk.virtualWrite(V22, schTimeSleep[3]);
      break;

    case 4:
      Blynk.virtualWrite(V15, schTempAway[4]);
      Blynk.virtualWrite(V18, schTempHome[4]);
      Blynk.virtualWrite(V21, schTempSleep[4]);
      Blynk.virtualWrite(V16, schTimeAway[4]);
      Blynk.virtualWrite(V19, schTimeHome[4]);
      Blynk.virtualWrite(V22, schTimeSleep[4]);
      break;
    
    case 5:
      Blynk.virtualWrite(V15, schTempAway[5]);
      Blynk.virtualWrite(V18, schTempHome[5]);
      Blynk.virtualWrite(V21, schTempSleep[5]);
      Blynk.virtualWrite(V16, schTimeAway[5]);
      Blynk.virtualWrite(V19, schTimeHome[5]);
      Blynk.virtualWrite(V22, schTimeSleep[5]);
      break;
    
    case 6: 
      Blynk.virtualWrite(V15, schTempAway[6]);
      Blynk.virtualWrite(V18, schTempHome[6]);
      Blynk.virtualWrite(V21, schTempSleep[6]);
      Blynk.virtualWrite(V16, schTimeAway[6]);
      Blynk.virtualWrite(V19, schTimeHome[6]);
      Blynk.virtualWrite(V22, schTimeSleep[6]);
      break;
    
    case 7: 
      Blynk.virtualWrite(V15, schTempAway[7]);
      Blynk.virtualWrite(V18, schTempHome[7]);
      Blynk.virtualWrite(V21, schTempSleep[7]);
      Blynk.virtualWrite(V16, schTimeAway[7]);
      Blynk.virtualWrite(V19, schTimeHome[7]);
      Blynk.virtualWrite(V22, schTimeSleep[7]);
      break;
    }
}

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  while (Blynk.connect() == false) {}// wait for Blynk to connect
  //rtc.begin();
  Blynk.virtualWrite(V24,"Away Temperature");
  Blynk.virtualWrite(V25,"Home Temperature");
  Blynk.virtualWrite(V26,"Sleep Temperature");
}

void loop() {
  Blynk.run();
}

That’s rather different from the syntax used in the example you quoted…

  Blynk.virtualWrite(V1, startAt, stopAt, tz);

  //you may also pass day
  //char days[] = "1"; //Monday
  //Blynk.virtualWrite(V1, startAt, stopAt, tz, days);

  //or days
  //char days[] = "1,2,3"; //Monday, Tuesday, Wednesday
  //Blynk.virtualWrite(V1, startAt, stopAt, tz, days);

Pete.

Can you elaborate on what is different about it? The only thing I can see is that the example uses char, and I am using long. I tested this by substituting “long” in the example and it worked fine. Am I missing something here?

Well, the number of parameters that you’re passing to begin with.
The example uses 5 (the virtual pin identifier plus 4 others), yours uses 2.

Pete.

Yup. I tried the example with just one and it worked fine.

Hi Pete,

I went back and checked again, and found that you were correct! I change the Blynk.virtualWrite command from Blynk.virtualWrite(V16, schTimeAway[1]); to Blynk.virtualWrite(V16, schTimeAway[1],0,0);, and it worked - almost. In order for the UI to update to the new value, I have to toggle from “play mode” to setup mode" and back to “play mode”. Only then does the Value Display update. Any thoughts?

Charlie

I’m still confused about the way that you’re using parameters.

It clearly states that the format is:

Blynk.virtualWrite(V1, startAt, stopAt, tz, days);

yet your schTimeAway clearly represents day numbers in your code, but you’re passing 0 as the days parameter:

Blynk.virtualWrite(V16, schTimeAway[1],0,0);

Pete.

Hi Pete,

Actually, I’m not passing a day code at all. The variable schTimeAway is the start time, the first zero is the stop time, and the next zero is the timezone. Similar to the line in the example code: “Blynk.virtualWrite(V1, startAt, stopAt, tz);” This actually seems to work, but I have to toggle the UI.

Incidentally, when I run the example code exactly as written, I have to toggle the UI to get that to work as well. Is there. way to force this “toggle” in code?

Hey Pete,

So I just added a 3rd zero for the days code, “Blynk.virtualWrite(V16, schTimeAway[1],0,0,0);” and it works as expected! Apparently, you need all of the parameters to have a value even if they are just 0. Thanks for your help!

Charlie

Hmm, why am I not surprised?

Glad it’s fixed though.

Pete.

1 Like