MQTT Protocol:2 Uploading Data Points to the OneNET Platform ============================================================ 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 previous tutorial first. This tutorial will teach you how to create a new data stream on the OneNET platform and upload data points using the publish message format in the MQTT protocol. Creating a New 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 previous tutorial 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/mpython-s3/_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 them in, click [Add]. .. image:: /_static/products/mpython-s3/_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 data stream information just added. .. image:: /_static/products/mpython-s3/_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 uploading data points, you need to refer to the protocol rule description. You can download it from the OneNET platform document center. The OneNET platform document center address is: https://open.iot.10086.cn/doc/art431.html#118. Find [1.1 Documentation] and download the Device Terminal Access Protocol-MQTT in the MQTT project. 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/mpython-s3/_static/image/classic/oneNet_12.png .. image:: /_static/products/mpython-s3/_static/image/classic/oneNet_13.png .. image:: /_static/products/mpython-s3/_static/image/classic/oneNet_14.png Our example uses the JSON format of data type 1 (type == 1). We define a pubdata method whose function is to combine the message 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 message 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 # The MQTT server address domain is 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)) # publish message uploads data points 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 Demonstration ++++++++++++++++++++ Restart the mPython Board to run the program, and upload the sound and light data streams to OneNET every 1 second. .. image:: /_static/products/mpython-s3/_static/image/classic/oneNet_16.png The device indicator light showing green indicates a successful connection. Select [Data Display] to view the data stream reception. .. image:: /_static/products/mpython-s3/_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/mpython-s3/_static/image/classic/oneNet_15.gif