一.所需材料
1.固件是MQTT的ESP8266
2.USB转串口模块
3.安信可串口调试助手
4.MQTT三元素:client_id,username,password
二.连接MQTT
1.AT+CWMODE=1 //设置wifi模式 ap/sta/ap+sta
2.AT+CWJAP="BJ_Jpcx","x5s5a2q" //连接wifi
3.AT+MQTTUSERCFG=0,1,"client_id","username","password",0,0,"" // 设置MQTT三元素
4.AT+MQTTCONN=0,"xxxxxx.com",1883,0 //连接MQTT
5.AT+MQTTSUB=0,"topicName",1 //订阅主题
6.AT+MQTTPUB=0,"topicName","payload",1,0 //发布主题
至此连接成功,其中MQTT相关配置说明请看文档。
三.MCU配置ESP8266连接MQTT服务器的步骤
uart_send_string("AT+CWMODE=1\r\n");
uart_send_string("AT+CWJAP=\"BJ_Jpcx\",\"x5s5a2q\"\r\n");
uart_send_string("AT+MQTTUSERCFG=0,1,\"xxx\",\"xxx\",\"xxx\",0,0,\"\"\r\n");
uart_send_string("AT+MQTTCONN=0,\"xxx.com\",1883,0\r\n");
uart_send_string("AT+MQTTSUB=0,\"$thing/down/property/xxx/xxx\",1\r\n");
uart_send_string("AT+MQTTSUB?\r\n");
ESP8266-MQTT指令手册:
链接:https://pan.baidu.com/s/1MNtn4kDeMsV7CBgmKIexKQ
提取码:uqhh