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

Return function for BLYNK_WRITE()

Hi,

I’ve use webhook to read data from website and I need to have a return value of 2 parameters ( float webTemp and webHumid) to use in the code for comparison but in other void function, I need some help because I try with return at the end end is not working. The code are:

Code:

BLYNK_WRITE(V0)
  {
  StaticJsonDocument<1024> doc;
  String json = param.asStr();
  //Serial.println("WebHook data:");
  //Serial.println(json);
  //deserializeJson(doc, json);
  float webTemp = doc["main"]["temp"];
  float webHumid = doc ["main"] ["humidity"];
  Serial.println("Temperature= " + String(webTemp/10, 1) + "°C \t Humidity= " + String(webHumid, 1) + "%");
  deserializeJson(doc, json);
 return;
  }

BLYNK_WRITE(V0) is a callback that is triggered when the value of V0 changes on the server.
This change would normally be made from within the app (by changing the status of a widget attached to V0), but it could also be changed via the API.

There is no return value sent back to the server.

The solution in your case would be to write the humidity and temperature values to different virtual pins (or one virtual pin in a formatted string) and retrieve these values via separate API calls (or one additional API call if you are then able to unpack the formatted string data at the other end).

Pete.

Hi Pete,

Thanks for response. To be more clear for me, is that you suggest to create another webhook with another BLYNK_WRITE(V1) to read only temp and humid ?

Are you wanting the temp and humidity values to be available within your code, or to send somewhere else?

If you want them elsewhere within your code then simply use global rather than local variables.

If you want them to display in the app then write them to virtual pins using Blynk.virtualWrite commands.

If you want to send them to an external; server then write them to virtual pins, as above, them read them using an API call (or two).

Pete.

Hi Pete,

I’ve put variable webTemp and webHumid to be global variable, and also I use them inside BLYNK_WRITE(V0). But if I want to read again temp and humid with another function void Website() is not working. I miss something or my declaration are not correct.

On the Blynk Interface from phone are correct viewed. But on serial monitor not, because I call them from global variables that are not “linked” inside the BLYNK_WRITE function.

Entire Code here:

#include <ArduinoJson.h>
#define BLYNK_PRINT Serial
#define BLYNK_MAX_READBYTES 1024

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;

float webTemp;
float webHumid;

char auth[] =
//char ssid[] = 
//char pass[] =
char ssid[] = 
char pass[] = 

BLYNK_WRITE(V0)
  {
  StaticJsonDocument<1024> doc;
  String json = param.asStr();
  deserializeJson(doc, json);
  float webTemp = doc["main"]["temp"];
  float webHumid = doc ["main"] ["humidity"];
 // Serial.println("Temp= " + String(webTemp/10, 1) + "°C \t Humid= " + String(webHumid, 1) + "%");
  Blynk.virtualWrite(V1, webTemp/10);
  Blynk.virtualWrite(V2, webHumid);
  }

void Website()
{
  Serial.println("Temp= " + String(webTemp/10, 1) + "°C \t Humid= " + String(webHumid, 1) + "%");
}

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  Blynk.virtualWrite(V0, "http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=e22f0c6d6e931c14e1dc311fb03dd460");
  timer.setInterval(5000L, Website);
}


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

Result are:

[6575] Connected to WiFi
[6575] IP: 192.168.43.184
[6575] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on ESP8266

[6653] Connecting to blynk-cloud.com:80
[6847] Ready (ping: 55ms).
Temp= 0.0°C 	 Humid= 0.0%

Hi Pete,

Do you have any suggestion about how to declare my variables webTemp and webHumid to be used inside the code for other purpose ? I’ve searching in the forum but I not find anything that can help me.

Thanks in advance.

Declaring them as global variables, then re-declaring them as local again won’t work.
Remove the float declaration from the code in BLYNK_WRITE(V0) to stop the re-declaration…

Pete.