Using ArduinoHttpClient lib with ESP32

I asked about a minimum working code example of the ArduinoHttpClient library for the ESP32 (see also: https://github.com/arduino-libraries/ArduinoHttpClient/issues/111).

In the meantime I tried this ...

#include <Arduino.h>

#include <ArduinoHttpClient.h>

#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti wifiMulti;

WiFiClient wifi;
HttpClient http(wifi,"arduino.cc",80);

void setup() {
  Serial.begin(115200);
  wifiMulti.addAP("your-ssid","your-pw");
  
  // attempt to connect to WiFi
  Serial.print("Connecting to Wifi ... ");
  while (wifiMulti.run() != WL_CONNECTED) {
    // unsuccessful, retry in 4 seconds
    Serial.print("failed ... ");
    delay(4000);
    Serial.print("retrying ... ");
  }
  Serial.println("WiFi connected");
}

void loop() {
  Serial.println("making GET request");
  http.get("/");

  // read the status code and body of the response
  int statusCode = http.responseStatusCode();
  String response = http.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);
  Serial.println("Wait five seconds");
  delay(5000);
}

but it stalls after

Connecting to Wifi ... WiFi connected
making GET request
Status code: -3
Response: 
Wait five seconds

Is the Arduino - WiFiClient used in the ArduinoHttpClient lib not compatible with the ESP32 implementaton (e.g. used in https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WiFiClient/WiFiClient.ino)?

WiFiClient client; is called in loop() in the ESP32 / espressif example while WiFiClient wifi; is called already before setup in https://github.com/arduino-libraries/ArduinoHttpClient/blob/master/examples/SimpleGet/SimpleGet.ino ?? Is it perhaps the same syntax but different functions?

How can I get working the ArduinoHttpClient library with an ESP32? Any chance?

esp32 has own HTTPClient library. see the examples in the Examples menu

Yes, I know that the Arduino ESP32 core comes with his own HTTPClient library. But I want to use the TTGO T-Call, a SIM800 equiped ESP and use GPRS or WiFi, best with as much common code for both GPRS and WiFi. So the idea is to make the TCP connection separate only (different for WiFi vs. GPRS) and then use the same code (and lib) for the HTTP stuff.

I understand.
try http.get("/asciilogo.txt");. I think it is the only url accessible at port 80 an arduino.cc

You are right / on arduino.cc:80 throws an 301

# http arduino.cc:80/
HTTP/1.1 301 Moved Permanently
Connection: keep-alive
Content-Length: 184
Content-Type: text/html
Date: Mon, 06 Sep 2021 01:24:56 GMT

arduino.cc:80/asciilogo.txt is better for testing and returns a valid 200 on the shell

# http arduino.cc:80/asciilogo.txt
HTTP/1.1 200 OK
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/plain
Date: Mon, 06 Sep 2021 01:23:39 GMT

But unfortunately the same error with the sketch:

Connecting to Wifi ... WiFi connected
making GET request
Status code: -3
Response: 
Wait five seconds
making GET request

You can try my WiFiWebServer Library, where the Arduino HTTPClient library was merged. ESP32/ESP8266 using WiFi are also supported.

For example, this SimpleHTTPExample example

1 Like

I have installed your WiFiWebServer lib via the lib manage in the Arduino IDE. Board detection is done by the board name in case I see this correct. I'm using the TTGO T-Call as "ESP32 Dev Module" with nothing changed in the SimpleHTTPExample I get:

21:32:39.876 -> Starting SimpleHTTPExample on ESP32_DEV with ESP WiFi using WiFi Library
21:32:39.876 -> WiFiWebServer v1.3.0
21:32:39.876 -> WiFi shield not present

Do I have to configue something or what could be the reason it doesn't work. btw. I have only installed WiFiWebServer and your C+ extension lib.

Sorry I haven't modified the code in HTTPClient to work with ESP32, as I didn't expect ESP32 users would use this.

Please change the code's setup() temporarily to

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  Serial.print(F("\nStarting SimpleHTTPExample on "));
  Serial.print(BOARD_NAME);
  Serial.print(F(" with "));
  Serial.println(SHIELD_TYPE); 
  Serial.println(WIFI_WEBSERVER_VERSION);

#if WIFI_USING_ESP_AT

  // initialize serial for ESP module
  EspSerial.begin(115200);
  // initialize ESP module
  WiFi.init(&EspSerial);

  Serial.println(F("WiFi shield init done"));
  
#endif

#if !(ESP32 || ESP8266)
  
  // check for the presence of the shield
  #if USE_WIFI_NINA
    if (WiFi.status() == WL_NO_MODULE)
  #else
    if (WiFi.status() == WL_NO_SHIELD)
  #endif
    {
      Serial.println(F("WiFi shield not present"));
      // don't continue
      while (true);
    }

  #if USE_WIFI_NINA
    String fv = WiFi.firmwareVersion();
    
    if (fv < WIFI_FIRMWARE_LATEST_VERSION)
    {
      Serial.println(F("Please upgrade the firmware"));
    }
  #endif
  
#endif

  Serial.print(F("Connecting to SSID: "));
  Serial.println(ssid);
  
  status = WiFi.begin(ssid, pass);

  delay(1000);
   
  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED)
  {
    delay(500);
        
    // Connect to WPA/WPA2 network
    status = WiFi.status();
  }

  // you're connected now, so print out the data
  printWifiStatus();
}

then you'll get this on the terminal

Starting SimpleHTTPExample on ESP32_DEV with ESP WiFi using WiFi Library
WiFiWebServer v1.3.0
Connecting to SSID: HueNet1
You're connected to the network, IP = 192.168.2.80
SSID: HueNet1, Signal strength (RSSI):-28 dBm
startedRequest ok
Got status code: 200
Content length is: 2263

Body returned follows:

           `:;;;,`                      .:;;:.           
        .;;;;;;;;;;;`                :;;;;;;;;;;:     TM 
      `;;;;;;;;;;;;;;;`            :;;;;;;;;;;;;;;;      
     :;;;;;;;;;;;;;;;;;;         `;;;;;;;;;;;;;;;;;;     
    ;;;;;;;;;;;;;;;;;;;;;       .;;;;;;;;;;;;;;;;;;;;    
   ;;;;;;;;:`   `;;;;;;;;;     ,;;;;;;;;.`   .;;;;;;;;   
  .;;;;;;,         :;;;;;;;   .;;;;;;;          ;;;;;;;  
  ;;;;;;             ;;;;;;;  ;;;;;;,            ;;;;;;. 
 ,;;;;;               ;;;;;;.;;;;;;`              ;;;;;; 
 ;;;;;.                ;;;;;;;;;;;`      ```       ;;;;;`
 ;;;;;                  ;;;;;;;;;,       ;;;       .;;;;;
`;;;;:                  `;;;;;;;;        ;;;        ;;;;;
,;;;;`    `,,,,,,,,      ;;;;;;;      .,,;;;,,,     ;;;;;
:;;;;`    .;;;;;;;;       ;;;;;,      :;;;;;;;;     ;;;;;
:;;;;`    .;;;;;;;;      `;;;;;;      :;;;;;;;;     ;;;;;
.;;;;.                   ;;;;;;;.        ;;;        ;;;;;
 ;;;;;                  ;;;;;;;;;        ;;;        ;;;;;
 ;;;;;                 .;;;;;;;;;;       ;;;       ;;;;;,
 ;;;;;;               `;;;;;;;;;;;;                ;;;;; 
 `;;;;;,             .;;;;;; ;;;;;;;              ;;;;;; 
  ;;;;;;:           :;;;;;;.  ;;;;;;;            ;;;;;;  
   ;;;;;;;`       .;;;;;;;,    ;;;;;;;;        ;;;;;;;:  
    ;;;;;;;;;:,:;;;;;;;;;:      ;;;;;;;;;;:,;;;;;;;;;;   
    `;;;;;;;;;;;;;;;;;;;.        ;;;;;;;;;;;;;;;;;;;;    
      ;;;;;;;;;;;;;;;;;           :;;;;;;;;;;;;;;;;:     
       ,;;;;;;;;;;;;;,              ;;;;;;;;;;;;;;       
         .;;;;;;;;;`                  ,;;;;;;;;:         
                                                         
                                                         
                                                         
                                                         
    ;;;   ;;;;;`  ;;;;:  .;;  ;; ,;;;;;, ;;. `;,  ;;;;   
    ;;;   ;;:;;;  ;;;;;; .;;  ;; ,;;;;;: ;;; `;, ;;;:;;  
   ,;:;   ;;  ;;  ;;  ;; .;;  ;;   ,;,   ;;;,`;, ;;  ;;  
   ;; ;:  ;;  ;;  ;;  ;; .;;  ;;   ,;,   ;;;;`;, ;;  ;;. 
   ;: ;;  ;;;;;:  ;;  ;; .;;  ;;   ,;,   ;;`;;;, ;;  ;;` 
  ,;;;;;  ;;`;;   ;;  ;; .;;  ;;   ,;,   ;; ;;;, ;;  ;;  
  ;;  ,;, ;; .;;  ;;;;;:  ;;;;;: ,;;;;;: ;;  ;;, ;;;;;;  
  ;;   ;; ;;  ;;` ;;;;.   `;;;:  ,;;;;;, ;;  ;;,  ;;;;   

Hi @Clemens

I just released a new WiFiWebServer releases v1.3.1 to add support to ESP32/ESP8266.

Enjoy,


Releases v1.3.1

  1. Add support to ESP32/ESP8266 to use in some rare use-cases
  2. Update Packages' Patches
  3. Split changelog.md from README.md
2 Likes

I was able to make a connection via WiFi with the "original" WiFiClientBasic sketch: https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WiFiClientBasic/WiFiClientBasic.ino

Now I updated to your WiFiWebServer v1.3.1 version and opened the SimpleHTTPExample sketch, added my Wifi credentials on the bottom of the page in defines.h. Unfortunately it is not working.

Starting SimpleHTTPExample on ESP32_DEV with ESP WiFi using WiFi Library
WiFiWebServer v1.3.1
Connecting to SSID: MyWiFi
You're connected to the network, IP = 192.168.179.6
SSID: MyWiFi, Signal strength (RSSI):-68 dBm
startedRequest ok
Getting response failed: -3

I got a guru meditation also:

You're connected to the network, IP = 192.168.179.6
SSID: MyWiFi, Signal strength (RSSI):-64 dBm
making GET request
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x400d1120  PS      : 0x00060c30  A0      : 0x800d11e8  A1      : 0x3ffb1ea0  
A2      : 0x00000000  A3      : 0x3ffb1eef  A4      : 0x00000001  A5      : 0x00000001  
A6      : 0x00000000  A7      : 0x00000000  A8      : 0x00000000  A9      : 0x00000000  
A10     : 0x000000bf  A11     : 0x00000001  A12     : 0x00000050  A13     : 0x3ffb1e84  
A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x0000000a  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000008  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  

ELF file SHA256: 0000000000000000

Backtrace: 0x400d1120:0x3ffb1ea0 0x400d11e5:0x3ffb1ec0 0x4013feda:0x3ffb1ee0 0x4013fe05:0x3ffb1f10 0x400d2771:0x3ffb1f30 0x400d0dae:0x3ffb1f50 0x400d0716:0x3ffb1f80 0x400d2c0d:0x3ffb1fb0 0x40089792:0x3ffb1fd0

Rebooting...

After the trial with the ESP32 / TTGO T-Call, I tested the same code with an Arduino Nano 33 IoT and got the same response failed: -3

Any idea what could going on / wrong?

Very strange as I'm running my board here OK using the latest v1.3.1, with Arduino IDE 1.8.15, ESP32 core v2.0.0.

Starting SimpleHTTPExample on ESP32_DEV with ESP WiFi using WiFi Library
WiFiWebServer v1.3.1
Connecting to SSID: HueNet1
You're connected to the network, IP = 192.168.2.80
SSID: HueNet1, Signal strength (RSSI):-28 dBm
startedRequest ok
Got status code: 200
Content length is: 2263

Body returned follows:

           `:;;;,`                      .:;;:.           
        .;;;;;;;;;;;`                :;;;;;;;;;;:     TM 
      `;;;;;;;;;;;;;;;`            :;;;;;;;;;;;;;;;      
     :;;;;;;;;;;;;;;;;;;         `;;;;;;;;;;;;;;;;;;     
    ;;;;;;;;;;;;;;;;;;;;;       .;;;;;;;;;;;;;;;;;;;;    
   ;;;;;;;;:`   `;;;;;;;;;     ,;;;;;;;;.`   .;;;;;;;;   
  .;;;;;;,         :;;;;;;;   .;;;;;;;          ;;;;;;;  
  ;;;;;;             ;;;;;;;  ;;;;;;,            ;;;;;;. 
 ,;;;;;               ;;;;;;.;;;;;;`              ;;;;;; 
 ;;;;;.                ;;;;;;;;;;;`      ```       ;;;;;`
 ;;;;;                  ;;;;;;;;;,       ;;;       .;;;;;
`;;;;:                  `;;;;;;;;        ;;;        ;;;;;
,;;;;`    `,,,,,,,,      ;;;;;;;      .,,;;;,,,     ;;;;;
:;;;;`    .;;;;;;;;       ;;;;;,      :;;;;;;;;     ;;;;;
:;;;;`    .;;;;;;;;      `;;;;;;      :;;;;;;;;     ;;;;;
.;;;;.                   ;;;;;;;.        ;;;        ;;;;;
 ;;;;;                  ;;;;;;;;;        ;;;        ;;;;;
 ;;;;;                 .;;;;;;;;;;       ;;;       ;;;;;,
 ;;;;;;               `;;;;;;;;;;;;                ;;;;; 
 `;;;;;,             .;;;;;; ;;;;;;;              ;;;;;; 
  ;;;;;;:           :;;;;;;.  ;;;;;;;            ;;;;;;  
   ;;;;;;;`       .;;;;;;;,    ;;;;;;;;        ;;;;;;;:  
    ;;;;;;;;;:,:;;;;;;;;;:      ;;;;;;;;;;:,;;;;;;;;;;   
    `;;;;;;;;;;;;;;;;;;;.        ;;;;;;;;;;;;;;;;;;;;    
      ;;;;;;;;;;;;;;;;;           :;;;;;;;;;;;;;;;;:     
       ,;;;;;;;;;;;;;,              ;;;;;;;;;;;;;;       
         .;;;;;;;;;`                  ,;;;;;;;;:         
                                                         
                                                         
                                                         
                                                         
    ;;;   ;;;;;`  ;;;;:  .;;  ;; ,;;;;;, ;;. `;,  ;;;;   
    ;;;   ;;:;;;  ;;;;;; .;;  ;; ,;;;;;: ;;; `;, ;;;:;;  
   ,;:;   ;;  ;;  ;;  ;; .;;  ;;   ,;,   ;;;,`;, ;;  ;;  
   ;; ;:  ;;  ;;  ;;  ;; .;;  ;;   ,;,   ;;;;`;, ;;  ;;. 
   ;: ;;  ;;;;;:  ;;  ;; .;;  ;;   ,;,   ;;`;;;, ;;  ;;` 
  ,;;;;;  ;;`;;   ;;  ;; .;;  ;;   ,;,   ;; ;;;, ;;  ;;  
  ;;  ,;, ;; .;;  ;;;;;:  ;;;;;: ,;;;;;: ;;  ;;, ;;;;;;  
  ;;   ;; ;;  ;;` ;;;;.   `;;;:  ,;;;;;, ;;  ;;,  ;;;;   

Be sure to select the same settings in IDE

If you still have issue with this library, I suggest you post an issue on WiFiWebServer

Many thanks for your detailed setting description. I fear that's the right road! Same lib and IDE version here but 1.0.x ESP core. I used an old board manager URL for ESP32 (https://dl.espressif.com/dl/package_esp32_index.json) so ony 1.x versions showed up and no 2.x version was offered. I changed this now to https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json so the 2.x versions are available and updated my system.

My sketch in the initial posting isn't still working and with the SimpleHTTPExample in your lib is runnig now I get

Starting SimpleHTTPExample on ESP32_DEV with ESP WiFi using WiFi Library
WiFiWebServer v1.3.1
Connecting to SSID: MyWiFi
You're connected to the network, IP = 192.168.179.9
SSID: MyWiFi, Signal strength (RSSI):-58 dBm
startedRequest ok
Getting response failed: -3

Btw this is my compiling output:

In file included from C:\Users\mail\temporaer\SimpleHTTPExample-mod\SimpleHTTPExample-mod.ino:13:
C:\Users\mail\temporaer\SimpleHTTPExample-mod\defines.h:78:4: warning: #warning Using ESP WiFi with WiFi Library [-Wcpp]
   #warning Using ESP WiFi with WiFi Library
    ^~~~~~~
C:\Users\mail\temporaer\SimpleHTTPExample-mod\defines.h:328:4: warning: #warning ESP32 board selected [-Wcpp]
   #warning ESP32 board selected
    ^~~~~~~
In file included from C:\Users\mail\temporaer\SimpleHTTPExample-mod\defines.h:348,
                 from C:\Users\mail\temporaer\SimpleHTTPExample-mod\SimpleHTTPExample-mod.ino:13:
C:\Users\mail\Documents\Arduino\libraries\WiFiWebServer\src/WiFiWebServer.h:124:4: warning: #warning Use WiFi.h from WiFiWebServer [-Wcpp]
   #warning Use WiFi.h from WiFiWebServer
    ^~~~~~~
In file included from C:\Users\mail\temporaer\SimpleHTTPExample-mod\defines.h:348,
                 from C:\Users\mail\temporaer\SimpleHTTPExample-mod\SimpleHTTPExample-mod.ino:13:
C:\Users\mail\Documents\Arduino\libraries\WiFiWebServer\src/WiFiWebServer.h:142:4: warning: #warning SENDCONTENT_P_BUFFER_SZ using default 4 Kbytes [-Wcpp]
   #warning SENDCONTENT_P_BUFFER_SZ using default 4 Kbytes
    ^~~~~~~

In the end it is running, also my initial sketch (posting #1). :upside_down_face: I switch to an other WiFi network -- what I have done yesterday already for testing, but without success -- and today there it is working! I couldn't nail it down to the exact reason but

  • a "_" in the SSID?, no SSIDs with special character as "_" working
  • router <-> repeater issue?, direct connection to router (no wifi repeater) does not solve the issue
  • 2,4 GHz and 5 GHz WiFi with the same SSID?
  • restricted "guest" network?

Now I'm getting on

Connecting to Wifi ... failed ... retrying ... failed ... retrying ... failed ...

on the non-working WiFi-network / SSID

but after a while also

Connecting to Wifi ... failed ... retrying ... failed ... retrying ... failed ... retrying ... failed ... retrying ... WiFi connected
making GET request
Status code: -3
Response:

But it is still interesting that the network / SSID worked with the "original" WiFiClientBasic