Skip to content

Commit

Permalink
ESP-NOW WIP, added custom UDP conn #70, refractoring
Browse files Browse the repository at this point in the history
  • Loading branch information
seeul8er committed Mar 19, 2024
1 parent 31be2af commit 7060e56
Show file tree
Hide file tree
Showing 13 changed files with 916 additions and 239 deletions.
15 changes: 10 additions & 5 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@
</script>
<!--Minified version of dronebridge.js-->
<script type="text/javascript">
const ROOT_URL=window.location.href;let conn_status=0;function change_ap_ip_visibility(){var e=document.getElementById("ap_ip_div"),t=document.getElementById("ap_channel_div"),n=document.getElementById("esp-now-ap-disclaimer");"2"===document.getElementById("esp32_mode").value?(e.style.display="none",t.style.display="none"):(e.style.display="block",t.style.display="block"),"3"===document.getElementById("esp32_mode").value?n.style.display="block":n.style.display="none"}function change_msp_ltm_visibility(){var e=document.getElementById("msp_ltm_div"),t=document.getElementById("trans_pack_size_div");"1"===document.getElementById("telem_proto").value?(e.style.display="block",t.style.display="none"):(e.style.display="none",t.style.display="block")}function toJSONString(e){let t={},n=e.querySelectorAll("input, select");for(let e=0;e<n.length;++e){let s=n[e],o=s.name,i=s.value;isNaN(Number(i))||0===o.localeCompare("wifi_ssid")||0===o.localeCompare("wifi_pass")?o&&(t[o]=i):o&&(t[o]=parseInt(i))}return JSON.stringify(t)}async function get_json(e){let t=ROOT_URL+e;const n=new AbortController,s=(setTimeout((()=>{n.abort()}),1e3),await fetch(t,{signal:n.signal}));if(!s.ok){const e=`An error has occured: ${s.status}`;throw conn_status=0,new Error(e)}return await s.json()}async function send_json(e,t){let n=ROOT_URL+e;const s=await fetch(n,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json",charset:"UTF-8"},body:t});if(!s.ok){conn_status=0;const e=`An error has occured: ${s.status}`;throw new Error(e)}return await s.json()}function get_system_info(){return get_json("api/system/info").then((e=>{console.log("Received settings: "+e),document.getElementById("about").innerHTML="DroneBridge for ESP32 - v"+e.major_version+"."+e.minor_version+" - esp-idf "+e.idf_version})).catch((e=>(conn_status=0,e.message,-1))),0}function update_conn_status(){conn_status?document.getElementById("web_conn_status").innerHTML='<span class="dot_green"></span> connected to ESP32':(document.getElementById("web_conn_status").innerHTML='<span class="dot_red"></span> disconnected from ESP32',document.getElementById("current_client_ip").innerHTML="")}function get_stats(){get_json("api/system/stats").then((e=>{conn_status=1;let t=parseInt(e.read_bytes);!isNaN(t)&&t>1e3?document.getElementById("read_bytes").innerHTML=t/1e3+" kb":isNaN(t)||(document.getElementById("read_bytes").innerHTML=t+" bytes");let n=parseInt(e.tcp_connected);!isNaN(n)&&n>1?document.getElementById("tcp_connected").innerHTML=n+" clients":isNaN(n)||(document.getElementById("tcp_connected").innerHTML=n+" client");let s=parseInt(e.udp_connected);!isNaN(s)&&s>1?document.getElementById("udp_connected").innerHTML=s+" clients":isNaN(s)||(document.getElementById("udp_connected").innerHTML=s+" client");let o=parseInt(e.rssi);!isNaN(o)&&o<0?document.getElementById("current_client_ip").innerHTML="IP Address: "+e.current_client_ip+"<br />RSSI: "+o+"dBm":isNaN(o)||(document.getElementById("current_client_ip").innerHTML="IP Address: "+e.current_client_ip)})).catch((e=>{conn_status=0,e.message}))}function get_settings(){return get_json("api/settings/request").then((e=>{console.log("Received settings: "+e),conn_status=1;for(const t in e)if(e.hasOwnProperty(t)){document.getElementById(t).value=e[t]+""}})).catch((e=>(conn_status=0,e.message,show_toast(e.message),-1))),change_ap_ip_visibility(),change_msp_ltm_visibility(),0}function show_toast(e){Toastify({text:e,duration:5e3,newWindow:!0,close:!0,gravity:"top",position:"center",backgroundColor:"linear-gradient(to right, #b6e026, #abdc28)",stopOnFocus:!0}).showToast()}function save_settings(){send_json("api/settings/change",toJSONString(document.getElementById("settings_form"))).then((e=>{console.log(e),conn_status=1,show_toast(e.msg),get_settings()})).catch((e=>{show_toast(e.message)}))}function trigger_reboot(){get_json("api/system/reboot").then((e=>{show_toast(e.msg)})).catch((e=>{e.message,show_toast(e.message)}))} </script>
</head>
const ROOT_URL=window.location.href;let conn_status=0;function change_ap_ip_visibility(){var e=document.getElementById("ap_ip_div"),t=document.getElementById("ap_channel_div"),n=document.getElementById("esp-lr-ap-disclaimer");"2"===document.getElementById("esp32_mode").value?(e.style.display="none",t.style.display="none"):(e.style.display="block",t.style.display="block"),document.getElementById("esp32_mode").value>="3"?n.style.display="block":n.style.display="none"}function change_msp_ltm_visibility(){var e=document.getElementById("msp_ltm_div"),t=document.getElementById("trans_pack_size_div");"1"===document.getElementById("telem_proto").value?(e.style.display="block",t.style.display="none"):(e.style.display="none",t.style.display="block")}function toJSONString(e){let t={},n=e.querySelectorAll("input, select");for(let e=0;e<n.length;++e){let s=n[e],o=s.name,i=s.value;isNaN(Number(i))||0===o.localeCompare("wifi_ssid")||0===o.localeCompare("wifi_pass")?o&&(t[o]=i):o&&(t[o]=parseInt(i))}return JSON.stringify(t)}async function get_json(e){let t=ROOT_URL+e;const n=new AbortController,s=(setTimeout((()=>{n.abort()}),1e3),await fetch(t,{signal:n.signal}));if(!s.ok){const e=`An error has occured: ${s.status}`;throw conn_status=0,new Error(e)}return await s.json()}async function send_json(e,t){let n=ROOT_URL+e;const s=await fetch(n,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json",charset:"UTF-8"},body:t});if(!s.ok){conn_status=0;const e=`An error has occured: ${s.status}`;throw new Error(e)}return await s.json()}function get_system_info(){return get_json("api/system/info").then((e=>{console.log("Received settings: "+e),document.getElementById("about").innerHTML="DroneBridge for ESP32 - v"+e.major_version+"."+e.minor_version+" - esp-idf "+e.idf_version})).catch((e=>(conn_status=0,e.message,-1))),0}function update_conn_status(){conn_status?document.getElementById("web_conn_status").innerHTML='<span class="dot_green"></span> connected to ESP32':(document.getElementById("web_conn_status").innerHTML='<span class="dot_red"></span> disconnected from ESP32',document.getElementById("current_client_ip").innerHTML="")}function get_stats(){get_json("api/system/stats").then((e=>{conn_status=1;let t=parseInt(e.read_bytes);!isNaN(t)&&t>1e3?document.getElementById("read_bytes").innerHTML=t/1e3+" kb":isNaN(t)||(document.getElementById("read_bytes").innerHTML=t+" bytes");let n=parseInt(e.tcp_connected);!isNaN(n)&&n>1?document.getElementById("tcp_connected").innerHTML=n+" clients":isNaN(n)||(document.getElementById("tcp_connected").innerHTML=n+" client");let s=parseInt(e.udp_connected);!isNaN(s)&&s>1?document.getElementById("udp_connected").innerHTML=s+" clients":isNaN(s)||(document.getElementById("udp_connected").innerHTML=s+" client");let o=parseInt(e.rssi);!isNaN(o)&&o<0?document.getElementById("current_client_ip").innerHTML="IP Address: "+e.current_client_ip+"<br />RSSI: "+o+"dBm":isNaN(o)||(document.getElementById("current_client_ip").innerHTML="IP Address: "+e.current_client_ip)})).catch((e=>{conn_status=0,e.message}))}function get_settings(){return get_json("api/settings/request").then((e=>{console.log("Received settings: "+e),conn_status=1;for(const t in e)if(e.hasOwnProperty(t)){document.getElementById(t).value=e[t]+""}})).catch((e=>(conn_status=0,e.message,show_toast(e.message),-1))),change_ap_ip_visibility(),change_msp_ltm_visibility(),0}function show_toast(e){Toastify({text:e,duration:5e3,newWindow:!0,close:!0,gravity:"top",position:"center",backgroundColor:"linear-gradient(to right, #b6e026, #abdc28)",stopOnFocus:!0}).showToast()}function save_settings(){send_json("api/settings/change",toJSONString(document.getElementById("settings_form"))).then((e=>{console.log(e),conn_status=1,show_toast(e.msg),get_settings()})).catch((e=>{show_toast(e.message)}))}function trigger_reboot(){get_json("api/system/reboot").then((e=>{show_toast(e.msg)})).catch((e=>{e.message,show_toast(e.message)}))}
</script>
</head>
<body>
<div class="container">
<br>
Expand Down Expand Up @@ -92,9 +93,11 @@ <h3>Wi-Fi</h3>
<select id="esp32_mode" name="esp32_mode" form="settings_form" onchange="change_ap_ip_visibility()">
<option value="1">WiFi Access Point Mode</option>
<option value="2">WiFi Client Mode</option>
<!-- <option value="3">ESP-NOW Access Point Mode</option>-->
<option value="3">Wifi Access Point LR Mode</option>
<option value="4">ESP-NOW LR Mode AIR</option>
<option value="5">ESP-NOW LR Mode GND</option>
</select>
<div id="esp-now-ap-disclaimer" style="color: #ff0f0f; margin-bottom: 0.8rem">ESP-NOW Access Point Mode makes the device invisible for non-ESP-NOW enabled devices!</br>You will not be able to change the config!</br>You will have to manually erase the flash memory of the ESP32 and re-flash DroneBridge for ESP32 to get back into normal Wi-Fi Mode!</div>
<div id="esp-lr-ap-disclaimer" style="color: #ff881a; margin-bottom: 0.8rem">LR Mode makes the device invisible for non-LR enabled devices!</br>You will not be able to change the config!</br>Data rate is reduced to ~0.25Mbps. Range may be increase by a factor of ~2.</br>You will have to manually erase the flash memory of the ESP32 and re-flash DroneBridge for ESP32 to get back into normal Wi-Fi Mode!</div>
</div>
</div>
<div class="row">
Expand Down Expand Up @@ -177,6 +180,7 @@ <h3>Serial</h3>
<select id="ltm_pp" name="ltm_pp" form="settings_form">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="6">6</option>
<option value="10">10</option>
Expand All @@ -191,7 +195,8 @@ <h3>Serial</h3>
<option value="32">32</option>
<option value="64">64</option>
<option value="128">128</option>
<option value="256">256</option>
<option value="240">240</option>
<option value="512">512</option>
</select>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions frontend_src/dronebridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ let conn_status = 0;
function change_ap_ip_visibility(){
var ap_ip_div = document.getElementById("ap_ip_div");
var ap_channel_div = document.getElementById("ap_channel_div");
var disclamer_div = document.getElementById("esp-now-ap-disclaimer");
var disclamer_div = document.getElementById("esp-lr-ap-disclaimer");
if (document.getElementById("esp32_mode").value === "2") {
ap_ip_div.style.display = "none";
ap_channel_div.style.display = "none";
} else {
ap_ip_div.style.display = "block";
ap_channel_div.style.display = "block";
}
if (document.getElementById("esp32_mode").value === "3") {
if (document.getElementById("esp32_mode").value >= "3") {
disclamer_div.style.display = "block";
} else {
disclamer_div.style.display = "none";
Expand Down
7 changes: 1 addition & 6 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
idf_component_register(SRCS main.c db_esp32_control.c globals.h sdkconfig.h msp_ltm_serial.c
msp_ltm_serial.h db_protocol.h http_server.c http_server.h db_esp32_comm.c
db_esp32_comm.h db_comm_protocol.h db_comm.c db_comm.h db_crc.c db_crc.h tcp_server.c tcp_server.h
http_server.c http_server.h
http_server.c http_server.h db_esp_now.c db_esp_now.h db_serial.c db_serial.h
INCLUDE_DIRS ".")

if(CONFIG_WEB_DEPLOY_SF)
set(WEB_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../frontend")
spiffs_create_partition_image(www ${WEB_SRC_DIR} FLASH_IN_PROJECT)
# if(EXISTS ${WEB_SRC_DIR}/dist)
# spiffs_create_partition_image(www ${WEB_SRC_DIR}/dist FLASH_IN_PROJECT)
# else()
# message(FATAL_ERROR "${WEB_SRC_DIR}/dist doesn't exit. Please run 'npm run build' in ${WEB_SRC_DIR}")
# endif()
endif()
Loading

0 comments on commit 7060e56

Please sign in to comment.