Home > Blockchain >  ESP8266Httpclient & WiFIClient(Secure) - connection fails both with HTTPS and HTTP
ESP8266Httpclient & WiFIClient(Secure) - connection fails both with HTTPS and HTTP

Time:09-13

I'm trying to connect to a server, both using ESP8266Httpclient and WiFiClient(Secure), but no matter what I do the connection fails.

WiFiClient(Secure) version:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

const char* ssid = "wifi";
const char* password = "pass";
WiFiClientSecure client;

char server[64] = "website.domain", url[64] = "/path/to/data";


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

  WiFi.begin(ssid, password);
  while(WiFi.status() != WL_CONNECTED) yield();

}

void loop() {
  if (!client.connect(server, 443)) Serial.println("Connection failed");
  else {
    Serial.println("Connection success");

  }
}

This always prints "Connection failed", no matter what combination I try: I tried connecting with port 80 and 443, using WiFiClient and WiFiClientSecure, adding and removing "http(s)://" from the server, they all return "Connection failed".

ESP8266Httpclient version:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

#ifndef STASSID
#define STASSID "wifi"
#define STAPSK "pass"
#endif

void setup() {
  Serial.begin(115200);
  WiFi.begin(STASSID, STAPSK);

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

void loop() {
  // wait for WiFi connection
  if ((WiFi.status() == WL_CONNECTED)) {

    WiFiClientSecure client;
    HTTPClient http;

    Serial.print("[HTTP] begin...\n");

    http.begin(client, "https://website.domain/path/to/data", 443);  
    http.addHeader("Content-Type", "application/json");

    Serial.print("[HTTP] POST...\n");

    int httpCode = http.POST("{\"points\":[{\"lat\":mylat,\"lng\":mylng}],\"fuelType\":\"1-x\",\"priceOrder\":\"asc\"}");

    if (httpCode > 0) {
      Serial.printf("[HTTP] POST... code: %d\n", httpCode);
      Serial.println("Redirect location: "   http.getLocation());

      //if (httpCode == HTTP_CODE_OK) {
      const String& payload = http.getString();
      Serial.println("received payload:\n<<");
      Serial.println(payload);
      Serial.println(">>");
      //}
    } else Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());

    http.end();
  }

  delay(10000);
}

This snippet behaves differently: if I connect to http://website.domain/path/to/data I get HTTP code 301 (moved permanently), which returns location https://website.domain/path/to/data.

If I then change the endpoint to https://website.domain/path/to/data, it returns error -1 (connection failed). I tried every combination in this code as well: port 80 and 443, with and without http(s)://, WiFiClient and WiFiClientSecure, nothing worked. I might be overlooking something very simple, or this might not be possible at all.

CodePudding user response:

Thanks to Juraj for the suggestion to put client.setInsecure(). I thought I'd already done that, but you actually need to call it after you create the client and before you pass it to httpclient. That way it'll actually be set as insecure. Thanks again Juraj.

  • Related