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

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

楼主: eagler8

MicroPython动手做(31)——物联网之Easy IoT

[复制链接]
 楼主| 发表于 2020-6-17 18:22 | 显示全部楼层
mPython X 实验图形编程

05.png


 楼主| 发表于 2020-6-17 18:25 | 显示全部楼层
二个设备接收到消息的情况

06--.png


07.png

 楼主| 发表于 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]

 楼主| 发表于 2020-6-17 19:36 | 显示全部楼层
通过Easy IoT平台远距控制开关RGB灯

12.gif


 楼主| 发表于 2020-6-17 19:39 | 显示全部楼层
mPython  实验图形编程

07-.png


 楼主| 发表于 2020-6-17 19:44 | 显示全部楼层
TvkJXezMR设备接收到的命令消息

08.png

 楼主| 发表于 2020-6-17 20:29 | 显示全部楼层
10、输入数字远距离画空心园


[mw_shl_code=arduino,true]#MicroPython动手做(31)——物联网之Easy IoT
#输入数字远距离画空心园

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("664fa81baa7fe777", "182.254.130.180", 1883, "qlZ0uezGR", "3_W0uezGgz", keepalive=30)

mqtt.set_last_will("ZlB0tWZMg", "Hello, world!")

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

def mqtt_topic_5a6c423074575a4d67(_msg):
    if int(_msg) < 32:
        rgb.fill((int(0), int(0), int(153)))
        rgb.write()
        time.sleep_ms(1)
        music.play('E5:1')
        oled.fill(0)
        oled.circle(64, 32, (int(_msg)), 1)
        oled.show()
    else:
        rgb.fill((int(102), int(0), int(0)))
        rgb.write()
        time.sleep_ms(1)
        oled.fill(0)
        oled.DispChar("请输入正整数", 30, 12, 1)
        oled.DispChar("数值应小于32", 30, 24, 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("ZlB0tWZMg")

def timer14_tick(_):
    mqtt.ping()

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


music.play('D5:1')
rgb[1] = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
oled.invert(0)
oled.DispChar("远距画空心圆", 30, 12, 1)
oled.show()
while True:
    mqtt.wait_msg()[/mw_shl_code]

 楼主| 发表于 2020-6-17 20:33 | 显示全部楼层
13-.jpg

19-.jpg

 楼主| 发表于 2020-6-17 20:37 | 显示全部楼层
mPython X 实验图形编程

23.png


 楼主| 发表于 2020-6-17 20:41 | 显示全部楼层
ZlB0tWZMg设备接收到的数字命令消息

20.png


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

本版积分规则

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

GMT+8, 2024-12-28 01:47 , Processed in 0.138097 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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