完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
首先,下面的Demo是在linux上运行的,说明了嵌入的MQTT客户端使用的,其就是把一些数据流程片MQTT协议打包好,再通过socket发送,你也可以移植到机器上, WiFi模块能够连接到服务通信,比如sp86模块WiFi模块,内置了TCP/IP协议片机,直接可以通过AT指令发送数据,直接通过AT指令发送数据,WiFi可以把数据栈发送到服务器。
MQTT Subscribe Demo #include #include #include #include #include #include #include #include #include #include #include #include "src/MQTTPacket.h" int sock; pthread_t ping; void * ping_thread(void * arg); void TcpConnect(const char *host, int port); int main(void) { //建立TCP连接 TcpConnect("127.0.0.1", 1883); //初始化连接参数,MQTTPacket_connectData_initializer定义在MQTTConnect.h unsigned char buf[256]; int bufsize = sizeof(buf); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.clientID.cstring = "zhou001"; //客户端ID data.keepAliveInterval = 30; //存活时间,如果30秒内设备没有向服务器发送ping数据就会短线; data.cleansession = 1; //清除会话 data.username.cstring = "admin"; //用户名 data.password.cstring = "admin"; //密码 //打包和发送connect数据 int len = MQTTSerialize_connect(buf, bufsize, &data); send(sock, buf, len, 0); printf("Connecting...n"); recv(sock, buf, bufsize, 0); //等待连接 //创建ping线程, 25秒ping一次 pthread_create(&ping, NULL, ping_thread, NULL); //订阅主题 MQTTString topicString = MQTTString_initializer; int msgid = 1; //消息id int req_qos = 0; //消息质量 topicString.cstring = "topic1"; //主题名称 len = MQTTSerialize_subscribe(buf, bufsize, 0, msgid, 1, &topicString, &req_qos); //打包subscribe数据 send(sock, buf, len, 0); //发送subscribe数据 printf("Subcribing...n"); while(1) { len = recv(sock, buf, bufsize, 0); if(len > 0) { if(buf[0] == 0x30) //判断接收到的消息是否是publish { //解包publish数据 unsigned char dup; int qos; unsigned char retained; unsigned short msgid; int payloadlen_in; unsigned char* payload_in; //指向payload int rc; MQTTString receivedTopic; MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic, &payload_in, &payloadlen_in, buf, bufsize); printf("message arrived %.*sn", payloadlen_in, payload_in); } } } } void TcpConnect(const char *host, int port) { //创建套接字 sock = socket(AF_INET, SOCK_STREAM, 0); //向服务器(特定IP和端口)发送请求 struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(host); serv_addr.sin_port = htons(port); int res = connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); if(res == 0) printf("TCP Connectedn"); } void * ping_thread(void *arg) { (void)arg; uint8_t buf[10]; int bufsize = sizeof(buf); int len = 0; printf("Ping thread is runningn"); while(1) { sleep(20); //打包和发送ping数据 len = MQTTSerialize_pingreq(buf, bufsize); send(sock, buf, len, 0); printf("Ping...n"); } } MQTT Publish Demo #include #include #include #include #include #include #include #include #include #include #include #include "src/MQTTPacket.h" int sock; pthread_t ping; void * ping_thread(void * arg); void TcpConnect(const char *host, int port); int main(void) { //建立TCP连接 TcpConnect("127.0.0.1", 1883); //初始化连接参数,MQTTPacket_connectData_initializer定义在MQTTConnect.h unsigned char buf[256]; int bufsize = sizeof(buf); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.clientID.cstring = "zhou002"; //客户端ID data.keepAliveInterval = 30; //存活时间,如果30秒内设备没有向服务器发送ping数据就会短线; data.cleansession = 1; //清除会话 data.username.cstring = "admin"; //用户名 data.password.cstring = "admin"; //密码 //打包和发送connect数据 int len = MQTTSerialize_connect(buf, bufsize, &data); send(sock, buf, len, 0); printf("Connecting...n"); recv(sock, buf, bufsize, 0); //等待连接 //创建ping线程,25秒ping一次 pthread_create(&ping, NULL, ping_thread, NULL); MQTTString topicString = MQTTString_initializer; topicString.cstring = "topic1"; unsigned char *payload = "hello"; while(1) { sleep(2); len = MQTTSerialize_publish(buf, bufsize, 0, 0, 0, 0, topicString, payload, strlen(payload)); send(sock, buf, len, 0); printf("message sent hellon"); } } void TcpConnect(const char *host, int port) { //创建套接字 sock = socket(AF_INET, SOCK_STREAM, 0); //向服务器(特定IP和端口)发送请求 struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(host); serv_addr.sin_port = htons(port); int res = connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); if(res == 0) printf("TCP Connectedn"); } void * ping_thread(void *arg) { (void)arg; uint8_t buf[10]; int bufsize = sizeof(buf); int len = 0; printf("Ping thread is runningn"); while(1) { sleep(25); //打包和发送ping数据 len = MQTTSerialize_pingreq(buf, bufsize); send(sock, buf, len, 0); printf("Ping...n"); } } |
|
|
|
只有小组成员才能发言,加入小组>>
3253 浏览 9 评论
2933 浏览 16 评论
3435 浏览 1 评论
8936 浏览 16 评论
4027 浏览 18 评论
9663浏览 3评论
1060浏览 3评论
551浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
546浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2283浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-3 03:26 , Processed in 0.818797 second(s), Total 79, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号