[STM32 x ESP8266] Connect to Alibaba Cloud MQTT server (message connection)

I won’t write out how to create an MQTT server in Alibaba Cloud, you can go to Baidu (mainly because I forgot, it’s not easy to demonstrate).
Some identifications of MQTT messages have been mentioned in my previous article . If you are interested, you can go and have a look ( the content explained in the code part is also there , so I won’t go into details here, mainly about how to connect
) In a nutshell, if some students can't find a place where they can create an Alibaba Cloud MQTT server, check whether East China 2 (Shanghai) is not selected in the region .

insert image description here

1. Obtain the connection parameters of the Alibaba Cloud MQTT server

The following connection method is connected by referring to the manual of Alibaba Cloud . Interested students can take a look at it at the same time.

1. After creating a product, create two devices (to be used in the demonstration later)

insert image description here
insert image description here

2. Enter a random device first, here is the test device. Click to view DeviceSecret.

insert image description here

3. Copy the contents of the triplet to this location of the mqtt_config.h file.

insert image description here

4. Click to view the MQTT connection parameters, just copy mqttHostUrl .

insert image description here
insert image description here

5. The password uses the password generation tool provided by Alibaba Cloud . After the download and decompression is complete, click sign.html , fill in the corresponding device information, and click Generate. Copy the generated password into Password .

insert image description here

At this point, the sample code can connect to the Alibaba Cloud MQTT server.

insert image description here

2. Understand the topics on the Alibaba Cloud MQTT server

The topic introduction of Alibaba Cloud is introduced in the official Alibaba Cloud manual , and interested students can go to learn about it.

insert image description here

These topics can be viewed in the product's Topic class list.

insert image description here

3. How to display data on the Alibaba Cloud MQTT server

1. Click the function definition of the product. Click Edit Draft.

insert image description here

2. Click Add Custom Function. Here it is intended to display a change in temperature. After adding, remember to click Publish to go online!

insert image description here
insert image description here
insert image description here

3. By consulting the official manual/sys/${productKey}/${deviceName}/thing/event/property/post , I learned that it is necessary to send data in JSON format to Topic:. The manual explains what content can be added to the JSON format data. An example is also given, the most important of which is the params parameter, and the name of the sub-parameter in this field is the identifier temp filled in earlier .

insert image description here
insert image description here

4. In the sample code, a message with a temperature value of 6 is sent to the server.

insert image description here

5. After running the code, the temperature value can be observed in the physical model data of the test device.

insert image description here
insert image description here

4. How to achieve communication between two devices

Under normal circumstances, our more common requirement is that after a device publishes a topic A carrying information like other MQTT servers, another device subscribing to the topic A can receive the message, but in the Alibaba Cloud MQTT server But it can't be like this, often the message sent to topic A is not sent to the device subscribed to A.
If you want to realize this function, you need to use another function of Alibaba Cloud: cloud product circulation.

1. To realize this function, two devices must be prepared first. A test device has been created before, and now a mqtt device is created. And customize two themes.

insert image description here
insert image description here

2. Click Cloud Product Transfer in the rule engine to switch to the old version.

insert image description here

3. Click Create Rule.

insert image description here

4. Go to the newly created rules. Click Write SQL.

insert image description here
insert image description here
The function of this SQL statement is to monitor the published topics of the device named mqtt in the product Ali mqtt /h18lpJMiSVO/mqtt/user/123, and then extract all the published messages in it .
① * means to extract all messages
② Select a custom topic
③ Select a device in the Ali mqtt product
④ Select the device named mqtt
⑤ Select the topic published by this device ( /h18lpJMiSVO/mqtt/user/123)

Vernacular:
Monitor the topic published by the device named mqtt /h18lpJMiSVO/mqtt/user/123
Suppose the message published by the device is: {"data": 123}
Execute the above configuration to {"data": 123}extract (Note: The format of the sent data must be in JSON format, and the format must be correct)

5. Now that the message is extracted, the server needs to forward it to another device: test. Click Add Action in Forwarding Data.

insert image description here
insert image description here
It is to {"data": 123}forward to /h18lpJMiSVO/test/user/abcthis topic
① Select Custom
② Select a product created by yourself
③ Select the device named test
④ Select a topic to be forwarded to this device to subscribe

6. Startup rules

insert image description here

7. Use MQTT.fx to log in to the mqtt device ( MQTT.fx connection document )

Here I use the newer version of Alibaba Cloud's web page for calculating passwords , which will be faster for connecting to MQTT.fx.

insert image description here
insert image description here

8. Send data to /h18lpJMiSVO/mqtt/user/123the topic {"data":123}, and subscribe to /h18lpJMiSVO/test/user/abcthe topic .

insert image description here
insert image description here
insert image description here

9. There are other interesting things about this data forwarding content. If you are interested, you can watch the article written by this big guy . The data forwarding part is in the second half of the article.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_48896613/article/details/127679452