Uploading Data Points to the OneNET Platform via MQTT Protocol:2 ================================================================ In the previous tutorial, we learned how to use the MQTT protocol to access the OneNET platform and obtain the commands issued by the platform to achieve remote control. If you have not been exposed to the MQTT protocol and the OneNET platform before, it is recommended to study the tutorial in the previous chapter first. In this tutorial, we will learn how to create a data stream on the OneNET platform and upload data points using the publish packet format in the MQTT protocol. Creating a Data Stream on the OneNET Platform +++++++++++++++++++++++++++++++++++++++++++++ OneNET platform official website: https://open.iot.10086.cn/. If you have not used this platform before, you can refer to the tutorial in the previous chapter to add products and devices first. Before reporting data points, we need to add a data stream template on the OneNET cloud platform. The steps are as follows: 1. Enter the [Device Management] page of the [Developer Center], select [Data Stream Template] in the left menu bar, and click [Add Data Stream]. .. image:: /_static/products/ledong-v2/_static/image/classic/oneNet_9.png 2. A dialog box for adding a data stream will pop up. The name is a required field, and the others are optional. After filling in, click [Add]. .. image:: /_static/products/ledong-v2/_static/image/classic/oneNet_10.png 3. After successful addition, the dialog box will close automatically. The data stream display list will automatically add the newly added data stream information. .. image:: /_static/products/ledong-v2/_static/image/classic/oneNet_11.png In this example, we need to add two data streams, sound and light, as shown above. Uploading Data Points to the OneNET Platform ++++++++++++++++++++++++++++++++++++++++++++ OneNET Data Point Reporting Format `````````````````````````````````` When transmitting data points, you need to refer to the protocol rule description. You can download it from the OneNET platform documentation center. OneNET platform documentation center address: https://open.iot.10086.cn/doc/art431.html#118. Find [1.1 Documentation] and find the Device Terminal Access Protocol-MQTT under the MQTT project to download. After opening it, find [5.2 Message Publishing]-[5.2.1 Data Point Reporting]. **The data point reporting format is as follows:** .. image:: /_static/products/ledong-v2/_static/image/classic/oneNet_12.png .. image:: /_static/products/ledong-v2/_static/image/classic/oneNet_13.png .. image:: /_static/products/ledong-v2/_static/image/classic/oneNet_14.png Our example uses the JSON format of data type 1 (type == 1). We define a custom pubdata method. The function of this method is to combine into the packet format required by the protocol:: def pubdata(data): j_d = json.dumps(data) j_l = len(j_d) arr = bytearray(j_l + 3) arr[0] = 1 # publish data type is json arr[1] = int(j_l / 256) # json data length high byte arr[2] = j_l % 256 # json data length low byte arr[3:] = j_d.encode('ascii') # json data return arr The device uses the publish packet to upload data points. $dp is the system command for uploading data points (a 2-byte string):: c.publish('$dp',pubdata(message)) Complete program example:: from umqtt.simple import MQTTClient from mpython import * from machine import Timer import json # MQTT server address domain: 183.230.40.39, unchanged SERVER = "183.230.40.39" # Device ID CLIENT_ID = "deviceID" # Product ID username='productID' # Product APIKey: password='APIKey' mywifi=wifi() message = {'datastreams':[ { 'id':'sound', 'datapoints':[{'value':0}] }, { 'id':'light', 'datapoints':[{'value':0}] } ]} tim1 = Timer(1) # Create a timer def pubdata(data): j_d = json.dumps(data) j_l = len(j_d) arr = bytearray(j_l + 3) arr[0] = 1 # publish data type is json arr[1] = int(j_l / 256) # json data length high byte arr[2] = j_l % 256 # json data length low byte arr[3:] = j_d.encode('ascii') # json data return arr def publishSenser(): message['datastreams'][0]['datapoints'][0]['value']=sound.read() message['datastreams'][1]['datapoints'][0]['value']=light.read() c.publish('$dp',pubdata(message)) # Upload data points with the publish packet print('publish message:',message) mywifi.connectWiFi("ssid","password") c = MQTTClient(CLIENT_ID, SERVER,6002,username,password) c.connect() print("Connected to %s" % SERVER) tim1.init(period=1000, mode=Timer.PERIODIC, callback=lambda _:publishSenser()) # Upload data points every second Effect Display ++++++++++++++ Restart the mPython Board to run the program, uploading the sound and light data streams to OneNET every 1 second. .. image:: /_static/products/ledong-v2/_static/image/classic/oneNet_16.png The device indicator light showing green indicates that the connection is successful. Select [Data Display] to view the data stream reception. .. image:: /_static/products/ledong-v2/_static/image/classic/oneNet_17.png Create an application in [Application Management], add elements such as gauge charts or line charts in the application, and select the data stream to display in the properties. The final effect is as follows: .. image:: /_static/products/ledong-v2/_static/image/classic/oneNet_15.gif