From 8b0020c387891961f76f2cae25b9e81923d7b423 Mon Sep 17 00:00:00 2001 From: jedi Date: Mon, 27 Jun 2022 20:00:48 +0200 Subject: [PATCH] stash --- docker/firmware/Dockerfile | 2 +- firmware/fiatlux.c | 13 ++++++++++--- firmware/mqtt.cpp | 17 +++++++++++------ firmware/mqtt.h | 6 ++++++ firmware/web.cpp | 24 +++++++++++++++--------- firmware/wifi.cpp | 36 +++++++++++++++++++++++++++++++++++- firmware/wifi.h | 1 + 7 files changed, 79 insertions(+), 20 deletions(-) diff --git a/docker/firmware/Dockerfile b/docker/firmware/Dockerfile index d114027..1c60599 100644 --- a/docker/firmware/Dockerfile +++ b/docker/firmware/Dockerfile @@ -8,6 +8,6 @@ RUN cd app/modules/sdk; export CT_EXPERIMENTAL=y; export CT_ALLOW_BUILD_AS_ROOT= USER 0 RUN apt remove --purge -y python2 && apt autoremove --purge -y && apt install -y python3 python3-serial RUN apt install -y --reinstall python-is-python3 -USER 0 +USER 1000 WORKDIR /app ENV PATH=/app/modules/sdk/xtensa-lx106-elf/bin:$PATH diff --git a/firmware/fiatlux.c b/firmware/fiatlux.c index 04e0ab1..31f12dd 100644 --- a/firmware/fiatlux.c +++ b/firmware/fiatlux.c @@ -11,9 +11,9 @@ #include #include -void user_init(void) -{ - +#define PUB_MSG_LEN 16 + +void user_init(void) { uart_set_baud(0, 115200); printf("SDK version: %s\n", sdk_system_get_sdk_version()); @@ -30,4 +30,11 @@ void user_init(void) xTaskCreate(&httpd_task, "httpd_task", 1024, NULL, 2, NULL); xTaskCreate(&lux_task, "lux_task", 512, NULL, 1, NULL); + + wifi_alive = xSemaphoreCreateBinary(); + publish_queue = xQueueCreate(3, PUB_MSG_LEN); + + xTaskCreate(&beat_task, "beat_task", 256, NULL, 3, NULL); + + } diff --git a/firmware/mqtt.cpp b/firmware/mqtt.cpp index 3dedc99..50ecaee 100644 --- a/firmware/mqtt.cpp +++ b/firmware/mqtt.cpp @@ -3,6 +3,7 @@ // #include "mqtt.h" +#include "wifi.h" #include #include @@ -22,15 +23,18 @@ extern "C" { /* You can use http://test.mosquitto.org/ to test mqtt_client instead * of setting up your own MQTT server */ -#define MQTT_HOST ("172.16.0.42") +//#define MQTT_HOST ("172.16.0.42") +#define MQTT_HOST ("10.23.42.187") #define MQTT_PORT 1883 #define MQTT_USER NULL #define MQTT_PASS NULL -QueueHandle_t publish_queue; + #define PUB_MSG_LEN 16 +QueueHandle_t publish_queue; + extern "C" void beat_task(void *pvParameters) { TickType_t xLastWakeTime = xTaskGetTickCount(); char msg[PUB_MSG_LEN]; @@ -98,13 +102,14 @@ extern "C" void mqtt_task(void *pvParameters) { strcat(mqtt_client_id, get_my_id()); while (1) { + xSemaphoreTake(wifi_alive, portMAX_DELAY); printf("%s: started\n\r", __func__); printf("%s: (Re)connecting to MQTT server %s ... ", __func__, MQTT_HOST); ret = mqtt_network_connect(&network, MQTT_HOST, MQTT_PORT); if(ret) { - printf("error: %d\n\r", ret); - taskYIELD(); + printf("error 1: %d\n\r", ret); + vTaskDelay(1000 / portTICK_PERIOD_MS); continue; } printf("done\n\r"); @@ -121,9 +126,9 @@ extern "C" void mqtt_task(void *pvParameters) { printf("Send MQTT connect ... "); ret = mqtt_connect(&client, &data); if(ret) { - printf("error: %d\n\r", ret); + printf("error 2: %d\n\r", ret); mqtt_network_disconnect(&network); - taskYIELD(); + vTaskDelay(1000 / portTICK_PERIOD_MS); continue; } printf("done\r\n"); diff --git a/firmware/mqtt.h b/firmware/mqtt.h index b8d8a8e..80d15d4 100644 --- a/firmware/mqtt.h +++ b/firmware/mqtt.h @@ -5,6 +5,10 @@ #ifndef FIRMWARE_MQTT_H #define FIRMWARE_MQTT_H + +#include +#include + #ifdef __cplusplus extern "C" { #endif @@ -12,6 +16,8 @@ extern "C" { void mqtt_task(void *pvParameters); void beat_task(void *pvParameters); +extern QueueHandle_t publish_queue; + #ifdef __cplusplus } #endif diff --git a/firmware/web.cpp b/firmware/web.cpp index f627fcc..38875ea 100644 --- a/firmware/web.cpp +++ b/firmware/web.cpp @@ -60,7 +60,8 @@ void websocket_task(void *pvParameter) { UNLOCK_TCPIP_CORE(); } else syslog("buffer too small -1\n"); - vTaskDelayMs(1000); + + taskYIELD(); } //Global Info @@ -94,7 +95,8 @@ void websocket_task(void *pvParameter) { UNLOCK_TCPIP_CORE(); } else syslog("buffer too small 0\n"); - vTaskDelayMs(1000); + + taskYIELD(); } //Connection Info @@ -117,7 +119,8 @@ void websocket_task(void *pvParameter) { UNLOCK_TCPIP_CORE(); } else syslog("buffer too small 1\n"); - vTaskDelayMs(1000); + + taskYIELD(); } if(has_changed.wifi) { @@ -175,7 +178,8 @@ void websocket_task(void *pvParameter) { syslog("buffer too small 2\n"); } - vTaskDelayMs(1000); + + taskYIELD(); if(opmode == STATION_MODE || opmode == STATIONAP_MODE) { uint8_t hwaddr[6]; @@ -204,7 +208,8 @@ void websocket_task(void *pvParameter) { } - vTaskDelayMs(250); + + taskYIELD(); { uint8_t response[3]; uint16_t val; @@ -354,11 +359,11 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, if(ret == OK) { if(cmd == 'R' || cmd == 'C') { // Restart printf("rebooting now"); - vTaskDelay(1000 / portTICK_PERIOD_MS); + taskYIELD(); vPortEnterCritical(); sdk_system_restart(); } else if(cmd == 'X') { // Clear Config - vTaskDelay(1000 / portTICK_PERIOD_MS); + taskYIELD(); system_clear_config(); } } @@ -378,8 +383,9 @@ void websocket_open_cb(struct tcp_pcb *pcb, const char *uri) { extern "C" void httpd_task(void *pvParameters) { (void) pvParameters; - while (!uxSemaphoreGetCount(wifi_available_semaphore)) - vTaskDelay(500 / portTICK_PERIOD_MS); + //while (!uxSemaphoreGetCount(wifi_available_semaphore)) + // taskYIELD(); + printf("httpd_task: wifi is available\n"); websocket_register_callbacks((tWsOpenHandler) websocket_open_cb, (tWsHandler) websocket_cb); httpd_init(); diff --git a/firmware/wifi.cpp b/firmware/wifi.cpp index c6af213..df919c5 100644 --- a/firmware/wifi.cpp +++ b/firmware/wifi.cpp @@ -32,6 +32,7 @@ char *wifi_ap_ssid = nullptr; char *wifi_ap_password = nullptr; SemaphoreHandle_t wifi_available_semaphore = nullptr; +SemaphoreHandle_t wifi_alive = nullptr; [[noreturn]] static void dns_task(void *pvParameters) { char *wifi_ap_ip_addr = nullptr; @@ -347,5 +348,38 @@ extern "C" void wifi_task(void *pvParameters) { xSemaphoreGive(wifi_available_semaphore); //monitor loop connection here - vTaskDelete(nullptr); + uint8_t status = 0; + uint8_t retries = 30; + + while (1) { + while ((status != STATION_GOT_IP) && (retries)) { + status = sdk_wifi_station_get_connect_status(); + printf("%s: status = %d\n\r", __func__, status); + if(status == STATION_WRONG_PASSWORD) { + printf("WiFi: wrong password\n\r"); + break; + } else if(status == STATION_NO_AP_FOUND) { + printf("WiFi: AP not found\n\r"); + break; + } else if(status == STATION_CONNECT_FAIL) { + printf("WiFi: connection failed\r\n"); + break; + } + vTaskDelay(1000 / portTICK_PERIOD_MS); + --retries; + } + if(status == STATION_GOT_IP) { + printf("WiFi: Connected\n\r"); + xSemaphoreGive(wifi_alive); + taskYIELD(); + } + + while ((status = sdk_wifi_station_get_connect_status()) == STATION_GOT_IP) { + xSemaphoreGive(wifi_alive); + taskYIELD(); + } + printf("WiFi: disconnected\n\r"); + sdk_wifi_station_disconnect(); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } } \ No newline at end of file diff --git a/firmware/wifi.h b/firmware/wifi.h index 9fec7da..3821e29 100644 --- a/firmware/wifi.h +++ b/firmware/wifi.h @@ -13,6 +13,7 @@ extern "C" { #endif extern SemaphoreHandle_t wifi_available_semaphore; +extern SemaphoreHandle_t wifi_alive; void wifi_task(void *pvParameters);