Wednesday, July 13, 2022

Update for HTTPClient library and many of my ESP8266 projects.

 For an index to all my stories click this text

Important for anybody that has problems accessing webpages with the ESP8266.

Suddenly during the past few weeks when I was on a long earned holliday abroad several readers started to mail me. They seemed to have a problem with projects on my weblog. And as I could see it was not just with one project but with several projects. Then I saw that all these projects had one thing in common: the used the ESP8266HTTPClient library.

So I had to test the code myself. Several possible errors came to my mind. Myabe they used a different version of the Arduino IDE. I am using version 1.8.10 but maybe they were using the new version 2.0 or an older version. And another option was that these readers were using a different version of the ESP8266HTTPClient library. Something must be going on as multiple readers contacted me and they all had the same error.

Obsolete API

 

error: call to 'HTTPClient::begin' declared with attribute error: obsolete API, use ::begin(WiFiClient, host, port, uri)
   68 |   http.begin(host,port,url);
      |   ~~~~~~~~~~^~~~~~~~~~~~~~~



And there is the error. I got the same error !!!

So I start compiling the program with Arduino 1.8.10 and got the error. Over to Arduino IDE 2.0 and the error persisted.

Examining the error it actually tells what the problem is:

obsolete API, use ::begin(WiFiClient, host, port, uri)

So we are using an obsolete API. And obviously the http.begin() command has changed.

The solution.

Actually it took me a some time to find what the error was. To my annoyance there was no documentation easy found. And in the end  the solution actually is easy.

At the start of your program the necessary libraries are loaded with:

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


Then a bit further the http client is defined:

HTTPClient http;

Here you have to add the line:

WiFiClient client;

and then in the loop or any other part of the program where the http code is used you need to change:

http.begin(host,port,url);

into

http.begin(client, host,port,url);

This is taken from my program that sends data from a Wemos D1 mini to Domoticz.

The same applies, for example, the project where data is send to Whatsapp from the ESP. Here the program starts with:

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

HTTPClient http;
WiFiClient client;

const char* ssid = "YOUR ROUTERS NAME";
const char* password = "YOUR ROUTERS PASSWORD";


And in the loop the code is altered in:

      //Specify request destination
      tobesend = "http://api.callmebot.com/whatsapp.php?";
      tobesend = tobesend + "phone=+1234567890";
      tobesend = tobesend + "&text=The+button+on+the+ESP+was+pressed";
      tobesend = tobesend + "&apikey=123456";
      http.begin(client,tobesend);


And that is all. Just the last line is altered.

This affects every project on this weblog that uses the ESP8266HTTPClient library and I do want to update all the listings on the weblog. But that may take some time. For now you know how to alter the program code to get things working again.

Documentation ???

Actually there is no real documentation about this.
However checking the Github pages for the Arduino IDE I saw that they used the new code in the examples:



So maybe my readers and I should have started looking there ????

https://github.com/esp8266/Arduino/tree/master/libraries

Problem solved.

Till next time
Have fun

Luc Volders