diff --git a/Source/data/index.html b/Source/data/index.html index cc890bc..9e51eb9 100644 --- a/Source/data/index.html +++ b/Source/data/index.html @@ -60,6 +60,10 @@ background-color: #00b3f0; } + #mainscreencontent { + display: flex; + } + .management { display: flex; flex-wrap: wrap; @@ -91,6 +95,10 @@ .setupicon a { text-decoration: none; } + + #fileList { + min-width: 200px; + } @@ -101,45 +109,66 @@

ESP32 PP v0.1

WS Connecting...
PP not connected to ESP
-
- No gui support + -
-
-
 
- -
 
-
-
- - - +
+
+ No gui support +
+ +
+
-
- - - +
+
+
 
+ +
 
+
+
+ + + +
+
+ + + +
+
+ + +
-
- - +
+
Temp: ?
+
Lat: ?
+
Lon: ?
+
Heading: ?
+
Sats: ?
+ +
-
-
Temp: ?
-
Lat: ?
-
Lon: ?
-
Heading: ?
-
Sats: ?
- -
- +

     
@@ -148,7 +177,8 @@

ESP32 PP v0.1

var respWaiting = PROMPT; var respCallBack = null; var enableSend = false; //keep track if sending is allowed or not - var gateway = `ws://${window.location.hostname}/ws`; + //var gateway = `ws://${window.location.hostname}/ws`; + var gateway = `ws://192.168.4.1/ws`; var websocket; var respLines = []; //resp lines list. var respLastLine = ""; //last line of resp @@ -158,18 +188,10 @@

ESP32 PP v0.1

function enadisaControls(ena) { enableSend = ena; - document.getElementById("btnCtrUp").disabled = !ena; - document.getElementById("btnCtrLeft").disabled = !ena; - document.getElementById("btnCtrEnter").disabled = !ena; - document.getElementById("btnCtrRight").disabled = !ena; - document.getElementById("btnCtrRotLeft").disabled = !ena; - document.getElementById("btnCtrDown").disabled = !ena; - document.getElementById("btnCtrRotRight").disabled = !ena; - document.getElementById("btnCtrDFU").disabled = !ena; - document.getElementById("btnCtrRST").disabled = !ena; - document.getElementById("bntRefresh").disabled = !ena; - document.getElementById("btnManualSend").disabled = !ena; - document.getElementById("btnGps").disabled = !ena; + var elementsToDisable = document.querySelectorAll('.uicontrols'); + elementsToDisable.forEach(function (element) { + element.disabled = !ena; + }); } function log(data) { @@ -187,7 +209,14 @@

ESP32 PP v0.1

websocket.onopen = onOpen; websocket.onclose = onClose; websocket.onmessage = onMessage; + websocket.error = onError; } + + function onError(event) { + log("WS error"); + console.log(event); + } + function onOpen(event) { log("WS Connected"); document.getElementById("connState").innerHTML = "WS Connected."; @@ -256,23 +285,28 @@

ESP32 PP v0.1

} //any ws message function onMessage(event) { - var str = String(event.data); - for (let i = 0; i < str.length; i++) { - var resetline = false; - respLastLine += str[i]; - if (str[i] == "\n") { - respLines.push(respLastLine); - onMessageLine(respLastLine); - resetline = true; - } - if (respLastLine.endsWith(respWaiting)) { - onDataArrived(); - } - if (respLastLine.endsWith(PROMPT)) {//this counts too - onMessageLine(respLastLine); - resetline = true; + try { + var str = String(event.data); + for (let i = 0; i < str.length; i++) { + var resetline = false; + respLastLine += str[i]; + if (str[i] == "\n") { + respLines.push(respLastLine); + onMessageLine(respLastLine); + resetline = true; + } + if (respLastLine.endsWith(respWaiting)) { + onDataArrived(); + } + if (respLastLine.endsWith(PROMPT)) {//this counts too + onMessageLine(respLastLine); + resetline = true; + } + if (resetline) respLastLine = ""; } - if (resetline) respLastLine = ""; + } + catch (err) { + console.log(err); } } @@ -369,6 +403,111 @@

ESP32 PP v0.1

respCallBack = screenUpdated; sendMessage(tosend); } + + function fileSelected() { + var selectElement = document.getElementById("fileList"); + var selectedOption = selectElement.options[selectElement.selectedIndex]; + var selectedValue = selectedOption.value; + if (selectedValue == "..") { + cwdup(); + return; + } + if (selectedValue.endsWith("/")) { + var path = document.getElementById("filePath").innerHTML; + path += selectedValue; + document.getElementById("filePath").innerHTML = path; + ls(); + return; + } + else { + if (!selectedValue.endsWith(".tar") && !selectedValue.endsWith(".bin")) { + fileDownload(); + } + else { + fileFlash(); + } + } + } + + function fileDownload() { + var path = document.getElementById("filePath").innerHTML; + if (!path.endsWith("/") && path.length > 1) path = path + "/"; + var selectElement = document.getElementById("fileList"); + var selectedOption = selectElement.options[selectElement.selectedIndex]; + var selectedValue = selectedOption.value; + if (selectedValue.endsWith("/")) { + log("Can't download a folder yet."); + } + else { + //start file download + alert("NIY"); //todo + } + } + + function fileUpload() { + alert("NIY"); //todo + } + + function fileFlash() { + var path = document.getElementById("filePath").innerHTML; + if (!path.endsWith("/") && path.length > 1) path = path + "/"; + var selectElement = document.getElementById("fileList"); + var selectedOption = selectElement.options[selectElement.selectedIndex]; + var selectedValue = selectedOption.value; + if (!selectedValue.endsWith(".tar") && !selectedValue.endsWith(".bin")) { + log("Can flash only TAR and BIN files. Selected: " + selectedValue); + } + else { + if (confirm("Really flash " + selectedValue + " ?")) { + alert("NIY"); //todo + } + + } + } + + function filesLsArrived() { + var fl = document.getElementById("fileList"); + fl.innerHTML = ""; + var path = document.getElementById("filePath").innerHTML; + if (path != "/") { + fl.innerHTML += ""; + } + for (let i = 0; i < respLines.length; i++) { + var line = respLines[i].trim(); + if (line.startsWith("ls ")) continue; + if (line.startsWith(PROMPT)) continue; + fl.innerHTML += ""; + } + } + function ls() { + var path = document.getElementById("filePath").innerHTML; + if (path.endsWith("/") && path.length > 1) path = path.substring(0, path.length - 1); + var tosend = "ls " + path + "\r\n"; + respWaiting = PROMPT; + respLines = []; + enadisaControls(false); + preventSrenneRefresh = true; + respCallBack = filesLsArrived; + sendMessage(tosend); + } + + function cwdup() { + var path = document.getElementById("filePath").innerHTML; + if (path.length > 1) { + path = path.replace(/\/+$/, ''); // Remove trailing '/' + + // Find the last occurrence of '/' + let lastSlashIndex = path.lastIndexOf('/'); + + // If '/' is found, remove the last segment + if (lastSlashIndex >= 0) { + path = path.substring(0, lastSlashIndex + 1); + } + } + document.getElementById("filePath").innerHTML = path; + ls(); + } + function manualSend() { var tosend = document.getElementById("manualtxt").value; if (!tosend.endsWith("\r\n")) { tosend += "\r\n"; } @@ -413,6 +552,19 @@

ESP32 PP v0.1

} } + function fileManClose() { + document.getElementById("filemanager").style.display = "none"; + document.getElementById("mainscreencontent").style.display = "flex"; + document.getElementById("manualcommand").style.display = "block"; + } + function fileManOpen() { + document.getElementById("filemanager").style.display = "block"; + document.getElementById("mainscreencontent").style.display = "none"; + document.getElementById("manualcommand").style.display = "none"; + + ls(); + } + diff --git a/Source/main/main.c b/Source/main/main.c index 583b107..8062f39 100644 --- a/Source/main/main.c +++ b/Source/main/main.c @@ -74,7 +74,7 @@ void app_main(void) ESP_ERROR_CHECK(err); // load prev settings nvs_handle_t nvs_handle; - err = nvs_open("storage", NVS_READWRITE, &nvs_handle); // https://github.com/espressif/esp-idf/blob/v5.1.2/examples/storage/nvs_rw_value/main/nvs_value_example_main.c + err = nvs_open("wifi", NVS_READWRITE, &nvs_handle); // https://github.com/espressif/esp-idf/blob/v5.1.2/examples/storage/nvs_rw_value/main/nvs_value_example_main.c if (err != ESP_OK) { printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err)); diff --git a/Source/main/orientation.c b/Source/main/orientation.c index fb7a5a7..b65bae5 100644 --- a/Source/main/orientation.c +++ b/Source/main/orientation.c @@ -1,17 +1,76 @@ #include "orientation.h" +#include "nvs_flash.h" float declinationAngle = 0; // todo setup to web interface http://www.magnetic-declination.com/ OrientationSensors orientation_inited = Orientation_none; +int16_t orientationXMin = INT16_MAX; +int16_t orientationYMin = INT16_MAX; +int16_t orientationZMin = INT16_MAX; +int16_t orientationXMax = INT16_MIN; +int16_t orientationYMax = INT16_MIN; +int16_t orientationZMax = INT16_MIN; + +void reset_orientation_calibration() +{ + orientationXMin = INT16_MAX; + orientationYMin = INT16_MAX; + orientationZMin = INT16_MAX; + orientationXMax = INT16_MIN; + orientationYMax = INT16_MIN; + orientationZMax = INT16_MIN; +} + +void load_calibration() +{ + nvs_handle_t nvs_handle; + esp_err_t err = nvs_open("orient", NVS_READWRITE, &nvs_handle); + if (err != ESP_OK) + { + nvs_get_i16(nvs_handle, "orientationXMin", &orientationXMin); + nvs_get_i16(nvs_handle, "orientationYMin", &orientationYMin); + nvs_get_i16(nvs_handle, "orientationZMin", &orientationZMin); + nvs_get_i16(nvs_handle, "orientationXMax", &orientationXMax); + nvs_get_i16(nvs_handle, "orientationYMax", &orientationYMax); + nvs_get_i16(nvs_handle, "orientationZMax", &orientationZMax); + int16_t tmp = 0; + nvs_get_i16(nvs_handle, "declination", &tmp); + declinationAngle = tmp / 100; // 2 decimal precision + nvs_close(nvs_handle); + } + else + { + reset_orientation_calibration(); + } +} + +void save_calibration() +{ + nvs_handle_t nvs_handle; + esp_err_t err = nvs_open("orient", NVS_READWRITE, &nvs_handle); + if (err != ESP_OK) + { + nvs_set_i16(nvs_handle, "orientationXMin", orientationXMin); + nvs_set_i16(nvs_handle, "orientationYMin", orientationYMin); + nvs_set_i16(nvs_handle, "orientationZMin", orientationZMin); + nvs_set_i16(nvs_handle, "orientationXMax", orientationXMax); + nvs_set_i16(nvs_handle, "orientationYMax", orientationYMax); + nvs_set_i16(nvs_handle, "orientationZMax", orientationZMax); + nvs_set_i16(nvs_handle, "declination", (int16_t)(declinationAngle * 100)); + nvs_commit(nvs_handle); + nvs_close(nvs_handle); + } +} + void init_orientation() { // HCM5883l hmc5883lInit(I2C0_DEV); if (hmc5883lTestConnection()) { - hmc5883lSetMode(0); + hmc5883lSetMode(HMC5883L_MODE_CONTINUOUS); ESP_LOGI("Orientation", "hmc5883lTestConnection OK"); orientation_inited = Orientation_hmc5883l; } @@ -21,6 +80,9 @@ void init_orientation() { ESP_LOGI("Orientation", "No compatible sensor found"); } + + // load calibration data + load_calibration(); } float fix_heading(float heading) @@ -39,27 +101,28 @@ float get_heading() return 0; if (orientation_inited == Orientation_hmc5883l) { - float heading_x = hmc5883lGetHeadingX(); - float heading_y = hmc5883lGetHeadingY(); - return (atan2(heading_y, heading_x) * 180) / M_PI + declinationAngle; + int16_t x, y, z = 0; + hmc5883lGetHeading(&x, &y, &z); + return atan2(y, x); } return 0; } float get_heading_degrees() { - return get_heading() * 180 / M_PI; -} - -void reset_orientation_calibration() -{ - // todo + return get_heading() * 180 / M_PI + declinationAngle; } void calibrate_orientation(uint8_t sec) { // todo, run a calibration for N sec to get min-max. should be around 10 + + save_calibration(); } -void set_declination(float declination) { declinationAngle = declination; } +void set_declination(float declination) +{ + declinationAngle = declination; + save_calibration(); +} float get_declination() { return declinationAngle; } \ No newline at end of file diff --git a/Source/main/webserver.h b/Source/main/webserver.h index 7c9edef..1eb6c4e 100644 --- a/Source/main/webserver.h +++ b/Source/main/webserver.h @@ -115,7 +115,7 @@ static esp_err_t post_req_handler_setup(httpd_req_t *req) buf[off] = '\0'; nvs_handle_t nvs_handle; - esp_err_t err = nvs_open("storage", NVS_READWRITE, &nvs_handle); + esp_err_t err = nvs_open("wifi", NVS_READWRITE, &nvs_handle); if (err == ESP_OK) { // parse rets diff --git a/Source/main/wifim.h b/Source/main/wifim.h index ef2b1f1..4478a26 100644 --- a/Source/main/wifim.h +++ b/Source/main/wifim.h @@ -38,7 +38,9 @@ static void event_handler(void *arg, { ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED"); if (ap_client_num <= 0) - esp_wifi_connect(); // only when no ap clients presents + { + // esp_wifi_connect(); // only when no ap clients presents + } } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) { @@ -52,7 +54,9 @@ static void event_handler(void *arg, ap_client_num--; ESP_LOGI(TAG, "WIFI_EVENT_AP_STADISCONNECTED"); if (ap_client_num <= 0) - esp_wifi_connect(); + { + esp_wifi_connect(); // todo connect later + } } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) @@ -106,7 +110,7 @@ static bool wifi_apsta() wifi_config_t sta_config = {0}; strcpy((char *)sta_config.sta.ssid, wifiStaSSID); strcpy((char *)sta_config.sta.password, wifiStaPASS); - sta_config.sta.failure_retry_cnt = 255; + sta_config.sta.failure_retry_cnt = 0; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &ap_config)); diff --git a/Source/sdkconfig b/Source/sdkconfig index c1247cb..e4316e1 100644 --- a/Source/sdkconfig +++ b/Source/sdkconfig @@ -1,6 +1,6 @@ # # Automatically generated file. DO NOT EDIT. -# Espressif IoT Development Framework (ESP-IDF) Project Configuration +# Espressif IoT Development Framework (ESP-IDF) 5.1.2 Project Configuration # CONFIG_SOC_MPU_MIN_REGION_SIZE=0x20000000 CONFIG_SOC_MPU_REGIONS_MAX_NUM=8