MicroPython动手做(29)——物联网之SIoT-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

123
返回列表 发新帖
楼主: eagler8

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

[复制链接]
 楼主| 发表于 2020-6-12 12:15 | 显示全部楼层
显示消息监控的光线值图表

[eagler8_zkb1]消息监控.png
 楼主| 发表于 2020-6-12 12:45 | 显示全部楼层
9、实时模式网页端控制板载RGB灯
Mind+ 实验图形编程

15.png
 楼主| 发表于 2020-6-12 12:49 | 显示全部楼层
网页端输入命令“on”或者"off"

16.png
 楼主| 发表于 2020-6-12 12:52 | 显示全部楼层
实时模式网页端控制板载RGB灯

20.gif
 楼主| 发表于 2020-6-13 10:50 | 显示全部楼层
10、通过主题消息“on”和“off”开关LED灯

[mw_shl_code=arduino,true]#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:18 | 显示全部楼层
通过主题消息“on”和“off”开关LED灯

25.gif
 楼主| 发表于 2020-6-13 11:21 | 显示全部楼层
mPython x 实验图形编程

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

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-27 10:22 , Processed in 0.109442 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表