Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to connect MQTT to Ali Cloud to receive and send data

Shulou Source: shulou.com Published: 2022-06-01 17:16:03 11月10日 Update

This article mainly explains "how to connect MQTT to Ali Cloud to receive and send data". 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 to connect MQTT to Aliyun to receive and send data.

Shift 4G module-ML302-OpenCpu development-(MQTT connect Aliyun-receive and send data)

According to the previous study, if you haven't seen it, you can take a look at the above 2.1C 4G module-ML302-OpenCpu Development-(MQTT connection Aliyun-subscription topic)

The data can be received, so how to send it?

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!\ "}" / / * splicing of 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); / / * stitching of topic * * / call IOT_MQTT_Publish_Simple to send data 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 function

Topic_name:topic name

Qos:qos

Data: data

Len: length of data sent

Int IOT_MQTT_Publish_Simple (void * handle, const char * topic_name, int qos, void * data, int len)

This is a relatively simple MQTT sending program

At this point, I believe you have a deeper understanding of "how to connect MQTT to Ali Cloud to receive and send data". 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!

Tags: Data Ali learning content modules development practical deeper themes interests functions names practicality practicality simplicity of operation methods more friends programs websites Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology Apple OPPO Reno Xiaomi