[Solved] Writing to dashboard button state in setup

I’m looking for the correct way to set a dashboard button HIGH in the setup part of the code. I’ve tried a simple Blynk.virtualWrite, as well as combining it with a Blynk.run command (both before and after), but the only way I’ve managed to do it is by repeating the commands a few times:

 for(int i=0; i < 3; i++){
    Blynk.run();
    Blynk.virtualWrite(VPIN, HIGH);
    delay(50);
  }

While this method works, I’m pretty sure there must be a simpler way to write to a widget in setup.

@chrome1000 just checked and the following will set V0, V1 and V2 HIGH if you put this in setup() AFTER the Blynk.begin().

   for(int i=0; i < 3; i++){
      Blynk.virtualWrite(i, HIGH);
      delay(50);
    }
1 Like

@chrome1000, you had it right.

void setup(){
 // .. all your other code
Blynk.begin(auth); // normal connection line
Blynk.virtualWrite(VPIN, HIGH);
}

@chrome1000 from the for loop I assumed you wanted to set several pins HIGH but if it’s only one then @jamin’s code is fine.

Do remember that you would also need to follow up with “sync” if you want Blynk to process any HIGH code within the virtual pins at boot time.

@jamin This is what I tried initially, only with Blynk.config replacing Blynk.begin, since I use WiFiManager to handle the wifi credentials. It didn’t work.

@costas I’m only trying to write a single pin HIGH. The loop is there just because it wasn’t working if I only called it once.

Tried this, and still no love:

Blynk.virtualWrite(VPIN, HIGH);
Blynk.syncVirtual(VPIN);

Just to be clear, VPIN is defined as V1, which is also the asignment of my button widget.

The code also seems to work if I put the Blynk.virtualWrite after the FOR loop, but it seems to need the 3X Blynk.run to sort of “jump start” the process.

This is your problem, config is configuration not connection.
I guess that’s why you were trying Blynk.run() in the loop.
I have WiFiManager running so I’ll check what you need.

@chrome1000 in our WiFIManager projects we use the following code to ensure the Blynk connection is live before we start trying to run Blynk functions:

Blynk.config(blynk_token, server); // obtained from WiFiManager
unsigned long blynktimeout = 4000;
unsigned long blynkstart = millis();
while (Blynk.connect() == false) {
 if (millis() > blynkstart + blynktimeout){
    digitalWrite(TX, HIGH);  // active LOW, so OFF
    Serial.println(F("Not connected to Blynk"));
    break;    
 }
}
if(Blynk.connect() == true){
  Blynk.virtualWrite(V5, LOW);
  Blynk.syncVirtual(V5);
  digitalWrite(TX, LOW);  // active LOW, so ON  
}

Aaaaahhhh, thanks, @costas! The Blynk.connect() function is what I needed. I simplified your version a bit, and just allowed it to time out 10 seconds after boot if it’s unable to connect.

while (Blynk.connect() != true && millis() < 10000){
  }
  Blynk.virtualWrite(VPIN, HIGH);
  Blynk.syncVirtual(VPIN);