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
135
main/main.c
135
main/main.c
|
@ -31,6 +31,9 @@
|
|||
#include "lwip/sockets.h"
|
||||
#include "lwip/dns.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "esp_ota_ops.h"
|
||||
#include "esp_http_client.h"
|
||||
#include "esp_https_ota.h"
|
||||
|
||||
#include "mqtt_client.h"
|
||||
|
||||
|
@ -53,7 +56,11 @@
|
|||
#define LED_COUNT (1)
|
||||
#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;
|
||||
|
||||
|
@ -127,8 +134,6 @@ void led_display_loop() {
|
|||
trans[1].tx_data[0] = (0b11100000 | 1);
|
||||
memset(&trans[SPI_BUF - 1].tx_data, 0xFF, 4);
|
||||
|
||||
bool update = false;
|
||||
|
||||
while(true) {
|
||||
/// TODO check _new vs _cur, ramping the channel value per step until they match
|
||||
/// only transmit spi data if a change was made
|
||||
|
@ -147,53 +152,50 @@ void led_display_loop() {
|
|||
}
|
||||
|
||||
void led_control_loop() {
|
||||
uint8_t v = 0;
|
||||
|
||||
while(true) {
|
||||
if(xQueueReceive(wm_event_queue, &v, portMAX_DELAY)) {
|
||||
EventBits_t ev_bits = xEventGroupGetBits(wm_event_group);
|
||||
ESP_LOGI(TAG, "got event: %d", ev_bits);
|
||||
EventBits_t ev_bits = xEventGroupWaitBits(wm_event_group, 0xFF, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
// TODO XXX rewrite to only update led_state_new and let led_display_loop
|
||||
// handle rendering
|
||||
ESP_LOGI(TAG, "got event: %d", ev_bits);
|
||||
|
||||
static spi_transaction_t trans[SPI_BUF];
|
||||
// TODO XXX rewrite to only update led_state_new and let led_display_loop
|
||||
// handle rendering
|
||||
|
||||
int i;
|
||||
for (i = 0; i < SPI_BUF; i++) {
|
||||
memset(&trans[i], 0, sizeof(spi_transaction_t));
|
||||
static spi_transaction_t trans[SPI_BUF];
|
||||
|
||||
trans[i].length=8*4;
|
||||
trans[i].flags = SPI_TRANS_USE_TXDATA;
|
||||
}
|
||||
int i;
|
||||
for (i = 0; i < SPI_BUF; i++) {
|
||||
memset(&trans[i], 0, sizeof(spi_transaction_t));
|
||||
|
||||
trans[1].tx_data[0] = (0b11100000 | 1);
|
||||
trans[i].length=8*4;
|
||||
trans[i].flags = SPI_TRANS_USE_TXDATA;
|
||||
}
|
||||
|
||||
if(ev_bits & WIFI_CONNECTED) {
|
||||
esp_mqtt_client_start(client);
|
||||
trans[1].tx_data[0] = (0b11100000 | 1);
|
||||
|
||||
trans[1].tx_data[1] = 0;
|
||||
trans[1].tx_data[2] = 127;
|
||||
trans[1].tx_data[3] = 0;
|
||||
} else if(ev_bits & WIFI_SCANNING) {
|
||||
trans[1].tx_data[1] = 0;
|
||||
trans[1].tx_data[2] = 55;
|
||||
trans[1].tx_data[3] = 127;
|
||||
} else if(ev_bits & WIFI_CONNECTING) {
|
||||
trans[1].tx_data[1] = 127;
|
||||
trans[1].tx_data[2] = 127;
|
||||
trans[1].tx_data[3] = 0;
|
||||
} else if(ev_bits & WIFI_IDLE) {
|
||||
trans[1].tx_data[1] = 0;
|
||||
trans[1].tx_data[2] = 0;
|
||||
trans[1].tx_data[3] = 127;
|
||||
}
|
||||
if(ev_bits & WIFI_CONNECTED) {
|
||||
esp_mqtt_client_start(client);
|
||||
|
||||
memset(&trans[SPI_BUF - 1].tx_data, 0xFF, 4);
|
||||
trans[1].tx_data[1] = 0;
|
||||
trans[1].tx_data[2] = 127;
|
||||
trans[1].tx_data[3] = 0;
|
||||
} else if(ev_bits & WIFI_SCANNING) {
|
||||
trans[1].tx_data[1] = 0;
|
||||
trans[1].tx_data[2] = 55;
|
||||
trans[1].tx_data[3] = 127;
|
||||
} else if(ev_bits & WIFI_CONNECTING) {
|
||||
trans[1].tx_data[1] = 127;
|
||||
trans[1].tx_data[2] = 127;
|
||||
trans[1].tx_data[3] = 0;
|
||||
} else if(ev_bits & WIFI_IDLE) {
|
||||
trans[1].tx_data[1] = 0;
|
||||
trans[1].tx_data[2] = 0;
|
||||
trans[1].tx_data[3] = 127;
|
||||
}
|
||||
|
||||
for(i = 0; i < SPI_BUF; i++) {
|
||||
spi_device_queue_trans(spi, &trans[i], portMAX_DELAY);
|
||||
}
|
||||
memset(&trans[SPI_BUF - 1].tx_data, 0xFF, 4);
|
||||
|
||||
for(i = 0; i < SPI_BUF; i++) {
|
||||
spi_device_queue_trans(spi, &trans[i], portMAX_DELAY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -204,16 +206,43 @@ static void IRAM_ATTR gpio_isr_handler(void* arg)
|
|||
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)
|
||||
{
|
||||
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_client_handle_t client = event->client;
|
||||
int msg_id;
|
||||
|
||||
switch ((esp_mqtt_event_id_t)event_id) {
|
||||
case 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);
|
||||
break;
|
||||
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) {
|
||||
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;
|
||||
case MQTT_EVENT_ERROR:
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
|
||||
|
@ -281,8 +326,6 @@ void gpio_loop() {
|
|||
if(level == 0 && (ev_bits & WIFI_CONNECTED) == 0) {
|
||||
ESP_LOGI(TAG, "No connectiong, connecting");
|
||||
|
||||
wifi_manager_scan();
|
||||
|
||||
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]);
|
||||
|
||||
wm_event_group = wifi_manager_start();
|
||||
wm_event_group = wifi_manager_start("dustbutt");
|
||||
wifi_manager_reset_store();
|
||||
|
||||
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_control_loop, "led_control_loop", 4096, NULL, 6, NULL);
|
||||
|
||||
wifi_manager_scan();
|
||||
|
||||
while(true) {
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue