From 654558abcd0ff1bf5089ff7b1ec07d850012f95b Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Tue, 28 Dec 2021 19:24:10 -0800 Subject: [PATCH 1/6] Partial work for NTP client --- platformio.ini | 9 +++++---- src/gps/RTC.h | 5 ++++- src/mesh/http/WiFiAPClient.cpp | 33 +++++++++++++++++++++++++++------ 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/platformio.ini b/platformio.ini index 1d35ddeb9..3e1056b7d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,10 +9,10 @@ ; https://docs.platformio.org/page/projectconf.html [platformio] -default_envs = tbeam +;default_envs = tbeam ;default_envs = tbeam0.7 ;default_envs = heltec-v2.0 -;default_envs = tlora-v1 +default_envs = tlora-v1 ;default_envs = tlora_v1_3 ;default_envs = tlora-v2 ;default_envs = lora-relay-v1 # nrf board @@ -114,6 +114,7 @@ lib_deps = paulstoffregen/OneWire@^2.3.5 robtillaart/DS18B20@^0.1.11 h2zero/NimBLE-Arduino@1.3.1 + arduino-libraries/NTPClient#531eff39d9fbc831f3d03f706a161739203fbe2a # Hmm - this doesn't work yet # board_build.ldscript = linker/esp32.extram.bss.ld lib_ignore = @@ -127,8 +128,8 @@ platform_packages = ;upload_port = /dev/ttyUSB0 ;monitor_port = /dev/ttyUSB0 -;upload_port = /dev/cu.SLAB_USBtoUART -;monitor_port = /dev/cu.SLAB_USBtoUART +upload_port = /dev/cu.SLAB_USBtoUART +monitor_port = /dev/cu.SLAB_USBtoUART ; customize the partition table ; http://docs.platformio.org/en/latest/platforms/espressif32.html#partition-tables diff --git a/src/gps/RTC.h b/src/gps/RTC.h index 3a89d9810..730d1914e 100644 --- a/src/gps/RTC.h +++ b/src/gps/RTC.h @@ -11,8 +11,11 @@ enum RTCQuality { /// Some other node gave us a time we can use RTCQualityFromNet = 1, + /// Our time is based on NTP + RTCQualityNTP= 2, + /// Our time is based on our own GPS - RTCQualityGPS = 2 + RTCQualityGPS = 3 }; RTCQuality getRTCQuality(); diff --git a/src/mesh/http/WiFiAPClient.cpp b/src/mesh/http/WiFiAPClient.cpp index 91892b5de..b1d1ba917 100644 --- a/src/mesh/http/WiFiAPClient.cpp +++ b/src/mesh/http/WiFiAPClient.cpp @@ -3,13 +3,15 @@ #include "concurrency/Periodic.h" #include "configuration.h" #include "main.h" -#include "mqtt/MQTT.h" #include "mesh/http/WebServer.h" #include "mesh/wifi/WiFiServerAPI.h" +#include "mqtt/MQTT.h" #include "target_specific.h" #include #include #include +#include +#include using namespace concurrency; @@ -18,6 +20,10 @@ static void WiFiEvent(WiFiEvent_t event); // DNS Server for the Captive Portal DNSServer dnsServer; +// NTP +WiFiUDP ntpUDP; +NTPClient timeClient(ntpUDP, "0.pool.ntp.org"); + uint8_t wifiDisconnectReason = 0; // Stores our hostname @@ -46,10 +52,11 @@ static WifiSleepObserver wifiSleepObserver; static int32_t reconnectWiFi() { + const char *wifiName = radioConfig.preferences.wifi_ssid; + const char *wifiPsw = radioConfig.preferences.wifi_password; + if (radioConfig.has_preferences && needReconnect) { - const char *wifiName = radioConfig.preferences.wifi_ssid; - const char *wifiPsw = radioConfig.preferences.wifi_password; if (!*wifiPsw) // Treat empty password as no password wifiPsw = NULL; @@ -60,9 +67,21 @@ static int32_t reconnectWiFi() DEBUG_MSG("... Reconnecting to WiFi access point\n"); WiFi.mode(WIFI_MODE_STA); WiFi.begin(wifiName, wifiPsw); + + // Starting timeClient; + + } } + if (*wifiName) { + DEBUG_MSG("Updating NTP time\n"); + timeClient.update(); + + Serial.println(timeClient.getFormattedTime()); + Serial.println(timeClient.getEpochTime()); + } + return 30 * 1000; // every 30 seconds } @@ -128,14 +147,17 @@ static void onNetworkConnected() MDNS.addService("https", "tcp", 443); } + DEBUG_MSG("Starting NTP time client\n"); + timeClient.begin(); + initWebServer(); initApiServer(); APStartupComplete = true; - } + } // FIXME this is kinda yucky, instead we should just have an observable for 'wifireconnected' - if(mqtt) + if (mqtt) mqtt->reconnect(); } @@ -176,7 +198,6 @@ bool initWifi(bool forceSoftAP) } else { DEBUG_MSG("Starting WIFI AP: ssid=%s, ok=%d\n", wifiName, WiFi.softAP(wifiName, wifiPsw)); - } WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); From 4ca8846c2f1241ef683c78e7c15d79417df0461f Mon Sep 17 00:00:00 2001 From: linagee Date: Sat, 1 Jan 2022 12:05:19 -0700 Subject: [PATCH 2/6] Don't commit vim swap files to repo --- .gitignore | 3 +++ bin/.promote-release.sh.swp | Bin 12288 -> 0 bytes 2 files changed, 3 insertions(+) delete mode 100644 bin/.promote-release.sh.swp diff --git a/.gitignore b/.gitignore index 40bba6f69..3d181a39a 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ flash.uf2 cmake-build* __pycache__ +*.swp +*.swo +*~ diff --git a/bin/.promote-release.sh.swp b/bin/.promote-release.sh.swp deleted file mode 100644 index 0dddde02b17cd4bae1d425c6efcf64ae2f262208..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI&ON$dh5CGtcSMh=Vfif$IxXC2ZgBKA*(cnhKN6yWoe32?g%@aySUb#Qb5Uru^(v@BGp$)~AZ zZT8^B_>QrKo{T2r9Sc8f4$L09qFs`WA$x4%lQz$m6m(P}nqr_ymD%iUT++%El;y@| zjdx2IY2c|Q6=)drj%+d|0wS<2Z~<;l#-sGzUjG_iz4B&zT1Fxu0wN#+A|L`HAOa#F z0{^u@YFO z^#A|n_y6}x0N*%1b9~}39Gc@k$4!o(y#7bG&!@f8L_h>YKmYKmV?Bc>Ue{(aTrU=g;0_(*{i?Mu(M&y3O~y9#s*IvjKy4ZC#>u$Cwl5t*J|6 zD~y`3^lA}t^C%o~xDKv*uz;HB5JiFR5-`R)54sjOp(v5$aiG`bd}yaOb^++>o44}ImO!9 z>JXq~(S~a+ylEoxCui$ZTsV*X3Z81$kPmpwe9F_NmZV^bRT4?s(WM+B&K%#Z4QW&9 R?2X9l*mzxgpojxP_ziF4!36*S From 38baebe48f34d8c9ba846f299e183d26ff37cc91 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Mon, 3 Jan 2022 17:35:20 -0800 Subject: [PATCH 3/6] Use NTP time if connected to wifi --- src/mesh/http/WiFiAPClient.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/mesh/http/WiFiAPClient.cpp b/src/mesh/http/WiFiAPClient.cpp index 2037d06eb..1101ad1cd 100644 --- a/src/mesh/http/WiFiAPClient.cpp +++ b/src/mesh/http/WiFiAPClient.cpp @@ -1,5 +1,6 @@ #include "mesh/http/WiFiAPClient.h" #include "NodeDB.h" +#include "RTC.h" #include "concurrency/Periodic.h" #include "configuration.h" #include "main.h" @@ -9,9 +10,9 @@ #include "target_specific.h" #include #include +#include #include #include -#include using namespace concurrency; @@ -55,8 +56,7 @@ static int32_t reconnectWiFi() const char *wifiName = radioConfig.preferences.wifi_ssid; const char *wifiPsw = radioConfig.preferences.wifi_password; - if (radioConfig.has_preferences && needReconnect) { - + if (radioConfig.has_preferences && needReconnect && !WiFi.isConnected()) { if (!*wifiPsw) // Treat empty password as no password wifiPsw = NULL; @@ -67,19 +67,27 @@ static int32_t reconnectWiFi() DEBUG_MSG("... Reconnecting to WiFi access point\n"); WiFi.mode(WIFI_MODE_STA); WiFi.begin(wifiName, wifiPsw); + // Starting timeClient; - - } } - if (*wifiName) { + //if (*wifiName) { + if (WiFi.isConnected()) { DEBUG_MSG("Updating NTP time\n"); - timeClient.update(); + if (timeClient.update()) { + DEBUG_MSG("NTP Request Success - Setting RTCQualityNTP if needed\n"); - Serial.println(timeClient.getFormattedTime()); - Serial.println(timeClient.getEpochTime()); + struct timeval tv; + tv.tv_sec = timeClient.getEpochTime(); + tv.tv_usec = 0; + + perhapsSetRTC(RTCQualityNTP, &tv); + + } else { + DEBUG_MSG("NTP Update failed\n"); + } } return 30 * 1000; // every 30 seconds @@ -149,6 +157,7 @@ static void onNetworkConnected() DEBUG_MSG("Starting NTP time client\n"); timeClient.begin(); + timeClient.setUpdateInterval(60*60); // Update once an hour initWebServer(); initApiServer(); From 12f1fda934fa62854e4079f0ecbfb41d551c1069 Mon Sep 17 00:00:00 2001 From: linagee Date: Tue, 4 Jan 2022 18:32:58 -0700 Subject: [PATCH 4/6] NimBLE: less debugging data, no more constant stream of hex esp_nimble_cfg.h and syscfg/syscfg.h were providing the default previously. This should get rid of: ble_sm_alg_s1() and messages like that proving low-level BLE data. --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index fa1e8f622..218e65020 100644 --- a/platformio.ini +++ b/platformio.ini @@ -106,7 +106,7 @@ upload_speed = 921600 debug_init_break = tbreak setup build_flags = ${arduino_base.build_flags} -Wall -Wextra -Isrc/esp32 -Isrc/esp32-mfix-esp32-psram-cache-issue -lnimble -std=c++11 - -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG + -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG -DMYNEWT_VAL_BLE_HS_LOG_LVL=4 -DAXP_DEBUG_PORT=Serial lib_deps = ${arduino_base.lib_deps} From 1157419e0527756b9bdea310975ac64a02d25396 Mon Sep 17 00:00:00 2001 From: linagee Date: Tue, 4 Jan 2022 18:38:05 -0700 Subject: [PATCH 5/6] Constants are better than magic numbers --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 218e65020..e4f4807c9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -106,7 +106,7 @@ upload_speed = 921600 debug_init_break = tbreak setup build_flags = ${arduino_base.build_flags} -Wall -Wextra -Isrc/esp32 -Isrc/esp32-mfix-esp32-psram-cache-issue -lnimble -std=c++11 - -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG -DMYNEWT_VAL_BLE_HS_LOG_LVL=4 + -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG -DMYNEWT_VAL_BLE_HS_LOG_LVL=LOG_LEVEL_CRITICAL -DAXP_DEBUG_PORT=Serial lib_deps = ${arduino_base.lib_deps} From a8d10329f863738ce7bb480ff43fb18c32e29886 Mon Sep 17 00:00:00 2001 From: linagee Date: Tue, 4 Jan 2022 18:58:56 -0700 Subject: [PATCH 6/6] Added some platformio.ini comments --- platformio.ini | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/platformio.ini b/platformio.ini index e4f4807c9..7d2ac6b44 100644 --- a/platformio.ini +++ b/platformio.ini @@ -104,6 +104,10 @@ src_filter = ${arduino_base.src_filter} - upload_speed = 921600 debug_init_break = tbreak setup + +# Remove -DMYNEWT_VAL_BLE_HS_LOG_LVL=LOG_LEVEL_CRITICAL for low level BLE logging. +# See library directory for BLE logging possible values: .pio/libdeps/tbeam/NimBLE-Arduino/src/log_common/log_common.h +# This overrides the BLE logging default of LOG_LEVEL_INFO (1) from: .pio/libdeps/tbeam/NimBLE-Arduino/src/esp_nimble_cfg.h build_flags = ${arduino_base.build_flags} -Wall -Wextra -Isrc/esp32 -Isrc/esp32-mfix-esp32-psram-cache-issue -lnimble -std=c++11 -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG -DMYNEWT_VAL_BLE_HS_LOG_LVL=LOG_LEVEL_CRITICAL