Article directory
1. Background description
Recently, I happened to do some cloud-related work, mainly related to mqtt, and I happened to see the Alibaba Cloud platform. After using it, I feel that the platform is very powerful and the information is also very rich. I would like to record it here, maybe I will continue to use it in the future. .
The entrance is as follows, you can see it directly by typing in the search box:
You need to log in here, just go in
here I only use the part of the Internet of Things, and you can find the corresponding block directly in it,
as shown below, you can find what I need The blocks
here are divided into public instances and enterprise instances. If you just want to experience it, you can use public instances. There is no need to use enterprise instances. After all, enterprise instances need to pay and click to enter the
most common page, as shown below :
2. MQTT protocol summary record
I will not explain the specific content of MQTT. I have actually written it in the previous article. Here I mainly talk about some new understandings.
Because I found out that this cloud needs to be implemented through the MQTT protocol before I knew it, so I read some documents about the MQTT protocol in the early stage. By the way, I will also summarize some of the documents I read earlier, mainly a control message . , the format of the control message is as follows :
the following is the remaining data length, the official definition is: the remaining length (Remaining Length) indicates the number of bytes in the remaining part of the current message, including variable header and payload data. The remaining length does not include the number of bytes used to encode the remaining length field itself.
The remaining length field uses a variable-length encoding scheme, and for values less than 128 it uses a single-byte encoding. Larger values are handled as follows. The lower 7 significant bits are used to encode the data, and the most significant bits are used to indicate if there are more bytes . So each byte can encode 128 values and a continuation bit. The maximum remaining length field is 4 bytes. (That is to say, the maximum remaining length is byte2, byte3, byte4, byte5)
What needs to be noted here is that the maximum field is : 256MB size control message, the first one is up to 128, and the second byte is about to start, so the representation of this value in the message is: 0xFF, 0xFF, 0xFF, 0x7F.
The composition of the control message is: fixed header + variable header + payload
Not all variable headers are available. According to the official description, its type is: The
control message that requires this identifier is as follows: ( Note: This is only required when the quality level is greater than 0, and it is not needed at other times The following
is the payload, which can also be called the data part, which is generally data in json format. This piece is added at the end of the message. Of course, not all messages require payload. The requirements are as follows
: A little more detailed description of each part, first the connection part:
CONNECT - connect to the server
Fixed header part: Mainly add a variable length
header, the following is the part of the protocol name
byte7 refers to the protocol level
byte8 refers to the connection flag
The corresponding meanings of the flags are:
- clean session
- testament sign
- Will quality
- will reservation
- password flag
- username logo
byte9 and byte10 are to keep the connection.
The overall effect of a variable header is as follows:
CONNACK – confirm the connection request
3. Some encryption method records
Here are mainly some encryption methods used by Alibaba Cloud during the transmission process, because they should also be used in the projects I do, and I will record some of my processes.
4. Alibaba Cloud Mechanism
Alibaba Cloud provides more detailed documents for easy access to usage methods. The location is in the bottom column of the product, as shown below:
Click to enter and you can see some SDK options. Here are mainly LInux-related SDKs, as shown below:
Generally, you can enter the SDK customization to view. There are many functions here. For example, the device OS is the device selection. Generally, Linux or other solutions are selected, and the communication method can also be selected. If you choose a single board, you can use Raspberry Pi, or use MCU+WIFI The module solution is also possible, and the estimated resource consumption is also marked on the right side, which makes it easier for us to choose models.
After selecting here, you can directly generate it. Generally, there is no special requirement. It is enough to select the object model here, and
then click to download the content.
5. Use MQTTFX connection record
The official description is as follows: https://help.aliyun.com/document_detail/140507.html
The one used here is mqttfx1.7.1. Now the official website seems to have taken this off the shelves, but you can find some previous ones online. The main reason is that the current version seems to be charged, so I will use this older version first.
First of all, establish a connection, which is also very convenient. The device page provides the parameters required for the connection, as shown below: The options on the
mqtt connection page are as follows: Here is a server address, you can write the domain name or directly use the ip address, domain name The conversion with the ip address only needs to be pinged, as shown below:
Use ping to ping: you
can fill in at the end, here I tried to fill in the ip address directly, but it has no effect
Here you need to choose encryption, the selection is as follows:
Then click to connect.
This mqttfx page mainly pays attention to three pieces of content, publishing, subscription and logs. I haven’t used it in other places, and it’s not very clear.
Entering online operation and maintenance, you can issue commands to the device.
Let’s subscribe to this in advance. The topic can receive the message sent to us by the server. The relevant parameters of the subscription are as follows. Here we only need to replace the device name inside with our own device name. Here I have
subscribed to three, which are setting parameters and request parameters , and requests to reply to these three topics.
Let’s first set the parameters in the cloud:
Receive the message as follows:
Check the specific content as follows:
{
"method": "thing.service.property.set",
"id": "1644637829",
"params": {
"StreamVideoQuality": 1,
"MotionDetectSensitivity": 1,
"ImageFlipState": 1,
"SubStreamVideoQuality": 1,
"test111": 2,
"AlarmSwitch": 1,
"AlarmFrequencyLevel": 1
},
"version": "1.0.0"
}
Let's continue to look at publishing news, because we have already subscribed before, there is no need to subscribe here, just go to the publishing page to publish the news, fill in the subject and content we want to publish, as shown below: Go
to On another page, you can see that our published message has received the returned data from the subscription side.
There is a reply to the returned message here.
The specific content is as follows:
{
"code" : 200,
"data" : { },
"id" : "1662469292",
"message" : "success",
"method" : "thing.event.property.post",
"version" : "1.0"
}
It can be seen that the returned data has been received correctly ( it should be noted that there is a timestamp in this that needs to be modified, and the sent timestamp must be greater than the current timestamp, so that’s fine )
The above part has actually realized the up and down distribution of events and attributes ( Properties and events ). At the same time, we can add object models to view information. The process of adding is as follows:
Click Edit Draft to see the addition of our own objects Model
Select the corresponding object model type to add.
For example, I added a new attribute here, a service, as shown below.
Click the object model stl to directly export the current object model message, and open it as a large json format content:
But just looking at this is not very clear, because the amount of data is relatively large, let’s use a special json tool to check the specific data content. Here is the link of the json tool I use: https://www.json.cn /
Compress all the parts, and the data is as follows:
You can see that it is actually realized according to our object model, and what is in the object model is what it is.
But he still has some mechanisms here, that is, this is not pure object model data, but also some mechanisms for responding and reporting errors, such as opening an attribute to check: some of the constraints are as follows: Through the
above
process Now that we have a basic understanding of the composition of this object model, we can also set up a service below, mainly to set up a service to see how it is implemented. You need to enter a page for service calls for device debugging: here first
select After specifying the function that needs to be called,
look up the rules of this module in the object model, and you can see the identifier that I need to call here and the related constraints of the identifier, as shown in the figure below: Therefore,
a call can be set for the identifier The service content:
{
"Scheme":0,
"EncryptKey":"222222",
"EncryptType":23,
"PreTime":50,
"PushUrl":"111111",
"StreamType":1
}
At the same time, subscribe to this topic on the mqttfx side, as follows:
send a message, you can check whether the message is sent successfully ( if the message is not sent successfully, it may be because the json format is not correct, you need to check the format yourself, including our own Is this data out of range or something )
The topic we subscribed to can also be received on the device side, and the content is completely consistent.
So far, all types of data interaction between the device side and the cloud have been completed.
6. SDK usage records
Here we first need to download the SDK, as shown below:
This contains all the documents, I think there are two places that can be referred to, as shown below, the quick access is convenient for us to experience it quickly, and the specifics depend on the device access Documentation
Let’s take a look at the content released by the quick access portal first, and generate an SDK here when the device is connected, as shown below: ( there are many types of SDKs, here choose the LINUX c version )
Here you can generate it according to the official instructions
. Import the SDK into our LINUX system:
modify the parameters and compile it later, as follows: ( The information here can directly use the information exported from the platform )
The connection process is as follows, you can see that the first is to establish a TCP connection, and then TLS encryption Verify, and then establish an MQTT connection. After the connection is established, the device subscribes to the corresponding topic.
If you need to publish a topic here, you should pay attention to the topic list
below. Subscribe and report. The following figure shows the parameter settings for subscription and report.
Let’s test it After a while, you can see that the device attribute report is OK!
Other related reporting is also relatively simple. Basically, it is the same operation as using MQTTFX, just subscribe, and then check the message. Here is a look at the operation of receiving data returned from the cloud. Here, the SDK is implemented using a callback function. The callback function It can be obtained directly through comments on the source code
. Let’s take a look at the callback function of the attribute command reply. Let the cloud send a command
to see the trigger callback function. The callback function only executes the function of printing the content, and other related callbacks are also The same,
I have to say one thing, I feel that his SDK is very complicated, maybe I am too good at it, it is difficult to analyze the source code!
7. Cloud SDK usage
First of all, two libraries are needed. The installation of these two libraries requires python3.6 version and above, and then we first need to update a pip
sudo pip3 install aliyun-python-sdk-core
sudo pip3 install aliyun-python-sdk-iot
The command to update pip is as follows:
The link given by the official document is as follows : https://help.aliyun.com/document_detail/42700.htm?spm=a2c4g.11186623.0.0.7c023a5eGVRkN6#reference-qdh-ywb-zdb
Some connection tests can be done using the official documentation: