Numerous fixes, first working commit.
This commit is contained in:
parent
3569508c88
commit
e74fe369e5
3 changed files with 125 additions and 76 deletions
79
wifi.h
79
wifi.h
|
@ -1,17 +1,39 @@
|
|||
#include "config.h"
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266HTTPClient.h>
|
||||
//#include <WifiClientSecureBearSSL.h>
|
||||
#include <bearssl.h>
|
||||
#include <FS.h>
|
||||
#include <WiFiClientSecureBearSSL.h>
|
||||
|
||||
HTTPClient client;
|
||||
BearSSL::WifiClientSecure *transport;
|
||||
BearSSL::WiFiClientSecure *secure_transport;
|
||||
BearSSL::Session *tls_session;
|
||||
BearSSL::X509List cert_list;
|
||||
|
||||
void init_wifi() {
|
||||
transport = new WifiClientSecure();
|
||||
void syncTime() {
|
||||
// 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("");
|
||||
}
|
||||
|
||||
Serial.println("Attempting to (re)connect to wifi");
|
||||
void initTLS() {
|
||||
secure_transport = new BearSSL::WiFiClientSecure();
|
||||
|
||||
syncTime();
|
||||
cert_list.append(ca_cert);
|
||||
secure_transport->setTrustAnchors(&cert_list);
|
||||
|
||||
tls_session = new BearSSL::Session();
|
||||
secure_transport->setSession(tls_session);
|
||||
}
|
||||
|
||||
void InitWifi() {
|
||||
Serial.println("Attempting to connect to wifi");
|
||||
WiFi.disconnect();
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
int elapsed = 0;
|
||||
|
@ -25,36 +47,23 @@ void init_wifi() {
|
|||
|
||||
Serial.println("Wifi connected");
|
||||
|
||||
sync_time();
|
||||
load_ca_cert();
|
||||
initTLS();
|
||||
}
|
||||
|
||||
void load_ca_cert() {
|
||||
// read cert from file
|
||||
if (!SPIFFS.begin()) {
|
||||
Serial.println("Failed to mount file system.");
|
||||
return
|
||||
String FetchURL(const char *path) {
|
||||
client.begin(*secure_transport, WEBHOOK_URL);
|
||||
|
||||
int status = client.GET();
|
||||
if (status < 0) {
|
||||
Serial.print("Client error communicating with server: ");
|
||||
Serial.println(status);
|
||||
return "";
|
||||
}
|
||||
if (status >= 400) {
|
||||
Serial.print("Received HTTP status code ");
|
||||
Serial.println(status);
|
||||
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("");
|
||||
return client.getString();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue