Tutorial 4: using the Internet of things platform to control the hardware side

<Wumart intelligence >Catalogue of series articles

Tutorial 1: Alibaba cloud uses docker to quickly deploy the server
Tutorial 2: configuring the server development environment locally
Tutorial 3: hardware side - Introduction to Arduino and ESP8266 development board
Tutorial 4: using the Internet of things platform to control the hardware side
Tutorial 5: Internet of things platform user manual
Tutorial 6: Official SDK code function supplement
Tutorial 7: APP usage and scene linkage function
diy: self made four kinds of smart small appliances

preface

The previous chapter has briefly introduced the basic use methods of Arduino and Esp8266 development boards. This chapter will introduce how to use Wumart intelligent platform to control the hardware side from simple to deep

Note: the official code documents and platforms can be found in the gitee link below
https://gitee.com/kerwincui/wumei-smart.git

Hardware code address

https://gitee.com/kerwincui/wumei-smart/tree/master/sdk/arduino/Arduino-ESP8266

1, Device access is divided into two steps: device authentication and device interaction

1. Equipment certification

  • Encryption authentication (recommended)
  • Simple authentication
  • Other authentication methods supported by EMQX

2. Equipment interaction

  • Mqtt topics related to publication model, equipment information and clock synchronization
  • Mqtt topics related to subscription model, device upgrade and clock synchronization

2, Code burning

1. Open the sdk code pulled in the first step from Arduino

2. Modify helper The cpp file configures its own wifi password. After creating a new product on the server, obtain the product number, Mqtt account number, Mqtt password and product secret key from the product details and configure them into the following code.
3. Compile and burn. After success, check whether the wifi is connected normally. Open the serial port debugging information and check whether the development board works normally
4. Supplementary code
When we connect the development board and the serial port information prints the data normally, we will find that although we click the server button, the development board does not make the corresponding action. This is because in the sdk code, after obtaining the corresponding operation, we only print the information and do not make the corresponding action, as follows:

If you want the development board to make corresponding response information, you can supplement the corresponding code under each condition.

3, Equipment certification

1. Encryption authentication

The authentication method recommended by the system supports the disabled function of the device. Obtain the product number, Mqtt account number, Mqtt password and product secret key in the product details. The password is encrypted by AES through the product secret key and transmitted to the back end. The back end authenticates by decrypting the product secret key. To connect to the Mqtt message server, you need to provide a unique client ID, user name and password. The specific format is as follows:

# client ID 
clientId = deviceNumber & productId 

# user name
userName = wumei-smart

# password
password = mqtt password & userId & Expiration time

  • The client ID is equal to the equipment number + product number, which is connected with the symbol & and there is no space in the middle;
  • Enter the user name directly into the Mqtt account
  • The password is equal to Mqtt password + user ID + password expiration time, and then AES encryption is performed. The user ID is the ID of the login user, and the Admin account is 1. For security, the password expiration time should be within 24 hours, using the timestamp format, accurate to milliseconds.

Example of account configuration information:

clientId = "D6329VL54419L1Y0&2"
userName = "wumei-smart"
password = "/W2A/4MK+9cEGBhyBDgr2K5c62DAjAK4m0b5pvwxX6FFMzI3h1pUmaDY3BH1P2mI"

2. Get current time

To obtain the current time, you can call the NTP time interface of the system. When the interface requests, the current running milliseconds of the sending device are returned, and the sending time of the device, the receiving time of the server and the sending time of the server are returned. Then obtain the current running milliseconds of the device as the device receiving room. Finally, calculate the current time of the equipment with the formula. The time must be in mm.

# The deviceSendTime value is the number of milliseconds the device is currently running
http://localhost:8080/iot/tool/ntp?deviceSendTime=35768

# computing time
 Device current time = (Server receiving time + Server sending time + Equipment receiving time - Device sending time) / 2

3. AES encryption description

CBC encryption mode of AES is adopted, the offset is fixed as Wumei smart open 16 bits, and the output is Base64.

Encryption mode:  CBC
 fill    Charge:  pkcs5padding
 Data block: 128 bits
 Offset:  wumei-smart-open
 transport    Out:  base64

dense    Code: the product secret key of the corresponding system
 Encrypted content:  mqtt password & userId & expireTime

4, Device interaction

{productId} represents the product ID and {deviceNum} represents the device number. Obtain the product ID and device number through the web. If the device is automatically added, the device number can be customized or the device MAC address can be used. After the device authentication is successful, a corresponding device entity will be automatically added at the back end.
1. Subscribe to topics

2. Release theme

3. Data format (JSON format is used for equipment and system interaction)

  • Publish device information, corresponding subject: / info/post
# Description: release the equipment information after the equipment is powered on
# rssi equipment strength (excellent signal [- 55-0], good signal [- 70-55], general signal [- 85-70], signal difference [- 100-85])
# Status device status, fixed to 3, indicates online
# userId ID of the user
# firmwareVersion firmware version
{
     "rssi": -20,
     "firmwareVersion": 1.10,
     "status": 3,
     "userId": 1
}
  • Subscribe to OTA upgrade, corresponding topic: / ota/get
# Description: after subscribing to the device upgrade message, Http requests to download firmware and upgrade according to the version number

{
    "version": 1.1
}
  • Subscribe to real-time monitoring, corresponding topic: / monitor/get
# Description: subscribe to real-time monitoring messages and publish real-time monitoring data according to quantity and interval
# count quantity
# Interval interval, in milliseconds

{
    "count": 60,
    "interval": 1000
}
  • Publish real-time monitoring, corresponding topic: / monitor/post
# Description: publishes monitoring data of specified quantity and interval according to the subscribed real-time monitoring messages
# id identifier, real-time monitoring is an attribute in the object model. View the identifier in the product details and the corresponding id value
# The value collected by the value device can only be an integer or decimal
# remark optional. It can be blank or use the current time of the device

[{
    "id": "temperature",
    "value": "27.43",
    "remark": ""
}, {
    "id": "humidity",
    "value": "32.18",
    "remark": ""
}]
  • Publish clock synchronization, corresponding topic: / ntp/post
# Description: optional. Publish the clock synchronization message. The server issues the clock synchronization message after subscribing to it
# deviceSendTime device send time

{
    "deviceSendTime": "1592361428000"
}

  • Subscription clock synchronization, corresponding topic: / ntp/get
# Description: optional. Subscribe to the clock synchronization message and calculate the current time = (server receiving time + server sending time + device receiving time - device sending time) / 2
# deviceSendTime device send time
# serverRecvTime server receiving time
# serverSendTime server sending time

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

  • Publish properties / functions / events, corresponding topics: / property/post, / function/post, / event/post
# Description: attributes, functions and events belong to the object model, and the definition of Jason is the same` If the value of value ` is Boolean, the value is "0" or "1", which represents on / off; The enumeration type corresponds to the key value of the enumeration item (for example, "1", representing medium speed gear); Array types are strings separated by English commas.
# id identifier: the identifier of the corresponding object model viewed in the product details
# Value corresponds to the value defined in the object model
# remark: optional. Note information can be viewed in the device log information

[{
    "id": "gear",
    "value": "1",
    "remark": "Remark information"
} {
    "id": "switch",
    "value": "0",
    "remark": "Remark information"
}]

  • Subscribe to properties / functions, corresponding topics: / property/get, / function/get, property online / get, / function online / get
# Description: attributes, functions and events all belong to the object model. The definition of Json is the same. The subscribed message has no 'remark' comment information. The online mode and normal mode of attributes and functions are used to distinguish different types of messages, but the processing of the device is the same. For example, all subscribe to the message on switch, and the processing of the device is to turn on the switch.   
# id identifier: the identifier of the corresponding object model viewed in the product details
# Value corresponds to the value defined in the view object model, as above

[{
    "id": "gear",
    "value": "1"
} {
    "id": "switch",
    "value": "0"
}]

(for more information, please check Wumart's official website: http://wumei.live/)

Posted by itaym02 on Mon, 18 Apr 2022 09:42:27 +0930