【BW16】和小安派的联动

[复制链接]
查看523 | 回复9 | 2024-4-4 21:23:43 | 显示全部楼层 |阅读模式

本帖最后由 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

SW2演示

SW2演示.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>

回复

使用道具 举报

WT_0213 | 2024-4-4 23:59:23 | 显示全部楼层
厉害
回复

使用道具 举报

putin | 2024-4-5 07:43:09 | 显示全部楼层
不错呦
回复

使用道具 举报

1055173307 | 2024-4-5 09:42:51 | 显示全部楼层
回复

使用道具 举报

lovzx | 2024-4-5 15:19:20 | 显示全部楼层
学习
回复

使用道具 举报

iiv | 2024-4-5 19:57:40 | 显示全部楼层
优秀
回复

使用道具 举报

干簧管 | 2024-4-6 09:52:37 | 显示全部楼层
回复

使用道具 举报

1084504793 | 2024-4-6 12:04:41 | 显示全部楼层
回复

使用道具 举报

WT_0213 | 2024-4-6 20:09:01 | 显示全部楼层
优秀
回复

使用道具 举报

lazy | 2024-4-6 22:20:19 | 显示全部楼层
学习了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则