Webhook widget

Okay, I’ve been doing some testing this afternoon.

I’ve been using an ESP32 and the HTTPClient.h library. I’ve not tested with an ESP8266 and the ESP8266HTTPClient.h library, but I wouldn’t expect the results to be much different.

I’ve managed to use the HTTPS API call with both the BlynkSimpleEsp32.h and BlynkSimpleEsp32_SSL.h libraries, however, the response times from the HTTPS API call are much longer than when using the HTTP API call. I guess this is what was causing @Blynk_Coeur’s -5 error, and I have seen this and a -1 error when I’ve been testing.

Response times with the HTTPS API are usually around the 2.5 second mark, but I’ve seen some as high as 46.561 seconds in testing!

HTTP response times are generally around 70ms, so I’d say that using the HTTP API url is the preferred option.

Here’s my test code, with the HTTP url…

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "REDACTED"  
#define BLYNK_DEVICE_NAME "REDACTED"
#include <BlynkSimpleEsp32_SSL.h>
#include <HTTPClient.h>

char auth[] = "REDACTED"; // Auth token for this device
char ssid[] = "REDACTED";
char pass[] = "REDACTED";

String server_name = "http://fra1.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(115200);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(10000L, push_some_data);
}

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


void push_some_data()
{
  api_bridge(bridge_token_1,3,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)
{
  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.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();
}

Note that the server declared here (Line 11):

String server_name = "http://fra1.blynk.cloud/external/api/"; // <<< SEE COMMENTS

will need to be changed depending on which Blynk cloud server your project sits on. The server is shown in the bottom right hand corner of your web portal:

image

Mine is fra1, but the options are currently:

fra1 – Frankfurt
lon1 – London
ny3 – New York
sgp1 – Singapore
blr1 – Bangalore

Don’t forget that your receiving device needs to have a datastream set up for the virtual pin that you’re sending the data to, and it needs to have the correct data type and suitable min-max values.

As you’ll see, the void push_some_data() function increments the temperature variable and sends it to the void api_bridge() function every 10 seconds. You’d replace this with whatever code you currently use to take sensor readings.

Hope this helps.

Pete.

3 Likes