-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[HTTPS] GET... failed, error: connection failed #8741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Dear, you use SSL or not SSL?
Add and configure http.header() in your sketch to connection.
…On Sun, 4 Dec 2022, 20:52 Rajendra, ***@***.***> wrote:
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"
—
Reply to this email directly, view it on GitHub
<#8741>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIPMDFD4CUWGCBHPWHJ2ICLWLTZADANCNFSM6AAAAAASTRGVQU>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
I am having error for both SSL and non ssl implementation. What should be http.header()? |
Dear, I need to read entire sketch. |
Find the code below. #include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <SPI.h>
#include "Adafruit_Si7021.h"
// #define _TASK_SLEEP_ON_IDLE_RUN
#include <TaskScheduler.h>
bool enableHeater = false;
Adafruit_Si7021 sensor = Adafruit_Si7021();
ESP8266WiFiMulti WiFiMulti;
Scheduler runner;
void dataCollection();
Task cloud_update(5000, TASK_FOREVER, &dataCollection, &runner, true); // adding task to the chain on creation
WiFiClientSecure client;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("myfi", "mypass");
if (!sensor.begin()) {
Serial.println("Did not find Si7021 sensor!");
while (true)
;
}
client.setInsecure();
cloud_update.setInterval(6000);
runner.startNow(); // set point-in-time for scheduling start
}
void loop() {
runner.execute();
}
void sendData(String path) {
if ((WiFiMulti.run() == WL_CONNECTED)) {
HTTPClient http;
Serial.print("[HTTP] begin...\n");
if (http.begin(client, path)) { // HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
}
}
}
void dataCollection() {
float t = sensor.readTemperature();
delay(200);
float h = sensor.readHumidity();
String path = "https://mycloudserver/input/post?node=";
path += "node12";
path += "&data={\"humidity\":";
path += String(h);
path += ",\"temp\":";
path += String(t);
path += "}&apikey=hiddenkey";
Serial.println(path);
sendData(path);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("\tTemperature: ");
Serial.println(t);
} |
The issue got resolved with the following approach. #include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <SPI.h>
#include "Adafruit_Si7021.h"
// #define _TASK_SLEEP_ON_IDLE_RUN
#include <TaskScheduler.h>
bool enableHeater = false;
Adafruit_Si7021 sensor = Adafruit_Si7021();
ESP8266WiFiMulti WiFiMulti;
Scheduler runner;
void dataCollection();
Task cloud_update(5000, TASK_FOREVER, &dataCollection, &runner, true); // adding task to the chain on creation
WiFiClientSecure client;
// WiFiClient client;
String path = "";
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("myfi", "mypass");
if (!sensor.begin()) {
Serial.println("Did not find Si7021 sensor!");
while (true)
;
}
client.setInsecure();
cloud_update.setInterval(6000);
runner.startNow(); // set point-in-time for scheduling start
}
void loop() {
runner.execute();
}
void sendData(String path) {
if ((WiFiMulti.run() == WL_CONNECTED)) {
HTTPClient http;
Serial.print("[HTTP] begin...\n");
if (http.begin(client, path)) { // HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
}
}
}
void formatPath(float t, float h, String& p)
{
p = "https://mycloudserver/input/post?node=";
p += "node12";
p += "&data={\"humidity\":";
p += String(h);
p += ",\"temp\":";
p += String(t);
p += "}&apikey=hiddenapikey";
}
void dataCollection() {
float t = sensor.readTemperature();
delay(200);
float h = sensor.readHumidity();
formatPath(t, h, path);
Serial.println(path);
sendData(path);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("\tTemperature: ");
Serial.println(t);
}
|
I am using the following code to send data to the server periodically every 1 minute.
This works once or twice in a loop, but after that, it fails continuously.
with error:
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"
The text was updated successfully, but these errors were encountered: