Added WiFi AP editor.

Tue, 12 Nov 2019 21:57:40 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 12 Nov 2019 21:57:40 +0100
changeset 33
331e7f700971
parent 32
7717ac1d2f7f
child 34
495b7eafbf5a

Added WiFi AP editor.

main/co2meter.c file | annotate | diff | comparison | revisions
main/config.c file | annotate | diff | comparison | revisions
main/config.h file | annotate | diff | comparison | revisions
main/task_user.c file | annotate | diff | comparison | revisions
--- a/main/co2meter.c	Tue Nov 12 16:55:36 2019 +0100
+++ b/main/co2meter.c	Tue Nov 12 21:57:40 2019 +0100
@@ -140,9 +140,6 @@
     read_config();
     read_units();
 
-//add_station((uint8_t *)"MBSE_WLR", (uint8_t *)"abcjkltuv");
-//remove_station((uint8_t *)"MBSE_WLP");
-
     /*
      * Create FreeRTOS tasks
      */
--- a/main/config.c	Tue Nov 12 16:55:36 2019 +0100
+++ b/main/config.c	Tue Nov 12 21:57:40 2019 +0100
@@ -123,7 +123,6 @@
     memset(dst, 0, sizeof(wifiStation));
     sprintf(wifiStation.SSID, "%s", (char *)SSID);
     sprintf(wifiStation.Password, "%s", (char *)Password);
-    wifiStation.hide = false;
     fwrite(dst, 1, sizeof(wifiStation), f);
     fclose(f);
 
@@ -202,7 +201,7 @@
             break;
 
         if ((strcmp((char *)SSID, wifiStation.SSID) == 0) || (strlen(wifiStation.SSID) == 0)) {
-            // Record to delete, don't copy
+	    ESP_LOGI(TAG, "remove station %s", (char *)SSID);
         } else {
             fwrite(dst, 1, sizeof(wifiStation), n);
         }
--- a/main/config.h	Tue Nov 12 16:55:36 2019 +0100
+++ b/main/config.h	Tue Nov 12 21:57:40 2019 +0100
@@ -148,7 +148,7 @@
 struct strStations {
     char        SSID[32];                       ///< Station SSID
     char        Password[64];                   ///< Station password
-    bool        hide;                           ///< Hide from AP scan.
+    bool        xhide;                          ///< Hide from AP scan.
 } wifiStation;                                  ///< Station record.
 
 /**
--- a/main/task_user.c	Tue Nov 12 16:55:36 2019 +0100
+++ b/main/task_user.c	Tue Nov 12 21:57:40 2019 +0100
@@ -22,8 +22,10 @@
 QueueHandle_t				event_queue;
 static xQueueHandle                     gpio_evt_queue = NULL;          ///< Rotary pushbutton queue
 static int				PushDuration = 0;		///< Duration of the pushed button
-char					SSIDs[10][32];			///< List of SSIDs we know
+struct strStations			APs[10];			///< List of APs we know
+int					edit_ssid = 0;			///< SSID being edited
 int					num_ssids = 0;			///< Number of SSIDs we know
+struct strStations			editAP;				///< Data of station to edit
 
 extern const esp_app_desc_t		*app_desc;
 extern unit_t				units[3];			///< Pressure test units
@@ -420,9 +422,9 @@
 void screen_unit_zero(int no, int sub)
 {
     screen_top("Meter %d nulpunt", no + 1);
-    menu_line(       0, 2, 25, "Huidig %d", units[no].pressure_zero);
-    menu_line(sub == 0, 2, 37, "Nieuw  %d", units[no].pressure_voltage / (adc_state->Batt_voltage / 1000));
-    menu_line(sub == 1, 2, 49, "Terug");
+    menu_line(       0, 1, 25, "Huidig %d", units[no].pressure_zero);
+    menu_line(sub == 0, 1, 37, "Nieuw  %d", units[no].pressure_voltage / (adc_state->Batt_voltage / 1000));
+    menu_line(sub == 1, 1, 49, "Terug");
     u8g2_SendBuffer(&u8g2);
 }
 
@@ -436,10 +438,10 @@
 void screen_unit_setup(int no, int sub)
 {
     screen_top("Meter %d setup", no + 1);
-    menu_line(sub == 0, 2, 25, "Werking %s", units[no].mode ? "Aan":"Uit");
-    menu_line(sub == 1, 2, 37, "Nulpunt %d", units[no].pressure_zero);
-    menu_line(sub == 2, 2, 49, "DS18B20 %s", units[no].temperature_rom_code);
-    menu_line(sub == 3, 2, 61, "Terug");
+    menu_line(sub == 0, 1, 25, "Werking %s", units[no].mode ? "Aan":"Uit");
+    menu_line(sub == 1, 1, 37, "Nulpunt %d", units[no].pressure_zero);
+    menu_line(sub == 2, 1, 49, "DS18B20 %s", units[no].temperature_rom_code);
+    menu_line(sub == 3, 1, 61, "Terug");
     u8g2_SendBuffer(&u8g2);
 }
 
@@ -474,9 +476,9 @@
 void screen_wifi_setup(int sub)
 {
     screen_top("WiFi AP setup");
-    menu_line(sub == 0, 2, 25, "Lijst AP");
-    menu_line(sub == 1, 2, 37, "Nieuw AP");
-    menu_line(sub == 2, 2, 49, "Terug");
+    menu_line(sub == 0, 1, 25, "Lijst AP");
+    menu_line(sub == 1, 1, 37, "Nieuw AP");
+    menu_line(sub == 2, 1, 49, "Terug");
     u8g2_SendBuffer(&u8g2);
 }
 
@@ -500,22 +502,23 @@
 		fclose(f);
 		break;
 	    }
-	    memcpy(SSIDs[num_ssids], ap.SSID, 32);
+	    memcpy(APs[num_ssids].SSID, ap.SSID, 32);
+	    memcpy(APs[num_ssids].Password, ap.Password, 64);
 	    num_ssids++;
 	}
     }
-printf("loaded %d SSIDs sub %d  offset %d\n", num_ssids, sub, offset);
+//printf("loaded %d SSIDs sub %d  offset %d\n", num_ssids, sub, offset);
 
     screen_top("WiFi AP lijst");
     if (num_ssids == 0) {
 	menu_line(0, 1, 25, "Geen AP's");
     } else {
 	for (i = 0; i < num_ssids; i++) {
-	    menu_line(sub == i, 1, 25 + (i * 12), SSIDs[i + offset]);
+	    menu_line(sub == i, 1, 25 + (i * 12), APs[i + offset].SSID);
 	    if (i == 3)
 		break;
 	}
-	if (i < 4)
+	if ((i + offset) == num_ssids)
 	    menu_line(sub == i, 1, 25 + (i * 12), "Terug");
     }
     u8g2_SendBuffer(&u8g2);
@@ -523,6 +526,20 @@
 
 
 
+void screen_edit_ap(int sub)
+{
+    screen_top("WiFi wijzig AP");
+    menu_line(sub == 0, 1, 25, "SSID %s", editAP.SSID);
+    menu_line(sub == 1, 1, 37, "PSK  %s", editAP.Password);
+    menu_line(sub == 2, 1, 49, "Opslaan");
+    if (edit_ssid >= 0) {
+    	menu_line(sub == 3, 1, 61, "Verwijder");
+    }
+    u8g2_SendBuffer(&u8g2);
+}
+
+
+
 void screen_network()
 {
     char	ip[17], nm[17], gw[17];
@@ -549,9 +566,9 @@
 void screen_network_setup(int sub)
 {
     screen_top("Netwerk setup");
-    menu_line(sub == 0, 2, 25, "Naam %s", config.hostname);
-    menu_line(sub == 1, 2, 37, "NTP  %s", config.ntp_server);
-    menu_line(sub == 2, 2, 49, "Terug");
+    menu_line(sub == 0, 1, 25, "Naam %s", config.hostname);
+    menu_line(sub == 1, 1, 37, "NTP  %s", config.ntp_server);
+    menu_line(sub == 2, 1, 49, "Terug");
     u8g2_SendBuffer(&u8g2);
 }
 
@@ -571,10 +588,10 @@
 void screen_mqtt_setup(int sub)
 {
     screen_top("MQTT Setup");
-    menu_line(sub == 0, 2, 25, "serv %s", config.mqtt_server);
-    menu_line(sub == 1, 2, 37, "port %d", config.mqtt_port);
-    menu_line(sub == 2, 2, 49, "user %s", config.mqtt_user);
-    menu_line(sub == 3, 2, 61, "Terug");
+    menu_line(sub == 0, 1, 25, "serv %s", config.mqtt_server);
+    menu_line(sub == 1, 1, 37, "port %d", config.mqtt_port);
+    menu_line(sub == 2, 1, 49, "user %s", config.mqtt_user);
+    menu_line(sub == 3, 1, 61, "Terug");
     u8g2_SendBuffer(&u8g2);
 }
 
@@ -705,7 +722,6 @@
 	rc = true;
     }
 
-printf("rotate_to_sub(%d, %d, %d, %d)\n", pos, max, sub, offset);
     *cursub = sub;
     *curoffset = offset;
     return rc;
@@ -752,6 +768,11 @@
 		screen_list_aps(SubMenu, SubOffset);
 		break;
 
+	    case ML2_EDIT_AP:
+		ESP_LOGI(TAG, "Loop user: WiFi edit AP");
+		screen_edit_ap(0);
+		break;
+
             case ML2_NETWORK:
                 ESP_LOGI(TAG, "Loop user: Network");
                 screen_network();
@@ -834,6 +855,14 @@
 	case ML2_LIST_APS:	if (rotate_to_sub(event.state.position, num_ssids, &SubMenu, &SubOffset))
 				    screen_list_aps(SubMenu, SubOffset);
 				break;
+	case ML2_EDIT_AP:	if (edit_ssid < 0) {
+				    if (rotate_to_sub(event.state.position, 2, &SubMenu, &SubOffset))
+                                    	screen_edit_ap(SubMenu);
+				} else {
+				    if (rotate_to_sub(event.state.position, 3, &SubMenu, &SubOffset))
+				    	screen_edit_ap(SubMenu);
+				}
+				break;
 	case ML2_SETUP_MQTT:	if (rotate_to_sub(event.state.position, 3, &SubMenu, &SubOffset))
 				    screen_mqtt_setup(SubMenu);
 				break;
@@ -930,7 +959,11 @@
 		    New_Loop2 = ML2_LIST_APS;
 		}
 		if (SubMenu == 1) {
-
+		    edit_ssid = -1;
+		    editAP.SSID[0] = '\0';
+		    editAP.Password[0] = '\0';
+		    SubMenu = SubOffset = 0;
+                    New_Loop2 = ML2_EDIT_AP;
 		}
 		if (SubMenu == 2) {
 		    New_Loop2 = ML2_WIFI;
@@ -939,13 +972,58 @@
 
 	case ML2_LIST_APS:
 		if ((SubMenu + SubOffset) < num_ssids) {
-		    // Edit AP
+		    edit_ssid = SubMenu + SubOffset;
+		    memcpy(editAP.SSID, APs[edit_ssid].SSID, 32);
+                    memcpy(editAP.Password, APs[edit_ssid].Password, 64);
+		    SubMenu = SubOffset = 0;
+		    New_Loop2 = ML2_EDIT_AP;
 		} else {
 		    New_Loop2 = ML2_SETUP_WIFI;
 		    SubMenu = SubOffset = 0;
 		}
 		break;
 
+	case ML2_EDIT_AP:
+		if (SubMenu == 0) {
+		    rotary_editer("SSID", editAP.SSID, "", 16, EDIT_TYPE_TEXT);
+		    screen_edit_ap(SubMenu);
+		}
+		if (SubMenu == 1) {
+		    rotary_editer("PSK", editAP.Password, "", 16, EDIT_TYPE_TEXT);
+                    screen_edit_ap(SubMenu);
+		}
+		if (SubMenu == 2 || SubMenu == 3) {
+		    update_running = 1; // Block measurements
+		    int timeout = 600;
+		    if (edit_ssid < 0)
+                        New_Loop2 = ML2_SETUP_WIFI;
+                    else
+                        New_Loop2 = ML2_LIST_APS;
+
+    		    for (;;) {
+        		vTaskDelay(100 / portTICK_PERIOD_MS);
+        		if (Main_Loop1 == ML1_DONE)
+        		    break;
+        		if (timeout)
+        		    timeout--;
+        		else {
+        		    ESP_LOGE(TAG, "Timout request stop");
+        		    goto saveerr;
+        		}
+    		    }
+		    if (edit_ssid >= 0) {
+			ESP_LOGI(TAG, "Remove %s", APs[edit_ssid].SSID);
+                    	remove_station((uint8_t *)APs[edit_ssid].SSID);
+		    }
+		    if (SubMenu == 2) {
+		    	ESP_LOGI(TAG, "Add %s %s", editAP.SSID, editAP.Password);
+		    	add_station((uint8_t *)editAP.SSID, (uint8_t *)editAP.Password);
+		    }
+		}
+saveerr:
+		update_running = 0;
+		break;
+
 	case ML2_NETWORK:
 		New_Loop2 = ML2_SETUP_NETWORK;
 		break;

mercurial