From 010279c1919c0f64c3dc0278ece1be3ca5b8322f Mon Sep 17 00:00:00 2001 From: jedi Date: Sun, 12 Sep 2021 23:01:00 +0200 Subject: [PATCH] Added wifi settings --- firmware/fsdata/fs/index.html | 143 ++++++++++++++++++++++++++++++++++ firmware/web.cpp | 56 +++++++++++++ firmware/wifi.cpp | 8 +- 3 files changed, 203 insertions(+), 4 deletions(-) diff --git a/firmware/fsdata/fs/index.html b/firmware/fsdata/fs/index.html index 8ceefa7..8735ade 100644 --- a/firmware/fsdata/fs/index.html +++ b/firmware/fsdata/fs/index.html @@ -16,6 +16,7 @@ @@ -58,6 +59,87 @@ +
+

Wifi Settings

+
+
+

AP Mode

+
+
+
+ + +
+
+ + +
+
+ + +
+
+ AP IP + N/A +
+
+ AP MAC + N/A +
+
+ + +
+
+
+
+
+

Station Mode current connection

+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ Sation IP + N/A +
+
+ Station MAC + N/A +
+
+ + +
+
+
+

Status

@@ -190,6 +272,18 @@ return pos; }; + DataView.prototype.setInt8Vec = function (pos, vec) { + for (var i = 0; i < vec.length; i++) { + this.setInt8(pos++, vec[i]); + } + return pos; + }; + + function colorStringToVec(hex) { + var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); + return result ? [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)] : null; + } + var ws; var retries; var series = new TimeSeries(); @@ -394,6 +488,55 @@ }) } } + + var sta_toggle = document.getElementById("sta_toggle"); + var sta_ssid = document.getElementById("sta_ssid"); + var sta_pw = document.getElementById("sta_pw"); + + function sta_update() { + var en = sta_toggle.checked; + + const ssid = sta_ssid.value; + const password = sta_pw.value; + + const buffer = new ArrayBuffer(ssid.length + password.length + 4); + const view1 = new DataView(buffer); + var tx_len = 0; + view1.setChar(tx_len++, 'S'); + view1.setChar(tx_len++, (en ? "E" : "D")); + tx_len = view1.setString(tx_len, ssid); + tx_len = view1.setString(tx_len, password); + wsWrite(buffer); + } + + var ap_toggle = document.getElementById("ap_toggle"); + var ap_ssid = document.getElementById("ap_ssid"); + var ap_pw = document.getElementById("ap_pw"); + + function ap_update() { + var en = ap_toggle.checked; + + const ssid = ap_ssid.value; + const password = ap_pw.value; + + const buffer = new ArrayBuffer(ssid.length + password.length + 4 + 8); + const view1 = new DataView(buffer); + var tx_len = 0; + view1.setChar(tx_len++, 'A'); + view1.setChar(tx_len++, (en ? "E" : "D")); + tx_len = view1.setString(tx_len, ssid); + tx_len = view1.setString(tx_len, password); + tx_len = view1.setInt8Vec(tx_len, [192, 168, 111, 1]); + tx_len = view1.setInt8Vec(tx_len, [255, 255, 255, 0]); + wsWrite(buffer); + } + + function update_progress(progressBar, progress) { + var iP = Math.floor(progress); + var dBar = document.getElementById("progress_bar_" + progressBar); + dBar.innerText = iP + "%"; + dBar.style.width = progress + "%"; + } diff --git a/firmware/web.cpp b/firmware/web.cpp index 6176a43..dc43e08 100644 --- a/firmware/web.cpp +++ b/firmware/web.cpp @@ -184,6 +184,18 @@ void websocket_task(void *pvParameter) { } + vTaskDelayMs(250); + { + uint8_t response[3]; + uint16_t val; + val = sdk_system_adc_read(); + response[2] = (uint8_t) val; + response[1] = val >> 8; + response[0] = 'V'; + LOCK_TCPIP_CORE(); + websocket_write(pcb, response, 3, WS_BIN_MODE); + UNLOCK_TCPIP_CORE(); + } vTaskDelayMs(500); } @@ -221,7 +233,18 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, bool togl = 0; + int8_t en = 0; + /*uint8_t ap_disable_if_sta = 0; + uint8_t ssid_hidden = 0; + uint8_t dns_enable = 0; + uint8_t mdns_enable = 0;*/ + switch (data[0]) { + case 'V': // ADC + /* This should be done on a separate thread in 'real' applications */ + val = sdk_system_adc_read(); + cmd = 'V'; + break; case 'R': // Restart cmd = 'R'; break; @@ -258,6 +281,39 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, } cmd = 'C'; break; + case 'S': { + if(data[1] == 'E') + en = 1; + char *ssid = &data[2]; + size_t ssid_len = strlen(ssid); + char *password = &data[3 + ssid_len]; + size_t password_len = strlen(password); + (void) password_len; + + sysparam_set_int8("wifi_sta_enable", en); + sysparam_set_string("wifi_sta_ssid", ssid); + sysparam_set_string("wifi_sta_password", password); + } + cmd = 'S'; + break; + case 'A': { + if(data[1] == 'E') + en = 1; + char *ssid = &data[2]; + size_t ssid_len = strlen(ssid); + char *password = &data[3 + ssid_len]; + size_t password_len = strlen(password); + (void) password_len; + + sysparam_set_int8("wifi_ap_enable", en); + sysparam_set_string("wifi_ap_ssid", ssid); + sysparam_set_string("wifi_ap_password", password); + + //sysparam_set_int8("wifi_ap_dns", dns_enable); + //sysparam_set_int8("wifi_ap_mdns", mdns_enable); + } + cmd = 'A'; + break; default: printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data); printf("Unknown command %c\n", data[0]); diff --git a/firmware/wifi.cpp b/firmware/wifi.cpp index 65b114b..a958a95 100644 --- a/firmware/wifi.cpp +++ b/firmware/wifi.cpp @@ -210,7 +210,7 @@ extern "C" void wifi_task(void *pvParameters) { if(wifi_sta_enable) { printf("try STA Mode: %s %s\n", wifi_sta_ssid, wifi_sta_password); - sdk_station_config config; + sdk_station_config config{}; strcpy((char *) config.ssid, wifi_sta_ssid); strcpy((char *) config.password, wifi_sta_password); config.bssid_set = 0; @@ -230,7 +230,7 @@ extern "C" void wifi_task(void *pvParameters) { wifi_sta_netmask && strlen(wifi_sta_netmask) > 4 && wifi_sta_gateway && strlen(wifi_sta_gateway) > 4) { sdk_wifi_station_dhcpc_stop(); - ip_info info; + ip_info info{}; memset(&info, 0x0, sizeof(info)); info.ip.addr = ipaddr_addr(wifi_sta_ip_addr); info.netmask.addr = ipaddr_addr(wifi_sta_netmask); @@ -305,13 +305,13 @@ extern "C" void wifi_task(void *pvParameters) { } if(strlen(wifi_ap_ip_addr) >= 7 && strlen(wifi_ap_netmask) >= 7) { - ip_info ap_ip; + ip_info ap_ip{}; ap_ip.ip.addr = ipaddr_addr(wifi_ap_ip_addr); ap_ip.netmask.addr = ipaddr_addr(wifi_ap_netmask); IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0); sdk_wifi_set_ip_info(1, &ap_ip); - sdk_softap_config ap_config; + sdk_softap_config ap_config{}; strcpy((char *) ap_config.ssid, wifi_ap_ssid); ap_config.ssid_len = strlen(wifi_ap_ssid); strcpy((char *) ap_config.password, wifi_ap_password);