ESP32-PaxCounter/src/OTA.cpp

79 lines
2.1 KiB
C++
Raw Normal View History

2018-08-12 15:42:58 +02:00
#include "OTA.h"
const BintrayClient bintray(BINTRAY_USER, BINTRAY_REPO, BINTRAY_PACKAGE);
bool Wifi_Connected = false;
2018-08-16 21:10:13 +02:00
esp_err_t event_handler(void *ctx, system_event_t *event) {
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
2018-08-16 21:10:13 +02:00
ESP_LOGI(TAG, "Event STA_START");
break;
case SYSTEM_EVENT_STA_GOT_IP:
Wifi_Connected = true;
2018-08-16 21:10:13 +02:00
ESP_LOGI(TAG, "Event STA_GOT_IP");
// 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));
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Wifi_Connected = false;
2018-08-16 21:10:13 +02:00
ESP_LOGI(TAG, "Event STA_DISCONNECTED");
break;
default:
break;
}
}
2018-08-12 15:42:58 +02:00
void ota_wifi_init(void) {
2018-08-16 21:10:13 +02:00
tcpip_adapter_if_t tcpip_if = TCPIP_ADAPTER_IF_STA;
// initialize the tcp stack
2018-08-16 21:10:13 +02:00
// nvs_flash_init();
2018-08-12 15:42:58 +02:00
tcpip_adapter_init();
2018-08-16 21:10:13 +02:00
tcpip_adapter_set_hostname(tcpip_if, PROGNAME);
tcpip_adapter_dhcpc_start(tcpip_if);
// initialize the wifi event handler
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
2018-08-16 21:10:13 +02:00
// 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));
2018-08-16 21:10:13 +02:00
wifi_sta_config_t cfg;
strcpy((char *)cfg.ssid, WIFI_SSID);
strcpy((char *)cfg.password, WIFI_PASS);
cfg.bssid_set = false;
2018-08-16 21:10:13 +02:00
wifi_config_t sta_cfg;
sta_cfg.sta = cfg;
2018-08-12 15:42:58 +02:00
2018-08-16 21:10:13 +02:00
wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT();
2018-08-12 15:42:58 +02:00
2018-08-16 21:10:13 +02:00
ESP_ERROR_CHECK(esp_wifi_init(&wifi_cfg));
ESP_ERROR_CHECK(
esp_wifi_set_storage(WIFI_STORAGE_RAM)); // we don't need NVRAM
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_cfg));
ESP_ERROR_CHECK(esp_wifi_start());
}
2018-08-12 15:42:58 +02:00
void start_ota_update() {
ESP_LOGI(TAG, "Stopping Wifi task on core 0");
vTaskDelete(WifiLoopTask);
2018-08-12 15:42:58 +02:00
ESP_LOGI(TAG, "Stopping LORA task on core 1");
vTaskDelete(LoraTask);
2018-08-12 15:42:58 +02:00
ESP_LOGI(TAG, "Connecting to %s", WIFI_SSID);
ota_wifi_init();
2018-08-16 21:10:13 +02:00
delay(2000);
delay(2000);
checkFirmwareUpdates();
ESP.restart(); // reached if update was not successful
2018-08-12 15:42:58 +02:00
}