Skip to content

[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

Open
bkrajendra opened this issue Dec 4, 2022 · 5 comments
Open

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

bkrajendra opened this issue Dec 4, 2022 · 5 comments

Comments

@bkrajendra
Copy link

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"

@Bighoneypot
Copy link
Contributor

Bighoneypot commented Dec 4, 2022 via email

@bkrajendra
Copy link
Author

bkrajendra commented Dec 5, 2022

I am having error for both SSL and non ssl implementation.

What should be http.header()?

@Bighoneypot
Copy link
Contributor

I am having error for both SSL and non ssl implementation.

What should be http.header()?

Dear, I need to read entire sketch.
Another question, in whicn server you need to send your data?

@bkrajendra
Copy link
Author

Find the code below.
This is almost all code. I have removed some unrelated functions.
dataCollection is the function that runs periodically. sendData function makes an HTTP call inside it.
At first, I thought it might be due to timerschduler task I was using for periodical runs. So I tried in a loop. but the results are the same.
You might find both codes there.
The server is a PHP application. (self-hosted emoncms instance)

#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);
}

@bkrajendra
Copy link
Author

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants