Skip to content
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

how to use esp8266 with WiFiClientSecure #612

Open
pasagame opened this issue Oct 19, 2023 · 0 comments
Open

how to use esp8266 with WiFiClientSecure #612

pasagame opened this issue Oct 19, 2023 · 0 comments

Comments

@pasagame
Copy link

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

}
`

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

1 participant