refactor to use current wifi manager, first pass at OTA
This commit is contained in:
parent
91da7dff4c
commit
d26e21502e
1 changed files with 88 additions and 47 deletions
69
main/main.c
69
main/main.c
|
@ -31,6 +31,9 @@
|
||||||
#include "lwip/sockets.h"
|
#include "lwip/sockets.h"
|
||||||
#include "lwip/dns.h"
|
#include "lwip/dns.h"
|
||||||
#include "lwip/netdb.h"
|
#include "lwip/netdb.h"
|
||||||
|
#include "esp_ota_ops.h"
|
||||||
|
#include "esp_http_client.h"
|
||||||
|
#include "esp_https_ota.h"
|
||||||
|
|
||||||
#include "mqtt_client.h"
|
#include "mqtt_client.h"
|
||||||
|
|
||||||
|
@ -54,6 +57,10 @@
|
||||||
#define SPI_BUF (LED_COUNT + 2)
|
#define SPI_BUF (LED_COUNT + 2)
|
||||||
|
|
||||||
#define TOPIC_POWER_STATUS "stat/tasmota_E74A79/POWER"
|
#define TOPIC_POWER_STATUS "stat/tasmota_E74A79/POWER"
|
||||||
|
#define TOPIC_OTA "buttn32/ota"
|
||||||
|
|
||||||
|
extern const uint8_t certs_pem_start[] asm("_binary_certs_pem_start");
|
||||||
|
extern const uint8_t certs_pem_end[] asm("_binary_certs_pem_end");
|
||||||
|
|
||||||
static uint8_t id;
|
static uint8_t id;
|
||||||
|
|
||||||
|
@ -127,8 +134,6 @@ void led_display_loop() {
|
||||||
trans[1].tx_data[0] = (0b11100000 | 1);
|
trans[1].tx_data[0] = (0b11100000 | 1);
|
||||||
memset(&trans[SPI_BUF - 1].tx_data, 0xFF, 4);
|
memset(&trans[SPI_BUF - 1].tx_data, 0xFF, 4);
|
||||||
|
|
||||||
bool update = false;
|
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
/// TODO check _new vs _cur, ramping the channel value per step until they match
|
/// TODO check _new vs _cur, ramping the channel value per step until they match
|
||||||
/// only transmit spi data if a change was made
|
/// only transmit spi data if a change was made
|
||||||
|
@ -147,11 +152,9 @@ void led_display_loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void led_control_loop() {
|
void led_control_loop() {
|
||||||
uint8_t v = 0;
|
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
if(xQueueReceive(wm_event_queue, &v, portMAX_DELAY)) {
|
EventBits_t ev_bits = xEventGroupWaitBits(wm_event_group, 0xFF, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||||
EventBits_t ev_bits = xEventGroupGetBits(wm_event_group);
|
|
||||||
ESP_LOGI(TAG, "got event: %d", ev_bits);
|
ESP_LOGI(TAG, "got event: %d", ev_bits);
|
||||||
|
|
||||||
// TODO XXX rewrite to only update led_state_new and let led_display_loop
|
// TODO XXX rewrite to only update led_state_new and let led_display_loop
|
||||||
|
@ -196,7 +199,6 @@ void led_control_loop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static void IRAM_ATTR gpio_isr_handler(void* arg)
|
static void IRAM_ATTR gpio_isr_handler(void* arg)
|
||||||
{
|
{
|
||||||
|
@ -204,16 +206,43 @@ static void IRAM_ATTR gpio_isr_handler(void* arg)
|
||||||
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
|
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
esp_err_t _http_event_handler(esp_http_client_event_t *evt) {
|
||||||
|
switch (evt->event_id) {
|
||||||
|
case HTTP_EVENT_ERROR:
|
||||||
|
ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
|
||||||
|
break;
|
||||||
|
case HTTP_EVENT_ON_CONNECTED:
|
||||||
|
ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
|
||||||
|
break;
|
||||||
|
case HTTP_EVENT_HEADER_SENT:
|
||||||
|
ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
|
||||||
|
break;
|
||||||
|
case HTTP_EVENT_ON_HEADER:
|
||||||
|
ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
|
||||||
|
break;
|
||||||
|
case HTTP_EVENT_ON_DATA:
|
||||||
|
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
|
||||||
|
break;
|
||||||
|
case HTTP_EVENT_ON_FINISH:
|
||||||
|
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
|
||||||
|
break;
|
||||||
|
case HTTP_EVENT_DISCONNECTED:
|
||||||
|
ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
||||||
{
|
{
|
||||||
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
|
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
|
||||||
esp_mqtt_event_handle_t event = event_data;
|
esp_mqtt_event_handle_t event = event_data;
|
||||||
esp_mqtt_client_handle_t client = event->client;
|
esp_mqtt_client_handle_t client = event->client;
|
||||||
int msg_id;
|
|
||||||
switch ((esp_mqtt_event_id_t)event_id) {
|
switch ((esp_mqtt_event_id_t)event_id) {
|
||||||
case MQTT_EVENT_CONNECTED:
|
case MQTT_EVENT_CONNECTED:
|
||||||
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
|
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
|
||||||
esp_mqtt_client_subscribe(client, "espbutton/update", 0);
|
esp_mqtt_client_subscribe(client, TOPIC_OTA, 0);
|
||||||
esp_mqtt_client_subscribe(client, TOPIC_POWER_STATUS, 0);
|
esp_mqtt_client_subscribe(client, TOPIC_POWER_STATUS, 0);
|
||||||
break;
|
break;
|
||||||
case MQTT_EVENT_DISCONNECTED:
|
case MQTT_EVENT_DISCONNECTED:
|
||||||
|
@ -239,7 +268,23 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
|
||||||
} else if(strncmp(event->data, "OFF", 3) == 0) {
|
} else if(strncmp(event->data, "OFF", 3) == 0) {
|
||||||
reset_sleep_timer();
|
reset_sleep_timer();
|
||||||
}
|
}
|
||||||
|
} else if(strncmp(event->topic, (const char*)TOPIC_OTA, event->data_len) == 0) {
|
||||||
|
esp_http_client_config_t config = {
|
||||||
|
.url = "https://ota.oit.cloud/esp32-button/latest.bin",
|
||||||
|
.cert_pem = (char *)certs_pem_start,
|
||||||
|
.event_handler = _http_event_handler,
|
||||||
|
.keep_alive_enable = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
esp_err_t err = esp_https_ota(&config);
|
||||||
|
|
||||||
|
if (err == ESP_OK) {
|
||||||
|
esp_restart();
|
||||||
|
} else {
|
||||||
|
ESP_LOGE(TAG, "Firmware upgrade failed");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case MQTT_EVENT_ERROR:
|
case MQTT_EVENT_ERROR:
|
||||||
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
|
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
|
||||||
|
@ -281,8 +326,6 @@ void gpio_loop() {
|
||||||
if(level == 0 && (ev_bits & WIFI_CONNECTED) == 0) {
|
if(level == 0 && (ev_bits & WIFI_CONNECTED) == 0) {
|
||||||
ESP_LOGI(TAG, "No connectiong, connecting");
|
ESP_LOGI(TAG, "No connectiong, connecting");
|
||||||
|
|
||||||
wifi_manager_scan();
|
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -392,7 +435,7 @@ void app_main() {
|
||||||
|
|
||||||
ESP_LOGI(TAG, "MAC: %X:%X:%X:%X:%X:%X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
ESP_LOGI(TAG, "MAC: %X:%X:%X:%X:%X:%X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||||
|
|
||||||
wm_event_group = wifi_manager_start();
|
wm_event_group = wifi_manager_start("dustbutt");
|
||||||
wifi_manager_reset_store();
|
wifi_manager_reset_store();
|
||||||
|
|
||||||
if(wifi_manager_ap_count() == 0) {
|
if(wifi_manager_ap_count() == 0) {
|
||||||
|
@ -409,8 +452,6 @@ void app_main() {
|
||||||
xTaskCreate(&led_display_loop, "led_display_loop", 4096, NULL, 6, NULL);
|
xTaskCreate(&led_display_loop, "led_display_loop", 4096, NULL, 6, NULL);
|
||||||
xTaskCreate(&led_control_loop, "led_control_loop", 4096, NULL, 6, NULL);
|
xTaskCreate(&led_control_loop, "led_control_loop", 4096, NULL, 6, NULL);
|
||||||
|
|
||||||
wifi_manager_scan();
|
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue