Alibaba Cloud usage records

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:
insert image description here
You need to log in here, just go in
insert image description here
here I only use the part of the Internet of Things, and you can find the corresponding block directly in it,
insert image description here
as shown below, you can find what I need The blocks
insert image description here
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
insert image description here
most common page, as shown below :
insert image description here

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 :
insert image description here
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.
insert image description here
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
insert image description here
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
insert image description here
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
insert image description here
: 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
insert image description here
header, the following is the part of the protocol name
insert image description here
insert image description here
byte7 refers to the protocol level
insert image description here
byte8 refers to the connection flag
insert image description here
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.
insert image description here
The overall effect of a variable header is as follows:
insert image description here
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:
insert image description here
Click to enter and you can see some SDK options. Here are mainly LInux-related SDKs, as shown below:
insert image description here
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.
insert image description here
After selecting here, you can directly generate it. Generally, there is no special requirement. It is enough to select the object model here, and
insert image description here
then click to download the content.
insert image description here

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
insert image description here
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:
insert image description here
Use ping to ping: you
insert image description here
can fill in at the end, here I tried to fill in the ip address directly, but it has no effect
insert image description here
Here you need to choose encryption, the selection is as follows:
insert image description here
Then click to connect.
insert image description here
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.
insert image description here
Entering online operation and maintenance, you can issue commands to the device.
insert image description here
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
insert image description here
subscribed to three, which are setting parameters and request parameters , and requests to reply to these three topics.
insert image description here
Let’s first set the parameters in the cloud:
insert image description here
Receive the message as follows:
insert image description here
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
insert image description here
to On another page, you can see that our published message has received the returned data from the subscription side.
insert image description here
There is a reply to the returned message here.
insert image description 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:
insert image description here
Click Edit Draft to see the addition of our own objects Model
insert image description here
Select the corresponding object model type to add.
insert image description here
For example, I added a new attribute here, a service, as shown below.
insert image description here
Click the object model stl to directly export the current object model message, and open it as a large json format content:
insert image description here
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:
insert image description here
You can see that it is actually realized according to our object model, and what is in the object model is what it is.
insert image description here
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
insert image description here
above
insert image description here
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
insert image description here
select After specifying the function that needs to be called,
insert image description here
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,
insert image description here
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:
insert image description here
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 )
insert image description here
The topic we subscribed to can also be received on the device side, and the content is completely consistent.
insert image description here
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:
insert image description here
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
insert image description here
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 )
insert image description here
Here you can generate it according to the official instructions
insert image description here
. Import the SDK into our LINUX system:
insert image description here
modify the parameters and compile it later, as follows: ( The information here can directly use the information exported from the platform )
insert image description here
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.
insert image description here
If you need to publish a topic here, you should pay attention to the topic list
insert image description here
below. Subscribe and report. The following figure shows the parameter settings for subscription and report.
insert image description here
Let’s test it After a while, you can see that the device attribute report is OK!
insert image description here
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
insert image description here
. Let’s take a look at the callback function of the attribute command reply. Let the cloud send a command
insert image description here
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,
insert image description here
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:
insert image description here
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:

Guess you like

Origin blog.csdn.net/m0_51220742/article/details/126686920