diff --git a/00_prologue.ino b/00_prologue.ino index 883082f..a27ee3d 100644 --- a/00_prologue.ino +++ b/00_prologue.ino @@ -2,21 +2,18 @@ * Libraries and global scope code */ -#include -#include -#include - -BearSSL::WiFiClientSecure net; -time_t now; unsigned long lastMillis = 0; String devname; // Device identifier for MQTT +char nodename[80] = "UNDEF"; struct Settings { String name = ""; String ip = ""; } settings; +time_t now; + /* * Debugging might be nice sometimes */ diff --git a/10_wifi_conf.ino b/10_wifi_conf.ino index f0be7c1..251b6d2 100644 --- a/10_wifi_conf.ino +++ b/10_wifi_conf.ino @@ -1,11 +1,60 @@ -// Future use +/* + * Wifi and OTA related stuff + */ -wifi_setup() { - +#include +#include +#include +#include +#include + +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 diff --git a/30_interface_mqtt.ino b/30_interface_mqtt.ino index 8b36edf..3017cba 100644 --- a/30_interface_mqtt.ino +++ b/30_interface_mqtt.ino @@ -1,5 +1,8 @@ #ifdef ENABLE_MQTT + +// sample schema's to use: https://www.home-assistant.io/integrations/light.mqtt/ + /* * Define a function newcmd in your module to be able to accept mqtt commands * Syntax and parameters are like this: @@ -72,34 +75,7 @@ void mqtt_callback(char* topic, byte* payload, unsigned int length) { } } -void WifiMQTTconnect() { - - if(WiFi.status() != WL_CONNECTED) { - PRINTLN_SERIAL("Wifi not connected, connecting..."); - WiFi.mode(WIFI_STA); // Weird MQTT connection bug, https://github.com/knolleary/pubsubclient/issues/138#issuecomment-326113915 - WiFi.begin(WLAN_SSID, WLAN_PASS); - while (WiFi.status() != WL_CONNECTED) { - PRINTLN_SERIAL("Waiting for Wifi..."); - delay(500); - } - settings.ip = WiFi.localIP().toString(); - PRINTLN_SERIAL("Wifi connected"); - } - - 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)); - net.setInsecure(); // verification options set to none +void mqtt_connect() { mqtt.setServer(MQTT_SERVER, MQTT_SERVERPORT); mqtt.setCallback(mqtt_callback); @@ -141,7 +117,7 @@ void mqtt_loop() { // Check for MQTT instructions if(!mqtt.connected()) { - WifiMQTTconnect(); + mqtt_connect(); } else { mqtt.loop(); diff --git a/99_setup_loop.ino b/99_setup_loop.ino index 0754b90..1ed9ab3 100644 --- a/99_setup_loop.ino +++ b/99_setup_loop.ino @@ -24,7 +24,8 @@ void setup() { settings.name.remove(devlen+1, 6); PRINTLN_SERIAL("device name generated"); - + + wifi_setup(); #ifdef ENABLE_MQTT mqtt_setup(); @@ -40,6 +41,7 @@ void setup() { } void loop() { + wifi_loop(); #ifdef ENABLE_MQTT mqtt_loop(); #endif