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

How can I run BLYNK_WRITE in a loop?

Hi,
I want that the BLYNK_WRITE function will run in a loop?
Can somebody tell me, how can I do this?
I don’t mean that it only will run when I move the slider.
It should run in a loop.
Thanks for help.

Here is the code:

#include "DHT.h"
#define DHTPIN 0     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
int Thermostat = 16;
 

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxx";
char pass[] = "xxxxxxxxxxxxx";
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer; 
// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin 1

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  Serial.print("V1 Slider value is: ");
  Serial.println(pinValue);
  float h = dht.readHumidity();    // Lesen der Luftfeuchtigkeit und speichern in die Variable h
  float t = dht.readTemperature(); // Lesen der Temperatur in °C und speichern in die Variable t
  
/*********************( Überprüfen ob alles richtig Ausgelesen wurde )*********************/ 
  if (isnan(h) || isnan(t)) {       
    Serial.println("Fehler beim auslesen des Sensors!");
    return;
  }

  // Nun senden wir die gemessenen Werte an den PC dise werden wir im Seriellem Monitor sehen
  Serial.print("Luftfeuchtigkeit: ");
  Serial.print(h);                  // Ausgeben der Luftfeuchtigkeit
  Serial.print("%\t");              // Tabulator
  Serial.print("Temperatur: ");
  Serial.print(t);                  // Ausgeben der Temperatur
  Serial.write('°');                // Schreiben des ° Zeichen
  Serial.println("C");
    if (t > pinValue)
  {
    digitalWrite(Thermostat, LOW);
  }
  else 
  {
    digitalWrite(Thermostat, HIGH);
  }
}

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  dht.begin();
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
  pinMode(Thermostat, OUTPUT);
  timer.setInterval(1000L, sendSensor);
}

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

I think you will have it easy then, blynk_write IS in a loop.

to be exact, here:

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

I don’t know the details but during blynk run all the ‘channels’ and pins are checked for updates. This is when the write function is called IF that pin has changed.

I’m guessing however you want to have it run regardless of the ‘IF’ in that case do NOT put it in there. Create a separate function which you put on a timer (you do NOT want to have sensor readout a few 1000 times per second) and you can also call this function in the ‘write’ function.
http://docs.blynk.cc/#blynk-firmware-blynktimer

So you mean that I put a function inside that:
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("V1 Slider value is: ");Preformatted text
Serial.println(pinValue);
float h = dht.readHumidity(); // Lesen der Luftfeuchtigkeit und speichern in die Variable h
float t = dht.readTemperature(); // Lesen der Temperatur in °C und speichern in die Variable t

                  if (isnan(h) || isnan(t)) {       
                    Serial.println("Fehler beim auslesen des Sensors!");
                    return;
                  }

                  Serial.print("Luftfeuchtigkeit: ");
                  Serial.print(h);                  // Ausgeben der Luftfeuchtigkeit
                  Serial.print("%\t");              // Tabulator
                  Serial.print("Temperatur: ");
                  Serial.print(t);                  // Ausgeben der Temperatur
                  Serial.write('°');                // Schreiben des ° Zeichen
                  Serial.println("C");
                    if (t > pinValue)
                  {
                    digitalWrite(Thermostat, LOW);
                  }
                  else 
                  {
                    digitalWrite(Thermostat, HIGH);
                  }
                }

i don’t know how to do that but you could just store the value recieved in the form of param.asInt() in a global integer and then create a void Function that does what your BLYNK_WRITE does and then you can just call that funtion using BlynkTImer again and again.

How can I call this function in the ‘write’ function?

let say you have the function

void test(){
  Serial.println("test");
}

then in blynk_write you just put

test();

and it will run.

@Psuch BLYNK_WRITE(vPin) is a function (AKA handler) that gets called whenever the associated vPin state/value changes. You can run code inside this function just like any other Arduino function.

To call this function from within the script itself, you simply use Blynk.virtualWrite(vPin, data) to send the required state/value change to the associated widget (this step is optional), followed by a Blynk.syncVirtual(vPin) to emulate the widget calling the function and requesting the Server to refresh that function with whatever was the last vPin state/value…

Don’t forget to read through the Documents…

http://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynksyncvirtualvpin

image

So Blynk.syncVirtual(vPin) is refrashing BLYNK_WRITE ?
And when it is so, where should it be put in the code?

Thanks guys I got it.

how did you do that. I experienced the same thing. Can you share the coding example?

@Wahyudi if you’ve read the rest of this topic, you should understand that BLYNK_WRITE(vPin) is a special callback function which is called whenever the value of the corresponding virtual pin changes on the Blynk server.
It is triggered once, automatically, and cannot be called as part of a loop function.

If, once the value of the virtual pin changes on the server, you want to run a process multiple times then you need to use the BLYNK_WRITE(vPin) to initialise a timer or to set a flag so that a process which is already running via a timer behaves differently when the state of the flag is tested.

I’m going to close this 2-year old topic. If you have a specific issue that isn’t answered by this topic then I’d suggest that you start a new topic and provide all of the necessary information to allow is to understand the hardware and software that you are using, and exactly what the issue you are experiencing is.

Pete.