diff --git a/README.md b/README.md index 8c6400aa..0889b2b6 100644 --- a/README.md +++ b/README.md @@ -278,6 +278,7 @@ Note: all settings are stored in NVRAM and will be reloaded when device starts. 1 = reset MAC counter to zero 2 = reset device to factory settings 3 = flush send queues + 9 = OTA software update via Wifi 0x0A set LoRaWAN payload send cycle diff --git a/platformio.ini b/platformio.ini index e42dbb31..90c8fdb7 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,8 +11,8 @@ ;env_default = heltec ;env_default = ttgov1 ;env_default = ttgov2 -;env_default = ttgov21 -env_default = ttgobeam +env_default = ttgov21 +;env_default = ttgobeam ;env_default = lopy ;env_default = lopy4 ;env_default = fipy @@ -29,7 +29,7 @@ package = ttgov21_old api_token = 2e10f923df5d47b9c7e25752510322a1d65ee997 [common] -release_version = 5 +release_version = 6 [ota] ; build configuration based on Bintray and Wi-Fi settings @@ -62,6 +62,7 @@ build_flags = -D_lmic_config_h_ -include "src/paxcounter.conf" -include "src/hal/${PIOENV}.h" + ${ota.build_flags} -w ; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <--- ; otherwise device may leak RAM @@ -89,7 +90,6 @@ monitor_speed = 115200 lib_deps = ${common_env_data.lib_deps_all} build_flags = - ${ota.build_flags} ${common_env_data.build_flags} [env:heltec] @@ -143,6 +143,9 @@ lib_deps = ${common_env_data.lib_deps_display} build_flags = ${common_env_data.build_flags} +;upload_protocol = custom +;extra_scripts = pre:publish_firmware.py + [env:ttgobeam] platform = ${common_env_data.platform_espressif32} @@ -155,11 +158,10 @@ lib_deps = ${common_env_data.lib_deps_all} ${common_env_data.lib_deps_gps} build_flags = - ${ota.build_flags} ${common_env_data.build_flags} -mfix-esp32-psram-cache-issue -upload_protocol = custom -extra_scripts = pre:publish_firmware.py +;upload_protocol = custom +;extra_scripts = pre:publish_firmware.py [env:fipy] platform = ${common_env_data.platform_espressif32} @@ -200,7 +202,6 @@ lib_deps = ${common_env_data.lib_deps_rgbled} ${common_env_data.lib_deps_gps} build_flags = - ${ota.build_flags} ${common_env_data.build_flags} -mfix-esp32-psram-cache-issue @@ -256,5 +257,4 @@ lib_deps = ${common_env_data.lib_deps_gps} ${common_env_data.lib_deps_display} build_flags = - ${ota.build_flags} ${common_env_data.build_flags} diff --git a/src/OTA.cpp b/src/OTA.cpp index 888c28cd..702c8fcb 100644 --- a/src/OTA.cpp +++ b/src/OTA.cpp @@ -36,16 +36,8 @@ volatile bool isValidContentType = false; static const char TAG[] = "main"; void start_ota_update() { - ota_update = false; // clear ota trigger switch - - ESP_LOGI(TAG, "Stopping Wifi scanner"); - vTaskDelete(WifiLoopTask); ESP_LOGI(TAG, "Starting Wifi OTA update"); - // switch off monitor more - ESP_ERROR_CHECK( - esp_wifi_set_promiscuous(false)); // now switch on monitor mode - ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(NULL)); WiFi.begin(WIFI_SSID, WIFI_PASS); @@ -56,7 +48,7 @@ void start_ota_update() { ESP_LOGI(TAG, "connected to %s", WIFI_SSID); - checkFirmwareUpdates(); + checkFirmwareUpdates(); // gets and flashes new firmware and restarts ESP.restart(); // reached only if update was not successful } // start_ota_update diff --git a/src/OTA.h b/src/OTA.h index a20e54df..52698c39 100644 --- a/src/OTA.h +++ b/src/OTA.h @@ -4,7 +4,6 @@ #include #include #include "globals.h" -#include "wifiscan.h" void checkFirmwareUpdates(); void processOTAUpdate(const String &version); diff --git a/src/configmanager.cpp b/src/configmanager.cpp index 4ff40a84..8fa6b2f9 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -31,6 +31,7 @@ void defaultConfig() { cfg.rgblum = RGBLUMINOSITY; // RGB Led luminosity (0..100%) cfg.gpsmode = 1; // 0=disabled, 1=enabled cfg.monitormode = 0; // 0=disabled, 1=enabled + cfg.runmode = 0; // 0=normal, 1=update strncpy(cfg.version, PROGVERSION, sizeof(cfg.version) - 1); } @@ -143,6 +144,10 @@ void saveConfig() { flash8 != cfg.monitormode) nvs_set_i8(my_handle, "monitormode", cfg.monitormode); + if (nvs_get_i8(my_handle, "runmode", &flash8) != ESP_OK || + flash8 != cfg.runmode) + nvs_set_i8(my_handle, "runmode", cfg.runmode); + if (nvs_get_i16(my_handle, "rssilimit", &flash16) != ESP_OK || flash16 != cfg.rssilimit) nvs_set_i16(my_handle, "rssilimit", cfg.rssilimit); @@ -326,6 +331,14 @@ void loadConfig() { saveConfig(); } + if (nvs_get_i8(my_handle, "runmode", &flash8) == ESP_OK) { + cfg.runmode = flash8; + ESP_LOGI(TAG, "Run mode = %d", flash8); + } else { + ESP_LOGI(TAG, "Run mode set to default %d", cfg.runmode); + saveConfig(); + } + nvs_close(my_handle); ESP_LOGI(TAG, "Done"); } diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 45ce16cd..ba7968bf 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -15,8 +15,9 @@ void doHomework() { // update uptime counter uptime(); - if (ota_update) - start_ota_update(); + // check if update mode trigger switch was set + if (cfg.runmode == 1) + ESP.restart(); // read battery voltage into global variable #ifdef HAS_BATTERY_PROBE diff --git a/src/globals.h b/src/globals.h index 53aad820..20acd5cb 100644 --- a/src/globals.h +++ b/src/globals.h @@ -5,7 +5,7 @@ #include // attn: increment version after modifications to configData_t truct! -#define PROGVERSION "1.4.23" // use max 10 chars here! +#define PROGVERSION "1.4.30" // use max 10 chars here! #define PROGNAME "PAXCNT" // std::set for unified array functions @@ -31,6 +31,7 @@ typedef struct { uint8_t rgblum; // RGB Led luminosity (0..100%) uint8_t gpsmode; // 0=disabled, 1=enabled uint8_t monitormode; // 0=disabled, 1=enabled + uint8_t runmode; // 0=normal, 1=update char version[10]; // Firmware version } configData_t; @@ -43,7 +44,6 @@ typedef struct { // global variables extern configData_t cfg; // current device configuration -extern bool ota_update; extern char display_line6[], display_line7[]; // screen buffers extern uint8_t channel; // wifi channel rotation counter extern uint16_t macs_total, macs_wifi, macs_ble, batt_voltage; // display values diff --git a/src/lorawan.cpp b/src/lorawan.cpp index ea0709ff..e3850847 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -196,11 +196,9 @@ void onEvent(ev_t ev) { if (LMIC.dataLen) { ESP_LOGI(TAG, "Received %d bytes of payload, RSSI %d SNR %d", - LMIC.dataLen, LMIC.rssi, (signed char)LMIC.snr / 4); - // LMIC.snr = SNR twos compliment [dB] * 4 - // LMIC.rssi = RSSI [dBm] (-196...+63) + LMIC.dataLen, LMIC.rssi, (signed char)LMIC.snr); sprintf(display_line6, "RSSI %d SNR %d", LMIC.rssi, - (signed char)LMIC.snr / 4); + (signed char)LMIC.snr); // check if command is received on command port, then call interpreter if ((LMIC.txrxFlags & TXRX_PORT) && diff --git a/src/main.cpp b/src/main.cpp index d52b4a7a..a49253f3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,8 +27,7 @@ licenses. Refer to LICENSE.txt file in repository for more details. #include "globals.h" #include "main.h" -configData_t cfg; // struct holds current device configuration -bool ota_update = false; // triggers OTA update +configData_t cfg; // struct holds current device configuration char display_line6[16], display_line7[16]; // display buffers uint8_t channel = 0; // channel rotation counter uint16_t macs_total = 0, macs_wifi = 0, macs_ble = 0, @@ -97,7 +96,7 @@ void setup() { // initialize system event handler for wifi task, needed for // wifi_sniffer_init() // esp_event_loop_init(NULL, NULL); - //ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + // ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); // print chip information on startup if in verbose mode #ifdef VERBOSE @@ -123,6 +122,13 @@ void setup() { // read settings from NVRAM loadConfig(); // includes initialize if necessary + // reboot to firmware update mode if ota trigger switch is set + if (cfg.runmode == 1) { + cfg.runmode = 0; + saveConfig(); + start_ota_update(); + } + #ifdef VENDORFILTER strcat_P(features, " OUIFLT"); #endif @@ -302,8 +308,8 @@ void setup() { ESP_LOGI(TAG, "Starting Wifi task on core 0"); wifi_sniffer_init(); // initialize salt value using esp_random() called by random() in - // arduino-esp32 core. Note: do this *after* wifi has started, since function - // gets it's seed from RF noise + // arduino-esp32 core. Note: do this *after* wifi has started, since + // function gets it's seed from RF noise reset_salt(); // get new 16bit for salting hashes xTaskCreatePinnedToCore(wifi_channel_loop, "wifiloop", 2048, (void *)1, 1, &WifiLoopTask, 0); @@ -318,7 +324,8 @@ void setup() { void loop() { while (1) { - // state machine for switching display, LED, button, housekeeping, senddata + // state machine for switching display, LED, button, housekeeping, + // senddata #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED) led_loop(); diff --git a/src/main.h b/src/main.h index 89dc0be4..58919160 100644 --- a/src/main.h +++ b/src/main.h @@ -8,6 +8,7 @@ #include "senddata.h" #include "cyclic.h" #include "beacon_array.h" +#include "ota.h" #include // needed for reading ESP32 chip attributes #include // needed for Wifi event handler diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 2bae172a..7f984cdb 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -31,6 +31,11 @@ void set_reset(uint8_t val[]) { sprintf(display_line6, "Queue reset"); flushQueues(); break; + case 9: // reset and ask for software update via Wifi OTA + ESP_LOGI(TAG, "Remote command: software update via Wifi"); + sprintf(display_line6, "Software update"); + cfg.runmode = 1; + break; default: ESP_LOGW(TAG, "Remote command: reset called with invalid parameter(s)"); } @@ -220,27 +225,22 @@ void get_gps(uint8_t val[]) { #endif }; -void set_update(uint8_t val[]) { - ESP_LOGI(TAG, "Remote command: get firmware update"); - ota_update = true; -}; - // assign previously defined functions to set of numeric remote commands // format: opcode, function, #bytes params, -// flag (1 = do make settings persistent / 0 = don't) +// flag (true = do make settings persistent / false = don't) // cmd_t table[] = { {0x01, set_rssi, 1, true}, {0x02, set_countmode, 1, true}, {0x03, set_gps, 1, true}, {0x04, set_display, 1, true}, {0x05, set_lorasf, 1, true}, {0x06, set_lorapower, 1, true}, {0x07, set_loraadr, 1, true}, {0x08, set_screensaver, 1, true}, - {0x09, set_reset, 1, false}, {0x0a, set_sendcycle, 1, true}, + {0x09, set_reset, 1, true}, {0x0a, set_sendcycle, 1, true}, {0x0b, set_wifichancycle, 1, true}, {0x0c, set_blescantime, 1, true}, {0x0d, set_vendorfilter, 1, false}, {0x0e, set_blescan, 1, true}, {0x0f, set_wifiant, 1, true}, {0x10, set_rgblum, 1, true}, {0x11, set_monitor, 1, true}, {0x12, set_beacon, 7, false}, - {0x20, set_update, 0, false}, {0x80, get_config, 0, false}, - {0x81, get_status, 0, false}, {0x84, get_gps, 0, false}}; + {0x80, get_config, 0, false}, {0x81, get_status, 0, false}, + {0x84, get_gps, 0, false}}; const uint8_t cmdtablesize = sizeof(table) / sizeof(table[0]); // number of commands in command table