esp8266 / esp8266/Arduino

[HTTPS] GET... failed, error: connection failed

Open
#8,741 5 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

I am using the following code to send data to the server periodically every 1 minute.

```
void sendData(char *h, char *t) {
const char api_key[] = "myapiKey";
String serve_url = "http://myserver/input/post?";

char spath[250];
sprintf(spath, "node=%s&fulljson={\"humidity\":%s,\"temp\":%s}&apikey=%s", clientId.c_str(), h, t, api_key);
String p = serve_url + spath;

Serial.println("Connecting to: ");
Serial.println(p);
HTTPClient https;

Serial.print("[HTTPS] begin...\n");
https.begin(client, p);

Serial.print("[HTTPS] GET...\n");

int httpCode = https.GET();
if (httpCode > 0) {

Serial.printf("[HTTPS] GET... code: %d\n", httpCode);

if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = https.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}

https.end();
}
```
This works once or twice in a loop, but after that, it fails continuously.
with error:
```
[HTTPS] GET... failed, error: connection failed
```
The same code runs when I use the same URL printed in the console directly in the same code.

What could be wrong here?

Environment:

ESP8266
toll chain SDK "version": "5.100300.220621"
framework: "version": "3.1.0-dev"

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.