70 lines
1.5 KiB
C
70 lines
1.5 KiB
C
#include "config.h"
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESP8266HTTPClient.h>
|
|
#include <WiFiClientSecureBearSSL.h>
|
|
|
|
HTTPClient client;
|
|
BearSSL::WiFiClientSecure *secure_transport;
|
|
BearSSL::Session *tls_session;
|
|
BearSSL::X509List cert_list;
|
|
|
|
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("");
|
|
}
|
|
|
|
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;
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
Serial.print("Wifi connecting for ");
|
|
Serial.print(elapsed);
|
|
Serial.println(" seconds");
|
|
elapsed++;
|
|
delay(1000);
|
|
}
|
|
|
|
Serial.println("Wifi connected");
|
|
|
|
initTLS();
|
|
}
|
|
|
|
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 "";
|
|
}
|
|
|
|
return client.getString();
|
|
}
|