Refactor the increasingly complex wifi code into its own header, update some docs.
This commit is contained in:
parent
bca75ff5e6
commit
3569508c88
3 changed files with 97 additions and 41 deletions
60
wifi.h
Normal file
60
wifi.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266HTTPClient.h>
|
||||
//#include <WifiClientSecureBearSSL.h>
|
||||
#include <bearssl.h>
|
||||
#include <FS.h>
|
||||
|
||||
HTTPClient client;
|
||||
BearSSL::WifiClientSecure *transport;
|
||||
BearSSL::X509List cert_list;
|
||||
|
||||
void init_wifi() {
|
||||
transport = new WifiClientSecure();
|
||||
|
||||
Serial.println("Attempting to (re)connect to wifi");
|
||||
WiFi.disconnect();
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
int elapsed = 0;
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.print("Wifi connecting for ");
|
||||
Serial.print(elapsed);
|
||||
Serial.println(" seconds");
|
||||
elapsed++;
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
Serial.println("Wifi connected");
|
||||
|
||||
sync_time();
|
||||
load_ca_cert();
|
||||
}
|
||||
|
||||
void load_ca_cert() {
|
||||
// read cert from file
|
||||
if (!SPIFFS.begin()) {
|
||||
Serial.println("Failed to mount file system.");
|
||||
return
|
||||
}
|
||||
|
||||
File cert = SPIFFS.open("/ca_cert.pem", "r");
|
||||
if (!cert) {
|
||||
Serial.println("Couldn't open cert file.");
|
||||
return;
|
||||
}
|
||||
|
||||
cert_list.append(strdup(cert.readString().c_str()));
|
||||
netClient->setTrustAnchors(&certList);
|
||||
}
|
||||
|
||||
void sync_time() {
|
||||
// sync time
|
||||
Serial.print("Syncing time");
|
||||
configTime(8 * 3600, 0, "pool.ntp.org", "time.nist.gov");
|
||||
time_t now = time(nullptr);
|
||||
while (now < 8 * 3600 * 2) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
now = time(nullptr);
|
||||
}
|
||||
Serial.println("");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue