当前位置:网站首页>微信小程序基于udp协议与esp8266进行通信
微信小程序基于udp协议与esp8266进行通信
2022-04-23 14:02:00 【1029179954】
1、概述
微信小程序有关于UDP协议的API,具体内容可前往官网查看;整体实现的思路就是利用路由组一个局域网,手机、路由器和esp8266,手机和esp8266连接路由器,登录路由器查看esp8266的ip,微信小程序利用udp协议可以往esp8266发送数据,进而实现微信小程序与esp8266通信,也就实现了与单片机通信。
2、硬件准备
路由器、手机、esp8266
3、实现思路
(1)组局域网
路由器、手机和esp8266组局域网;手机和esp8266连接路由器
(2)查看ip
登录路由的后台查看esp8266的ip,微信小程序udp协议用ip(这里仅仅查看esp8266即可)
4、微信小程序发送命令到esp8266
下面以控制灯光为例
wxml
<view class="scan">
<view class="time-section">
<view class="time">
<view class="hourminuts">
<text>{
{hours}}:{
{minutes}}</text>
</view>
<view class="seconds">
<text>{
{seconds}}</text>
</view>
</view>
<view class="date">
<text>{
{month}}月{
{day}}日 {
{week}}</text>
</view>
</view>
</view>
<view class="section">
<view class="section-title">灯1</view>
<view class="section-form">
<view class="form-group">
<label>灯1的控制</label>
<switch color="#007aff" checked bindchange="light" ></switch>
</view>
</view>
</view>
<view class="section">
<view class="section-title">灯2</view>
<view class="section-form">
<view class="form-group">
<label>灯2的控制</label>
<switch color="#007aff" checked bindchange="wind" ></switch>
</view>
</view>
</view>
<view class="section">
<view class="section-title">灯3</view>
<view class="section-form">
<view class="form-group">
<label>灯3的控制</label>
<switch color="#007aff" checked bindchange="buzzer" ></switch>
</view>
</view>
</view>
wxss
/* pages/scan/scan.wxss */
page {
background-color: #fafafa;
}
.name {
margin-top: 20rpx;
margin-bottom: 40rpx;
line-height: 32px;
font-size: 17.25pt;
color: #000000;
}
.time-section {
margin-top: 110rpx;
}
.time {
display: flex;
flex-direction: row;
justify-content: center;
}
.hourminuts {
line-height: 154rpx;
font-size: 41.25pt;
color: rgba(0, 0, 0, 0.87);
}
.seconds {
line-height: 72rpx;
font-size: 18.75pt;
color: #f5a623;
margin: auto 0rpx 20rpx 0rpx;
}
.date {
margin-top: 12rpx;
text-align: center;
font-weight: 500;
line-height: 22px;
font-size: 12pt;
color: rgba(0, 0, 0, 0.54);
margin-bottom: 60rpx;
}
.footer {
/*display: flex;
flex-direction: column;
justify-content: center;*/
/*align-items: center;*/
margin: 0 auto;
width: 80%;
}
.to-clock {
background-color: #22a1e0;
color: #ffffff;
margin-bottom: 40rpx;
}
.hover-to-clock {
opacity: 0.7;
}
.to-list {
color: rgba(0, 0, 0, 0.54);
margin-bottom: 40rpx;
}
.hover-to-list {
opacity: 0.7;
}
.main-body {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
}
.section {
width: 100%;
padding: 20rpx;
}
.section-title {
font-size: 35rpx;
width: 300rpx;
height: 80rpx;
line-height: 80rpx;
padding-left: 20rpx;
border-bottom: 4rpx solid #000;
}
.section-form {
display: flex;
flex-direction: column;
padding: 10rpx 20rpx;
}
.form-group {
display: flex;
flex-direction: row;
width: 100%;
height: 80rpx;
line-height: 80rpx;
font-size: 32rpx;
}
.form-group label {
width: 75%;
}
.form-group input,
.form-group switch {
height: 80rpx;
width: 60rpx;
line-height: 80rpx;
text-align: center;
}
.form-group input {
border-bottom: 2rpx solid #000;
height: 60rpx;
line-height: 60rpx;
}
js
以灯1为列,核心代码
//灯 26 high1 low1
light(e){
var iip=wx.getStorageSync('ip')//这里是手动输入绑定ip 获取缓存 也可以在这里写死
console.log("iip"+iip)
var that=this
var status=e.detail.value
if(status==true){
console.log("开灯")
var mess="on"
var ip1 = iip;
var port = "";
const udp = wx.createUDPSocket();
udp.bind();
udp.send({
address: ip1,
port: port,
message: mess
});
}else{
console.log("关灯")
var mess="off"
var ip2 = iip;
var port = "";
const udp = wx.createUDPSocket();
udp.bind();
udp.send({
address: ip2,
port: port,
message: mess
});
}
},
5、esp8266接受微信小程序传输过来的数据
版权声明
本文为[1029179954]所创,转载请带上原文链接,感谢
https://blog.csdn.net/baidu_38978508/article/details/124318118
边栏推荐
- Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
- PySide2
- Go语言 RPC通讯
- Quartus prime hardware experimental development (de2-115 board) experiment II function adjustable comprehensive timer design
- 1256:献给阿尔吉侬的花束
- Haruki Murakami -- Excerpt from "what do I talk about when I talk about running"
- Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
- Problems encountered in the project (V) understanding of operating excel interface poi
- Android interview theme collection
- request模块
猜你喜欢
[VMware] address of VMware Tools
redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
Elmo (bilstm-crf + Elmo) (conll-2003 named entity recognition NER)
Nodejs安装及环境配置
2021年秋招,薪资排行NO
linux安装mysql后修改密码
How does redis solve the problems of cache avalanche, cache breakdown and cache penetration
服务器中挖矿病毒了,屮
Taobao released the baby prompt "your consumer protection deposit is insufficient, and the expiration protection has been started"
Nacos+AspnetCore+Ocelot实战编码
随机推荐
2022年江西最新建筑八大员(质量员)模拟考试题库及答案解析
linux MySQL数据定时dump
接口文档yaml
leetcode--977. Squares of a Sorted Array
freeCodeCamp----arithmetic_ Arranger exercise
scikit-learn构建模型的万能模板
3300万IOPS、39微秒延迟、碳足迹认证,谁在认真搞事情?
Strange bug of cnpm
Autumn recruitment in 2021, salary ranking No
Go语言 RPC通讯
项目中遇到的问题(五)操作Excel接口Poi的理解
Interesting talk about network protocol
Reading notes: fedgnn: Federated graph neural network for privacy preserving recommendation
Express②(路由)
mysql新表,自增id长达20位,原因竟是......
Decimal 格式化小数位/DateTime 转换处理
第一章 电商秒杀商品回顾
[code analysis (6)] communication efficient learning of deep networks from decentralized data
专题测试05·二重积分【李艳芳全程班】
Reading notes: Secure federated matrix factorization