本篇內(nèi)容主要講解“MQTT如何連接阿里云訂閱主題”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“MQTT如何連接阿里云訂閱主題”吧!
成都創(chuàng)新互聯(lián)公司是一家專業(yè)從事網(wǎng)站制作、成都做網(wǎng)站的網(wǎng)絡(luò)公司。作為專業(yè)的建站公司,成都創(chuàng)新互聯(lián)公司依托的技術(shù)實(shí)力、以及多年的網(wǎng)站運(yùn)營(yíng)經(jīng)驗(yàn),為您提供專業(yè)的成都網(wǎng)站建設(shè)、營(yíng)銷型網(wǎng)站建設(shè)及網(wǎng)站設(shè)計(jì)開(kāi)發(fā)服務(wù)!
cm_main.c文件里面的cm_main_task是主函數(shù),在主函數(shù)里面調(diào)用cm_test_aliyun函數(shù)。
cm_test_aliyun函數(shù):
void cm_test_aliyun(){ cm_printf("[ALIYUN]: aliyun demo start\n"); void *pclient = NULL; int res = 0; int rpc_res = 0; int loop_cnt = 0; iotx_mqtt_param_t mqtt_params; HAL_GetProductKey(DEMO_PRODUCT_KEY); HAL_GetDeviceName(DEMO_DEVICE_NAME); HAL_GetDeviceSecret(DEMO_DEVICE_SECRET); memset(&mqtt_params, 0x0, sizeof(mqtt_params)); mqtt_params.handle_event.h_fp = example_event_handle; pclient = IOT_MQTT_Construct(&mqtt_params); if (NULL == pclient) { cm_printf("[ALIYUN]: MQTT construct failed\n"); return -1; } res = example_subscribe(pclient); //調(diào)用example_subscribe函數(shù) if (res < 0) { IOT_MQTT_Destroy(&pclient); return -1; } while (1) { if (0 == loop_cnt % 20) { example_publish(pclient); } IOT_MQTT_Yield(pclient, 200); loop_cnt += 1; if(loop_cnt >= 100) { //break; } } cm_printf("[ALIYUN]: aliyun demo end\n"); }
example_subscribe函數(shù):
example_subscribe一開(kāi)始進(jìn)行字符串連接,把${YourProductKey}/${YourDeviceName}/user/get拼接出來(lái),然后調(diào)用IOT_MQTT_Subscribe函數(shù)訂閱topic
int example_subscribe(void *handle){ int res = 0; const char *fmt = "/%s/%s/user/get"; //訂閱的MQTT路徑 char *topic = NULL; int topic_len = 0; 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); //通過(guò)HAL_Snprintf函數(shù)的拼接得出最終的MQTT路徑 //示例:${YourProductKey}/${YourDeviceName}/user/get //其中的:example_message_arrive函數(shù)為接收的數(shù)據(jù)的回調(diào)函數(shù) res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS0, example_message_arrive, NULL); if (res < 0) { cm_printf("[ALIYUN]: subscribe failed\n"); HAL_Free(topic); return -1; } HAL_Free(topic); return 0; }
example_message_arrive函數(shù)
example_subscribe為topic接收回調(diào)函數(shù),當(dāng)接收到此topic的數(shù)據(jù)時(shí)會(huì)放到這里處理
void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg){ iotx_mqtt_topic_info_t *topic_info = (iotx_mqtt_topic_info_pt) msg->msg; cm_printf("example_message_arrive \n"); switch (msg->event_type) { case IOTX_MQTT_EVENT_PUBLISH_RECEIVED: /* print topic name and topic message */ cm_printf("[ALIYUN]: Message Arrived:"); cm_printf("Topic : %.*s", topic_info->topic_len, topic_info->ptopic); cm_printf("Payload: %.*s", topic_info->payload_len, topic_info->payload); cm_printf("\n"); // topic_info->payload為接收到的數(shù)據(jù) if(strcmp(topic_info->payload,"1") == 0){ cm_printf("開(kāi)燈\n"); cm_gpio_write(21,CM_GPIO_LOW); }else{ cm_printf("關(guān)燈\n"); cm_gpio_write(21,CM_GPIO_HIGH); } break; default: break; } }
前端通過(guò)傳入lightState來(lái)控制GPIO21是高電平還是低電平
@GetMapping(path="hello") public WebResult setGPIOState(int lightState){ // XXXXXX:ProductKey // YYYYYY: 設(shè)備名稱 PubRequest request = new PubRequest(); request.setProductKey("XXXXXXXXXXXX"); request.setMessageContent(Base64.encodeBase64String((Integer.toString(lightState)).getBytes())); request.setTopicFullName("/XXXXXXXXXX/YYYYYYYYYYY/user/get"); request.setQos(0); //目前支持QoS0和QoS1。 try { PubResponse response = defaultAcsClient.getAcsResponse(request); //System.out.println(response.getSuccess()); //System.out.println(response.getErrorMessage()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } return WebResult.success(1,"hello"); }
<div class="my-2 my-tab"> <v-btn small @click="setLightState(1)">燈光開(kāi)</v-btn> <v-btn small @click="setLightState(0)">燈光關(guān)</v-btn> </div>
methods:{ setLightState(state){ console.log(state); // 開(kāi)關(guān)燈 axios({ method: 'get', url: "/iot/hello", params: { 'lightState':state } }).then(res => { console.log(res) }) }, }
到此,相信大家對(duì)“MQTT如何連接阿里云訂閱主題”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
當(dāng)前名稱:MQTT如何連接阿里云訂閱主題
標(biāo)題路徑:http://bm7419.com/article8/pphcop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、域名注冊(cè)、自適應(yīng)網(wǎng)站、微信小程序、網(wǎng)站制作、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)