BlynkEdgent with HTTPClient and WiFiClientSecure

Hello Blynkers,

I have an issue sending Https request (HTTPClient and WiFiClientSecure) when using BlynkEdgent BUT on the other hand it will work seamlessly when using Blynk.begin() and Blynk.run().

Regards

What sort of issue?
What board are you using?
What code have you added to the Edgent example?
Have you avoided conflicts with the existing HTTPClient code in the Edgent example?

Pete.

Hi Pete,

I am using NodeMCU 1.0 (ESP-12E Module) board. The issue was “connection failed.” upon sending the https request. Please see my code below. You can switch between the Blynk and BlynkEdgent.

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

/* Fill in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "TMPL"
#define BLYNK_TEMPLATE_NAME         "Smart Switch"
//#define BLYNK_AUTH_TOKEN            "D57eigh24JULuLIo2jdrlbVuT6FUwyaE"

#define BLYNK_FIRMWARE_VERSION "1.0.1"  

#include "BlynkEdgent.h"

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>

#include <TimeLib.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourSSID";
char pass[] = "YourPassword";

BlynkTimer timer;

void getHttpsRequest() {  
  HTTPClient https;
  
  String url = "https://api.currencylayer.com/live";
  String result = "127.0.0.1";

  https.setTimeout(60000); // 60 seconds

  std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
  //WiFiClientSecure* client = new WiFiClientSecure();

  //client->setFingerprint(fingerprint_sni_cloudflaressl_com);
  // Or, if you happy to ignore the SSL certificate, then use the following line instead:
  client->setInsecure();

  // start connection and send HTTP header
   Serial.print("[HTTPS] begin...\n");
  
  if (https.begin(*client, url)) {

    Serial.print(F("[Url] --> "));
    Serial.println(url);

    int httpCode = https.GET();

    // httpCode will be negative on error
    if(httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTPS] GET... code: %d\n", httpCode);      

        if(httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
            result = https.getString();
        }
    } else {
        result = https.errorToString(httpCode).c_str();
    }

    https.end();   

    Serial.println(result);
  }
  else {
      Serial.printf("[HTTPS] Unable to connect\n");
  }  
}

void setup()
{
  // Debug console
  Serial.begin(115200);

  Blynk.begin("D57eigh24JULuLIo2jdrlbVuT6FUwyaE", ssid, pass);
  //BlynkEdgent.begin();
 
  // set interval in millisecond(s) --> check every 10 second(s)
  timer.setInterval((1) * 10000, getHttpsRequest);
}

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

I have no idea what you mean.

Why are you using Edgent yet specifying the SSID, WiFi password and Auth token when these are provisioned dynamically with Edgent.

Clearly the HTTP client and WiFi files that you’re including are going to conflict with those already used by the Edgent sketch.

Why have you chosen to use the Edgent example as the basis for your project?

Pete.

Hi Pete, The code was by default is using the default Blynk() without errors but if we change it to BlynkEdgent() by commenting the Blynk.begin() and Blynk.run(). The error connection failed will manifest.

Why have you chosen to use the Edgent example as the basis for your project? – I’d like to use Edgent because of the OTA. My project has two options either I will use the Edgent or the other way around.

I think you’re right there’s a conflict with my references. Can you show me how to call https request using built-in Edgent HTTP client?

Regards

I think you’d be better starting with a non-edgent sketch and adding-in Blynk.Air OTA like this…

You’ll see that Blynk.Air uses the HTTP client, so take care not to create conflicts with your HTTP client code.
Note that there currently appears to be a problem with Blynk.Air when using version 1.3.0 of the Blynk C++ library…

Pete.

Thanks for your help guys but I have found the solution to my problem. Instead of creating a new instance of my WiFiClientSecure(). I just reused the existing BlynkEdgent() wifi client connection and it works like a charm. I have no issues anymore when using BlynkEdgent when calling HTTS requests.

void getHttpsRequest() { 
...
      //std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
      //WiFiClientSecure* client = new WiFiClientSecure();
      WiFiClientSecure* client = &_blynkWifiClient;
...
}

Regards.