Remove the need to have a separate WiFiClient that's destroyed after the HTTPClient. Let the object handle its own client, and pass through any SSL requests. Also supports the original ::begin methods which need a WiFiClient(Secure) to be passed in and managed by the app.
80 lines
1.8 KiB
C++
80 lines
1.8 KiB
C++
/**
|
|
BasicHTTPSClient-Hard.ino
|
|
|
|
Demonstrates the manual way of making a WiFiClient and passing it in to the HTTPClient
|
|
|
|
Created on: 20.08.2018
|
|
|
|
*/
|
|
|
|
#include <Arduino.h>
|
|
#include <WiFi.h>
|
|
#include <HTTPClient.h>
|
|
|
|
#ifndef STASSID
|
|
#define STASSID "your-ssid"
|
|
#define STAPSK "your-password"
|
|
#endif
|
|
|
|
const char *ssid = STASSID;
|
|
const char *pass = STAPSK;
|
|
|
|
WiFiMulti WiFiMulti;
|
|
|
|
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(ssid, pass);
|
|
}
|
|
|
|
void loop() {
|
|
// wait for WiFi connection
|
|
if ((WiFiMulti.run() == WL_CONNECTED)) {
|
|
|
|
WiFiClientSecure client;
|
|
client.setInsecure(); // Not safe against MITM attacks
|
|
|
|
HTTPClient https;
|
|
|
|
Serial.print("[HTTPS] begin...\n");
|
|
if (https.begin(client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS
|
|
|
|
Serial.print("[HTTPS] GET...\n");
|
|
// start connection and send HTTP header
|
|
int httpCode = https.GET();
|
|
|
|
// httpCode will be negative on error
|
|
if (httpCode > 0) {
|
|
// HTTP header has been send and Server response header has been handled
|
|
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
|
|
|
|
// file found at server
|
|
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();
|
|
} else {
|
|
Serial.printf("[HTTPS] Unable to connect\n");
|
|
}
|
|
}
|
|
|
|
Serial.println("Wait 10s before next round...");
|
|
delay(10000);
|
|
}
|