【安信可BW16-Kit】 智能家居项目应用
<div class='showpostmsg'># 【安信可BW16-Kit】 智能家居项目应用## 前言
本次项目使用BW16作为受控端,接收MQTT指令,操作继电器实现控制电器的目的
首先需要BW16烧录代码
其次,安装MQTT客户端,我这里使用MQTTX
具体效果请看下方演示
## 代码
```c
#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));
}
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,免费还好用
## 演示效果
## 使用安信可小安派实现
#### 之前使用小安派做了一个MQTT控制器
#### 这次使用那个小安派来控制BW16
#### 演示看视频
d571f0052867c34617aa239c9191759f<br/>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>楼主gif用的什么工具?推荐一下吧。感觉蛮好。</p>
页:
[1]