move wifi stuff to separate module, implement WifiManager for AP configuration

This commit is contained in:
2019-11-09 22:39:46 +01:00
parent 392a2523c0
commit 4b571786ef
4 changed files with 65 additions and 41 deletions

View File

@@ -1,11 +1,60 @@
// Future use
/*
* Wifi and OTA related stuff
*/
wifi_setup() {
#include <time.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <WiFiManager.h>
#include <ESP8266httpUpdate.h>
WiFiManager WifiManager;
WiFiClientSecure net;
//BearSSL::WiFiClientSecure net;
//
// If the WifiManager configuration portal is called, we can do stuff
//
//void WifiManagerCallback(WiFiManager *myWiFiManager) {
// PRINTLN_SERIAL("Entered AP config mode");
//}
void wifi_associate() {
WifiManager.setConfigPortalTimeout(300); // If no configuration is done in 5 mins, exit/restart quietly
if (WifiManager.autoConnect(nodename)) { // @@@FIXME@@@ we should really set a PSK for the AP (but in my brief testing it crashed the ESP, so what's up with that)
PRINTLN_SERIAL("Wifi connected");
//wificlient.setFingerprint(WTR_SHA1);
net.setInsecure(); // Do not check fingerprint
net.setTimeout(15000); // 15 Seconds
PRINT_SERIAL("Setting time using SNTP");
configTime(-5 * 3600, 0, "pool.ntp.org", "time.nist.gov");
now = time(nullptr);
while (now < 1510592825) {
delay(500);
APPEND_SERIAL(".");
now = time(nullptr);
}
APPENDLN_SERIAL(" done!");
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
PRINT_SERIAL("Current time: ");
APPENDLN_SERIAL(asctime(&timeinfo));
}
}
wifi_loop() {
void wifi_setup() {
// WifiManager.setAPCallback(WifiManagerCallback); // Enable WifiManager callback to clear the cache
wifi_associate();
}
void wifi_loop() {
if(WiFi.status() != WL_CONNECTED) {
PRINTLN_SERIAL("Wifi disconnected, trying to reconnect");
wifi_associate();
}
}
/*
// Check for firmware upgrades