本帖最后由 noonezero 于 2024-4-4 22:03 编辑
前言
本次项目使用BW16作为受控端,接收MQTT指令,操作继电器实现控制电器的目的
首先需要BW16烧录代码
其次,安装MQTT客户端,我这里使用MQTTX
具体效果请看下方演示
代码
#include <WiFi.h>
#include <PubSubClient.h>
#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演示
![SW1演示.gif SW1演示.gif](data/attachment/forum/202404/04/212219ezp6bf2h6zlk9xqb.gif)
SW2演示
![SW2演示.gif SW2演示.gif](data/attachment/forum/202404/04/212250o7lx3w7qw195ax1x.gif)
使用安信可小安派实现
之前使用小安派做了一个MQTT控制器
这次使用那个小安派来控制BW16
<iframe src="https://player.bilibili.com/player.html?aid=1202710804&bvid=BV1cf421Z7za&cid=1493949045&p=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe> |