Hi all
I’ve bought an annual license of new Blynk but I used legacy Blynk for some prototypes
Which is the fastest and best way to send values between devices? The old bridge is not here anymore and http get sometimes brings my device offline.
Any suggestion?
John93
November 10, 2021, 12:26pm
2
Hey there, I think you should try automation
1 Like
It depends what you are trying to achieve, and what hardware you are using. You can use the HTTP(S) API to send values from one device to another, but you’ll probably need to be using an ESP based device at the ‘sending’ end.
Pete.
1 Like
They are both esp32 devices. It works but sometimes the sender goes offline
Http GET method to update virtual pin on device 2
#define BLYNK_TEMPLATE_ID "xxx"
#define BLYNK_DEVICE_NAME "xxx"
#define BLYNK_AUTH_TOKEN "TOKEN_OF_CURRENT_DEVICE"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "Adafruit_SHT31.h"
#include <HTTPClient.h>
float temp=0;
int umidita=0;
int temp_riferimento=0;
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ssid";
char pass[] = "pwd";
BlynkTimer timer;
String server_name = "http://fra1.blynk.cloud/external/api/"; // <<< Server Fra1 di Blynk
Adafruit_SHT31 sht31 = Adafruit_SHT31();
String bridge_token_1 = "TOKEN_RECEIVER"; // token for the receiving device
BLYNK_CONNECTED() {
// Request Blynk server to re-send latest values for all pins
Blynk.syncAll();
Blynk.syncVirtual(V2);
}
BLYNK_WRITE(V2) // synch by app
{
temp_riferimento = param.asInt();
}
void setup()
{
Serial.begin(9600);
delay(100);
Blynk.begin(auth, ssid, pass);
Serial.println("SHT31 test");
if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
Serial.println("SONDA SHT31 non disponibile");
}
timer.setInterval(3000L, SHTReadTemp);
timer.setInterval(4000L, SHTReadHum);
timer.setInterval(5000L, checkTemp);
}
void loop()
{
Blynk.run();
if(Blynk.connected()){
timer.run();
}
}
void SHTReadTemp(){
temp = sht31.readTemperature();
Blynk.virtualWrite(V0, temp);
if (! isnan(temp)) { // check if 'is not a number'
Serial.print("Temp *C = "); Serial.print(temp); Serial.print("\t\t");
} else {
Serial.println("Failed to read temperature");
}
Serial.print("Temperatura:");
Serial.print(temp);
if(Blynk.connected()){
api_bridge(bridge_token_1,1,temp); // Token for receiving device, virtual pin number, value to send
}
}
void SHTReadHum(){
umidita = sht31.readHumidity();
if(umidita!=0){
Blynk.virtualWrite(V1, umidita);
}
if (! isnan(umidita)) { // check if 'is not a number'
Serial.print("Hum. % = "); Serial.println(umidita);
} else {
Serial.println("Failed to read humidity");
}
Serial.print("Umidita:");
Serial.print(umidita);
}
void api_bridge(String token, int virtual_pin, float value_to_send)
{
HTTPClient http;
String server_path = server_name + "update?token=" + token + "&pin=v" + String(virtual_pin) + "&value=" + float(value_to_send);
// Your Domain name with URL path or IP address with path
http.begin(server_path.c_str());
// Send HTTP GET request
Serial.println("Sending ");
Serial.print(value_to_send);
Serial.print(" to pin V");
Serial.println(virtual_pin);
long request_time = millis();
int httpResponseCode = http.GET();
if (httpResponseCode>0)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
}
else
{
Serial.print("Error code: ");
Serial.print(httpResponseCode);
Serial.print(" <-----------------------------------");
}
Serial.print("Response time = ");
Serial.print(millis() - request_time);
Serial.println(" milliseconds");
Serial.println();
// Free resources
http.end();
}
void checkTemp(){
if (temp <= temp_riferimento){
Blynk.logEvent("myEvent", "Temperatura inferiore a minima impostata!{DEVICE_NAME}");
Serial.println("ALLARME Temperatura!");
}
Serial.println("Temp Riferimento = ");
Serial.print(temp_riferimento);
}
One problem is that sometimes http GET takes too much time and heartbeat falls, next it is impossible to auto reconnect to Blynk cloud
What happens if you comment-out the other timers and just read the temperature, and maybe extend the reading interval to around 10 seconds?
Also, why are you doing Blynk.syncAll()
in BLYNK_CONNECTED
, and why are you sending the readings to your virtual pins before doing the NAN test?
Pete.
It works, the only problem is the http get that sometimes takes too long but it is a blocking function
The syncAll() because I will implement some virtual pins
And sending readings before NAN test because I wrote a fast example of what I’m doing
SyncAll doesn’t work with Blynk IoT unless you have the “Sync with latest server value every time device connects to the cloud” option turned on for every virtual pin you want to sync, and it’s quite a crude tool to use. You’re better having a Blynk.syncVirtual(vPin)
command for each pin you want to sync.
Have you tried increasing the heartbeat timeout with #define BLYNK_HEARTBEAT xx
where xx
is the heartbeat interval in seconds?
Pete.
Thank you for the suggestion, I will try to change the heartbeat but I would like to find something else more elegant like a no blocking http get. I saw that the esp32 has a library for async http get/post