Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.

Fri, 08 Nov 2019 22:40:15 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 08 Nov 2019 22:40:15 +0100
changeset 26
8a3696620c0a
parent 25
cc7c423f03fb
child 27
8bb63daa7b46

Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.

main/co2meter.c file | annotate | diff | comparison | revisions
main/config.h file | annotate | diff | comparison | revisions
main/task_user.c file | annotate | diff | comparison | revisions
main/task_user.h file | annotate | diff | comparison | revisions
main/updates.c file | annotate | diff | comparison | revisions
main/updates.h file | annotate | diff | comparison | revisions
--- a/main/co2meter.c	Fri Nov 08 10:57:46 2019 +0100
+++ b/main/co2meter.c	Fri Nov 08 22:40:15 2019 +0100
@@ -155,7 +155,7 @@
     xSemaphoreADC = xSemaphoreCreateMutex();
     xSemaphoreUnits = xSemaphoreCreateMutex();
 
-    xTaskCreate(&task_user,    "task_user",     3072, NULL,10, &xTaskUser);
+    xTaskCreate(&task_user,    "task_user",     4096, NULL,10, &xTaskUser);
     xTaskCreate(&task_ds18b20, "task_ds18b20",  2560, NULL, 8, &xTaskDS18B20);
     xTaskCreate(&task_adc,     "task_adc",      2560, NULL, 8, &xTaskADC);
     esp_log_level_set("wifi", ESP_LOG_ERROR);
--- a/main/config.h	Fri Nov 08 10:57:46 2019 +0100
+++ b/main/config.h	Fri Nov 08 22:40:15 2019 +0100
@@ -60,6 +60,7 @@
 #include "task_mqtt.h"
 #include "task_user.h"
 #include "xutil.h"
+#include "updates.h"
 
 
 /**
@@ -95,6 +96,7 @@
     ML2_NETWORK,		///< Network status
     ML2_MQTT,			///< MQTT status
     ML2_UPDATE,			///< Update
+    ML2_DO_UPDATE,		///< Perform update
     ML2_SETUP_UNIT1,		///< Unit 1 setup
     ML2_SETUP_UNIT2,		///< Unit 2 setup
     ML2_SETUP_UNIT3,		///< Unit 3 setup
--- a/main/task_user.c	Fri Nov 08 10:57:46 2019 +0100
+++ b/main/task_user.c	Fri Nov 08 22:40:15 2019 +0100
@@ -33,7 +33,7 @@
 extern SemaphoreHandle_t		xSemaphoreWiFi;			///< WiFi lock semaphore
 extern int				count_pub;			///< Published MQTT messages in transit
 extern int				Main_Loop1;			///< Main measure loop
-
+extern int				update_running;			///< If update is running
 
 const int TASK_USER_COLD = BIT0;					///< System cold start
 const int TASK_USER_WAKEUP = BIT1;					///< System wakeup from deepsleep
@@ -55,7 +55,7 @@
 {
     SecsCount++;
     if ((SecsCount % 60) == 0) {
-	if (Main_Loop1 == ML1_DONE)
+	if (Main_Loop1 == ML1_DONE && update_running == 0)
 	    Main_Loop1 = ML1_INIT;
     }
 
@@ -390,9 +390,9 @@
     	sprintf(buf, "%.2f bar", units[no].pressure / 1000.0);
     	w = u8g2_GetUTF8Width(&u8g2, buf);
     	u8g2_DrawUTF8(&u8g2, (128 - w) / 2,63, buf);
+	u8g2_SendBuffer(&u8g2);
 
 	xSemaphoreGive(xSemaphoreUnits);
-	u8g2_SendBuffer(&u8g2);
     } else {
 	ESP_LOGE(TAG, "screen_unit(%d) lock error", no);
     }
@@ -445,8 +445,8 @@
     	u8g2_DrawStr(&u8g2, 1, 43, buf);
     	snprintf(buf, 65, "Online %s", wifi_state->STA_online ? "Yes":"No");
     	u8g2_DrawStr(&u8g2, 1, 59, buf);
+	u8g2_SendBuffer(&u8g2);
 	xSemaphoreGive(xSemaphoreWiFi);
-	u8g2_SendBuffer(&u8g2);
     } else {
 	ESP_LOGE(TAG, "screen_wifi() lock error");
     }
@@ -468,12 +468,17 @@
 
 void screen_network()
 {
-    screen_top("Network Status");
-    menu_line(0, 1, 25, "IP   %s", wifi_state->STA_ip);
-    menu_line(0, 1, 37, "Mask %s", wifi_state->STA_nm);
-    menu_line(0, 1, 49, "GW   %s", wifi_state->STA_gw);
-    menu_line(0, 1, 61, "Name %s", config.hostname);
-    u8g2_SendBuffer(&u8g2);
+    if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
+    	screen_top("Network Status");
+    	menu_line(0, 1, 25, "IP   %s", wifi_state->STA_ip);
+    	menu_line(0, 1, 37, "Mask %s", wifi_state->STA_nm);
+    	menu_line(0, 1, 49, "GW   %s", wifi_state->STA_gw);
+    	menu_line(0, 1, 61, "Name %s", config.hostname);
+    	u8g2_SendBuffer(&u8g2);
+        xSemaphoreGive(xSemaphoreWiFi);
+    } else {
+        ESP_LOGE(TAG, "screen_network() lock error");
+    }
 }
 
 
@@ -521,6 +526,21 @@
 
 
 
+void screen_updating(char *m1, char *m2)
+{
+    screen_top("Updating ...");
+    u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols);
+    if (m1) {
+	u8g2_DrawUTF8(&u8g2,2,30, m1);
+    }
+    if (m2) {
+	u8g2_DrawUTF8(&u8g2,2,55, m2);
+    }
+    u8g2_SendBuffer(&u8g2);
+}
+
+
+
 /**
  * @brief Interrupt service routine for the rotary pushbutton.
  */
@@ -665,6 +685,10 @@
                 screen_update();
                 break;
 
+	    case ML2_DO_UPDATE:
+		ESP_LOGI(TAG, "Loop user: Do update");
+		break;
+
             case ML2_SETUP_UNIT1:
             case ML2_SETUP_UNIT2:
             case ML2_SETUP_UNIT3:
@@ -859,6 +883,15 @@
 		}
 		break;
 
+	case ML2_UPDATE:
+		New_Loop2 = ML2_DO_UPDATE;
+		break;
+
+	case ML2_DO_UPDATE:
+		bin_update();
+		New_Loop2 = ML2_UPDATE;
+		break;
+
 	default:
 		break;
     }
--- a/main/task_user.h	Fri Nov 08 10:57:46 2019 +0100
+++ b/main/task_user.h	Fri Nov 08 22:40:15 2019 +0100
@@ -46,6 +46,16 @@
 bool user_busy(void);
 
 
+
+/**
+ * @brief Write messages on the screen during binary update.
+ * @param m1 Message 1 or NULL.
+ * @param m2 Message 2 or NULL.
+ */
+void screen_updating(char *m1, char *m2);
+
+
+
 /**
  * @brief The FreeRTOS task to run the user interface.
  */
--- a/main/updates.c	Fri Nov 08 10:57:46 2019 +0100
+++ b/main/updates.c	Fri Nov 08 22:40:15 2019 +0100
@@ -10,7 +10,10 @@
 #define BUFFSIZE 1024					///< Download buffer size
 static char ota_write_data[BUFFSIZE + 1] = { 0 };
 static const char               *TAG = "update";
+int				update_running = 0;	///< Not zero if update is running.
 
+extern u8g2_t			u8g2;			///< Structure for the display.
+extern int			Main_Loop1;
 
 
 static void http_cleanup(esp_http_client_handle_t client)
@@ -31,8 +34,19 @@
     const esp_partition_t       *update_partition = NULL;
     esp_ota_handle_t            update_handle = 0;
 
-//    TFT_setFont(DEJAVU18_FONT, NULL);
-//    _fg = TFT_CYAN;
+    ESP_LOGI(TAG, "Update begin");
+    update_running = 1;
+    screen_updating("Stop tests", NULL);
+    requestWiFi_user(true);
+
+    for (;;) {
+	vTaskDelay(50 / portTICK_PERIOD_MS);
+	if (ready_WiFi() && Main_Loop1 == ML1_DONE)
+	    break;
+    }
+    ESP_LOGI(TAG, "System is ready for update");
+    screen_updating("Prepare update", NULL);
+
     const esp_partition_t       *running = esp_ota_get_running_partition();
 
     /*
@@ -77,7 +91,7 @@
 	goto updateerr;
     }
 
-//    TFT_print((char *)"Begin firmware download.\r\n", 0, LASTY);
+    screen_updating("Begin download", NULL);
     ESP_LOGI(TAG, "Download update %s size %d", update.url, content_length);
     int binary_file_length = 0;
     /*deal with all receive packet*/
@@ -118,6 +132,7 @@
                     }
 
 		    if (memcmp(new_app_info.app_elf_sha256, running_app_info.app_elf_sha256, 32) == 0) {
+			screen_updating("No new update", NULL);
                         ESP_LOGI(TAG, "Current running version is the same as a new.");
                         http_cleanup(client);
 			goto updateok;
@@ -161,8 +176,8 @@
 	ESP_LOGE(TAG, "esp_ota_end failed!");
 	goto updateerr;
     }
-    snprintf(temp, 63, "Received image %d bytes\r\n", binary_file_length);
-//    TFT_print(temp, 0, LASTY);
+    snprintf(temp, 63, "Ok %d bytes", binary_file_length);
+    screen_updating("Begin download", temp);
 
     /*
      * Here we have new version, install and boot it.
@@ -174,132 +189,19 @@
     }
 
     ESP_LOGI(TAG, "Prepare to restart system!");
- //   TFT_print((char *)"Rebooting ...", 0, LASTY);
+    screen_updating("Reboot...", "...Reboot");
     vTaskDelay(1000 / portTICK_PERIOD_MS);
+    update_running = 0;
     esp_restart();
     return;
 
 updateerr:
-//    _fg = TFT_RED;
-//    TFT_print((char *)"Error\r\n", 0, LASTY);
+    screen_updating("** ERROR **", "Update failed");
 
 updateok:
+    update_running = 0;
+    requestWiFi_user(false);
     vTaskDelay(3000 / portTICK_PERIOD_MS);
 }
 
 
-
-/**
- * @brief Download a file to /spiffs
- * @param filename The name and path of the file to download.
- * @return Return 0 if ok, negative if errors.
- */
-int DownloadSpiffs(char *filename)
-{
-    esp_err_t	err;
-    static char	theurl[73], thefile[41];
-    FILE	*f;
-
-//    static char	todel[41];
-//    snprintf(todel, 40, "/spiffs//%s", filename);
-//    unlink(todel);
-//    return 0;
-
-    snprintf(theurl, 72, "http://update.mbse.eu/ap1/image/%s", filename);
-    snprintf(thefile, 40, "/spiffs/%s", filename);
-
-    esp_http_client_config_t update = {
-	.url = theurl,
-    };
-
-    esp_http_client_handle_t client = esp_http_client_init(&update);
-    if (client == NULL) {
-	ESP_LOGE(TAG, "Failed to init HTTP connection");
-	return -1;
-    }
-
-    err = esp_http_client_open(client, 0);
-    if (err != ESP_OK) {
-	ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
-	esp_http_client_cleanup(client);
-	return -1;
-    }
-
-    int content_length = esp_http_client_fetch_headers(client);
-    int status_code = esp_http_client_get_status_code(client);
-    if (status_code != 200) {
-	ESP_LOGE(TAG, "GET %s error %d", update.url, status_code);
-	esp_http_client_cleanup(client);
-	return -1;
-    }
-
-    /*
-     * Remove a possible stale download.
-     */
-    unlink("/spiffs/tmpfile");
-    f = fopen("/spiffs/tmpfile", "w");
-    if (f == NULL) {
-	ESP_LOGE(TAG, "Cannot create /spiffs/tmpfile");
-	esp_http_client_cleanup(client);
-	return -1;
-    }
-
-    int read_length = 0;
-    int write_length = 0;
-    while (1) {
-	int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE);
-	if (data_read < 0) {
-	    ESP_LOGE(TAG, "Error: data read error %s", theurl);
-	    http_cleanup(client);
-	    return -1;
-	} else if (data_read > 0) {
-	    size_t bytes = fwrite(ota_write_data, 1, data_read, f);
-	    if (bytes != data_read) {
-		ESP_LOGE(TAG, "fwrite %s %d/%d at %d", theurl, bytes, data_read, write_length);
-	    }
-	    write_length += bytes;
-	    read_length += data_read;
-	} else if (data_read == 0) {
-	    break;
-	}
-	vTaskDelay(10 / portTICK_PERIOD_MS);
-    }
-    fclose(f);
-
-    if (content_length != write_length) {
-	ESP_LOGE(TAG, "Download %s size %d but got %d bytes", theurl, content_length, write_length);
-	unlink("/spiffs/tmpfile");
-	return -1;
-    }
-
-    ESP_LOGI(TAG, "Download %s size %d Ok", theurl, content_length);
-    unlink(thefile);
-    rename("/spiffs/tmpfile", thefile);
-    esp_http_client_cleanup(client);
-    return 0;
-}
-
-
-
-/*
- * Files init function, only runs once a new screen is entered.
- */
-void Updates_Init(void)
-{
-//			_bg = TFT_BLACK;
-//			TFT_fillScreen(_bg);
-//			TopMessage((char *)"Update");
-
-}
-
-
-
-/*
- * Updates management loop, non-blocking.
- */
-void Updates_Loop(void)
-{
-    bin_update();
-}
-
-
--- a/main/updates.h	Fri Nov 08 10:57:46 2019 +0100
+++ b/main/updates.h	Fri Nov 08 22:40:15 2019 +0100
@@ -7,14 +7,11 @@
 #ifndef _UPDATES_H
 #define _UPDATES_H
 
-/**
- * @brief Updates init fases.
- */
-void Updates_Init(void);
 
 /**
- * @brief Updates loop screens. Blocking.
+ * @brief Update firmware via internet.
  */
-void Updates_Loop(void);
+void bin_update(void);
+
 
 #endif

mercurial