Emilio
March 25, 2022, 5:18pm
1
Hi,
How to retrieve a json data from a web site while using blynk.edgent code? The code below works when executed alone, but not when used within blynk.edgent. It seems that blink.edgent locks in with its server and doesn’t allow to have an additional connection to the rest of the world.
Thanks
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
const char* ssid = "myssid";
const char* password = "mypass";
String server = "https://www.myweb.net/iot/blynk.json";
String json_data;
int relays[8];
String GET_Request(const char* server) {
HTTPClient http;
http.begin(server);
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
return payload;
}
void setup()
{
Serial.begin(115200);
delay(150);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
json_data = GET_Request(server.c_str());
JSONVar my_obj = JSON.parse(json_data);
if (JSON.typeof(my_obj) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON object = ");
Serial.println(my_obj);
relays[0] = my_obj["relay1"];
Serial.println(relays[0]);
}
void loop() {
Serial.println("Looping...");
delay(1000);
}
I don’t think that’s correct.
Would you like to post your Edgent_ESP32.ino file, along with your serial output?
Pete.
Emilio
March 25, 2022, 6:07pm
3
Sure thanks, here it is…
#define BLYNK_TEMPLATE_ID "****"
#define BLYNK_DEVICE_NAME "ESP"
#define BLYNK_FIRMWARE_VERSION "1.0.8"
#include <Arduino_JSON.h>
#define BLYNK_PRINT Serial
#include "BlynkEdgent.h"
const int relay[] = {16, 17, 18, 19, 21, 22, 23, 25};
String server_json = "https://www.myweb.net/iot/blynk.json";
String json_data;
int tempos[8];
//timers
BlynkTimer timer;
String GET_Request(const char* server) {
HTTPClient http;
http.begin(server_json);
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
return payload;
}
void setup()
{
Serial.begin(115200);
delay(100);
//set relays pins as outputs
for (int x = 0; x < 8; x++) {
pinMode(relay[x], OUTPUT);
digitalWrite(relay[x], LOW);
}
BlynkEdgent.begin();
delay(1000);
//retreive timers
json_data = GET_Request(server_json.c_str());
JSONVar my_obj = JSON.parse(json_data);
if (JSON.typeof(my_obj) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON object = ");
Serial.println(my_obj);
Serial.println("Done setup");
}
void loop() {
BlynkEdgent.run();
timer.run();
}
19:06:24.369 -> >[238]
19:06:24.369 -> ___ __ __
19:06:24.369 -> / _ )/ /_ _____ / /__
19:06:24.369 -> / _ / / // / _ \/ '_/
19:06:24.369 -> /____/_/\_, /_//_/_/\_\
19:06:24.369 -> /___/ v1.0.1 on ESP32
19:06:24.369 ->
19:06:26.381 -> Error code: -1
19:06:26.381 -> JSON object = {}
19:06:26.381 -> 0
19:06:26.381 -> Done setup
19:06:30.278 -> [6173] Using Dynamic IP: 192.168.1.9
19:06:30.325 -> [6183] Connecting to blynk.cloud:443
19:06:31.308 -> [7192] Certificate OK
19:06:31.355 -> [7244] Ready (ping: 50ms).
Emilio
March 25, 2022, 6:17pm
4
Peter, it looks like the json data is retrived before blynk is connected thus its failure.
I thought BlynkEdgent.begin();
does make a connection to internet.
Emilio
March 25, 2022, 6:31pm
5
Therefore, adding the following lines in setup seems to solve the problem but not sure if it’s the way to go.
BlynkEdgent.begin();
delay(1000);
BlynkEdgent.run();
Maybe put the code in BLYNK_CONNECTED()
That way it won’t be called until the WiFi connection and Blynk server connection are established.
Pete.
Also, you can use webhooks. For example, you send the value to some datastream. Datastream triggers webhook, webhook returns the json to the hardware to the same datastream. This may work small json’s < 32 kb.
2 Likes