ESP32 crashes after HTTP request

KenthJensen
Posts: 6
Joined: Mon Jan 20, 2020 6:00 pm

ESP32 crashes after HTTP request

Postby KenthJensen » Mon Jan 20, 2020 6:23 pm

Hi

Please bear with me, I'm completely new in this ESP32 world.

I'm trying to make a very simple HTTP request from my ESP32 towards a webserver is running on a Raspberry Pi running Win 10 IOT, I have been banging my head for several hours and days trying to solve my problem below.

In most cases the code succeeds getting a response as expected, but when I call http.end() it crashes, sometimes with Guru Meditation Error: Core 1 panic'ed (InstrFetchProhibited), other times with (LoadProhibited).

If I instead use any other URLs instead of my own webserver, everything works fine.
When I test the URL of my Raspberry PI webserver with a normal internet browser on my PC, everything works fine too.

The http header is set to following:
HTTP/1.1 200 OK
Centent-Length: 148 (the length seems to be correct)
Connection: Close

I have added some Serial.println to see exactly where it crashes, and it is always when calling http.end();

Code and serial monitor logs are added below.

I tried to install and use ESP Exception Decoder, but couldn't get it to decode the stacktrace.

Any help is greatly appreciated.

Code: Select all

#include <WiFi.h>
#include <HTTPClient.h>
 
const char* ssid = "MySSD";
const char* password =  "MyPassword";


void setup(void)
{
  Serial.begin(9600);
  delay(4000);

  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Waiting for Wifi");
  }
 
  Serial.println("Wifi connected");
  delay(1000);
}



void loop()
{ 
  if ((WiFi.status() == WL_CONNECTED)) //Check the current connection status
  {  
    HTTPClient http;;
    http.begin("http://192.168.0.105:485/esp32.html");
    int httpCode = http.GET();
  
    Serial.print("httpCode: ");
    Serial.println(httpCode);
   
    if (httpCode > 0) //Check for the returning code
    { 
      String payload = http.getString();
  
      Serial.print("Payload: ");
      Serial.println(payload);
    }
    else 
    {
      Serial.println("Error on HTTP request");
    }
    
    Serial.println("Trace just before http.end()");
    http.end(); //Free the resources
    Serial.println("Trace just after http.end()");
  }
  delay(1000);
}
Logs from serial monitor

Code: Select all

Waiting for Wifi
18:46:51.684 -> Wifi connected
18:46:53.935 -> httpCode: 200
18:46:53.935 -> Payload: <!doctype html><html lang="en"><head><meta charset="utf-8"><title>Esp32</title></head><body><p>2020-01-20 18:46:53;22.94;5.56;2.3;</p></body></html>
18:46:54.122 -> Trace just before http.end()
18:46:54.169 -> Guru Meditation Error: Core  1 panic'ed (InstrFetchProhibited). Exception was unhandled.
18:46:54.263 -> Core 1 register dump:
18:46:54.263 -> PC      : 0xf4000000  PS      : 0x00060430  A0      : 0x800d2d72  A1      : 0x3ffb1ea0  
18:46:54.357 -> A2      : 0x3ffb1f10  A3      : 0xf4000000  A4      : 0x0000001c  A5      : 0x0000ff00  
18:46:54.450 -> A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x8015be96  A9      : 0x3ffb1e80  
18:46:54.544 -> A10     : 0x3ffba8bc  A11     : 0x3f4014a4  A12     : 0x00000002  A13     : 0x0000ff00  
18:46:54.638 -> A14     : 0x00ff0000  A15     : 0xff000000  SAR     : 0x0000000a  EXCCAUSE: 0x00000014  
18:46:54.732 -> EXCVADDR: 0xf4000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  
18:46:54.825 -> 
18:46:54.825 -> Backtrace: 0x74000000:0x3ffb1ea0 0x400d2d6f:0x3ffb1ec0 0x400d2de1:0x3ffb1ee0 0x400d184a:0x3ffb1f00 0x400d5d49:0x3ffb1fb0 0x40088b9d:0x3ffb1fd0
18:46:54.966 -> 
18:46:54.966 -> Rebooting...
18:46:55.013 -> 


18:47:03.671 -> Rebooting...
18:47:03.718 -> ⸮Mʠ⸮⸮_⸮Ƃ(⸮De⸮⸮⸮`⸮Waiting for Wifi
18:47:09.100 -> Wifi connected
18:47:11.350 -> httpCode: 200
18:47:11.350 -> Payload: <!doctype html><html lang="en"><head><meta charset="utf-8"><title>Esp32</title></head><body><p>2020-01-20 18:47:10;22.94;5.56;2.3;</p></body></html>
18:47:11.537 -> Trace just before http.end()
18:47:11.584 -> Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
18:47:11.631 -> Core 1 register dump:
18:47:11.678 -> PC      : 0x4015be90  PS      : 0x00060430  A0      : 0x800d2d72  A1      : 0x3ffb1ea0  
18:47:11.772 -> A2      : 0x3ffb1f10  A3      : 0x00000000  A4      : 0x0000001c  A5      : 0x0000ff00  
18:47:11.865 -> A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x800d4b65  A9      : 0x3ffb1e80  
18:47:11.959 -> A10     : 0x3ffba750  A11     : 0x3f4014a4  A12     : 0x00000002  A13     : 0x0000ff00  
18:47:12.053 -> A14     : 0x00ff0000  A15     : 0xff000000  SAR     : 0x0000000a  EXCCAUSE: 0x0000001c  
18:47:12.147 -> EXCVADDR: 0x00000010  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  
18:47:12.241 -> 
18:47:12.241 -> Backtrace: 0x4015be90:0x3ffb1ea0 0x400d2d6f:0x3ffb1ec0 0x400d2de1:0x3ffb1ee0 0x400d184a:0x3ffb1f00 0x400d5d49:0x3ffb1fb0 0x40088b9d:0x3ffb1fd0
18:47:12.381 -> 
18:47:12.381 -> Rebooting...
18:47:12.428 -> 	

ESP_Sprite
Posts: 3937
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 crashes after HTTP request

Postby ESP_Sprite » Tue Jan 21, 2020 9:23 am

Moving to the Arduino subforum.

KenthJensen
Posts: 6
Joined: Mon Jan 20, 2020 6:00 pm

Re: ESP32 crashes after HTTP request

Postby KenthJensen » Tue Jan 21, 2020 4:34 pm

I have now finally figured out how to use the ESP Exception Decoder tool, but unfurtunately I didn't get any wiser from that

But I'm hoping it could help anybody else, which can help me understand what the problem is ;)

Code: Select all

PC: 0x4015be90: HTTPClient::connected() at C:\Users\kfj41\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\HTTPClient\src\HTTPClient.cpp line 395
0x4015be90: HTTPClient::connected() at C:\Users\kfj41\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\HTTPClient\src\HTTPClient.cpp line 395
0x400d2d6f: HTTPClient::disconnect(bool) at C:\Users\kfj41\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\HTTPClient\src\HTTPClient.cpp line 359
0x400d2de1: HTTPClient::end() at C:\Users\kfj41\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\HTTPClient\src\HTTPClient.cpp line 347
0x400d184a: loop() at C:\Users\kfj41\Documents\Arduino\Display SPI test\TFT_eSPI_WiFi_Http_req_5 without display\TFT_eSPI_WiFi_Http_req_4/TFT_eSPI_WiFi_Http_req_4.ino line 50
0x400d5d49: loopTask(void*) at C:\Users\kfj41\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\cores\esp32\main.cpp line 19
0x40088b9d: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143
EXCVADDR: 0x00000013

Decoding stack results
0x4015be90: HTTPClient::connected() at C:\Users\kfj41\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\HTTPClient\src\HTTPClient.cpp line 395
0x400d2d6f: HTTPClient::disconnect(bool) at C:\Users\kfj41\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\HTTPClient\src\HTTPClient.cpp line 359
0x400d2de1: HTTPClient::end() at C:\Users\kfj41\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\HTTPClient\src\HTTPClient.cpp line 347
0x400d184a: loop() at C:\Users\kfj41\Documents\Arduino\Display SPI test\TFT_eSPI_WiFi_Http_req_5 without display\TFT_eSPI_WiFi_Http_req_4/TFT_eSPI_WiFi_Http_req_4.ino line 50
0x400d5d49: loopTask(void*) at C:\Users\kfj41\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\cores\esp32\main.cpp line 19
0x40088b9d: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143


boarchuz
Posts: 287
Joined: Tue Aug 21, 2018 5:28 am

Re: ESP32 crashes after HTTP request

Postby boarchuz » Wed Jan 22, 2020 8:13 am

You probably should be checking that it began successfully. eg.

Code: Select all

bool httpInitResult = http.begin("http://192.168.0.105:485/esp32.html");
if( httpInitResult == false ) {
  Serial.println("http.begin() failed!"); //debug
}
else {
  int httpCode = http.GET();
  if (httpCode > 0) { //Check for the returning code 
      String payload = http.getString();
      Serial.print("Payload: ");
      Serial.println(payload);
  }
  else 
  {
    Serial.println("Error on HTTP request");
  }
  //etc etc
  http.end();
}
Though I agree it should be failing gracefully anyway, especially in the Arduino environment. It would be worth opening an issue on GitHub if nobody can figure it out.

KenthJensen
Posts: 6
Joined: Mon Jan 20, 2020 6:00 pm

Re: ESP32 crashes after HTTP request

Postby KenthJensen » Wed Jan 22, 2020 8:46 am

Thanks for your suggestion Boarchuz

I tried it, but it didn't make any difference, in my case http.begin always returns a true.

I also get a successful response back with http payload including expected html code, but just after that it crashes.

The strange thing is that it only crashes when it sends a http req to my homemade webserver, it works fine with all other webservers.

I double checked my http header from the server, and also added "Content-Type: text/html" "Date:....." and "Server:..." to the header.

Further more I checked the header and html response using a program called Fiddler and using FireFoxs Inspector tool, and I really can't see any problem with it, but there must be something in the response from my webserver causing the crash

KenthJensen
Posts: 6
Joined: Mon Jan 20, 2020 6:00 pm

Re: ESP32 crashes after HTTP request

Postby KenthJensen » Sat Feb 01, 2020 4:46 am

Anybody able to help here?

I would really appriciate some help with this.

Thanks in advance

boarchuz
Posts: 287
Joined: Tue Aug 21, 2018 5:28 am

Re: ESP32 crashes after HTTP request

Postby boarchuz » Sat Feb 01, 2020 4:46 pm

Dump as much debug output as you can, especially all of the response headers. Get separate output for a successful one and one that crashes.
Get the raw response in Fiddler and post it verbatim here.

KenthJensen
Posts: 6
Joined: Mon Jan 20, 2020 6:00 pm

Re: ESP32 crashes after HTTP request

Postby KenthJensen » Sun Feb 02, 2020 4:45 pm

Header from Fiddler

Code: Select all

HTTP/1.1 200 Connection Established
Content-Length: 148
Content-Type: text/html
Server: rpi
Date: Sun, 02 Feb 2020 15:54:59 GMT
Connection: close
Raw content from Fiddler

Code: Select all

HTTP/1.1 200 Connection Established
Content-Length: 148
Content-Type: text/html
Server: rpi
Date: Sun, 02 Feb 2020 15:54:59 GMT
Connection: close

<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Esp32</title></head><body><p>2020-02-02 16:54:59;23.88;3.56;2.3;</p></body></html>
Http header from another server which doesn't crash the ESP32

Code: Select all

HTTP/1.1 200 Connection Established
FiddlerGateway: Direct
StartTime: 17:05:32.917
Connection: close
I can see in Fiddler that the http response from my Raspberry Pi is marked with blue where all other requests towards other servers are black. But I can't see any kind of error indications for the request.

Serial Monitor with debug traces enabled

Code: Select all

15:36:22.978 -> Waiting for Wifi
15:36:22.978 -> Wifi connected
15:36:23.934 -> [V][HTTPClient.cpp:235] beginInternal(): url: http://192.168.0.105:485/esp32.html
15:36:24.034 -> [D][HTTPClient.cpp:276] beginInternal(): host: 192.168.0.105 port: 485 url: /esp32.html
15:36:24.135 -> [D][HTTPClient.cpp:1025] connect():  connected to 192.168.0.105:485
15:36:24.382 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'HTTP/1.1 200 OK'
15:36:24.471 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Content-Length: 148'
15:36:24.552 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Content-Type: text/html'
15:36:24.633 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Server: rpi'
15:36:24.715 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Date: Thu, 23 Jan 2020 14:36:23 GMT'
15:36:24.796 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Connection: close'
15:36:24.877 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: ''
15:36:24.959 -> [D][HTTPClient.cpp:1158] handleHeaderResponse(): code: 200
15:36:25.000 -> [D][HTTPClient.cpp:1161] handleHeaderResponse(): size: 148
15:36:25.081 -> [D][HTTPClient.cpp:1295] writeToStreamDataBlock(): connection closed or file end (written: 148).
15:36:25.163 -> [D][HTTPClient.cpp:370] disconnect(): tcp stop
15:36:25.246 -> 
15:36:25.246 -> Payload: <!doctype html><html lang="en"><head><meta charset="utf-8"><title>Esp32</title></head><body><p>2020-01-23 15:36:23;23.13;4.63;2.3;</p></body></html>
15:36:25.399 -> Trace just before http.end()
15:36:25.399 -> [D][HTTPClient.cpp:383] disconnect(): tcp is closed
15:36:25.500 -> 
15:36:26.350 -> [V][HTTPClient.cpp:235] beginInternal(): url: http://192.168.0.105:485/esp32.html
15:36:26.450 -> [D][HTTPClient.cpp:276] beginInternal(): host: 192.168.0.105 port: 485 url: /esp32.html
15:36:26.504 -> [D][HTTPClient.cpp:1025] connect():  connected to 192.168.0.105:485
15:36:26.851 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'HTTP/1.1 200 OK'
15:36:26.952 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Content-Length: 148'
15:36:27.005 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Content-Type: text/html'
15:36:27.106 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Server: rpi'
15:36:27.152 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Date: Thu, 23 Jan 2020 14:36:26 GMT'
15:36:27.253 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Connection: close'
15:36:27.353 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: ''
15:36:27.406 -> [D][HTTPClient.cpp:1158] handleHeaderResponse(): code: 200
15:36:27.453 -> [D][HTTPClient.cpp:1161] handleHeaderResponse(): size: 148
15:36:27.507 -> [D][HTTPClient.cpp:1295] writeToStreamDataBlock(): connection closed or file end (written: 148).
15:36:27.607 -> [D][HTTPClient.cpp:370] disconnect(): tcp stop
15:36:27.707 -> 
15:36:27.707 -> Payload: <!doctype html><html lang="en"><head><meta charset="utf-8"><title>Esp32</title></head><body><p>2020-01-23 15:36:25;23.13;4.63;2.3;</p></body></html>
15:36:27.839 -> Trace just before http.end()
15:36:27.886 -> Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
15:36:27.970 -> Core 1 register dump:
15:36:27.970 -> PC      : 0x4015d4d8  PS      : 0x00060430  A0      : 0x800d36db  A1      : 0x3ffb1e80  
15:36:28.109 -> A2      : 0x3ffb1f10  A3      : 0x00000002  A4      : 0x0000001c  A5      : 0x0000ff00  
15:36:28.155 -> A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x800d6591  A9      : 0x3ffb1e60  
15:36:28.256 -> A10     : 0x3ffbda34  A11     : 0x3f40240a  A12     : 0x00000002  A13     : 0x0000ff00  
15:36:28.356 -> A14     : 0x00ff0000  A15     : 0xff000000  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
15:36:28.456 -> EXCVADDR: 0x00000012  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  
15:36:28.557 -> 
15:36:28.557 -> Backtrace: 0x4015d4d8:0x3ffb1e80 0x400d36d8:0x3ffb1ea0 0x400d37a9:0x3ffb1ec0 0x400d1ba6:0x3ffb1ee0 0x400d7775:0x3ffb1fb0 0x40088b9d:0x3ffb1fd0
15:36:28.710 -> 
15:36:28.710 -> Rebooting...
15:36:28.710 -> 	1⸮⸮⸮��⸮V�ŌHT⸮⸮Xl@�⸮[D][WiFiGeneric.cpp:337] _eventCallback(): Event: 0 - WIFI_READY
15:36:33.174 -> [D][WiFiGeneric.cpp:337] _eventCallback(): Event: 2 - STA_START
15:36:33.328 -> [D][WiFiGeneric.cpp:337] _eventCallback(): Event: 4 - STA_CONNECTED
15:36:33.629 -> [D][WiFiGeneric.cpp:337] _eventCallback(): Event: 7 - STA_GOT_IP
15:36:33.676 -> [D][WiFiGeneric.cpp:381] _eventCallback(): STA IP: 192.168.0.112, MASK: 255.255.255.0, GW: 192.168.0.1
15:36:34.130 -> Waiting for Wifi
15:36:34.130 -> Wifi connected
15:36:35.132 -> [V][HTTPClient.cpp:235] beginInternal(): url: http://192.168.0.105:485/esp32.html
15:36:35.232 -> [D][HTTPClient.cpp:276] beginInternal(): host: 192.168.0.105 port: 485 url: /esp32.html
15:36:37.038 -> [D][HTTPClient.cpp:1025] connect():  connected to 192.168.0.105:485
15:36:37.393 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'HTTP/1.1 200 OK'
15:36:37.493 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Content-Length: 148'
15:36:37.540 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Content-Type: text/html'
15:36:37.641 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Server: rpi'
15:36:37.694 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Date: Thu, 23 Jan 2020 14:36:36 GMT'
15:36:37.794 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: 'Connection: close'
15:36:37.895 -> [V][HTTPClient.cpp:1123] handleHeaderResponse(): RX: ''
15:36:37.941 -> [D][HTTPClient.cpp:1158] handleHeaderResponse(): code: 200
15:36:37.995 -> [D][HTTPClient.cpp:1161] handleHeaderResponse(): size: 148
15:36:38.095 -> [D][HTTPClient.cpp:1295] writeToStreamDataBlock(): connection closed or file end (written: 148).
15:36:38.164 -> [D][HTTPClient.cpp:370] disconnect(): tcp stop
15:36:38.233 -> 
15:36:38.233 -> Payload: <!doctype html><html lang="en"><head><meta charset="utf-8"><title>Esp32</title></head><body><p>2020-01-23 15:36:36;23.13;4.63;2.3;</p></body></html>
15:36:38.370 -> Trace just before http.end()
15:36:38.404 -> Guru Meditation Error: Core  1 panic'ed (IllegalInstruction). Exception was unhandled.
15:36:38.496 -> Core 1 register dump:
15:36:38.543 -> PC      : 0x54000000  PS      : 0x00060430  A0      : 0x800d36db  A1      : 0x3ffb1e80  
15:36:38.644 -> A2      : 0x3ffb1f10  A3      : 0x54000000  A4      : 0x0000001c  A5      : 0x0000ff00  
15:36:38.744 -> A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x8015d4de  A9      : 0x3ffb1e60  
15:36:38.797 -> A10     : 0x3ffbdaf0  A11     : 0x3f40240a  A12     : 0x00000002  A13     : 0x0000ff00  
15:36:38.898 -> A14     : 0x00ff0000  A15     : 0xff000000  SAR     : 0x00000004  EXCCAUSE: 0x00000000  
15:36:38.998 -> EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  
15:36:39.098 -> 
15:36:39.098 -> Backtrace: 0x54000000:0x3ffb1e80 0x400d36d8:0x3ffb1ea0 0x400d37a9:0x3ffb1ec0 0x400d1ba6:0x3ffb1ee0 0x400d7775:0x3ffb1fb0 0x40088b9d:0x3ffb1fd0
15:36:39.246 -> 
15:36:39.246 -> Rebooting...
15:36:39.299 -> 	
Could the error be related to that I am using a fixed IP address instead of a domain name, and no DNS lookup is done?

KenthJensen
Posts: 6
Joined: Mon Jan 20, 2020 6:00 pm

Re: ESP32 crashes after HTTP request

Postby KenthJensen » Wed Feb 05, 2020 6:27 pm

The problem has been solved, or a least a workaround, by reporting an issue on GitHub.

I got an Apache webserver up and running on Raspbian as suggested on GitHub, and then things worked fine using this new webserver, the ESP32 didn't crash here.

I noticed that my old server used the following in the http header:
Connection: close

But the Apache webserver used:
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive

I tried using the same in the header from my original webserver, and voila, now the ESP32 didn't crash anymore.

This is a workaround I easily can live with permanently, but I guess it's something the httpCLient should be robust for, and not crash.

Who is online

Users browsing this forum: No registered users and 20 guests