ThingsCloud控制多路继电器【留存自用】

 

#include <ESP8266WiFi.h>// esp8266连接wifi使用的库
#include <PubSubClient.h> // esp8266连接、 接收mqtt消息使用的库
#include <ArduinoJson.h>// esp826解析mqtt下发的控制指令的json库
#include <WiFiManager.h>
#define MAX_CMD_LENGTH 64 //定义mqtt下发的指令的最大长度

int RELAY1_PIN=5;//1号灯泡继电器输出引脚
int RELAY2_PIN=4;//2号灯泡继电器输出引脚
int RELAY3_PIN=2;//3号灯泡继电器输出引脚
int RELAY4_PIN=14;//4号灯泡继电器输出引脚
const char* mqtt_server = ""; // mqtt连接地址,从ThingsCloud设备类型中可以看到
const char* sub_topic ="attributes/push";//订阅的mqtt的topic,控制指令就是服务端通过该topic下发的
const char* upload_topic ="attributes";//设备向mqtt服务器上报状态的topic
const char* mqtt_username = "";// mqtt连接用户名
const char* mqtt_password = "" ; //mqtt连接密码

const char* S1 = "switch1";
const char* S2 = "switch2";
const char* S3 = "switch3";
const char* S4 = "switch4";

WiFiClient espClient;
PubSubClient mqttClient (espClient);

char msg[MAX_CMD_LENGTH];

void setup(){
Serial.begin(115200); 
pinMode(RELAY1_PIN, OUTPUT);
pinMode(RELAY2_PIN, OUTPUT);
pinMode(RELAY3_PIN, OUTPUT);
pinMode(RELAY4_PIN, OUTPUT);

//开始连接wifi
Serial.println("开始连接wifi...");
WiFiManager wifiManager;
//自动连接WiFi。以下语句的参数是连接ESP8266时的WiFi名称
wifiManager.autoConnect("ESP8266");
Serial.print( "ESP8266已经连接到");
Serial.println(WiFi.SSID( )); // WiFi名称
Serial.print("IP地址为:\t");
Serial.println(WiFi.localIP()); // IP
// wifi连接成功后,连接mqtt
mqttClient.setServer(mqtt_server,1883);
mqttClient.setCallback(messageRecevieHandler);
}

//在循环里面不断检测mqtt的连接状态,如果发现连接断开了,马上重连
void loop(){
  if(!mqttClient.connected()){
    reconnect();
  }
  mqttClient.loop();
}

//接收mqtt下发的控制信号的处理函数
void messageRecevieHandler(char* topic, byte* payload, unsigned int length) {
  char message[length + 1];
  for(int i = 0;i < length; i++){
    message[i] = (char)payload[i];
  }
  message[length] = '\0';
  Serial.print("收到mqtt下发的控制指令: ");
  Serial.println(message);
  
  //使用json库解析下发的指令信息
  StaticJsonDocument<64> doc ;
  DeserializationError error = deserializeJson(doc, message, MAX_CMD_LENGTH);
  if (error) {
    return;
  }
  
  if (doc.containsKey(S1)) {
    bool switch_1 = doc[S1];
    digitalWrite(RELAY1_PIN, switch_1 ? LOW:HIGH);
  }
  if (doc.containsKey(S2)) {
    bool switch_2 = doc[S2];
    digitalWrite(RELAY2_PIN, switch_2 ? HIGH:LOW);
  }
  if (doc.containsKey(S3)) {
    bool switch_3 = doc[S3];
    digitalWrite(RELAY3_PIN, switch_3 ? HIGH:LOW);
  }
  if (doc.containsKey(S4)) {
    bool switch_4 = doc[S4];
    digitalWrite(RELAY4_PIN, switch_4 ? HIGH:LOW);
  }
}

void reconnect() {
  while (!mqttClient.connected()) {
    Serial.println("mqtt断线重连...");
    String clientId = "smart-light-" + WiFi.macAddress(); // 连接mqtt时需要用到的clientId,为了保证值不冲突,加上设备wifi网卡的mac地址
    if (mqttClient.connect(clientId.c_str(), mqtt_username, mqtt_password)) {
      mqttClient.subscribe(sub_topic); 
      Serial.println("连接mqtt成功" );
      Serial.println("订阅mqtt topic成功");
    }else {
      delay(2000); // 如果连接失败,等待2秒后再重试
    }
  }
}

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值