Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect MQTT to Aliyun subscription topic

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to connect MQTT to Aliyun subscription topic". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how MQTT connects to Aliyun subscription topics".

Move 4G module-ML302-OpenCpu development-(MQTT connection Aliyun-subscribe topic) module OpenCpu part

Cm_main_task in the cm_main.c file is the main function, and the cm_test_aliyun function is called in the main function.

Cm_test_aliyun function:

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); / / call example_subscribe function 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 >

) {/ / break;}} cm_printf ("[ALIYUN]: aliyun demo end\ n");}

Example_subscribe function:

Example_subscribe starts with string concatenation, splices ${YourProductKey} / ${YourDeviceName} / user/get, and then calls the IOT_MQTT_Subscribe function to subscribe to topic

Int example_subscribe (void * handle) {int res = 0; const char * fmt = "/% s/%s/user/get"; / / MQTT path for subscription 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) / / the final MQTT path is obtained by stitching the HAL_Snprintf function / / example: ${YourProductKey} / ${YourDeviceName} / user/get / / where: the example_message_arrive function is the callback function res = IOT_MQTT_Subscribe (handle, topic, IOTX_MQTT_QOS0, example_message_arrive, NULL) of the received data; if (res)

< 0) { cm_printf("[ALIYUN]: subscribe failed\n"); HAL_Free(topic); return -1; } HAL_Free(topic); return 0;} example_message_arrive函数 example_subscribe为topic接收回调函数,当接收到此topic的数据时会放到这里处理 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 is the received data if (strcmp (topic_info- > payload, "1") = = 0) {cm_printf ("turn on the light\ n") Cm_gpio_write (21 CMM GPIOLOW);} else {cm_printf ("lights off\ n"); cm_gpio_write (21 CMM GPIOL high);} break; default: break;}} server part

The front end controls whether the GPIO21 is high or low by inputting lightState

GetMapping (path= "hello") public WebResult setGPIOState (int lightState) {/ / XXXXXX:ProductKey / / YYYYYY: device name 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 and QoS1 are currently supported. 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") } Front-end web page part

Methods: {setLightState (state) {console.log (state) / / axios ({method: 'get', url: "/ iot/hello", params: {' lightState':state}) .then (res = > {console.log (res)})} } so far I believe you have a deeper understanding of "how MQTT connects to Aliyun subscription topics". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report