Foreword
Things is a very broad category
In fact, that white is the thing - Network
In which knowledge is actually involved in numerous, too broad ...
But then! Basically can not do without is the remote control communication
In order to achieve communication between the device and the phone / PC / Web
And to do or many-to-many communication
So there must be a middleware
This middleware as the forwarding station data
So there are two of the most widely used
1. WebSocket
2.MQTT
In fact, these two guys are encapsulated in TCP on the basis of further
In fact, TCP communication
But WebSocket is coming
TCP -- http -- WebSocket
The MQTT is a TCP server
TCP -- MQTT
If you want to learn more MQTT, please take a look at the following article link
https://www.cnblogs.com/yangfengwu/p/11762642.html
Software installation MQTT
I will give you ready installation package on Windows
Please refer to the article attached to their local computer
https://www.cnblogs.com/yangfengwu/p/10547024.html
Note: The article is installed on my computer cloud
We test install it on your local computer can
This is my installation
I use console mode is activated, then the test
Test whether the software can communicate MQTT
1. Check the local IP address
2. Open the debugging assistant test
As long as your own hair own income, it
Let connected MQTT 8266
Note: I use the project files inside
This is the official offer, but there is a mistake
See not see what I wrote
ESA2GJK1DH1K Basics:! Come thorough look MQTT
When you subscribe to identify if a subscription fails to return the same, but the message class will be 0x80
So: the official failed to do so even when subscriptions still considered a success!
amend as below:
1. "package" inside the file copies according to the following
2. Add the header file, define a structure variable mqtt
#include "driver/mqtt.h" MQTT_Client mqttClient;
3.编写连接,还有设置一些回调函数
MQTT_InitConnection(&mqttClient, "192.168.191.1", 1883, 0);//MQTT服务器IP地址,端口号,是否SSL MQTT_InitClient(&mqttClient, "client_id", "yang", "11223344", 3, 1);//ClientID,用户名,密码,心跳包时间,清除连接信息 MQTT_InitLWT(&mqttClient, "/lwt", "offline", 0, 0);//遗嘱 MQTT_OnConnected(&mqttClient, mqttConnectedCb);//设置连接回调 MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb);//设置断开回调 MQTT_OnPublished(&mqttClient, mqttPublishedCb);//设置发送完消息回调 MQTT_OnData(&mqttClient, mqttDataCb);//接收数据回调
//连接上MQTT void mqttConnectedCb(uint32_t *args){ MQTT_Client* client = (MQTT_Client*)args; os_printf("MQTT: Connected\r\n"); MQTT_Subscribe(client, "111111", 0);//订阅主题:111111 } //连接断开 void mqttDisconnectedCb(uint32_t *args){ MQTT_Client* client = (MQTT_Client*)args; os_printf("MQTT: Disconnected\r\n"); } //发送完消息 void mqttPublishedCb(uint32_t *args){ MQTT_Client* client = (MQTT_Client*)args; os_printf("MQTT: Published\r\n"); } //接收到数据 void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len) { char *topicBuf = (char*)os_zalloc(topic_len+1), *dataBuf = (char*)os_zalloc(data_len+1);//用来缓存主题和消息 MQTT_Client* client = (MQTT_Client*)args; os_memcpy(topicBuf, topic, topic_len); topicBuf[topic_len] = 0; os_memcpy(dataBuf, data, data_len); dataBuf[data_len] = 0; MQTT_Publish(client, "222222", dataBuf, data_len, 0, 0);//转发接收的消息:发布的主题222222 os_printf("Receive topic: %s, data: %s \r\n", topicBuf, dataBuf);//打印接收的消息 os_free(topicBuf); os_free(dataBuf); }
4.让模块连接上路由器以后,在执行连接MQTT程序
void wifiConnectCb(uint8_t status) { if(status == STATION_GOT_IP){ MQTT_Connect(&mqttClient); } else { MQTT_Disconnect(&mqttClient); } }
测试
扩展
如果用户想知道官方是具体是怎么封装的MQTT
请用户先看我这两篇文章以后再去了解官方是怎么做的
https://www.cnblogs.com/yangfengwu/p/12536382.html
https://www.cnblogs.com/yangfengwu/p/12540710.html