4148浏览
楼主: 驴友花雕

[MP动手做] MicroPython动手做(29)——物联网之SIoT

[复制链接]

驴友花雕  中级技神
 楼主|

发表于 2020-6-12 12:15:51

显示消息监控的光线值图表

MicroPython动手做(29)——物联网之SIoT图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-12 12:46:09

9、实时模式网页端控制板载RGB灯
Mind+ 实验图形编程

MicroPython动手做(29)——物联网之SIoT图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-12 12:51:39

网页端输入命令“on”或者"off"

MicroPython动手做(29)——物联网之SIoT图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-12 12:55:42

实时模式网页端控制板载RGB灯
MicroPython动手做(29)——物联网之SIoT图1





回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-13 10:51:16

10、通过主题消息“on”和“off”开关LED灯

[mw_shl_code=python,false]#MicroPython动手做(29)——物联网之SIoT
#通过主题消息“on”和“off”开关LED灯

from mpython import *
import network
from umqtt.simple import MQTTClient
import music
import time
from machine import Timer
import ubinascii

my_wifi = wifi()

my_wifi.connectWiFi("zh", "zy1567")

mqtt = MQTTClient("ok", "192.168.31.248", 1883, "siot", "dfrobot", keepalive=30)

try:
    mqtt.connect()
    print('Connected')
except:
    print('Disconnected')

def mqtt_topic_6561676c6572382f7a6b6231(_msg):
    global i
    if str(_msg) == "on":
        rgb.fill((int(255), int(0), int(0)))
        rgb.write()
        time.sleep_ms(1)
        oled.DispChar("                 开灯", 0, 32, 1)
        oled.show()
    if str(_msg) == "off":
        rgb.fill( (0, 0, 0) )
        rgb.write()
        time.sleep_ms(1)
        oled.DispChar("                关灯", 0, 32, 1)
        oled.show()

def on_button_a_down(_):
    global i
    time.sleep_ms(10)
    if button_a.value() == 1: return
    mqtt.publish("eagler8/zkb1", "on")
    music.play('E5:1')

def on_button_b_down(_):
    global i
    time.sleep_ms(10)
    if button_b.value() == 1: return
    mqtt.publish("eagler8/zkb1", "off")
    music.play('D5:1')

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("eagler8/zkb1")

def timer14_tick(_):
    mqtt.ping()

tim14 = Timer(14)
tim14.init(period=20000, mode=Timer.PERIODIC, callback=timer14_tick)

button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)

button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)


mqtt.publish("eagler8/zkb1", "hello")
music.play('G5:1')
oled.fill(0)
oled.DispChar("        连接SIoT成功", 0, 16, 1)
oled.show()
rgb[1] = (int(0), int(51), int(0))
rgb.write()
time.sleep_ms(1)
while True:
    mqtt.wait_msg()[/mw_shl_code]
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-13 11:19:02

通过主题消息“on”和“off”开关LED灯

MicroPython动手做(29)——物联网之SIoT图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-13 11:22:15

mPython x 实验图形编程

MicroPython动手做(29)——物联网之SIoT图1
回复

使用道具 举报

12
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail