[EDIT 14th January 2022]
Version 3.0.0 of the ESP8266 Core now requires that the http.begin
command now includes the WiFi Client name as well as the server path.
In the original sketch the start of the api_bridge
function looked like this…
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());
The edited version below now looks like this:
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());
The updated version of the code below works with the latest ESP8266 core (v3.0.2) and also works with earlier versions (tested with 2.5.0).
[END OF 14th January 2022 EDIT]
I just tested the code using Edgent on a NodeMCU and it works perfectly…
// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "REDACTED"
#define BLYNK_DEVICE_NAME "REDACTED"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI
#include "BlynkEdgent.h"
#include <ESP8266HTTPClient.h>
String server_name = "http://lon1.blynk.cloud/external/api/"; // <<< SEE COMMENTS
String bridge_token_1 = "REDACTED"; // 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()
{
Serial.begin(74880);
delay(100);
BlynkEdgent.begin();
timer.setInterval(10000L, push_some_data);
}
void loop()
{
BlynkEdgent.run();
timer.run();
}
void push_some_data()
{
api_bridge(bridge_token_1,0,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(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();
}
Serial monitor…
[2109] Hold the button for 10 seconds to reset configuration...
[4718] Using Dynamic IP: REDACTED
[4718] CONNECTING_NET => CONNECTING_CLOUD
[4830] Current time: Sat Jul 17 12:51:47 2021
[4830] Connecting to blynk.cloud:443
[5686] Ready (ping: 11ms).
[5769] CONNECTING_CLOUD => RUNNING
Sending 10.00 to pin V0
HTTP Response code: 200
Response time = 73 milliseconds
Sending 10.30 to pin V0
HTTP Response code: 200
Response time = 29 milliseconds
Sending 10.60 to pin V0
HTTP Response code: 200
Response time = 35 milliseconds
Pete.