Making simple http request to get data on blynk 2.0

I am using blynk 2.0 and want to make a http request to get some data.
I have to use WIFI client as http.begin in <ESP8266HTTPClient.h> needs it. (Makes no sense as my NODE8266 is already connected to internet.

My code

void getDataFromHTTP() {
  String url = "";
  resp = http.begin(wifiClient,url);
  Serial.println("Resp: "+resp);
  Serial.println("WIFI: "+WiFi.status());
  httpResponseCode = http.GET();
  httpData = http.getString();

the http response is code is -5. (Not able to establish connection)
Is there a simple way in blynk 2.0 to make a http call and store the value in a variable.

my setup method

void setup() {
  pinMode(LED1, OUTPUT); // Initialize the LED1 pin as an output
  pinMode(LED2, OUTPUT); // Initialize the LED1 pin as an output
  pinMode(relay, OUTPUT); // Initialize the Relay Pin D1 pin as an output

  // Timers to call functions every x intervals (16 availabe, 0th is a sacrificial timer due to blynk bug)
  timer.setTimeout(3600000L, [] () {} ); // sacrificial Function - not used anywhere in logic.
    loggerTimerID = timer.setInterval(1L*60L*60L*1000L, resetIsWriteToTermial); // Once every 1 hour
    controllerTimerID = timer.setInterval(delayTime, getDataFromHTTP); // Setting Timer with Default Values to run every 15 secs, Check BLYNK_WRITE(VPIN_CHECKINTERVAL) for overridding

and loop

void loop() {;;



First of all, I think you’re approaching this from the wrong direction.
Presumably you are trying to get data from one Blynk connected device to another, but rather than obtaining this via an HTTP request from this device, you’d be better pushing the data via an HTTP command from your other device.
The only drawback you’ll face with that approach when using Edgent is that your other device needs to know the Blynk auth token of this device, which is allocated dynamically, but that’s easy enough to work around.

This approach (which was previously supported in Blynk Legacy as a system known as Bridge, is discussed in this topic…

You should scroll-up a bit to post #27 where I first described the code that I wrote for the ESP32.
The advantage of using this data push method from your slave device(s) is that it will trigger the BLYNK_WRITE(vPin) callback function on your master device when new data becomes available. This saves you having to constantly poll the Blynk server for new data, and makes the system more responsive too.

As far as your code is concerned, here are a few comments…

The newer ESP8266 HTTP client does indeed require you to specify the connection that the HTTP client is going to use. However, as you point-out, that WiFi connection already exists, and you don’t need to create another connection. You just need to create an object that you’ll use, which acts as an alias for the pre-existing WiFi connection, like this…

WiFiClient my_wifi_client;

then use my_wifi_client when you initialise the HTTP client.

You aren’t initialising an HTTP client object either, like this…

HTTPClient http;

and ending that client object with an http.end(); either.

You are making the HTTP call using // and it’s safer to add-in the subdomain for the regional server where your project lives. My project is on the 'lon1server, so I'd use//`


Thank you for your prompt response.
I used your code above for Edgent, and changed the token and url.
However the response i get is still -5.

Sending 11.80 to pin V2
Error code: -5 <-----------------------------------Response time = 98 milliseconds

Sending 12.10 to pin V2
Error code: -5 <-----------------------------------Response time = 97 milliseconds

Sending 12.40 to pin V2
Error code: -5 <-----------------------------------Response time = 99 milliseconds

Here is the code

// Fill-in information from your Blynk Template here

#define APP_DEBUG
#define BLYNK_WIFI_SSID  "mywifi"
#define BLYNK_WIFI_PASS  "mypass"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WEMOS_D1_MINI

#include "BlynkEdgent.h"
#include <ESP8266HTTPClient.h>

String server_name = ""; // <<< SEE COMMENTS

String bridge_token_1 = "mytoken"; // token for the receiving device

float temperature = 10.0; // Used for testing, will be incremented in the "push_some_data" function

BlynkTimer timer;

void setup()

  timer.setInterval(10000L, push_some_data);  

void loop()

void push_some_data()
  api_bridge(bridge_token_1,2,temperature); // Token for receiving device, virtual pin number, value to send

  // Increment the temperature value to simulate changing data...  
  temperature = temperature + 0.3;
  if (temperature >= 50)
    temperature = 10.0;

void api_bridge(String token, int virtual_pin, float value_to_send)
  WiFiClient my_wifi_client;
  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(my_wifi_client, server_path.c_str());
  // Send HTTP GET request
  Serial.print("Sending ");
  Serial.print(" to pin V");
  long request_time = millis();
  int httpResponseCode = http.GET();
  if (httpResponseCode>0)
    Serial.print("HTTP Response code: ");
    String payload = http.getString();
    Serial.print("Error code: ");
    Serial.print(" <-----------------------------------");    
  Serial.print("Response time = ");
  Serial.print(millis() - request_time);
  Serial.println(" milliseconds");
  // Free resources

I figured it out, I was using https instead of http.
The code is now working.
Thank you Pete for your help.

1 Like