esp8266 / esp8266/Arduino

ESP8266 crashes while HTTPClient fails to connect

Open
#8,513 19 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
16.7k
Forks
13.1k
PR merge metrics
No merged PRs in 30d

Description

### Basic Infos

- [x] This issue complies with the [issue POLICY doc](https://github.com/esp8266/Arduino/blob/master/POLICY.md).
- [x] I have read the documentation at [readthedocs](https://arduino-esp8266.readthedocs.io/en/latest) and the issue is not addressed there.
- [x] I have tested that the issue is present in current master branch (aka latest git).
- [x] I have searched the issue tracker for a similar issue.
- [x] If there is a stack dump, I have decoded it.
- [x] I have filled out all fields below.

#### Platform

- Hardware: ESP-12E
- Core Version: 3.2.0
- Development Env: Platformio
- Operating System: Windows

### Settings in IDE

- Module: [Generic ESP8266|Nodemcu]
- Flash Mode: [qio]
- Flash Size: [4MB]
- lwip Variant: [v1.4|v2 Lower Memory|Higher Bandwidth]
- Reset Method: [ck|nodemcu]
- Flash Frequency: [40Mhz]
- CPU Frequency: [80Mhz]
- Upload Using: [SERIAL]
- Upload Speed: [115200] (serial upload only)

### Problem Description

I wrote a program to send data with AWS4 protocol to AWS and I have to use WiFiClientSecure since AWS accept SSL connection. The ESP8266 works fine and it creates signature based the JSON packet, however, after sending data to the server, HTTPClient fails to connect to the server and after that when system attempts to send data again, the ESP8266 crashes.

### [MCVE](https://stackoverflow.com/help/mcve) Sketch

```cpp

#include
#include
#include
#include
#include "AWSClient4.h"

#define Log_M(s) Serial.println(F(s))

HTTPClient httpClient;
WiFiClientSecure client;

AWSClient4 aws4;
DateTimeProvider dateTimeProvider;

const char *ssid = "SSID1234";
const char *password = "@Password$";
const char *awsAccessKey = "awsAccessKey ";
const char *awsSecretKey = "awsSecretKey ";
const char *awsDomain = "awsDomain ";
const char *awsPath = "awsPath ";
const char *awsEndpoint = "amazonaws.com";
const char *awsRegion = "ca-central-1";
const char *url = "https://*********************.amazonaws.com/***********";
char *json;

void connectToWiFi()
{
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Log_M("");
Log_M("WiFi connected");
Log_M("IP address: ");
Serial.println(WiFi.localIP());
}

void sendPacket()
{
Log_M("Send Packet called");
httpClient.setTimeout(6000);
// Your Domain name with URL path or IP address with path
httpClient.begin(client, url);
httpClient.addHeader("Content-Type", "application/json");
httpClient.addHeader("x-amz-content-sha256", aws4.getPayloadHash());
httpClient.addHeader("x-amz-date", aws4.getDateTime());
httpClient.addHeader("Authorization", aws4.getAuthorization());

Log_M("Sending data to the server");
int httpResponseCode = httpClient.POST(json);
if (httpResponseCode > 0)
{
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] POST... code: %d\n", httpResponseCode);

// file found at server
if (httpResponseCode == HTTP_CODE_OK)
{
const String &payload = httpClient.getString();
Log_M("received payload:\n<<");
Serial.println(payload);
Log_M(">>");
}
else if (httpResponseCode == HTTP_CODE_FORBIDDEN)
{
Log_M("Access denied");
}
else if (httpResponseCode == HTTP_CODE_BAD_REQUEST)
{
Log_M("Bad request");
}
else
{
Serial.printf("[HTTP] POST... failed, error: %s\n", httpClient.errorToString(httpResponseCode).c_str());
}
}
else
{
Log_M("Failed to connect");
}

// Free resources
httpClient.end();
Log_M("HTTP closed");
}

void setup() {
Serial.begin(115200);
delay(100);

aws4.setQuery("");
aws4.setMethod("POST");
aws4.setContentType("application/json");
aws4.setSignedHeaders("content-length;content-type;host;x-amz-content-sha256;x-amz-date");
aws4.setAWSRegion(awsRegion);
aws4.setAWSEndpoint(awsEndpoint);
aws4.setAWSDomain(awsDomain);
aws4.setAWSPath(awsPath_intro);
aws4.setAWSService("*********");
aws4.setAWSKeyID(awsAccessKey);
aws4.setAWSSecretKey(awsSecretKey);
aws4.setDateTimeProvider(&dateTimeProvider);
// Connect to WAP
connectToWiFi();

//
client.setInsecure();

json = new char[196]();
DynamicJsonDocument doc(196);
doc["cid"] = 1989;
doc["stationId"] = 10;
doc["deviceType"] = 0;
doc["mac"] = WiFi.macAddress();

// Generate the minified JSON and send it to the Serial port.
serializeJson(doc, json, 196);
Serial.printf("\n\nJSON Command: %s", json);
}

void loop() {
Log_M("\n\nMain loop called");
///**************************************************************
aws4.createSignature(json);
Serial.println(aws4.getSignature());

sendPacket();
aws4.clearCach();
Log_M("AWS cash cleared");

delay(30000);
}

```

### Debug Messages

```
Main loop called
Send Packet called
Sending data to the server
[HTTP] POST... code: 200
received payload:
<<
{"Blocked": false, "Date": "2022:03:16:15:13:57"}
>>
HTTP closed
AWS cash cleared

Main loop called
Send Packet called
Sending data to the server
[HTTP] POST... code: 200
received payload:
<<
{"Blocked": false, "Date": "2022:03:16:15:14:29"}
>>
HTTP closed
AWS cash cleared

Main loop called
Send Packet called
Sending data to the server
[HTTP] POST... code: 200
received payload:
<<
{"Blocked": false, "Date": "2022:03:16:15:15:01"}
>>
HTTP closed
AWS cash cleared

Main loop called
Send Packet called
Sending data to the server
[HTTP] POST... code: 200
received payload:
<<
{"Blocked": false, "Date": "2022:03:16:15:15:33"}
>>
HTTP closed
AWS cash cleared

Main loop called
Send Packet called
Sending data to the server
[HTTP] POST... code: 200
received payload:
<<
{"Blocked": false, "Date": "2022:03:16:15:16:05"}
>>
HTTP closed
AWS cash cleared

Main loop called
Send Packet called
Sending data to the server
[HTTP] POST... code: 200
received payload:
<<
{"Blocked": false, "Date": "2022:03:16:15:16:37"}
>>
HTTP closed
AWS cash cleared

Main loop called
Send Packet called
Sending data to the server
Failed to connect
HTTP closed
AWS cash cleared

Main loop called
Send Packet called
Sending data to the server

User exception (panic/abort/assert)
--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Unhandled C++ exception: OOM

>>>stack>>>

ctx: cont
sp: 3ffffc90 end: 3fffffc0 offset: 0000
3ffffc90: 402658a0 0000010d 000001bb 00000030
3ffffca0: 000000fe 00000000 00000000 00000000
3ffffcb0: 00000000 00000000 00000000 00000001
3ffffcc0: 00007fff 00000000 c030c02c 00000000
3ffffcd0: 00000000 40210717 000005d8 402170de
3ffffce0: 00000000 40210717 00000020 402170fd
3ffffcf0: c02ac026 c00ec004 000005d8 40216874
3ffffd00: c09dc09c 00000001 3fff14bc 40210717
3ffffd10: 00000000 c00dc003 3fff000a 40210384
3ffffd20: 00000000 00000000 00000d60 3fff47ec
3ffffd30: 00000000 3fff4d2c 3fff14bc 40210bb9
3ffffd40: 0000010d 3fff0714 00000001 40216af0
3ffffd50: 00000000 00000000 000001bb 40217786
3ffffd60: 00000000 3fffbd04 3fff14bc 4020fc02
3ffffd70: 3fffb5c4 00000000 3ffffe24 3ffe9782
3ffffd80: 000001bb 3fff47ec 3fff14bc 3ffe9782
3ffffd90: 000001bb 3fff47ec 3fff14bc 40210d45
3ffffda0: 4021aca4 095f3c34 4021aca4 095f3c34
3ffffdb0: 3fff4838 00000000 3ffeff40 402191ea
3ffffdc0: 00000000 00000000 3ffeff40 40213311
3ffffdd0: 00000000 00000084 3ffeff40 40213d5a
3ffffde0: 00000000 00000002 3ffe9154 3ffefc88
3ffffdf0: 3fff454c 00000090 3ffffe60 40215bc5
3ffffe00: 00000000 00000020 3fff4834 3ffefc88
3ffffe10: 3fffbab4 3ffe9153 00000001 00000000
3ffffe20: 3fff454c 00000084 3ffffe60 3ffefc88
3ffffe30: 3ffefce4 3ffeff40 3fffbab4 40213e9a
3ffffe40: 3ffefce4 3fff454c 3ffffe60 40213eca
3ffffe50: 3ffefce4 3ffeff40 3fff0474 402085b3
3ffffe60: 3fffbab4 0084008f 80000000 4020b6ab
3ffffe70: 646e6553 74614420 61632061 33323100
3ffffe80: 37363534 00003038 00000000 00000000
3ffffe90: 00000000 00000000 3fffff10 3ffffe86
3ffffea0: 00000000 00000000 00000009 0000000a
3ffffeb0: 3fffff00 00000011 3fffff60 3fff0600
3ffffec0: 3ffe97fa 3fff4164 00000000 000d000f
3ffffed0: 00000000 00000011 3fffff60 3fff0600
3ffffee0: 3ffe97fa 3fff4164 3fffff10 4020b8cc
3ffffef0: 0000009f 00000089 3fff4244 3fff0600
3fffff00: 00000180 00000000 3fff454c 4020b9ac
3fffff10: 3fff46cc 3fff45d0 00000084 3fff0600
3fffff20: 3fffdad0 3fff32ac 3ffefc88 4020bbc7
3fffff30: 40217f28 d02cfa9c 3fff40c4 3fff40d6
3fffff40: 3fff4164 3fff4244 00000000 40214280
3fffff50: 3fff4234 3fff4164 00000020 401015b4
3fffff60: 3fff4214 3fffff38 00000000 3fff4224
3fffff70: 3fffff38 00000000 3fff0474 40201c84
3fffff80: 3fff1094 00000000 3fff0474 4020bc0f
3fffff90: 3fffdad0 00000000 00000001 4020be01
3fffffa0: 3fffdad0 00000000 3fff05ec 40216c10
3fffffb0: feefeffe feefeffe 3ffe85fc 40101409
<<

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.