By GXIC wongxmig Finish, welcome to follow IoT developer community.
1. Preparation
1.1 register alicloud account
Use personal Taobao account or cell phone number to open the account of Ali cloud, and pass the real name authentication (can be certified by Alipay).
1.2 free IoT Suite
Product official website https://www.aliyun.com/product/iot
1.3 software environment
__python2 installation: https://www.python.org/downloads/
Editor sublimeText/nodepad++/vscode
2. Development steps
2.1 cloud development
1) create advanced products
2) function definition, adding attributes to product model
Add product attribute definition
Attribute name | identifier | data type | Range |
---|---|---|---|
temperature | temperature | float | -50~100 |
humidity | humidity | float | 0~100 |
Object model corresponding attribute report topic
/sys / replace with productKey / replace with deviceName/thing/event/property/post
Attribute corresponding to object model reported to payload
{ id: 123452452, params: { temperature: 26.2, humidity: 60.4 }, method: "thing.event.property.post" }
3) device management > register devices to obtain identity triples
2.2 equipment end development
We use the python2 program to simulate the device, establish the connection and report the data.
1. Create the aliyun IOT demo Python folder 2. pip install aliyun-python-sdk-iot-client 3. Create the thermometer.py file and add content
2) download and install SDK
In the aliyun IOT demo Python folder, execute the command
$ pip install aliyun-python-sdk-iot-client
3) application directory structure
4) code of thermometer.js
# -*- coding: utf-8 -*- import aliyunsdkiotclient.AliyunIotMqttClient as iot import json import multiprocessing import time import random options = { 'productKey':'Your productKey', 'deviceName':'Your deviceName', 'deviceSecret':'Your deviceSecret', 'port':1883, 'host':'iot-as-mqtt.cn-shanghai.aliyuncs.com' } host = options['productKey'] + '.' + options['host'] # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): #topic = '/' + productKey + '/' + deviceName + '/update' print(msg.payload) def on_connect(client, userdata, flags_dict, rc): print("Connected with result code " + str(rc)) def on_disconnect(client, userdata, flags_dict, rc): print("Disconnected.") def worker(client): topic = '/sys/'+options['productKey']+'/'+options['deviceName']+'/thing/event/property/post' while True: time.sleep(5) payload_json = { 'id': int(time.time()), 'params': { 'temperature': random.randint(20, 30), 'humidity': random.randint(40, 50) }, 'method': "thing.event.property.post" } print('send data to iot server: ' + str(payload_json)) client.publish(topic, payload=str(payload_json)) if __name__ == '__main__': client = iot.getAliyunIotMqttClient(options['productKey'], options['deviceName'], options['deviceSecret'], secure_mode=3) client.on_connect = on_connect client.connect(host=host, port=options['port'], keepalive=60) p = multiprocessing.Process(target=worker, args=(client,)) p.start() client.loop_forever()
3. Start operation
3.1 equipment startup
$ python thermometer.py