Ali IoT
Example
from m5stack import *
from m5ui import *
from uiflow import *
from IoTcloud.Ali import AliIoT
import json
import time
import unit
setScreenColor(0x222222)
env2_0 = unit.get(unit.ENV2, unit.PORTA)
shadow_msg = None
raw_msg = None
user_msg = None
label0 = M5TextBox(16, 17, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
def shadow_get_cb(payload):
global shadow_msg, raw_msg, user_msg
shadow_msg = payload
label0.setText(str(shadow_msg))
pass
def raw_down_cb(payload):
global shadow_msg, raw_msg, user_msg
raw_msg = payload
label0.setText(str(raw_msg))
pass
def user_get_cb(payload):
global shadow_msg, raw_msg, user_msg
user_msg = payload
label0.setText(str(user_msg))
pass
ali = AliIoT(device_id='223', product_key='a1kJNBURsxj', device_name='ENV_UNIT', region_id='cn-shanghai', password='CB57870ED7708E24863DF9A7BD8A65BE')
ali.subscribe_shadow_get_msg(shadow_get_cb)
ali.subscribe_raw_down_msg(raw_down_cb)
ali.subscribe_user_get_msg(user_get_cb)
ali.start()
while True:
ali.publish_user_update_msg(str((json.dumps(({'Pressure':(env2_0.pressure),'Temperature':(env2_0.temperature),'Humidity':(env2_0.humidity)})))))
ali.publish_shadow_update_msg(desiredStr='DEVICE_STATE')
ali.publish_raw_up_msg(str('RAW MSG'))
wait(3)
wait_ms(2)
API
from IoTcloud.Ali import AliIoT
ali = AliIoT(device_id='', product_key='', device_name='', region_id='cn-qingdao', password='')
- Initialize client:
- device_id, product_key, device_name: Generated after completing device creation in the Aliyun console.
- region_id:Select the region where the service instance is located, and this information will be displayed in the AliCloud console.
- password:After filling in the block, it will be automatically calculated and generated, and you can switch to the code workspace in UIFlow to view it.
ali.start()
Message Publishing and Subscribing
UIFlow Ali IoT currently offers three up and down topic subscriptions, user, raw, and shadow update.
The role and display of different topic subscriptions in the AliCloud console will be different, for example, user can be used for general data interaction, shadow update is mainly used for synchronization of device status, raw type of topic data, allowing the user to configure custom scripts in the AliCloud console to process the data.
For more information, please see the AliCloud IoT documentation page.
。
ali.publish_raw_up_msg(str('RAW MSG'))
ali.publish_shadow_update_msg(desiredStr='DEVICE_STATE')
ali.publish_user_update_msg(str((json.dumps(({'Pressure':(env2_0.pressure),'Temperature':(env2_0.temperature),'Humidity':(env2_0.humidity)})))))
def raw_down_cb(payload):
global shadow_msg, raw_msg, user_msg
raw_msg = payload
pass
ali.subscribe_raw_down_msg(raw_down_cb)
def shadow_get_cb(payload):
global shadow_msg, raw_msg, user_msg
shadow_msg = payload
pass
ali.subscribe_shadow_get_msg(shadow_get_cb)
- subscribe shadow messages
def user_get_cb(payload):
global shadow_msg, raw_msg, user_msg
user_msg = payload
pass
ali.subscribe_user_get_msg(user_get_cb)