From 7bb74a43b9ddf6f488afbe973b3619fba14d279e Mon Sep 17 00:00:00 2001 From: Florian Overkamp Date: Sun, 10 Nov 2019 16:14:46 +0100 Subject: [PATCH] code cleanups (removed unused vars) and futher prepare for ota --- 00_prologue.ino | 5 ----- 10_wifi_conf.ino | 22 +++++++++++++++++----- 30_interface_mqtt.ino | 2 +- 99_setup_loop.ino | 4 ++-- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/00_prologue.ino b/00_prologue.ino index a27ee3d..9b0562e 100644 --- a/00_prologue.ino +++ b/00_prologue.ino @@ -2,11 +2,6 @@ * Libraries and global scope code */ -unsigned long lastMillis = 0; - -String devname; // Device identifier for MQTT -char nodename[80] = "UNDEF"; - struct Settings { String name = ""; String ip = ""; diff --git a/10_wifi_conf.ino b/10_wifi_conf.ino index 251b6d2..86f0b37 100644 --- a/10_wifi_conf.ino +++ b/10_wifi_conf.ino @@ -13,6 +13,8 @@ WiFiClientSecure net; //BearSSL::WiFiClientSecure net; +unsigned long ota_lastcheck = 0; + // // If the WifiManager configuration portal is called, we can do stuff // @@ -21,9 +23,13 @@ WiFiClientSecure net; //} void wifi_associate() { + char apname[80]; + settings.name.toCharArray(apname, 80); + 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"); + if (WifiManager.autoConnect(apname)) { // @@@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) + settings.ip = WiFi.localIP().toString(); + PRINTLN_SERIAL("Wifi IP: " + settings.ip); //wificlient.setFingerprint(WTR_SHA1); net.setInsecure(); // Do not check fingerprint @@ -55,10 +61,13 @@ void wifi_loop() { PRINTLN_SERIAL("Wifi disconnected, trying to reconnect"); wifi_associate(); } -} + + // OTA routine to be run once every X seconds + unsigned long ota_check = millis()/1000; + if((ota_lastcheck == 0) || ((ota_check - ota_lastcheck) > OTA_UPDATE_INTERVAL)) { + PRINTLN_SERIAL("Checking for OTA update"); /* // Check for firmware upgrades - Serial.println("Checking for firmware upgrade"); t_httpUpdate_return ret = ESPhttpUpdate.update(wificlient, WTR_SERVER, WTR_SRVPORT, "/fw/", nodeversion); switch(ret) { case HTTP_UPDATE_FAILED: // HTTP 403, 404 @@ -72,4 +81,7 @@ void wifi_loop() { break; } -*/ +*/ + ota_lastcheck = ota_check; + } +} diff --git a/30_interface_mqtt.ino b/30_interface_mqtt.ino index 3017cba..b3d410f 100644 --- a/30_interface_mqtt.ino +++ b/30_interface_mqtt.ino @@ -109,7 +109,7 @@ void mqtt_connect() { } void mqtt_setup() { - PRINTLN_SERIAL(devname + "initialising module MQTT"); + PRINTLN_SERIAL("Initialising MQTT"); } void mqtt_loop() { diff --git a/99_setup_loop.ino b/99_setup_loop.ino index 1ed9ab3..aeaacad 100644 --- a/99_setup_loop.ino +++ b/99_setup_loop.ino @@ -23,7 +23,7 @@ void setup() { // If we run in to trouble, this can painlessly be removed and the resulting device name will just be longer settings.name.remove(devlen+1, 6); - PRINTLN_SERIAL("device name generated"); + PRINTLN_SERIAL("Device name: " + settings.name); wifi_setup(); @@ -37,7 +37,7 @@ void setup() { moon_setup(); #endif // Start the cooperative scheduler loops - PRINTLN_SERIAL(devname + "done setting up, ready for main loop"); + PRINTLN_SERIAL(settings.name + " done setting up, ready for main loop"); } void loop() {