pdf-icon

UIFlow Guide

UIFlow 1.0 Blockly

Event

Unit

Atomic Base

UIFlow 1.0 Project

Tencent

创建产品与设备
使用UIFlow Tencent功能前请通过 腾讯云物联网开发平台 完成产品和设备的创建。

案例程序

from m5stack import *
from m5stack_ui import *
from uiflow import *
from IoTcloud.Tencent import Tencent
import time
import unit

screen = M5Screen()
screen.clean_screen()
screen.set_screen_bg_color(0xFFFFFF)
env20 = unit.get(unit.ENV2, unit.PORTA)

temp = None
humid = None

label0 = M5Label('温度:', x=15, y=25, color=0x000, font=FONT_UNICODE_24, parent=None)
label4 = M5Label('下发数据:', x=15, y=123, color=0x000, font=FONT_UNICODE_24, parent=None)
label1 = M5Label('湿度:', x=15, y=71, color=0x000, font=FONT_UNICODE_24, parent=None)
down_link_data = M5Label('text', x=5, y=165, color=0x000, font=FONT_MONT_14, parent=None)
label2 = M5Label('Text', x=124, y=33, color=0x000, font=FONT_MONT_14, parent=None)
label3 = M5Label('Text', x=124, y=79, color=0x000, font=FONT_MONT_14, parent=None)

def tencent_fun(property_data):
  global temp, humid
  down_link_data.set_text(str(property_data))
  pass


down_link_data.obj.set_long_mode(lv.label.LONG.BREAK)
down_link_data.obj.set_size(320, 100)
tencent = Tencent(product_id='xxxxx', device_name='xxxxx', username='xxxxxxxxxx;12010126;YWLY7;1810721745', password='d7201f1da56972d46ffb9498a86d08ed51644d8d;hmacsha1', port=1883, keepalive=30)
tencent.subscribe_property_msg(tencent_fun)
tencent.start()
while True:
  temp = env20.temperature
  humid = env20.humidity
  label2.set_text(str(temp))
  label3.set_text(str(humid))
  tencent.publish_property_msg(temperature=temp,humidity=humid)
  wait(5)
  wait_ms(2)

功能说明

from IoTcloud.Tencent import Tencent
tencent = Tencent(product_id='', device_name='', username='', password='', port=1883, keepalive=0)
  • 初始化客户端连接信息
tencent.start()
  • 启用客户端连接
tencent.publish(topic,msg)
  • 消息发布
tencent.publish_property_msg(temperature=temp,humidity=humid)
  • 发布property消息:
    • key-value形式发送,其中key值需要与自定义功能中的标识符保持一致。设备运行时,用户可以设备日志中,查看设备的上行数据。
def fun_subscribe_(topic_data):
  # global params
  print(topic_data)
  pass

tencent.subscribe(str('subscribe'), fun_subscribe_)
  • 消息订阅回调函数
def tencent_fun(property_data):
  # global params
  print(property_data)
  pass

tencent.subscribe_property_msg(tencent_fun)
  • property消息订阅回调函数
On This Page