From 50a301899efcd090e283052e1d3352a797116766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Mon, 14 Nov 2022 09:49:50 +0100 Subject: [PATCH 01/18] start to sort radiolib out. If we have macros, use them! :-) Also we need Godmode for the SX1280 for now, hopefully can revert to more sane operation. --- src/mesh/RF95Interface.cpp | 1 + src/mesh/SX126xInterface.cpp | 5 +++-- src/mesh/SX128xInterface.cpp | 11 ++++++++--- variants/tlora_v2_1_18/platformio.ini | 2 +- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/mesh/RF95Interface.cpp b/src/mesh/RF95Interface.cpp index d56681412..5b88d45ed 100644 --- a/src/mesh/RF95Interface.cpp +++ b/src/mesh/RF95Interface.cpp @@ -129,6 +129,7 @@ bool RF95Interface::reconfigure() if (power > MAX_POWER) // This chip has lower power limits than some power = MAX_POWER; + err = lora->setOutputPower(power); if (err != RADIOLIB_ERR_NONE) RECORD_CRITICALERROR(CriticalErrorCode_INVALID_RADIO_SETTING); diff --git a/src/mesh/SX126xInterface.cpp b/src/mesh/SX126xInterface.cpp index 64758cd0d..257d8fd04 100644 --- a/src/mesh/SX126xInterface.cpp +++ b/src/mesh/SX126xInterface.cpp @@ -144,8 +144,9 @@ bool SX126xInterface::reconfigure() if (err != RADIOLIB_ERR_NONE) RECORD_CRITICALERROR(CriticalErrorCode_INVALID_RADIO_SETTING); - if (power > 22) // This chip has lower power limits than some - power = 22; + if (power > SX126X_MAX_POWER) // This chip has lower power limits than some + power = SX126X_MAX_POWER; + err = lora.setOutputPower(power); assert(err == RADIOLIB_ERR_NONE); diff --git a/src/mesh/SX128xInterface.cpp b/src/mesh/SX128xInterface.cpp index 0044ae7e5..ab513cece 100644 --- a/src/mesh/SX128xInterface.cpp +++ b/src/mesh/SX128xInterface.cpp @@ -105,8 +105,9 @@ bool SX128xInterface::reconfigure() if (err != RADIOLIB_ERR_NONE) RECORD_CRITICALERROR(CriticalErrorCode_INVALID_RADIO_SETTING); - if (power > 22) // This chip has lower power limits than some - power = 22; + if (power > SX128X_MAX_POWER) // This chip has lower power limits than some + power = SX128X_MAX_POWER; + err = lora.setOutputPower(power); assert(err == RADIOLIB_ERR_NONE); @@ -218,7 +219,11 @@ bool SX128xInterface::isChannelActive() template bool SX128xInterface::isActivelyReceiving() { - return isChannelActive(); + // return isChannelActive(); + + uint16_t irq = lora.getIrqStatus(); + bool hasPreamble = (irq & RADIOLIB_SX128X_IRQ_HEADER_VALID); + return hasPreamble; } template diff --git a/variants/tlora_v2_1_18/platformio.ini b/variants/tlora_v2_1_18/platformio.ini index 4160be5de..f5af98904 100644 --- a/variants/tlora_v2_1_18/platformio.ini +++ b/variants/tlora_v2_1_18/platformio.ini @@ -5,4 +5,4 @@ lib_deps = ${esp32_base.lib_deps} caveman99/ESP32 Codec2@^1.0.1 build_flags = - ${esp32_base.build_flags} -D TLORA_V2_1_18 -I variants/tlora_v2_1_18 \ No newline at end of file + ${esp32_base.build_flags} -D TLORA_V2_1_18 -I variants/tlora_v2_1_18 -D RADIOLIB_GODMODE \ No newline at end of file From 79eff42c3ca9e76f48d4114d44fe44837a9a6b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Mon, 14 Nov 2022 10:53:16 +0100 Subject: [PATCH 02/18] Fix and Break radiolib --- arch/portduino/portduino.ini | 2 +- platformio.ini | 12 ++++++++++++ src/mesh/InterfacesTemplates.cpp | 2 +- src/mesh/RadioLibInterface.cpp | 13 ------------- src/mesh/RadioLibInterface.h | 4 ---- src/mesh/SX1280Interface.cpp | 2 +- src/mesh/SX1280Interface.h | 2 +- src/mesh/SX128xInterface.cpp | 4 ++-- src/mesh/SX128xInterface.h | 2 +- variants/portduino/variant.h | 13 ------------- 10 files changed, 19 insertions(+), 37 deletions(-) diff --git a/arch/portduino/portduino.ini b/arch/portduino/portduino.ini index b61071007..1aff0d07c 100644 --- a/arch/portduino/portduino.ini +++ b/arch/portduino/portduino.ini @@ -16,5 +16,5 @@ lib_deps = ${env.lib_deps} ${networking_base.lib_deps} rweather/Crypto@^0.4.0 - https://github.com/meshtastic/RadioLib.git#5582ac30578ff3f53f20630a00b2a8a4b8f92c74 + jgromes/RadioLib@5.4.1 build_flags = ${arduino_base.build_flags} -Isrc/platform/portduino diff --git a/platformio.ini b/platformio.ini index 618c7cddb..2d7c8209c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -37,12 +37,24 @@ extra_scripts = bin/platformio-custom.py ; note: TINYGPS_OPTION_NO_CUSTOM_FIELDS is VERY important. We don't use custom fields and somewhere in that pile ; of code is a heap corruption bug! ; FIXME: fix lib/BluetoothOTA dependency back on src/ so we can remove -Isrc +; The Radiolib stuff will speed up building considerably. Exclud all the stuff we dont need. build_flags = -Wno-missing-field-initializers -Wno-format -Isrc -Isrc/mesh -Isrc/gps -Isrc/buzz -Wl,-Map,.pio/build/output.map -DUSE_THREAD_NAMES -DTINYGPS_OPTION_NO_CUSTOM_FIELDS -DPB_ENABLE_MALLOC=1 + -DRADIOLIB_EXCLUDE_CC1101 + -DRADIOLIB_EXCLUDE_NRF24 + -DRADIOLIB_EXCLUDE_RF69 + -DRADIOLIB_EXCLUDE_SX1231 + -DRADIOLIB_EXCLUDE_SI443X + -DRADIOLIB_EXCLUDE_RFM2X + -DRADIOLIB_EXCLUDE_AFSK + -DRADIOLIB_EXCLUDE_HELLSCHREIBER + -DRADIOLIB_EXCLUDE_MORSE + -DRADIOLIB_EXCLUDE_RTTY + -DRADIOLIB_EXCLUDE_SSTV monitor_speed = 115200 diff --git a/src/mesh/InterfacesTemplates.cpp b/src/mesh/InterfacesTemplates.cpp index ccef2df23..9602525b5 100644 --- a/src/mesh/InterfacesTemplates.cpp +++ b/src/mesh/InterfacesTemplates.cpp @@ -8,6 +8,6 @@ template class SX126xInterface; template class SX126xInterface; template class SX126xInterface; -#if !defined(ARCH_PORTDUINO) +#if defined(RADIOLIB_GODMODE) template class SX128xInterface; #endif \ No newline at end of file diff --git a/src/mesh/RadioLibInterface.cpp b/src/mesh/RadioLibInterface.cpp index c08220555..567215600 100644 --- a/src/mesh/RadioLibInterface.cpp +++ b/src/mesh/RadioLibInterface.cpp @@ -11,17 +11,6 @@ // FIXME, we default to 4MHz SPI, SPI mode 0, check if the datasheet says it can really do that static SPISettings spiSettings(4000000, MSBFIRST, SPI_MODE0); -#ifdef ARCH_PORTDUINO - -void LockingModule::SPItransfer(uint8_t cmd, uint8_t reg, uint8_t *dataOut, uint8_t *dataIn, uint8_t numBytes) -{ - concurrency::LockGuard g(spiLock); - - Module::SPItransfer(cmd, reg, dataOut, dataIn, numBytes); -} - -#else - void LockingModule::SPIbeginTransaction() { spiLock->lock(); @@ -36,8 +25,6 @@ void LockingModule::SPIendTransaction() Module::SPIendTransaction(); } -#endif - RadioLibInterface::RadioLibInterface(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE busy, SPIClass &spi, PhysicalLayer *_iface) : NotifiedWorkerThread("RadioIf"), module(cs, irq, rst, busy, spi, spiSettings), iface(_iface) diff --git a/src/mesh/RadioLibInterface.h b/src/mesh/RadioLibInterface.h index fbad830cd..f368cf83e 100644 --- a/src/mesh/RadioLibInterface.h +++ b/src/mesh/RadioLibInterface.h @@ -41,12 +41,8 @@ class LockingModule : public Module { } -#ifdef ARCH_PORTDUINO - void SPItransfer(uint8_t cmd, uint8_t reg, uint8_t *dataOut, uint8_t *dataIn, uint8_t numBytes) override; -#else void SPIbeginTransaction() override; void SPIendTransaction() override; -#endif }; class RadioLibInterface : public RadioInterface, protected concurrency::NotifiedWorkerThread diff --git a/src/mesh/SX1280Interface.cpp b/src/mesh/SX1280Interface.cpp index 37aad1d40..97a3febe3 100644 --- a/src/mesh/SX1280Interface.cpp +++ b/src/mesh/SX1280Interface.cpp @@ -2,7 +2,7 @@ #include "SX1280Interface.h" #include "error.h" -#if !defined(ARCH_PORTDUINO) +#if defined(RADIOLIB_GODMODE) SX1280Interface::SX1280Interface(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE busy, SPIClass &spi) diff --git a/src/mesh/SX1280Interface.h b/src/mesh/SX1280Interface.h index 1c2e24900..a9661501a 100644 --- a/src/mesh/SX1280Interface.h +++ b/src/mesh/SX1280Interface.h @@ -6,7 +6,7 @@ * Our adapter for SX1280 radios */ -#if !defined(ARCH_PORTDUINO) +#if defined(RADIOLIB_GODMODE) class SX1280Interface : public SX128xInterface { diff --git a/src/mesh/SX128xInterface.cpp b/src/mesh/SX128xInterface.cpp index ab513cece..36eb0bb94 100644 --- a/src/mesh/SX128xInterface.cpp +++ b/src/mesh/SX128xInterface.cpp @@ -2,7 +2,7 @@ #include "SX128xInterface.h" #include "error.h" -#if !defined(ARCH_PORTDUINO) +#if defined(RADIOLIB_GODMODE) // Particular boards might define a different max power based on what their hardware can do #ifndef SX128X_MAX_POWER @@ -107,7 +107,7 @@ bool SX128xInterface::reconfigure() if (power > SX128X_MAX_POWER) // This chip has lower power limits than some power = SX128X_MAX_POWER; - + err = lora.setOutputPower(power); assert(err == RADIOLIB_ERR_NONE); diff --git a/src/mesh/SX128xInterface.h b/src/mesh/SX128xInterface.h index f712b8bc4..9994ce850 100644 --- a/src/mesh/SX128xInterface.h +++ b/src/mesh/SX128xInterface.h @@ -1,6 +1,6 @@ #pragma once -#if !defined(ARCH_PORTDUINO) +#if defined(RADIOLIB_GODMODE) #include "RadioLibInterface.h" diff --git a/variants/portduino/variant.h b/variants/portduino/variant.h index 328afaf19..76696b7af 100644 --- a/variants/portduino/variant.h +++ b/variants/portduino/variant.h @@ -23,16 +23,3 @@ // HOPE RFM90 does not have a TCXO therefore not SX126X_E22 #endif -// Temporary shim for radio lib macros until we upgrade to upstream for portduino -#define RADIOLIB_PREAMBLE_DETECTED PREAMBLE_DETECTED - -#define RADIOLIB_ERR_NONE ERR_NONE -#define RADIOLIB_ERR_WRONG_MODEM ERR_WRONG_MODEM - -#define RADIOLIB_SX126X_IRQ_HEADER_VALID SX126X_IRQ_HEADER_VALID -#define RADIOLIB_SX126X_LORA_CRC_ON SX126X_LORA_CRC_ON - -#define RADIOLIB_SX127X_REG_TCXO SX127X_REG_TCXO -#define RADIOLIB_SX127X_REG_MODEM_STAT SX127X_REG_MODEM_STAT -#define RADIOLIB_SX127X_SYNC_WORD SX127X_SYNC_WORD -#define RADIOLIB_SX127X_MASK_IRQ_FLAG_VALID_HEADER SX127X_MASK_IRQ_FLAG_VALID_HEADER From b5ebfa9cc332c99de7095ccd6e16287aecf49437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Tue, 15 Nov 2022 07:59:01 +0100 Subject: [PATCH 03/18] Portduino WIP --- arch/portduino/portduino.ini | 5 ++++- variants/portduino/platformio.ini | 14 ++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/arch/portduino/portduino.ini b/arch/portduino/portduino.ini index 1aff0d07c..7e90afa11 100644 --- a/arch/portduino/portduino.ini +++ b/arch/portduino/portduino.ini @@ -1,5 +1,7 @@ ; The Portduino based sim environment on top of any host OS, all hardware will be simulated [portduino_base] +platform = https://github.com/caveman99/platform-native.git#develop +framework = arduino build_src_filter = ${env.build_src_filter} - @@ -16,5 +18,6 @@ lib_deps = ${env.lib_deps} ${networking_base.lib_deps} rweather/Crypto@^0.4.0 - jgromes/RadioLib@5.4.1 + ; jgromes/RadioLib@5.4.1 + https://github.com/caveman99/RadioLib.git#master build_flags = ${arduino_base.build_flags} -Isrc/platform/portduino diff --git a/variants/portduino/platformio.ini b/variants/portduino/platformio.ini index c95ae37fe..5bbde2adf 100644 --- a/variants/portduino/platformio.ini +++ b/variants/portduino/platformio.ini @@ -1,16 +1,22 @@ [env:native] -platform = https://github.com/meshtastic/platform-native.git +extends = portduino_base build_flags = ${portduino_base.build_flags} -O0 -I variants/portduino -framework = arduino board = cross_platform lib_deps = ${portduino_base.lib_deps} build_src_filter = ${portduino_base.build_src_filter} ; The Portduino based sim environment on top of a linux OS and touching linux hardware devices [env:linux] -platform = https://github.com/meshtastic/platform-native.git +extends = portduino_base build_flags = ${portduino_base.build_flags} -O0 -lgpiod -I variants/portduino -framework = arduino board = linux_hardware lib_deps = ${portduino_base.lib_deps} build_src_filter = ${portduino_base.build_src_filter} + +; The Portduino based sim environment on top of a linux OS and touching linux hardware devices +[env:linux-arm] +extends = portduino_base +build_flags = ${portduino_base.build_flags} -O0 -lgpiod -I variants/portduino +board = linux_arm +lib_deps = ${portduino_base.lib_deps} +build_src_filter = ${portduino_base.build_src_filter} From d42797ffebda38e7ed583e6052da79bf319d17f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Tue, 15 Nov 2022 10:49:55 +0100 Subject: [PATCH 04/18] fix Portduino Step 1 --- arch/portduino/portduino.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/portduino/portduino.ini b/arch/portduino/portduino.ini index 7e90afa11..3b2f8efb9 100644 --- a/arch/portduino/portduino.ini +++ b/arch/portduino/portduino.ini @@ -1,6 +1,6 @@ ; The Portduino based sim environment on top of any host OS, all hardware will be simulated [portduino_base] -platform = https://github.com/caveman99/platform-native.git#develop +platform = https://github.com/meshtastic/platform-native.git#develop framework = arduino build_src_filter = ${env.build_src_filter} From e9a34fca7bde66c45699942e126c5a60a14a8651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Tue, 15 Nov 2022 17:01:15 +0100 Subject: [PATCH 05/18] Portduino Radiolib upstream --- arch/portduino/portduino.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/portduino/portduino.ini b/arch/portduino/portduino.ini index 3b2f8efb9..fb1e45af7 100644 --- a/arch/portduino/portduino.ini +++ b/arch/portduino/portduino.ini @@ -19,5 +19,5 @@ lib_deps = ${networking_base.lib_deps} rweather/Crypto@^0.4.0 ; jgromes/RadioLib@5.4.1 - https://github.com/caveman99/RadioLib.git#master + https://github.com/jgromes/RadioLib.git#63208f1e89d4dac6eedaafbe234bf90f1fd5402b ; 5.4.1 with some fixes, remove when 5.4.2 is released build_flags = ${arduino_base.build_flags} -Isrc/platform/portduino From 5991b59ba3616becf07167c72135fa2f1c23577b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Thu, 17 Nov 2022 17:48:46 +0100 Subject: [PATCH 06/18] try fixing Wifi reconnects - don't manually reconnect and set auto-reconnect at the same time. --- src/mesh/http/WiFiAPClient.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/mesh/http/WiFiAPClient.cpp b/src/mesh/http/WiFiAPClient.cpp index 09f5acf48..d290c9fab 100644 --- a/src/mesh/http/WiFiAPClient.cpp +++ b/src/mesh/http/WiFiAPClient.cpp @@ -42,21 +42,19 @@ static int32_t reconnectWiFi() const char *wifiName = config.network.wifi_ssid; const char *wifiPsw = config.network.wifi_psk; - if (config.network.wifi_enabled && needReconnect && !WiFi.isConnected()) { + if (config.network.wifi_enabled && needReconnect) { if (!*wifiPsw) // Treat empty password as no password wifiPsw = NULL; - if (*wifiName) { - needReconnect = false; + needReconnect = false; - // Make sure we clear old connection credentials - WiFi.disconnect(false, true); + // Make sure we clear old connection credentials + WiFi.disconnect(false, true); - DEBUG_MSG("... Reconnecting to WiFi access point\n"); - WiFi.mode(WIFI_MODE_STA); - WiFi.begin(wifiName, wifiPsw); - } + DEBUG_MSG("... Reconnecting to WiFi access point\n"); + WiFi.mode(WIFI_MODE_STA); + WiFi.begin(wifiName, wifiPsw); } #ifndef DISABLE_NTP @@ -169,7 +167,7 @@ bool initWifi() WiFi.mode(WIFI_MODE_STA); WiFi.setHostname(ourHost); WiFi.onEvent(WiFiEvent); - WiFi.setAutoReconnect(true); + WiFi.setAutoReconnect(false); WiFi.setSleep(false); if (config.network.eth_mode == Config_NetworkConfig_EthMode_STATIC && config.network.ipv4_config.ip != 0) { WiFi.config(config.network.ipv4_config.ip, @@ -231,6 +229,7 @@ static void WiFiEvent(WiFiEvent_t event) break; case SYSTEM_EVENT_STA_DISCONNECTED: DEBUG_MSG("Disconnected from WiFi access point\n"); + WiFi.disconnect(false, true); needReconnect = true; break; case SYSTEM_EVENT_STA_AUTHMODE_CHANGE: @@ -243,6 +242,7 @@ static void WiFiEvent(WiFiEvent_t event) break; case SYSTEM_EVENT_STA_LOST_IP: DEBUG_MSG("Lost IP address and IP address is reset to 0\n"); + WiFi.disconnect(false, true); needReconnect = true; break; case SYSTEM_EVENT_STA_WPS_ER_SUCCESS: @@ -259,7 +259,6 @@ static void WiFiEvent(WiFiEvent_t event) break; case SYSTEM_EVENT_AP_START: DEBUG_MSG("WiFi access point started\n"); - onNetworkConnected(); break; case SYSTEM_EVENT_AP_STOP: DEBUG_MSG("WiFi access point stopped\n"); From b260c8b0584d0c09f913af3ec63bc59b5e04738a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Thu, 17 Nov 2022 20:09:08 +0100 Subject: [PATCH 07/18] tryfix wifi issues --- src/mesh/http/WiFiAPClient.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/mesh/http/WiFiAPClient.cpp b/src/mesh/http/WiFiAPClient.cpp index d290c9fab..36400b834 100644 --- a/src/mesh/http/WiFiAPClient.cpp +++ b/src/mesh/http/WiFiAPClient.cpp @@ -35,6 +35,8 @@ char ourHost[16]; bool APStartupComplete = 0; +unsigned long lastrun_ntp = 0; + static bool needReconnect = true; // If we create our reconnector, run it once at the beginning static int32_t reconnectWiFi() @@ -58,7 +60,7 @@ static int32_t reconnectWiFi() } #ifndef DISABLE_NTP - if (WiFi.isConnected()) { + if (WiFi.isConnected() && () && ((millis() - lastrun_ntp) > 43200000)) { // every 12 hours DEBUG_MSG("Updating NTP time\n"); if (timeClient.update()) { DEBUG_MSG("NTP Request Success - Setting RTCQualityNTP if needed\n"); @@ -68,6 +70,7 @@ static int32_t reconnectWiFi() tv.tv_usec = 0; perhapsSetRTC(RTCQualityNTP, &tv); + lastrun_ntp = millis(); } else { DEBUG_MSG("NTP Update failed\n"); @@ -75,7 +78,11 @@ static int32_t reconnectWiFi() } #endif - return 43200 * 1000; // every 12 hours + if (config.network.wifi_enabled && !WiFi.isConnected()) { + return 1000; // check once per second + } else { + return 300000; // every 5 minutes + } } static Periodic *wifiReconnect; @@ -231,6 +238,7 @@ static void WiFiEvent(WiFiEvent_t event) DEBUG_MSG("Disconnected from WiFi access point\n"); WiFi.disconnect(false, true); needReconnect = true; + wifiReconnect->setIntervalFromNow(1000); break; case SYSTEM_EVENT_STA_AUTHMODE_CHANGE: DEBUG_MSG("Authentication mode of access point has changed\n"); @@ -244,6 +252,7 @@ static void WiFiEvent(WiFiEvent_t event) DEBUG_MSG("Lost IP address and IP address is reset to 0\n"); WiFi.disconnect(false, true); needReconnect = true; + wifiReconnect->setIntervalFromNow(1000); break; case SYSTEM_EVENT_STA_WPS_ER_SUCCESS: DEBUG_MSG("WiFi Protected Setup (WPS): succeeded in enrollee mode\n"); From 70d44b8838937537fdc36c744b520405a5f08981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Thu, 17 Nov 2022 20:19:11 +0100 Subject: [PATCH 08/18] next try. --- src/mesh/http/WiFiAPClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/http/WiFiAPClient.cpp b/src/mesh/http/WiFiAPClient.cpp index 36400b834..cf27d9302 100644 --- a/src/mesh/http/WiFiAPClient.cpp +++ b/src/mesh/http/WiFiAPClient.cpp @@ -60,7 +60,7 @@ static int32_t reconnectWiFi() } #ifndef DISABLE_NTP - if (WiFi.isConnected() && () && ((millis() - lastrun_ntp) > 43200000)) { // every 12 hours + if (WiFi.isConnected() && ((millis() - lastrun_ntp) > 43200000)) { // every 12 hours DEBUG_MSG("Updating NTP time\n"); if (timeClient.update()) { DEBUG_MSG("NTP Request Success - Setting RTCQualityNTP if needed\n"); From 9665c08b590b9a4e96ca1fcadac545e2d1e39cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Thu, 17 Nov 2022 21:14:49 +0100 Subject: [PATCH 09/18] MQTT: don't subscribe to JSON topic if JSON is disabled in config. --- src/mqtt/MQTT.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 194683939..244cf1458 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -176,9 +176,11 @@ void MQTT::sendSubscriptions() String topic = cryptTopic + channels.getGlobalId(i) + "/#"; DEBUG_MSG("Subscribing to %s\n", topic.c_str()); pubSub.subscribe(topic.c_str(), 1); // FIXME, is QOS 1 right? - String topicDecoded = jsonTopic + channels.getGlobalId(i) + "/#"; - DEBUG_MSG("Subscribing to %s\n", topicDecoded.c_str()); - pubSub.subscribe(topicDecoded.c_str(), 1); // FIXME, is QOS 1 right? + if (moduleConfig.mqtt.json_enabled == true) { + String topicDecoded = jsonTopic + channels.getGlobalId(i) + "/#"; + DEBUG_MSG("Subscribing to %s\n", topicDecoded.c_str()); + pubSub.subscribe(topicDecoded.c_str(), 1); // FIXME, is QOS 1 right? + } } } } From 144afee29ea91d19bd16ef88f67888020d4ab86e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Fri, 18 Nov 2022 18:03:30 +0100 Subject: [PATCH 10/18] trigger wifi reconnect by mqtt or ntp failures. --- src/mesh/http/WiFiAPClient.cpp | 25 ++++++++++++------------- src/mesh/http/WiFiAPClient.h | 2 ++ src/mqtt/MQTT.cpp | 5 +++++ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/mesh/http/WiFiAPClient.cpp b/src/mesh/http/WiFiAPClient.cpp index cf27d9302..d5a8fb3e9 100644 --- a/src/mesh/http/WiFiAPClient.cpp +++ b/src/mesh/http/WiFiAPClient.cpp @@ -39,6 +39,14 @@ unsigned long lastrun_ntp = 0; static bool needReconnect = true; // If we create our reconnector, run it once at the beginning +static Periodic *wifiReconnect; + +void triggerReconnect() +{ + needReconnect = true; + wifiReconnect->setIntervalFromNow(1000); +} + static int32_t reconnectWiFi() { const char *wifiName = config.network.wifi_ssid; @@ -74,6 +82,9 @@ static int32_t reconnectWiFi() } else { DEBUG_MSG("NTP Update failed\n"); + WiFi.disconnect(false, true); + needReconnect = true; + return 1000; } } #endif @@ -85,8 +96,6 @@ static int32_t reconnectWiFi() } } -static Periodic *wifiReconnect; - bool isWifiAvailable() { @@ -100,20 +109,10 @@ bool isWifiAvailable() // Disable WiFi void deinitWifi() { - /* - Note from Jm (jm@casler.org - Sept 16, 2020): - - A bug in the ESP32 SDK was introduced in Oct 2019 that keeps the WiFi radio from - turning back on after it's shut off. See: - https://github.com/espressif/arduino-esp32/issues/3522 - - Until then, WiFi should only be allowed when there's no power - saving on the 2.4g transceiver. - */ - DEBUG_MSG("WiFi deinit\n"); if (isWifiAvailable()) { + WiFi.disconnect(true); WiFi.mode(WIFI_MODE_NULL); DEBUG_MSG("WiFi Turned Off\n"); // WiFi.printDiag(Serial); diff --git a/src/mesh/http/WiFiAPClient.h b/src/mesh/http/WiFiAPClient.h index 9729c24b5..f45eb0c5d 100644 --- a/src/mesh/http/WiFiAPClient.h +++ b/src/mesh/http/WiFiAPClient.h @@ -13,6 +13,8 @@ bool initWifi(); void deinitWifi(); +void triggerReconnect(); + bool isWifiAvailable(); uint8_t getWifiDisconnectReason(); diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 244cf1458..a3ae51ae1 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -1,4 +1,5 @@ #include "MQTT.h" +#include "mesh/http/WiFiAPClient.h" #include "MeshService.h" #include "NodeDB.h" #include "PowerFSM.h" @@ -164,6 +165,10 @@ void MQTT::reconnect() sendSubscriptions(); } else DEBUG_MSG("Failed to contact MQTT server...\n"); +#if HAS_WIFI + WiFi.disconnect(false, true); + triggerReconnect(); +#endif } } From 990c0119a7eeb8e4b4b1b82b9a29653da24a5392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Fri, 18 Nov 2022 18:22:35 +0100 Subject: [PATCH 11/18] fix portduino - again --- src/mqtt/MQTT.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index a3ae51ae1..a1c31c26b 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -165,7 +165,7 @@ void MQTT::reconnect() sendSubscriptions(); } else DEBUG_MSG("Failed to contact MQTT server...\n"); -#if HAS_WIFI +#if HAS_WIFI && !defined(ARCH_PORTDUINO) WiFi.disconnect(false, true); triggerReconnect(); #endif From 95cc328b5cfc43695828e9cabe10e288ca6633f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Fri, 18 Nov 2022 19:37:56 +0100 Subject: [PATCH 12/18] That wifi update didn't go down well. --- src/mesh/http/WiFiAPClient.cpp | 9 --------- src/mesh/http/WiFiAPClient.h | 2 -- src/mqtt/MQTT.cpp | 5 ----- 3 files changed, 16 deletions(-) diff --git a/src/mesh/http/WiFiAPClient.cpp b/src/mesh/http/WiFiAPClient.cpp index d5a8fb3e9..c00738d1a 100644 --- a/src/mesh/http/WiFiAPClient.cpp +++ b/src/mesh/http/WiFiAPClient.cpp @@ -41,12 +41,6 @@ static bool needReconnect = true; // If we create our reconnector, run it once a static Periodic *wifiReconnect; -void triggerReconnect() -{ - needReconnect = true; - wifiReconnect->setIntervalFromNow(1000); -} - static int32_t reconnectWiFi() { const char *wifiName = config.network.wifi_ssid; @@ -82,9 +76,6 @@ static int32_t reconnectWiFi() } else { DEBUG_MSG("NTP Update failed\n"); - WiFi.disconnect(false, true); - needReconnect = true; - return 1000; } } #endif diff --git a/src/mesh/http/WiFiAPClient.h b/src/mesh/http/WiFiAPClient.h index f45eb0c5d..9729c24b5 100644 --- a/src/mesh/http/WiFiAPClient.h +++ b/src/mesh/http/WiFiAPClient.h @@ -13,8 +13,6 @@ bool initWifi(); void deinitWifi(); -void triggerReconnect(); - bool isWifiAvailable(); uint8_t getWifiDisconnectReason(); diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index a1c31c26b..244cf1458 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -1,5 +1,4 @@ #include "MQTT.h" -#include "mesh/http/WiFiAPClient.h" #include "MeshService.h" #include "NodeDB.h" #include "PowerFSM.h" @@ -165,10 +164,6 @@ void MQTT::reconnect() sendSubscriptions(); } else DEBUG_MSG("Failed to contact MQTT server...\n"); -#if HAS_WIFI && !defined(ARCH_PORTDUINO) - WiFi.disconnect(false, true); - triggerReconnect(); -#endif } } From 7ceb52103e956a7fc812ce377eeeee3697f85dae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sat, 19 Nov 2022 09:58:29 +0100 Subject: [PATCH 13/18] Cleaning up GPS Code a bit --- platformio.ini | 2 +- src/GPSStatus.h | 16 ---------------- src/gps/NMEAGPS.cpp | 4 ++-- 3 files changed, 3 insertions(+), 19 deletions(-) diff --git a/platformio.ini b/platformio.ini index 2d7c8209c..94e3e9f5b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -42,7 +42,7 @@ build_flags = -Wno-missing-field-initializers -Wno-format -Isrc -Isrc/mesh -Isrc/gps -Isrc/buzz -Wl,-Map,.pio/build/output.map -DUSE_THREAD_NAMES - -DTINYGPS_OPTION_NO_CUSTOM_FIELDS +; -DTINYGPS_OPTION_NO_CUSTOM_FIELDS // this should work now... -DPB_ENABLE_MALLOC=1 -DRADIOLIB_EXCLUDE_CC1101 -DRADIOLIB_EXCLUDE_NRF24 diff --git a/src/GPSStatus.h b/src/GPSStatus.h index cde351818..35a0b11f2 100644 --- a/src/GPSStatus.h +++ b/src/GPSStatus.h @@ -25,22 +25,6 @@ class GPSStatus : public Status public: GPSStatus() { statusType = STATUS_TYPE_GPS; } - // // proposed for deprecation - // GPSStatus(bool hasLock, bool isConnected, int32_t latitude, int32_t longitude, int32_t altitude, uint32_t dop, - // uint32_t heading, uint32_t numSatellites) - // : Status() - // { - // this->hasLock = hasLock; - // this->isConnected = isConnected; - - // this->p.latitude_i = latitude; - // this->p.longitude_i = longitude; - // this->p.altitude = altitude; - // this->p.PDOP = dop; - // this->p.ground_track = heading; - // this->p.sats_in_view = numSatellites; - // } - // preferred method GPSStatus(bool hasLock, bool isConnected, const Position &pos) : Status() { diff --git a/src/gps/NMEAGPS.cpp b/src/gps/NMEAGPS.cpp index 1b103f534..0ff302bab 100644 --- a/src/gps/NMEAGPS.cpp +++ b/src/gps/NMEAGPS.cpp @@ -109,7 +109,7 @@ bool NMEAGPS::lookForLocation() #ifndef TINYGPS_OPTION_NO_CUSTOM_FIELDS fixType = atoi(gsafixtype.value()); // will set to zero if no data - DEBUG_MSG("FIX QUAL=%d, TYPE=%d\n", fixQual, fixType); + // DEBUG_MSG("FIX QUAL=%d, TYPE=%d\n", fixQual, fixType); #endif // check if GPS has an acceptable lock @@ -168,7 +168,7 @@ bool NMEAGPS::lookForLocation() #ifndef TINYGPS_OPTION_NO_CUSTOM_FIELDS p.HDOP = reader.hdop.value(); p.PDOP = TinyGPSPlus::parseDecimal(gsapdop.value()); - DEBUG_MSG("PDOP=%d, HDOP=%d\n", dop, reader.hdop.value()); + // DEBUG_MSG("PDOP=%d, HDOP=%d\n", p.PDOP, p.HDOP); #else // FIXME! naive PDOP emulation (assumes VDOP==HDOP) // correct formula is PDOP = SQRT(HDOP^2 + VDOP^2) From b54044fd00fd9d1572f97ad06d45aef1230e5323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 20 Nov 2022 12:00:57 +0100 Subject: [PATCH 14/18] if we have a filename, 'address' is the line number. Don't print that in hex ;-) If it's really a hex address, prefix it with 0x --- src/mesh/NodeDB.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 8a7ee6086..a290f4888 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -741,9 +741,9 @@ void recordCriticalError(CriticalErrorCode code, uint32_t address, const char *f String lcd = String("Critical error ") + code + "!\n"; screen->print(lcd.c_str()); if (filename) - DEBUG_MSG("NOTE! Recording critical error %d at %s:%lx\n", code, filename, address); + DEBUG_MSG("NOTE! Recording critical error %d at %s:%lu\n", code, filename, address); else - DEBUG_MSG("NOTE! Recording critical error %d, address=%lx\n", code, address); + DEBUG_MSG("NOTE! Recording critical error %d, address=0x%lx\n", code, address); // Record error to DB myNodeInfo.error_code = code; From 1c5292ac864661054c3a6c7a12e3f88941003e99 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sun, 20 Nov 2022 12:29:10 -0600 Subject: [PATCH 15/18] Init default mqtt configurations --- src/mesh/NodeDB.cpp | 5 +++++ src/mesh/NodeDB.h | 4 ++++ src/mqtt/MQTT.cpp | 25 ++++++++----------------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 8a7ee6086..9f092b346 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -204,6 +204,7 @@ void NodeDB::installDefaultModuleConfig() { DEBUG_MSG("Installing default ModuleConfig\n"); memset(&moduleConfig, 0, sizeof(ModuleConfig)); + moduleConfig.version = DEVICESTATE_CUR_VER; moduleConfig.has_mqtt = true; moduleConfig.has_range_test = true; @@ -213,6 +214,10 @@ void NodeDB::installDefaultModuleConfig() moduleConfig.has_external_notification = true; moduleConfig.has_canned_message = true; + strncpy(moduleConfig.mqtt.address, default_mqtt_address, sizeof(default_mqtt_address)); + strncpy(moduleConfig.mqtt.username, default_mqtt_username, sizeof(default_mqtt_username)); + strncpy(moduleConfig.mqtt.password, default_mqtt_password, sizeof(default_mqtt_password)); + initModuleConfigIntervals(); } diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index dcd518191..545c288ab 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -194,6 +194,10 @@ extern NodeDB nodeDB; #define default_min_wake_secs 10 #define default_screen_on_secs 60 * 10 +#define default_mqtt_address "mqtt.meshtastic.org" +#define default_mqtt_username "meshdev" +#define default_mqtt_password "large4cats" + inline uint32_t getConfiguredOrDefaultMs(uint32_t configuredInterval) { if (configuredInterval > 0) return configuredInterval * 1000; diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 194683939..f7193eb32 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -118,24 +118,16 @@ bool MQTT::connected() void MQTT::reconnect() { if (wantsLink()) { - const char *serverAddr = "mqtt.meshtastic.org"; // default hostname - int serverPort = 1883; // default server port - const char *mqttUsername = "meshdev"; - const char *mqttPassword = "large4cats"; + // Defaults + int serverPort = 1883; + const char *serverAddr = default_mqtt_address; + const char *mqttUsername = default_mqtt_username; + const char *mqttPassword = default_mqtt_password; if (*moduleConfig.mqtt.address) { - serverAddr = moduleConfig.mqtt.address; // Override the default - mqttUsername = - moduleConfig.mqtt.username; // do not use the hardcoded credentials for a custom mqtt server + serverAddr = moduleConfig.mqtt.address; + mqttUsername = moduleConfig.mqtt.username; mqttPassword = moduleConfig.mqtt.password; - } else { - // we are using the default server. Use the hardcoded credentials by default, but allow overriding - if (*moduleConfig.mqtt.username && moduleConfig.mqtt.username[0] != '\0') { - mqttUsername = moduleConfig.mqtt.username; - } - if (*moduleConfig.mqtt.password && moduleConfig.mqtt.password[0] != '\0') { - mqttPassword = moduleConfig.mqtt.password; - } } String server = String(serverAddr); @@ -148,8 +140,7 @@ void MQTT::reconnect() } pubSub.setServer(serverAddr, serverPort); - DEBUG_MSG("Connecting to MQTT server %s, port: %d, username: %s, password: %s\n", serverAddr, serverPort, mqttUsername, - mqttPassword); + DEBUG_MSG("Connecting to MQTT server %s, port: %d, username: %s, password: %s\n", serverAddr, serverPort, mqttUsername, mqttPassword); auto myStatus = (statusTopic + owner.id); bool connected = pubSub.connect(owner.id, mqttUsername, mqttPassword, myStatus.c_str(), 1, true, "offline"); if (connected) { From 4a2b02347fa3067cc968206cd7994f53a2114af8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 20 Nov 2022 20:59:48 +0100 Subject: [PATCH 16/18] Keymatrix Bugfix: Allow key 3 to send associated text Feature: setting a text "~" emulates IO5/Userbutton with keymatrix. --- src/modules/CannedMessageModule.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/modules/CannedMessageModule.cpp b/src/modules/CannedMessageModule.cpp index b00dfb796..e7b34b9a7 100644 --- a/src/modules/CannedMessageModule.cpp +++ b/src/modules/CannedMessageModule.cpp @@ -176,7 +176,7 @@ int CannedMessageModule::handleInputEvent(const InputEvent *event) DEBUG_MSG("Canned message event Matrix key pressed\n"); // this will send the text immediately on matrix press this->runState = CANNED_MESSAGE_RUN_STATE_ACTION_SELECT; - this->payload = event->kbchar; + this->payload = MATRIXKEY; this->currentMessageIndex = event->kbchar -1; this->lastTouchMillis = millis(); validEvent = true; @@ -246,7 +246,12 @@ int32_t CannedMessageModule::runOnce() } } else { if ((this->messagesCount > this->currentMessageIndex) && (strlen(this->messages[this->currentMessageIndex]) > 0)) { - sendText(NODENUM_BROADCAST, this->messages[this->currentMessageIndex], true); + if(strcmp (this->messages[this->currentMessageIndex], "~") == 0) { + powerFSM.trigger(EVENT_PRESS); + return INT32_MAX; + } else { + sendText(NODENUM_BROADCAST, this->messages[this->currentMessageIndex], true); + } this->runState = CANNED_MESSAGE_RUN_STATE_SENDING_ACTIVE; } else { DEBUG_MSG("Reset message is empty.\n"); From a33325f90fbd1c8ee6c46685ccfb98918887e21b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Mon, 21 Nov 2022 13:46:22 +0100 Subject: [PATCH 17/18] update web server reference to latest commit --- arch/esp32/esp32.ini | 2 +- arch/esp32/esp32s3.ini | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/esp32/esp32.ini b/arch/esp32/esp32.ini index ba98a6b1f..730b78942 100644 --- a/arch/esp32/esp32.ini +++ b/arch/esp32/esp32.ini @@ -31,7 +31,7 @@ lib_deps = ${arduino_base.lib_deps} ${networking_base.lib_deps} ${environmental_base.lib_deps} - https://github.com/meshtastic/esp32_https_server.git#657509856ce97e9dddeffb89a559f544faefd5cd + https://github.com/meshtastic/esp32_https_server.git#23665b3adc080a311dcbb586ed5941b5f94d6ea2 h2zero/NimBLE-Arduino@^1.4.0 https://github.com/lewisxhe/XPowersLib.git#84b7373faea3118b6c37954d52f98b8a337148d6 diff --git a/arch/esp32/esp32s3.ini b/arch/esp32/esp32s3.ini index f5338d9a9..0c2d7d8f1 100644 --- a/arch/esp32/esp32s3.ini +++ b/arch/esp32/esp32s3.ini @@ -31,7 +31,7 @@ lib_deps = ${arduino_base.lib_deps} ${networking_base.lib_deps} ${environmental_base.lib_deps} - https://github.com/meshtastic/esp32_https_server.git#657509856ce97e9dddeffb89a559f544faefd5cd + https://github.com/meshtastic/esp32_https_server.git#23665b3adc080a311dcbb586ed5941b5f94d6ea2 h2zero/NimBLE-Arduino@^1.4.0 https://github.com/lewisxhe/XPowersLib.git#84b7373faea3118b6c37954d52f98b8a337148d6 From 3b2b0bdc971c3f0be2ed1f50d134c3619476a853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Mon, 21 Nov 2022 15:50:47 +0100 Subject: [PATCH 18/18] fix TLora V1 Boards Display Init --- variants/tlora_v1/variant.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/variants/tlora_v1/variant.h b/variants/tlora_v1/variant.h index 3a1d61db5..6ab8bad4c 100644 --- a/variants/tlora_v1/variant.h +++ b/variants/tlora_v1/variant.h @@ -6,7 +6,7 @@ #define RESET_OLED 16 // If defined, this pin will be used to reset the display controller -// #define VEXT_ENABLE 21 // active low, powers the oled display and the lora antenna boost +#define VEXT_ENABLE 21 // active low, powers the oled display and the lora antenna boost #define LED_PIN 2 // If defined we will blink this LED #define BUTTON_PIN 0 // If defined, this will be used for user button presses #define BUTTON_NEED_PULLUP