6、尝试在SIoT平台输入消息,控制点亮或熄灭板载LED灯
#MicroPython动手做(29)——物联网之SIoT
#尝试在SIoT平台输入消息,控制点亮或熄灭板载LED灯
from umqtt.simple import MQTTClient
from machine import Timer
from mpython import *
import ubinascii
import network
import music
_mqtt_topic_list = []
def timer14_tick(_):
global mqtt
mqtt.ping()
def mqtt_callback(topic, msg):
try:
topic = topic.decode('utf-8', 'ignore')
_msg = msg.decode('utf-8', 'ignore')
eval('mqtt_topic_' + bytes.decode(ubinascii.hexlify(topic)) + '("' + _msg + '")')
except:
print((topic, msg))
brightness=9
# 事件回调函数
def mqtt_topic_6561676c6572382f7a6b6231(_msg):
global g_my_variable
if (_msg == on):
music.pitch(196, 50)
rgb.fill((255*brightness//9, 255*brightness//9, 153*brightness//9))
rgb.write()
if (_msg == off):
music.pitch(784, 50)
rgb.fill( (0, 0, 0) )
rgb.write()
my_wifi = wifi()
tim14 = Timer(14)
my_wifi.connectWiFi("zh","zy1567")
mqtt = MQTTClient("", "192.168.31.248", 1883, "siot", "dfrobot")
try:
mqtt.connect()
print('Connected')
except:
print('Disconnected')
mqtt.set_callback(mqtt_callback)
tim14.init(period=20000, mode=Timer.PERIODIC, callback=timer14_tick)
mqtt.subscribe("eagler8/zkb1")
rgb[1] = (0*brightness//9, 102*brightness//9, 0*brightness//9)
rgb.write()
music.pitch(392, 50)
mqtt.publish("eagler8/zkb1","on")
|