OTA werks... sort of!

This commit is contained in:
Sebastien
2019-09-25 17:37:51 -04:00
parent c5fc6b8a81
commit 46024a358e
9 changed files with 101 additions and 19 deletions

View File

@@ -282,9 +282,14 @@ void console_start() {
esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
cfg.thread_name= "console";
cfg.inherit_cfg = true;
#if RECOVERY_APPLICATION
// make sure the stack is large enough for http processing with redirects.
cfg.stack_size = 1024*100 ;
#endif
esp_pthread_set_cfg(&cfg);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&thread_console, &attr, console_thread, NULL);
pthread_attr_destroy(&attr);
}

View File

@@ -38,15 +38,17 @@
#include "lwip/api.h"
#include "lwip/err.h"
#include "lwip/netdb.h"
#include "nvs_utilities.h"
#include "http_server.h"
#include "wifi_manager.h"
#include "squeezelite-ota.h"
static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;
#define JOIN_TIMEOUT_MS (10000)
static const char TAG[] = "esp_app_main";
char * fwurl = NULL;
#ifdef CONFIG_SQUEEZEAMP
#define LED_GREEN_GPIO 12
@@ -55,15 +57,18 @@ static const char TAG[] = "esp_app_main";
#define LED_GREEN_GPIO 0
#define LED_RED_GPIO 0
#endif
static bool bWifiConnected=false;
/* brief this is an exemple of a callback that you can setup in your own app to get notified of wifi manager event */
void cb_connection_got_ip(void *pvParameter){
ESP_LOGI(TAG, "I have a connection!");
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
bWifiConnected=true;
led_unpush(LED_GREEN);
}
void cb_connection_sta_disconnected(void *pvParameter){
led_blink_pushed(LED_GREEN, 250, 250);
bWifiConnected=false;
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
}
bool wait_for_wifi(){
@@ -88,6 +93,7 @@ void app_main()
led_config(LED_GREEN, LED_GREEN_GPIO, 0);
led_config(LED_RED, LED_RED_GPIO, 0);
wifi_event_group = xEventGroupCreate();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
/* start the wifi manager */
led_blink(LED_GREEN, 250, 250);
@@ -95,6 +101,17 @@ void app_main()
wifi_manager_set_callback(EVENT_STA_GOT_IP, &cb_connection_got_ip);
wifi_manager_set_callback(WIFI_EVENT_STA_DISCONNECTED, &cb_connection_sta_disconnected);
char * fwurl = get_nvs_value_alloc(NVS_TYPE_STR, "fwurl");
if(fwurl){
// the first thing we need to do here is to erase the firmware url
// to avoid a boot loop
erase_nvs("fwurl");
while(!bWifiConnected){
wait_for_wifi();
}
ESP_LOGI(TAG,"Updating firmware from link: %s",fwurl);
start_ota(fwurl);
}
console_start();
}

View File

@@ -210,4 +210,22 @@ esp_err_t get_nvs_value(nvs_type_t type, const char *key, void*value, const uint
nvs_close(nvs);
return err;
}
esp_err_t erase_nvs(const char *key)
{
nvs_handle nvs;
esp_err_t err = nvs_open(current_namespace, NVS_READWRITE, &nvs);
if (err == ESP_OK) {
err = nvs_erase_key(nvs, key);
if (err == ESP_OK) {
err = nvs_commit(nvs);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Value with key '%s' erased", key);
}
}
nvs_close(nvs);
}
return err;
}

View File

@@ -9,6 +9,7 @@ esp_err_t store_nvs_value_len(nvs_type_t type, const char *key, void * data, siz
esp_err_t store_nvs_value(nvs_type_t type, const char *key, void * data);
esp_err_t get_nvs_value(nvs_type_t type, const char *key, void*value, const uint8_t buf_size);
void * get_nvs_value_alloc(nvs_type_t type, const char *key);
esp_err_t erase_nvs(const char *key);
#ifdef __cplusplus
}
#endif

View File

@@ -24,7 +24,7 @@
#include "esp_pthread.h"
extern void run_command(char * line);
extern bool wait_for_wifi();
extern bool wait_for_wifi();
extern void console_start();
extern pthread_cond_t wifi_connect_suspend_cond;
extern pthread_t wifi_connect_suspend_mutex;