11、使用Easy IoT平台远距离收集声光数据
#MicroPython动手做(31)——物联网之Easy IoT
#使用Easy IoT平台远距离收集声光数据
from mpython import *
import network
from umqtt.simple import MQTTClient
import music
import time
my_wifi = wifi()
my_wifi.connectWiFi("zh", "zy1567")
mqtt = MQTTClient("664fa81baa7fe777", "182.254.130.180", 1883, "qlZ0uezGR", "3_W0uezGgz", keepalive=30)
mqtt.set_last_will("TvkJXezMR", "I am offline")
try:
mqtt.connect()
print('Connected')
except:
print('Disconnected')
oled.fill(0)
oled.DispChar("远距离收集声光数据", 10, 15, 1)
oled.show()
music.play('D5:1')
rgb.fill((int(0), int(102), int(0)))
rgb.write()
time.sleep_ms(1)
while True:
rgb[1] = (int(255), int(0), int(0))
rgb.write()
time.sleep_ms(1)
mqtt.publish("TvkJXezMR", (str(light.read())))
mqtt.publish("ZlB0tWZMg", (str(sound.read())))
time.sleep(1)
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)
#MicroPython动手做(31)——物联网之Easy IoT
#使用移动端EasyIoT小程序控制RGB灯
#MicroPython动手做(31)——物联网之Easy IoT
#使用移动端EasyIoT小程序控制RGB灯
from mpython import *
import network
from umqtt.simple import MQTTClient
import time
import music
from machine import Timer
import ubinascii
my_wifi = wifi()
my_wifi.connectWiFi("zh", "zy1567")
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!")
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("移动版EasyIoT小程序", 5, 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()