|
楼主 |
发表于 2020-6-17 19:25
|
显示全部楼层
9、通过Easy IoT平台远距控制开关RGB灯
[mw_shl_code=arduino,true]#MicroPython动手做(31)——物联网之Easy IoT
#通过Easy IoT平台远距控制开关RGB灯
from mpython import *
import network
my_wifi = wifi()
my_wifi.connectWiFi('zh', 'zy1567')
from umqtt.simple import MQTTClient
mqtt = MQTTClient('0805e3d04f3b34e7', '182.254.130.180', 1883, 'qlZ0uezGR', '3_W0uezGgz', keepalive=30)
try:
mqtt.connect()
print('Connected')
except:
print('Disconnected')
mqtt.set_last_will('TvkJXezMR', 'Ready!')
import time
import music
from machine import Timer
import ubinascii
def mqtt_topic_54766b4a58657a4d52(_msg):
oled.DispChar((str(_msg)), 55, 24, 1)
oled.show()
if 'on' == _msg:
music.play('D5:1')
rgb.fill((int(255), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
oled.DispChar('开灯', 52, 36, 1)
oled.show()
elif 'off' == _msg:
music.play('F5:1')
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)
oled.DispChar('关灯', 52, 36, 1)
oled.show()
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))
mqtt.set_callback(mqtt_callback)
mqtt.subscribe("TvkJXezMR")
def timer14_tick(_):
mqtt.ping()
tim14 = Timer(14)
tim14.init(period=20000, mode=Timer.PERIODIC, callback=timer14_tick)
oled.invert(0)
oled.DispChar('Hello, world!', 24, 10, 1)
oled.show()
rgb[1] = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
music.play('G5:1')
while True:
mqtt.wait_msg()[/mw_shl_code]
|
|