本篇內(nèi)容主要講解“MQTT如何連接阿里云接收和發(fā)送數(shù)據(jù)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“MQTT如何連接阿里云接收和發(fā)送數(shù)據(jù)”吧!

我們提供的服務(wù)有:成都網(wǎng)站制作、網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、蓮池ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的蓮池網(wǎng)站制作公司
根據(jù)上一條的學(xué)習(xí),如果沒看過可以去看一下上面的2.1中移4G模塊-ML302-OpenCpu開發(fā)-(MQTT連接阿里云-訂閱主題)
數(shù)據(jù)是可以接收了,那怎么發(fā)送呢。
int example_publish(void *handle)
{
int res = 0;
const char *fmt = "/%s/%s/user/get";
char *topic = NULL;
int topic_len = 0;
char *payload = "{\"message\":\"hello!\"}";
//*** 對topic的拼接***
topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
topic = HAL_Malloc(topic_len);
if (topic == NULL) {
cm_printf("[ALIYUN]: memory not enough\n");
return -1;
}
memset(topic, 0, topic_len);
HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);
//*** 對topic的拼接***
// 調(diào)用IOT_MQTT_Publish_Simple發(fā)送數(shù)據(jù)
res = IOT_MQTT_Publish_Simple(0, topic, IOTX_MQTT_QOS0, payload, strlen(payload));
if (res < 0) {
cm_printf("[ALIYUN]: publish failed, res = %d\n", res);
HAL_Free(topic);
return -1;
}
HAL_Free(topic);
return 0;
}IOT_MQTT_Publish_Simple函數(shù)
topic_name:topic名字
qos:qos
data:數(shù)據(jù)
len:發(fā)送數(shù)據(jù)長度
int IOT_MQTT_Publish_Simple(void *handle, const char *topic_name, int qos, void *data, int len)
這是一個比較簡單的MQTT發(fā)送程序
到此,相信大家對“MQTT如何連接阿里云接收和發(fā)送數(shù)據(jù)”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
文章名稱:MQTT如何連接阿里云接收和發(fā)送數(shù)據(jù)
文章鏈接:http://chinadenli.net/article44/gghoee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、網(wǎng)站導(dǎo)航、網(wǎng)站策劃、網(wǎng)站改版、品牌網(wǎng)站制作、App設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)