diff --git a/platformio.ini b/platformio.ini index b07f4d49..ddf0adc7 100644 --- a/platformio.ini +++ b/platformio.ini @@ -32,14 +32,10 @@ user = cyberman54 repository = paxcounter package = esp32-paxcounter api_token = 9f02e2a2374c278fd79d5bcf4b4442fca9752012 -;api_token = ${env.BINTRAY_API_TOKEN} -; Wi-Fi network settings [wifi] -ssid = PRENZLNET-G -password = 435Huse8!? -;ssid = ${env.PIO_WIFI_SSID} -;password = ${env.PIO_WIFI_PASSWORD} +ssid = *** +password = *** [common] platform = https://github.com/platformio/platform-espressif32.git diff --git a/src/OTA.cpp b/src/OTA.cpp index 7b9849cd..23d64f5c 100644 --- a/src/OTA.cpp +++ b/src/OTA.cpp @@ -2,29 +2,61 @@ const BintrayClient bintray(BINTRAY_USER, BINTRAY_REPO, BINTRAY_PACKAGE); -void ota_wifi_init(void) { - const int RESPONSE_TIMEOUT_MS = 5000; - unsigned long timeout = millis(); +bool Wifi_Connected = false; - ESP_ERROR_CHECK(esp_wifi_set_promiscuous(false)); // switch off monitor mode +static esp_err_t event_handler(void *ctx, system_event_t *event) { + switch (event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_GOT_IP: + Wifi_Connected = true; + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + Wifi_Connected = false; + break; + default: + break; + } +} + +void ota_wifi_init(void) { + + // initialize the tcp stack tcpip_adapter_init(); + + // initialize the wifi event handler + ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + wifi_config_t sta_config = {}; + + strcpy((char *)sta_config.sta.ssid, WIFI_SSID); + strcpy((char *)sta_config.sta.password, WIFI_PASS); + sta_config.sta.bssid_set = false; + + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_config)); ESP_ERROR_CHECK(esp_wifi_start()); - WiFi.begin(WIFI_SSID, WIFI_PASS); - WiFi.setHostname(PROGNAME); + // print the local IP address + tcpip_adapter_ip_info_t ip_info; + ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info)); + ESP_LOGI(TAG, "IP %s", ip4addr_ntoa(&ip_info.ip)); -/* - while (WiFi.status() != WL_CONNECTED) { - ESP_LOGI(TAG, "WiFi Status %d", WiFi.status()); - if (millis() - timeout > RESPONSE_TIMEOUT_MS) { - ESP_LOGE(TAG, "WiFi connection timeout. Please check your settings!"); - } +} - delay(500); - } +void start_ota_update() { + ESP_LOGI(TAG, "Stopping Wifi task on core 0"); + vTaskDelete(WifiLoopTask); - configASSERT(WiFi.isConnected() == true); -*/ + ESP_LOGI(TAG, "Stopping LORA task on core 1"); + vTaskDelete(LoraTask); + ESP_LOGI(TAG, "Connecting to %s", WIFI_SSID); + ota_wifi_init(); + checkFirmwareUpdates(); + ESP.restart(); // reached if update was not successful } \ No newline at end of file diff --git a/src/OTA.h b/src/OTA.h index 5133a685..8b6a27df 100644 --- a/src/OTA.h +++ b/src/OTA.h @@ -5,7 +5,9 @@ #include "globals.h" #include #include +#include "ota.h" +#include "SecureOTA.h" -void ota_wifi_init(void); +void start_ota_update(); #endif // OTA_H \ No newline at end of file diff --git a/src/cyclic.cpp b/src/cyclic.cpp index adbadda4..45ce16cd 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -4,6 +4,7 @@ // Basic config #include "globals.h" #include "senddata.h" +#include "ota.h" // Local logging tag static const char TAG[] = "main"; @@ -14,6 +15,9 @@ void doHomework() { // update uptime counter uptime(); + if (ota_update) + start_ota_update(); + // read battery voltage into global variable #ifdef HAS_BATTERY_PROBE batt_voltage = read_voltage(); diff --git a/src/globals.h b/src/globals.h index 879a30e1..53aad820 100644 --- a/src/globals.h +++ b/src/globals.h @@ -42,7 +42,8 @@ typedef struct { } MessageBuffer_t; // global variables -extern configData_t cfg; // current device configuration +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 @@ -51,7 +52,7 @@ extern hw_timer_t *channelSwitch, *sendCycle; extern portMUX_TYPE timerMux; extern volatile int SendCycleTimerIRQ, HomeCycleIRQ, DisplayTimerIRQ, ChannelTimerIRQ, ButtonPressedIRQ; -//extern QueueHandle_t LoraSendQueue, SPISendQueue; +// extern QueueHandle_t LoraSendQueue, SPISendQueue; extern TaskHandle_t WifiLoopTask; extern std::array::iterator it; diff --git a/src/main.cpp b/src/main.cpp index fe5d5742..d52b4a7a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,7 +27,8 @@ 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 +configData_t cfg; // struct holds current device configuration +bool ota_update = false; // triggers OTA update 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, @@ -70,6 +71,9 @@ static const char TAG[] = "main"; void setup() { + // disable the default wifi logging + esp_log_level_set("wifi", ESP_LOG_NONE); + char features[100] = ""; // disable brownout detection @@ -92,7 +96,8 @@ void setup() { // initialize system event handler for wifi task, needed for // wifi_sniffer_init() - esp_event_loop_init(NULL, NULL); + // esp_event_loop_init(NULL, NULL); + //ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); // print chip information on startup if in verbose mode #ifdef VERBOSE @@ -333,7 +338,7 @@ void loop() { processSendBuffer(); // check send cycle and enqueue payload if cycle is expired sendPayload(); - // reset watchdog + // reset watchdog vTaskDelay(1 / portTICK_PERIOD_MS); } // loop() diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 63fcd690..a052c48e 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -222,17 +222,7 @@ void get_gps(uint8_t val[]) { void set_update(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: get firmware update"); - - ESP_LOGI(TAG, "Stopping Wifi task on core 0"); - vTaskDelete(WifiLoopTask); - - ESP_LOGI(TAG, "Stopping LORA task on core 1"); - vTaskDelete(LoraTask); - - ESP_LOGI(TAG, "Connecting to %s", WIFI_SSID); - ota_wifi_init(); - checkFirmwareUpdates(); - + ota_update = true; }; // assign previously defined functions to set of numeric remote commands diff --git a/src/rcommand.h b/src/rcommand.h index 880382ed..c656c9fd 100644 --- a/src/rcommand.h +++ b/src/rcommand.h @@ -6,10 +6,6 @@ #include "lorawan.h" #include "macsniff.h" #include -#include "ota.h" - -#include -#include "SecureOTA.h" // table of remote commands and assigned functions typedef struct {