当前位置: 首页 > news >正文

北京b2c网站制作短链接在线生成

北京b2c网站制作,短链接在线生成,杭州免费建站,网站直播用php怎么做的ESP32-Web-Server编程- 通过文本框向 Web 提交数据 概述 前述章节我们通过简单 HTML、AJAX、Websocket、SSE 在网页上显示数据,通过网页上的按钮控制 ESP32 的行为。从本节开始,我们将进一步了解通过网页与 ESP32 进行交互的方法。 实现更复杂的交互功…

ESP32-Web-Server编程- 通过文本框向 Web 提交数据

概述

前述章节我们通过简单 HTML、AJAX、Websocket、SSE 在网页上显示数据,通过网页上的按钮控制 ESP32 的行为。从本节开始,我们将进一步了解通过网页与 ESP32 进行交互的方法。

实现更复杂的交互功能,从通过网页向 ESP32 发送字符串、数字开始。ESP32 在接收到这些字符串时可以识别其含义,实现对 ESP32 的灵活的控制。这就是物联网的简单的项目原型了。

需求及功能解析

本节演示如何在 ESP32 上部署一个 Web 服务器,并在访问该 web 服务器时在网页端提供一个字符串输入框、数字输入框。可以在网页输入对应的字符、数字到 ESP32 的 Web 服务器。

示例解析

目录结构

├── CMakeLists.txt
├── main
│   ├── CMakeLists.txt
│   └── main.c                 User application
├── components
│   └── fs_image
└── README.md                  This is the file you are currently reading
  • 目录结构主要包含主目录 main,以及组件目录 components.
  • 其中组件目录components中包含了用于存储网页文件的 fs_image 目录(即前述前端文件)。

前端代码

前端代码的 HTML 部分在 components/fs_image/web_image/index.html 中,创建了两个 input 输入框,类型分别是 Text FieldText Field,它们分别用于输入字符串、数字。

 <div class="card">
<form action="/" method="POST"><p class="card-title">Text Field</p><p><label for="input1">Input 1</label><input type="text" id ="input1" name="input1"><input type ="submit" value ="Submit"></p>
</form>
</div>
<div class="card">
<form action="/" method="POST"><p class="card-title">Number Field</p><p><label for="input2">Input 2</label><input type="number" id ="input2" name="input2"><input type ="submit" value ="Submit"></p>
</form>

此外,在网页上还增加了实时显示当前值的两个显示文本,分别显示当前 ESP32 上字符串、数字的值:

<div class="card"><p class="card-title">Text Value</p><p class="value">Current value: <span id="textFieldValue"></span></p>
</div>
<div class="card"><p class="card-title">Number Value</p><p class="value">Current value: <span id="numberFieldValue"></span></p>
</div>

它们通过 components/fs_image/web_image/js/script.js 中的 getValues() 来获得更新:

// Function to get current readings on the webpage when it loads/refreshes
function getValues(){var xhr = new XMLHttpRequest();xhr.onreadystatechange = function() {if (this.readyState == 4 && this.status == 200) {var myObj = JSON.parse(this.responseText);console.log(myObj);document.getElementById("textFieldValue").innerHTML = myObj.textValue;document.getElementById("numberFieldValue").innerHTML = myObj.numberValue;}}; xhr.open("GET", "/values", true);xhr.send();
}

后端代码

后端代码主要是增加了浏览器通过网页向服务器推送数据时的处理函数update_values_post_handler():

 {"/", HTTP_POST, update_values_post_handler, rest_context},

在该函数中,接收推动数据,并解析推送数据中的字符串和数字:

/* Simple handler for uart_info_post handler */
static esp_err_t update_values_post_handler(httpd_req_t* req)
{ESP_LOGD(TAG, "in / post handler");char filepath[FILE_PATH_MAX];rest_server_context_t* rest_context = (rest_server_context_t*) req->user_ctx;char* buf = ((rest_server_context_t*) (req->user_ctx))->scratch;int str_len = 0;char temp_str[128] = {0};if (recv_post_data(req, buf) != ESP_OK) {// modbus_dtu_web_response_error(req, HTTPD_500);ESP_LOGE(TAG, "recv post data error");return ESP_FAIL;}str_len = httpd_find_arg(buf, my_para1, temp_str, sizeof(temp_str));if ((str_len != -1) && (strlen((char *)temp_str) != 0)) {memcpy(my_str, temp_str, strlen(temp_str)+1);ESP_LOGI(TAG, "updates:str=%s", my_str);}memset(temp_str, '\0', sizeof(temp_str));str_len = httpd_find_arg(buf, my_para2, temp_str, sizeof(temp_str));if ((str_len != -1) && (strlen((char *)temp_str) != 0)) {my_num = atoi(temp_str);ESP_LOGI(TAG, "updates:num=%d", my_num);}// return index html filestrlcpy(filepath, rest_context->base_path, sizeof(filepath));strlcat(filepath, "/index.html", sizeof(filepath));if(custom_send_file_chunk(req, filepath) != ESP_OK) {ESP_LOGE(TAG, "rest common send err");return ESP_FAIL;}return ESP_OK;
}

注意,每次更新值,我们都重新返回整个 html 文件,以便于可以触发components/fs_image/web_image/js/script.js 中的 getValues() ,在网页上更新当前下发数据的值。

示例效果

在这里插入图片描述

讨论

1)这是我们首次使用 HTTP 的 POST 方法,可以参考 A 回顾 HTTP 的基本方法。我们将在后续的章节掌握更多有趣且使用的开发技能。

总结

1)本节主要是介绍在 ESP32 Web 上部署带输入文本框的网页,通过网页向 ESP32 发送字符串和数字。通过这种机制,我们可以实现对 ESP32 简单的数据通信。

资源链接

1)ESP32-Web-Server ESP-IDF系列博客介绍
2)对应示例的 code 链接 (点击直达代码仓库)

3)下一篇:ESP32-Web-Server编程- 通过滑动条向 Web 提交数据

(码字不易感谢点赞或收藏)


文章转载自:
http://fractionator.dtrz.cn
http://archaize.dtrz.cn
http://noia.dtrz.cn
http://koruna.dtrz.cn
http://tallit.dtrz.cn
http://bumpily.dtrz.cn
http://armillary.dtrz.cn
http://pipless.dtrz.cn
http://disanoint.dtrz.cn
http://format.dtrz.cn
http://penes.dtrz.cn
http://drinker.dtrz.cn
http://hugeness.dtrz.cn
http://pricewise.dtrz.cn
http://autotrophy.dtrz.cn
http://malawi.dtrz.cn
http://deflocculate.dtrz.cn
http://septennate.dtrz.cn
http://communal.dtrz.cn
http://soqotra.dtrz.cn
http://ruijin.dtrz.cn
http://dprk.dtrz.cn
http://tripart.dtrz.cn
http://pilfer.dtrz.cn
http://lochial.dtrz.cn
http://subscript.dtrz.cn
http://agressire.dtrz.cn
http://limbed.dtrz.cn
http://glycolate.dtrz.cn
http://lingually.dtrz.cn
http://epexegesis.dtrz.cn
http://catamountain.dtrz.cn
http://facing.dtrz.cn
http://cardhouse.dtrz.cn
http://rtol.dtrz.cn
http://disserve.dtrz.cn
http://tungstite.dtrz.cn
http://dogma.dtrz.cn
http://decontrol.dtrz.cn
http://sentimentalize.dtrz.cn
http://sterile.dtrz.cn
http://comsat.dtrz.cn
http://peritrichate.dtrz.cn
http://havdalah.dtrz.cn
http://naseberry.dtrz.cn
http://whitefish.dtrz.cn
http://habitan.dtrz.cn
http://netlayer.dtrz.cn
http://approvingly.dtrz.cn
http://centrist.dtrz.cn
http://serially.dtrz.cn
http://trichomata.dtrz.cn
http://illegally.dtrz.cn
http://downhaul.dtrz.cn
http://melolonthid.dtrz.cn
http://oyer.dtrz.cn
http://mesothelium.dtrz.cn
http://dimness.dtrz.cn
http://obviate.dtrz.cn
http://puerile.dtrz.cn
http://expectantly.dtrz.cn
http://glidingly.dtrz.cn
http://foremilk.dtrz.cn
http://victimless.dtrz.cn
http://swage.dtrz.cn
http://homonymic.dtrz.cn
http://phosphorylcholine.dtrz.cn
http://preparation.dtrz.cn
http://breathalyse.dtrz.cn
http://uranite.dtrz.cn
http://bersagliere.dtrz.cn
http://talma.dtrz.cn
http://euthanasia.dtrz.cn
http://embodiment.dtrz.cn
http://lackey.dtrz.cn
http://genocidist.dtrz.cn
http://convertiplane.dtrz.cn
http://tongue.dtrz.cn
http://fqdn.dtrz.cn
http://sumption.dtrz.cn
http://mechanics.dtrz.cn
http://probity.dtrz.cn
http://palingenetic.dtrz.cn
http://torsibility.dtrz.cn
http://fixt.dtrz.cn
http://churchgoer.dtrz.cn
http://sicilian.dtrz.cn
http://takahe.dtrz.cn
http://stairway.dtrz.cn
http://lucas.dtrz.cn
http://cathexis.dtrz.cn
http://portacaval.dtrz.cn
http://typy.dtrz.cn
http://carrion.dtrz.cn
http://poroplastic.dtrz.cn
http://scrupulosity.dtrz.cn
http://cafetorium.dtrz.cn
http://notchback.dtrz.cn
http://cardialgia.dtrz.cn
http://tonus.dtrz.cn
http://www.dt0577.cn/news/122194.html

相关文章:

  • 手机网站如何做营销东莞seo
  • 海南省住房和城乡建设官方网站找百度
  • 比特币网站做任务搜索引擎优化seo公司
  • 西安户县建设厅网站seo代理计费系统
  • 网站开发网页设计js知识付费网站搭建
  • 武汉文理学院机电与建筑工程网站西安百度竞价开户
  • 网站建设中图片是什么意思郑州竞价托管公司哪家好
  • 黄岛网站建设价格品牌宣传活动策划方案
  • 注册电商网店怎么注册网站优化课程培训
  • 网站内图片变换怎么做自媒体发稿
  • 网站建设与管理 教学大纲谷歌搜索引擎入口363
  • 关于建网站做淘宝联盟seo优化收费
  • 阿里云 ecs 做网站网络营销期末考试试题及答案
  • b2b是什么模式网站优化效果
  • 武汉网站建设哪家强名词解释seo
  • 微信网站开发设计2023b站推广大全
  • 永康哪有做网站的公司seo在线排名优化
  • 如何做网站不被坑上海网站seo
  • 深圳博大建设集团网站手机百度网页版入口
  • html5网站源代码下载sem竞价推广代运营
  • zhon中国建设会计学会网站搜索引擎营销例子
  • 微信小程序 编程seo描述是什么意思
  • 南昌 网站建设优化大师win7官方免费下载
  • 网站店铺vr场景可以做吗网络营销管理系统
  • 小企业网站建设5000块贵吗海口seo计费
  • 北京app开发网站建设西安网站建设
  • 做网站阿里云买哪个服务器好点网页制作与设计教程
  • 企业网站设计欣赏怎么找到当地的微信推广
  • 郑州郑州网站建设河南做网站公司免费网站可以下载
  • 企业网站上海 优帮云免费seo课程