ESP32基礎應用之使用手機瀏覽器作為客戶端與ESP32作為服務器進行通信

文章目錄

  • 1 準備
  • 2 移植
    • 2.1 softAP工程移植到simple工程中
    • 2.2 移植注意事項
  • 3 驗證
  • 4 添加HTML
    • 4.1 瀏覽器顯示自己編譯的html
    • 4.2 在使用html發數據給ESP32
    • 4.3 HTML 內容
    • 4.4 更新 html_test.html

1 準備

  1. 參考工程 Espressif\frameworks\esp-idf-v5.2.1\examples\wifi\getting_started\softAP
    softAP工程演示將ESP32作為AP,即熱點,使手機等終端可以連接
  2. 參考工程 Espressif\frameworks\esp-idf-v5.2.1\examples\protocols\http_server\simple
    simple工程演示ESP32作為服務器,響應客戶端的請求

2 移植

2.1 softAP工程移植到simple工程中

  1. 在simple工程添加自定義ap文件
    在這里插入圖片描述

  2. 在CMakeLists.txt 添加新增 .c 文件
    在這里插入圖片描述

  3. 將 softAP 工程程序復制到 user_wifi_ap.c 中
    注意:1)可以刪除重復的共同的初始化函數;

  4. 簡化后的app_main函數

    void app_main(void)
    {static httpd_handle_t server = NULL;ESP_ERROR_CHECK(nvs_flash_init());ESP_ERROR_CHECK(esp_netif_init());ESP_ERROR_CHECK(esp_event_loop_create_default());wifi_init_softap();/* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,* and re-start it upon connection.*/ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));/* Start the server for the first time */server = start_webserver();
    }
    

2.2 移植注意事項

  1. 設置AP WiFi密碼時,長度要大于等于8

    #define EXAMPLE_ESP_WIFI_PASS      "12345678" // 密碼需要 >= 8
    
  2. WIFI_AUTH_WPA3_PSK和WIFI_AUTH_WPA2_PSK模式
    如果使用WIFI_AUTH_WPA3_PSK模式,可能部分電腦和手機不能連接該WiFi熱點
    在這里插入圖片描述

        wifi_config_t wifi_config = {.ap = {...
    #ifdef CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT.authmode = WIFI_AUTH_WPA3_PSK,.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
    #else /* CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT */.authmode = WIFI_AUTH_WPA2_PSK,
    #endif...},};
    

    關閉WIFI_AUTH_WPA3_PSK使能
    在這里插入圖片描述

3 驗證

  1. 編譯、燒錄、打開串口,idf.py -p COM11 flash monitor
  2. 使用手機連接WiFi;
  3. 使用手機瀏覽器輸入 192.168.4.1/hello
    在這里插入圖片描述
    在這里插入圖片描述
  4. 手機瀏覽器反饋數據與程序一致
    在這里插入圖片描述

4 添加HTML

注意下文所有與html有關的功能都要對應 html_test.html 詳細在文末

4.1 瀏覽器顯示自己編譯的html

  1. 添加一份html文件并壓縮為 .gz格式 到 main文件夾下
    在這里插入圖片描述
    .gz 格式可以使用 7-Zip壓縮軟件

  2. 打開 CMakeLists.txt文件添加 html_test.html.gz
    在這里插入圖片描述

  3. 在對應的 .c 文件添加如下程序

    extern const unsigned char html_test_html_gz_start[] asm("_binary_html_test_html_gz_start");
    extern const unsigned char html_test_html_gz_end[] asm("_binary_html_test_html_gz_end");static esp_err_t index_handler(httpd_req_t *req)
    {size_t CNB_803_web_html_gz_len = html_test_html_gz_end - html_test_html_gz_start;ESP_LOGI(TAG, "web refresh");httpd_resp_set_type(req, "text/html");httpd_resp_set_hdr(req, "Content-Encoding", "gzip");httpd_resp_set_hdr(req, "X-Content-Type-Options", "nosniff");return httpd_resp_send(req, (const char *)html_test_html_gz_start, CNB_803_web_html_gz_len);
    }httpd_uri_t index_uri = {.uri = "/",.method = HTTP_GET,.handler = index_handler,.user_ctx = NULL};static httpd_handle_t start_webserver(void){if (httpd_start(&server, &config) == ESP_OK) {// Set URI handlershttpd_register_uri_handler(server, &index_uri);  // 添加該函數}}
    
  4. 編譯燒錄打開串口,電腦連接無線熱點,并在瀏覽器輸入 192.168.4.1 ,效果如下
    在這里插入圖片描述

4.2 在使用html發數據給ESP32

  1. 添加程序

    static esp_err_t html_cmd_handler(httpd_req_t *req)
    {char *buf = NULL;char _cmd_value[16];static char json_response[256];char *p = json_response;if (html_data_parse(req, &buf) != ESP_OK || httpd_query_key_value(buf, "key_value", _cmd_value, sizeof(_cmd_value)) != ESP_OK) // 這里key_value改變時需要網頁端同步修改{free(buf);httpd_resp_send_404(req);return ESP_FAIL;}free(buf);uint8_t cmd_value = atoi(_cmd_value);ESP_LOGI(TAG, "receive from html cmd: %d ", cmd_value);if (cmd_value == 14){*p++ = '{';p += sprintf(p, "\"frame_sensitivity\":%u,", html_data.sensitivity);*p++ = '}';*p++ = 0;}else{html_data.cmd = cmd_value;*p++ = '{';p += sprintf(p, "\"cnb_board\":\"%s\",", PROJECT_NAME);p += sprintf(p, "\"cnb_ver\":\"%s\"", PROJECT_VERSION);*p++ = '}';*p++ = 0;}httpd_resp_set_type(req, "application/json");// Access-Control-Allow-Origin配置成*什么意思?意思是允許任意網站跨域訪問該服務端口,在這種情況下,任意一個前端程序都可以隨意集成該端口內容,實現數據獲取。httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");httpd_resp_set_hdr(req, "X-Content-Type-Options", "nosniff");return httpd_resp_send(req, json_response, strlen(json_response));
    }httpd_uri_t html_cmd_uri = {.uri = "/key_value",.method = HTTP_GET,.handler = html_cmd_handler,.user_ctx = NULL};static httpd_handle_t start_webserver(void){if (httpd_start(&server, &config) == ESP_OK) {// Set URI handlershttpd_register_uri_handler(server, &html_cmd_uri );  // 添加該函數}}
  2. 編譯燒錄打開串口,效果如下
    在這里插入圖片描述

4.3 HTML 內容

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="shortcut icon" href="#" /><title>html-test</title><style>/* style 屬于CSS部分  */body {font-family: Arial, Helvetica, sans-serif;background: #413f3f;color: #EFEFEF;font-size: 16px}/* 包裹“按鍵”、“下拉選項”的背景 */ .ctr_field_bkg {background-color: #e0cfcf;height: 100px;width: 350px;position: absolute;left: 10px;top: 50px;}/* 單個按鍵的屬性 */.button_key_1 {left: 50px;top: 10px;}/* 按鍵的父屬性 */.button {display: block;padding: 0 5px;border: 0;position: relative;height: 30px;width: 90px;left: 100px;top: 10px;cursor: pointer;/* cursor:用來設置鼠標光標/指針在指定元素上的形狀; */color: #fff;background: #5a8194;border-radius: 5px;font-size: 16px;}/* 鼠標移動到按鍵時,按鍵會變為該顏色*/.button:hover {background-color: #f88020;color: white;}/* 下拉選項 */.drop_down_option {/* display: flex; */cursor: pointer;/* 用來設置鼠標光標/指針在指定元素上的形狀; */flex-wrap: nowrap;line-height: 22px;position: relative;margin: 5px 0;border-radius: 15px;left: 100px;top: 20px;font-size: 16px;color: #fff;background: #a0b931;display: inline-block;}/* wifi修改按鍵入口 */.wifi_entry_button {display: block;padding: 0 5px;border: 0;position: absolute;height: 30px;width: 350px;left: 10px;top: 160px;cursor: pointer;/* cursor:用來設置鼠標光標/指針在指定元素上的形狀; */color: #fff;background: #96b1b9;border-radius: 5px;font-size: 16px;}/* wifi 彈窗的整體屬性 */.wifi_input_windows {width: 350px;height: 250px;border: 3px solid #f8f8f8;visibility: hidden;position: absolute;z-index: 999;opacity: 1;overflow: hidden;background-color: white;text-align: center;margin-top: 10%;margin-left: 1%;border-radius: 10px;}.open {visibility: visible;opacity: 1;}.wifi_input_windows_box {margin-top: 15px;width: 100%;height: 40px;color: #0e0d0c;}/* WiFi 彈窗中 “wifi賬號與密碼修改” 字體 */.wifi_input_windows_box .login_logo {text-align: left;font-size: 20px;font-weight: 300;padding-left: 80px;float: left;}/* WiFi 彈窗中右上角的X */.wifi_input_windows_box .close {width: 20px;height: 20px;color: #d83434;text-align: center;line-height: 20px;border: 1px solid #5d5d5d;border-radius: 50%;float: right;padding-top: 0px;margin-right: 10px;font-size: 12px;}/* 鼠標移動到對應位置時,按鍵會變為該顏色*/.wifi_input_windows_box .close:hover {cursor: pointer;background-color: #aa1111;color: white;}hr {background-color: #F8F8F8;}.wifi_input_windows_box .submit_1 {border: 2px solid #f88020;height: 40px;width: 80px;background-color: white;}.wifi_input_windows_box .submit_1:hover {background-color: #f88020;color: white;}.msg {display: inline-block;font-size: 16px;/* margin-top: 2%; */margin-left: 5%;color: #d83434;}</style>
</head><body><section class="main"><div id="logo"><label for="nav-toggle-cb" id="nav-toggle">&#9776;&nbsp;&nbsp;html test</label></div><canvas id="frame_check" width="340" height="200" class="detection_box">您的瀏覽器不支持 HTML5 canvas 標簽,感應框無法顯示。</canvas><!-- <div class="ctr_field_bkg"> --><div class="ctr_field_bkg"><button class="button button_key_1" id="key_id_1" value="2">按鍵</button><select title="下拉選項" class="drop_down_option" id="down_option"><option value="9">選項1</option><option value="10">選項2</option><option value="11">選項3</option></select></div><div><button class="wifi_entry_button" id="btn_1">點擊修改wifi賬號與密碼</button><div class="wifi_input_windows"><form><div class="wifi_input_windows_box"><div class="login_logo">wifi賬號與密碼修改</div><div class="close">X</div></div><hr><div class="wifi_input_windows_box">wifi 賬號輸入:<input type="text" name="ssid" id="id_ssid" value=""><span class="msg"id="id_span_ssid"></span></div><div class="wifi_input_windows_box">wifi 密碼輸入:<input type="text" name="password" id="id_password" value=""><span class="msg" id="id_span_password"></span></div><div class="wifi_input_windows_box"><input class="submit_1" type="submit" name="submit" id="id_submit" value="提&nbsp;交"></div></form></div></div></section><script>var baseHost = document.location.origin;var btn_1 = document.getElementById("btn_1");var close = document.getElementsByClassName("close");var wifi_input_windows = document.getElementsByClassName("wifi_input_windows");function fetchUrl(url, cb) {fetch(url).then(response => response.json()).then(function (data) {cb(200, data);}).catch(function (err) {cb(-1, err);});}function transmit_cmd_value(key_value, cb) {fetchUrl(`${baseHost}/key_value?key_value=${key_value}`, function (code, txt) {cb(code, txt);// cb(200, txt);  //測試用});}function transmit_wifi_value(wifi_ssid, wifi_password, cb) {fetchUrl(`${baseHost}/wifi?wifi_ssid=${wifi_ssid}&wifi_password=${wifi_password}`, function (code, txt) {cb(code, txt);// cb(200, txt);  //測試用});}btn_1.addEventListener('click', function () {wifi_input_windows[0].className = "wifi_input_windows open";})close[0].addEventListener('click', function () {var span_password = document.getElementById("id_span_password");var span_ssid = document.getElementById("id_span_ssid");span_password.innerHTML =''span_ssid.innerHTML =''wifi_input_windows[0].className = "wifi_input_windows";})const ssid = document.querySelector('[name=ssid]')ssid.addEventListener('change', verifyName)function verifyName() {const span = ssid.nextElementSiblingconst reg = /^[a-zA-Z0-9-_]{2,16}$/if (!reg.test(ssid.value)) {span.innerHTML = '2到16個字符且只能包含數字、字母、下劃線'return false}span.innerHTML = ''return true}const password = document.querySelector('[name=password]')password.addEventListener('change', verifyPassword)function verifyPassword() {const span = password.nextElementSiblingconst reg = /^[a-zA-Z0-9-_]{8,16}$/if (!reg.test(password.value)) {span.innerHTML = '8到16個字符且只能包含數字、字母、下劃線'return false}span.innerHTML = ''return true}const form = document.querySelector('form')form.addEventListener('submit', function (e) {if ((!verifyName()) || (!verifyPassword())) {e.preventDefault()}else {transmit_wifi_value(ssid.value, password.value, function (code, txt) {if (code != 200) {alert('Error[' + code + ']: ' + txt);} else {// console.log(txt.cnb_board);}});alert('請重新連接新的wifi賬號與密碼。')}})// 監聽來自MCU的信號document.addEventListener('DOMContentLoaded', function (event) {function update_frame() {transmit_cmd_value(14, function (code, txt) {if (code != 200) {alert('Error[' + code + ']: ' + txt);} else {console.log(txt);a_area_ctx.clearRect(0, 0, document.getElementById('frame_check').width, document.getElementById('frame_check').height);document.getElementById("down_option").selectedIndex = txt.frame_sensitivity; }});}//setInterval(update_frame, 1000);  // 網頁更新周期const sensitivity_option = document.getElementById('down_option')sensitivity_option.onchange = () => {transmit_cmd_value(sensitivity_option.value, function (code, txt) {if (code != 200) {alert('Error[' + code + ']: ' + txt);} else {console.log(txt.cnb_board);}});}const ButtonKey_1 = document.getElementById('key_id_1');ButtonKey_1.onclick = () => {let key_value = parseInt(document.getElementById('key_id_1').value);transmit_cmd_value(key_value, function (code, txt) {if (code != 200) {alert('Error[' + code + ']: ' + txt);} else {console.log(txt.cnb_board);}});}})</script>
</body></html>

4.4 更新 html_test.html

  1. 直接編輯html_test.html,使用瀏覽器打開就可以直接看效果
  2. 更新,使用7-Zip壓縮軟件重新壓縮
    在這里插入圖片描述
  3. 編譯燒錄即可

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/14743.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/14743.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/14743.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

PMapper:助你在AWS中實現IAM權限快速安全評估

關于PMapper PMapper是一款功能強大的腳本工具&#xff0c;該工具本質上是一個基于Python開發的腳本/代碼庫&#xff0c;可以幫助廣大研究人員識別一個AWS賬號或AWS組織中存在安全風險的IAM配置&#xff0c;并對IAM權限執行快速評估。 PMapper可以將目標AWS帳戶中的不同IAM用戶…

Hive環境搭建

1 安裝Hive 下載文件 # wget -P /opt/ https://mirrors.huaweicloud.com/apache/hive/hive-2.3.8/apache-hive-2.3.8-bin.tar.gz 解壓縮 # tar -zxvf /opt/apache-hive-2.3.8-bin.tar.gz -C /opt/ 修改hive文件夾名字 # mv /opt/apache-hive-2.3.8-bin /opt/hive 配置環境變量 …

torch Embedding 學習筆記

文本向量化&#xff08;Text Embedding&#xff09;&#xff1a;將文本數據&#xff08;詞、句子、文檔&#xff09;表示成向量的方法。 詞向量化將詞轉為二進制或高維實數向量&#xff0c;句子和文檔向量化則將句子或文檔轉為數值向量&#xff0c;通過平均、神經網絡或主題模…

幀動畫播放出現oom異常分析及解決

問題描述 需要播放序列幀&#xff0c;幀數特別多的時候會oom 問題分析 源代碼每一幀都創建一次bitmap&#xff0c;極度消耗內存 bitmap.recycle并不會立刻回收內存&#xff0c;內存還是會很緊張 問題解決 利用inbitmap&#xff0c;每一幀復用同一片內存區域 //設置Bitmap…

【大模型部署】在C# Winform中使用文心一言ERNIE-3.5 4K 聊天模型

【大模型部署】在C# Winform中使用文心一言ERNIE-3.5 4K 聊天模型 前言 今天來寫一個簡單的ernie-c#的例子&#xff0c;主要參考了百度智能云的例子&#xff0c;然后自己改了改&#xff0c;學習了ERNIE模型的鑒權方式&#xff0c;數據流的格式和簡單的數據解析&#xff0c;實…

軟件安裝:Linux安裝Nginx

軟件安裝&#xff1a;Linux如何安裝軟件&#xff0c;程序。 源碼安裝 類似于.exe 源碼包就是一堆源代碼程序組成的。 linux tar.gz 這個就是源碼包 源碼包--------二進制包&#xff0c;源碼包里面的代碼經過編譯之后形成的包。 優點&#xff1a;1、開源&#xff0c;可以二次…

面試八股之MySQL篇1——慢查詢定位篇

&#x1f308;hello&#xff0c;你好鴨&#xff0c;我是Ethan&#xff0c;一名不斷學習的碼農&#xff0c;很高興你能來閱讀。 ??目前博客主要更新Java系列、項目案例、計算機必學四件套等。 &#x1f3c3;人生之義&#xff0c;在于追求&#xff0c;不在成敗&#xff0c;勤通…

JavaScript 數組方法總結

JavaScript 數組方法總結 創建數組訪問和修改數組&#xff08;長度 &#xff06; 元素&#xff09;添加和刪除元素數組遍歷元素查找過濾和映射歸并和縮減數組的連接數組的扁平化數組的排序數組的反轉數組的復制數組的測試數組的填充 創建數組 Array.of(...elements): 創建一個…

Singer模型與CT模型狀態轉移矩陣的求解

Singer模型與CT模型狀態轉移矩陣的求解 文章目錄 Singer模型與CT模型狀態轉移矩陣的求解前言狀態方程矩陣指數函數泰勒展開拉普拉斯變換 Singer模型CT模型 前言 回想起來&#xff0c;第一次接觸Singer模型與CT模型時的狀態轉移矩陣時&#xff0c;對求解過程一知半解。現在&…

linux 上除了shell、python腳本以外,還有什么腳本語言用得比較多?

在開始前我有一些資料&#xff0c;是我根據網友給的問題精心整理了一份「 Linux的資料從專業入門到高級教程」&#xff0c; 點個關注在評論區回復“888”之后私信回復“888”&#xff0c;全部無償共享給大家&#xff01;&#xff01;&#xff01;說到在 Linux下的編程&#xf…

柯橋成人商務英語“?cold”是“冷”,“shoulder”是“肩膀”,?cold shoulder可不是冷肩膀!

英文中有很多俚語&#xff08;idioms&#xff09;都與身體部位有關&#xff0c;非常有趣。 今天&#xff0c;英語君就為大家介紹一些和身體部位有關的俚語&#xff0c;一起來看看吧&#xff01; cold shoulder “cold shoulder”不能用字面意思理解為“冷肩膀”&#xff0c;我們…

學習毛概思想(自用)

一、單項選擇題 毛澤東思想的主要創立者是&#xff08; A &#xff09; A、毛澤東 B、劉少奇 C、周恩來 D、朱德中國共產黨內第一個提出“毛澤東思想”科學概念的是&#xff08; D  &#xff09;    A、周恩來   B、劉少奇   C、朱德…

CommonJS 和 ESM 在模塊加載和處理依賴關系的方式上的不同點理解

模塊加載&#xff1a; CommonJS&#xff1a;當執行到 require() 函數時&#xff0c;Node.js 會同步地加載模塊文件&#xff0c;即立即讀取模塊文件并執行其中的代碼&#xff0c;然后返回模塊的導出值。這就意味著&#xff0c;直到運行時我們才知道具體導入了哪些模塊&#xff0…

智慧校園學工管理系統的部署

學工體系思政服務該怎么規劃建造&#xff1f;思政作為高校育人的中心使命&#xff0c;在做到讓學生健康高興生長的一起&#xff0c;也應滿意學生生長成才的各類需求。使用技術為學生供給優質的信息化服務&#xff0c;是其間的有效途徑。大數據讓個性化教育成為可能&#xff0c;…

【題解】AB33 相差不超過k的最多數(排序 + 滑動窗口)

https://www.nowcoder.com/practice/562630ca90ac40ce89443c91060574c6?tpId308&tqId40490&ru/exam/oj 排序 滑動窗口 #include <iostream> #include <vector> #include <algorithm> using namespace std;int main() {int n, k;cin >> n &…

css中彈性布局使用方法

最近寫企業家用到許多彈性&#xff0c;感覺到彈性的重要性&#xff0c;今天給大家總結一下 彈性布局&#xff08;Flexbox&#xff09;是一種在 CSS 中用來實現靈活的布局方式&#xff0c;它能夠方便地調整容器中子元素的尺寸、順序和對齊方式。以下是一些常見的彈性布局屬性及…

【探索數據結構】線性表之順序表

&#x1f389;&#x1f389;&#x1f389;歡迎蒞臨我的博客空間&#xff0c;我是池央&#xff0c;一個對C和數據結構懷有無限熱忱的探索者。&#x1f64c; &#x1f338;&#x1f338;&#x1f338;這里是我分享C/C編程、數據結構應用的樂園? &#x1f388;&#x1f388;&…

Vue3按順序調用新增和查詢接口

Vue3按順序調用新增和查詢接口 一、前言1、代碼 一、前言 如果你想將兩個調用接口的操作封裝在不同的方法中&#xff0c;你可以考慮將這兩個方法分別定義為異步函數&#xff0c;并在需要時依次調用它們。以下是一個示例代碼&#xff1a; 1、代碼 <template><div>…

豐田精益生產的模板

豐田精益生產&#xff0c;也被稱為豐田生產方式&#xff08;Toyota Production System, TPS&#xff09;&#xff0c;是一套完整的生產和管理系統&#xff0c;其核心目標是最大化效率、消除浪費&#xff0c;并通過持續改進來提升產品質量。 學習優秀企業 學習福特 豐田精益生產…