Hieromon / AutoConnect

An Arduino library for ESP8266/ESP32 WLAN configuration at runtime with the Web interface

Home Page:https://hieromon.github.io/AutoConnect/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to use esp8266 with WiFiClientSecure

pasagame opened this issue · comments

I use this code with esp32, it works normally, but when used with esp8266, it cannot retrieve data. how to fix issue? thank you.

`#include <AutoConnect.h>

#ifdef ARDUINO_ARCH_ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <ESP8266WebServer.h>
ESP8266WebServer Server;
AutoConnect Portal(Server);
#else
#include <WiFi.h>
#include <HTTPClient.h>
#include <WebServer.h>
WebServer Server;
AutoConnect Portal(Server);
#endif

void rootPage() {
char content[] = "Hello, world";
Server.send(200, "text/plain", content);
}

void setup() {
Serial.begin(115200); Serial.println("");
Server.on("/", rootPage);
if (Portal.begin()) Serial.println("WiFi connected: " + WiFi.localIP().toString());
updateData();
}

void loop() {
Portal.handleClient();
}

void updateData() {

String url = "https://www.myweb.com/api/xml/mydata=259874";

#ifdef ARDUINO_ARCH_ESP8266
std::unique_ptrBearSSL::WiFiClientSecureclient(new BearSSL::WiFiClientSecure);
client -> setInsecure();
HTTPClient http;
http.begin(*client, url);
#else
HTTPClient http;
http.begin(url);
#endif

int httpCode = http.GET();
Serial.println(httpCode);

if (httpCode > 0) {
String payload = http.getString();
Serial.println(payload);
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();

}
`