程序下载:点击下载
原理讲解:
在订阅时,可发送多条订阅指令:
if (client.connect(ID_MQTT)) {
Serial.println("connected");
client.subscribe(topicA);//修改,修改为你的主题
delay(100);
client.subscribe(topicB);//修改,修改为你的主题
}在消息接收处,可判断是发往哪个主题的消息,再执行不同的动作。
if(strcmp(topic,topicA)==0){//判断topic是否是topicA
Serial.println(" topicA msg");
if(Mqtt_Buff == "on") {//如果接收字符on,亮灯
turnOnLed();//开灯函数
} else if (Mqtt_Buff == "off") {//如果接收字符off,亮灯
turnOffLed();//关灯函数
}
}else if(strcmp(topic,topicB)==0){//判断topic是否是topicB
Serial.println(" topicB msg");
if (Mqtt_Buff == "on") {//如果接收字符on,亮灯
turnOnLed();//开灯函数
} else if (Mqtt_Buff == "off") {//如果接收字符off,亮灯
turnOffLed();//关灯函数
}
}