登录发现更多内容
首页
分类
发帖
账号
自动登录
找回密码
密码
登录
立即注册
立即登录
立即注册
其他登录
QQ
微信
首页
Portal
求助问答
Xiuno资源
Xiuno教程
Xiuno插件
Xiuno主题
休闲茶馆
定制主题
产品教程
BBS
求助问答
Xiuno资源
Xiuno教程
Xiuno插件
Xiuno主题
休闲茶馆
定制主题
开发资料
求助问答
Xiuno资源
Xiuno教程
Xiuno插件
Xiuno主题
休闲茶馆
定制主题
样品购买
求助问答
Xiuno资源
Xiuno教程
Xiuno插件
Xiuno主题
休闲茶馆
定制主题
IoT云平台
求助问答
Xiuno资源
Xiuno教程
Xiuno插件
Xiuno主题
休闲茶馆
定制主题
GitHub
求助问答
Xiuno资源
Xiuno教程
Xiuno插件
Xiuno主题
休闲茶馆
定制主题
技术博客
求助问答
Xiuno资源
Xiuno教程
Xiuno插件
Xiuno主题
休闲茶馆
定制主题
搜索
搜索
热搜:
LoRa
ESP8266
安信可
本版
帖子
用户
请
登录
后使用快捷导航
没有账号?
立即注册
每日签到
任务
广播
导读
排行榜
设置
我的收藏
退出
9
0
0
首页
BW系列
›
【BW16】和小安派的联动
返回列表
【BW16】和小安派的联动
[ 复制链接 ]
发布帖子
noonezero
论坛元老
27
主题
474
回帖
1万
积分
论坛元老
论坛元老, 积分 10187, 距离下一级还需 9989812 积分
论坛元老, 积分 10187, 距离下一级还需 9989812 积分
积分
10187
私信
9人留言
楼主
BW系列
1404
9
2024-4-4 21:23:43
[i=s] 本帖最后由 noonezero 于 2024-4-4 22:03 编辑 [/i]
# 前言 #### 本次项目使用BW16作为受控端,接收MQTT指令,操作继电器实现控制电器的目的 #### 首先需要BW16烧录代码 #### 其次,安装MQTT客户端,我这里使用MQTTX #### 具体效果请看下方演示 # 代码 ```cpp #include
#include
#define GPIO_sw1 PA26 // 定义继电器引脚 #define GPIO_sw2 PA27 #define ON LOW // 开关对应电平高低 char ssid[] = "wh5"; // wifi账号 char pass[] = "noonezero"; // wifi密码 int status = WL_IDLE_STATUS; // wifi状态 char mqttServer[] = "test.mosquitto.org"; // 测试MQTT服务器 char clientId[] = "noonezeroamebaClient"; // 客户端ID char topic_sw1[] = "topic_sw1"; // 订阅开关主题 char topic_sw2[] = "topic_sw2"; WiFiClient wifiClient; // 实例化Wifi PubSubClient client(wifiClient); // 创建一个MQTT客户端 // 订阅主题收到消息回调 void callback(char* topic, byte* payload, unsigned int length) { // 用于和收到的命令进行对比 char cmd_on[] = "1"; char cmd_off[] = "0"; // 主题1操作 if (strcmp(topic, topic_sw1) == 0) { // 开关操作 if (memcmp((char*)payload, cmd_on, strlen(cmd_on)) == 0) { digitalWrite(GPIO_sw1, ON); } else if (memcmp((char*)payload, cmd_off, strlen(cmd_off)) == 0) { digitalWrite(GPIO_sw1, !ON); } } else if (strcmp(topic, topic_sw2) == 0) { if (memcmp((char*)payload, cmd_on, strlen(cmd_on)) == 0) { digitalWrite(GPIO_sw2, ON); } else if (memcmp((char*)payload, cmd_off, strlen(cmd_off)) == 0) { digitalWrite(GPIO_sw2, !ON); } } Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)(payload[i])); } Serial.print(" -> sw1: "); Serial.print(digitalRead(GPIO_sw1)); Serial.print(" sw2: "); Serial.print(digitalRead(GPIO_sw2)); Serial.println(" Receive subscribe."); } void reconnect() { // 连接MQTT 订阅主题 while (!(client.connected())) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect(clientId)) { Serial.println("connected"); // Once connected, publish an announcement... // ... and resubscribe client.subscribe(topic_sw1); // 订阅主题 client.subscribe(topic_sw2); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void setup() { Serial.begin(115200); // 初始化串口 pinMode(GPIO_sw1, OUTPUT); // 初始化引脚 digitalWrite(GPIO_sw1, !ON); pinMode(GPIO_sw2, OUTPUT); digitalWrite(GPIO_sw2, !ON); // 连接wifi while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: delay(10000); } // 设置MQTT服务器url和端口 client.setServer(mqttServer, 1883); // 设置回调 client.setCallback(callback); // Allow the hardware to sort itself out delay(1500); } void loop() { if (!(client.connected())) { reconnect(); } client.loop(); } ``` # MQTT客户端安装 #### 建议搜索MQTTX,免费还好用 # 演示效果 #### SW1演示  #### SW2演示  # 使用安信可小安派实现 #### 之前使用小安派做了一个MQTT控制器 #### 这次使用那个小安派来控制BW16
点赞
0
收藏
0
淘帖
0
────
0
人觉得很赞
────
回复
使用道具
举报
9 回复
电梯直达
正序浏览
倒序浏览
正序浏览
沙发
WT_0213
回复
使用道具
举报
2024-4-4 23:59:23
厉害
回复
评论
使用道具
举报
板凳
putin
回复
使用道具
举报
2024-4-5 07:43:09
不错呦
回复
评论
使用道具
举报
地板
1055173307
回复
使用道具
举报
2024-4-5 09:42:51
赞
回复
评论
使用道具
举报
5
#
lovzx
回复
使用道具
举报
2024-4-5 15:19:20
学习
回复
评论
使用道具
举报
6
#
iiv
回复
使用道具
举报
2024-4-5 19:57:40
优秀
回复
评论
使用道具
举报
7
#
干簧管
回复
使用道具
举报
2024-4-6 09:52:37
回复
评论
使用道具
举报
8
#
1084504793
回复
使用道具
举报
2024-4-6 12:04:41
赞
回复
评论
使用道具
举报
9
#
WT_0213
回复
使用道具
举报
2024-4-6 20:09:01
优秀
回复
评论
使用道具
举报
10
#
lazy
回复
使用道具
举报
2024-4-6 22:20:19
学习了
回复
评论
使用道具
举报
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
立即登录
手机登录
点评
高级模式
本版积分规则
回帖并转播
回帖后跳转到最后一页
返回
今日推荐
基于 Ai-WV01-32S+STM32移植 emMCP 实现 AI 语音控制点灯
AiPi-PalChatV1_“湾湾小何”提示音测试固件V2.9_UART-MCP
[WB2] 实现自动发现局域网下的设备
热帖排行
基于 Ai-WV01-32S+STM32移植 emMCP 实现 AI 语音控制点灯
求助各位大佬PB-03F的断连问题
求助各位大佬PB-03F的断连问题
ra-01sc-p发射功率大小的使用问题
[AiPi-PalchatV1] [Windows] 克隆仓库和在线烧录遇到的一些小问
AT+MQTTPUBRAW指令所支持的最大数据传输量是多少
AiPi-PalChatV1_UART-MCP_v2.8 UART-MCP 协议配置 问题
Ai-WB2蓝牙怎么连接打印机
统计信息
会员数: 30490 个
话题数: 44655 篇
首页
分类
我的