Connect to Alibaba Cloud to obtain network time based on STM32+MQTT


Note: This time obtaining the network time from Alibaba Cloud is based on the previous chapter (connecting to Alibaba Cloud based on STM32+FreeRtos+DHT12+ESP8266+MQTT (link: link .) ) .

1. Process description

Insert image description here

2. Process analysis

2.1. Connect to the router

First connect to the router (wifi), and after getting the network status, connect to the server.
Insert image description here

2.2.Connect to the server

After the network connection is successful, connect to the Alibaba Cloud platform server and get the connection receipt.
Insert image description here

2.3. Subscribe to NTP clock synchronization response

After connecting to the server, we start subscribing to the NTP clock synchronization response for later issuing NTP clock synchronization requests and obtaining timestamps.
Insert image description here

2.4. Publish NTP clock synchronization request

After subscribing to the NTP clock synchronization response, we start to publish the NTP clock synchronization request and wait for the server to return a timestamp.
Insert image description here

2.5. Server returns data

Receive data returned by the server:

{
    
    "deviceSendTime":"1592361428000","serverSendTime":"1645599995121","serverRecvTime":"1645599995121"}

Insert image description here
Then parse the data and get the timestamp returned by the service.

Insert image description here

2.6. Convert network timestamp to Beijing time (note time zone: 8)

Insert image description here

3. The actual picture is as follows

Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/qq_39721016/article/details/123090137