MicroPython动手做(27)——物联网之微信小程序-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

楼主: eagler8

MicroPython动手做(27)——物联网之微信小程序

[复制链接]
 楼主| 发表于 2020-6-3 10:22 | 显示全部楼层
添加组件——这里添加了“开关”和“按钮”,计二个

10 (1).jpg
 楼主| 发表于 2020-6-3 10:59 | 显示全部楼层
组件调整为二个按钮,开灯键值设置为1,关灯键值设置为0

11.jpg
 楼主| 发表于 2020-6-3 11:02 | 显示全部楼层
拖动按钮至合适位置并保存

12.jpg

 楼主| 发表于 2020-6-3 11:50 | 显示全部楼层
#MicroPython动手做(27)——物联网之微信小程序
#尝试通过小程序来控制板载RGB灯

[mw_shl_code=python,true]#MicroPython动手做(27)——物联网之微信小程序
#尝试通过小程序来控制板载RGB灯

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

my_wifi = wifi()

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

def OneNET_recv(_msg):pass
_OneNET_msg_list = []
def OneNET_callback(_topic, _msg):
    global _OneNET_msg_list
    try: _msg = _msg.decode('utf-8', 'ignore')
    except: print(_msg);return
    OneNET_recv(_msg)
    if _msg in _OneNET_msg_list:
        eval('OneNET_recv_' + bytes.decode(ubinascii.hexlify(_msg)) + '()')

tim14 = Timer(14)

_iot_count = 0
def timer14_tick(_):
    global _iot, _iot_count
    _iot_count = _iot_count + 1
    if _iot_count == 1000: _iot.ping(); _iot_count = 0
    try: _iot.check_msg()
    except: machine.reset()

_iot = None
def OneNET_setup():
    global _iot
    _iot = MQTTClient("595250725", "183.230.40.39", 6002, "221628", "hiN7Cwz4Gd7d3ReMnNgtaEKBvv4=", keepalive=300)
    _iot.set_callback(OneNET_callback)
    if 1 == _iot.connect(): print('Successfully connected to MQTT server.')
    tim14.init(period=200, mode=Timer.PERIODIC, callback=timer14_tick)

def OneNET_recv(_msg):
    try:
        _msgObj = ujson.loads(_msg)
        _name = _msgObj.get('name')
        _value = _msgObj.get('value')
    except: return
    if _name == "开灯":
        print(_value)
        rgb.fill((int(255), int(0), int(0)))
        rgb.write()
        time.sleep_ms(1)
    if _name == "关灯":
        print(_value)
        rgb.fill( (0, 0, 0) )
        rgb.write()
        time.sleep_ms(1)


rgb[1] = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
OneNET_setup()[/mw_shl_code]

 楼主| 发表于 2020-6-3 12:08 | 显示全部楼层
尝试通过小程序来控制板载RGB灯

18.gif
 楼主| 发表于 2020-6-3 12:13 | 显示全部楼层
mPython X 实验图形编程

13.jpg
 楼主| 发表于 2020-6-3 12:32 | 显示全部楼层
6、小程序控制RGB灯升级版

[mw_shl_code=python,true]#MicroPython动手做(27)——物联网之微信小程序
#小程序控制RGB灯升级版

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

my_wifi = wifi()

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

def OneNET_recv(_msg):pass
_OneNET_msg_list = []
def OneNET_callback(_topic, _msg):
    global _OneNET_msg_list
    try: _msg = _msg.decode('utf-8', 'ignore')
    except: print(_msg);return
    OneNET_recv(_msg)
    if _msg in _OneNET_msg_list:
        eval('OneNET_recv_' + bytes.decode(ubinascii.hexlify(_msg)) + '()')

tim14 = Timer(14)

_iot_count = 0
def timer14_tick(_):
    global _iot, _iot_count
    _iot_count = _iot_count + 1
    if _iot_count == 1000: _iot.ping(); _iot_count = 0
    try: _iot.check_msg()
    except: machine.reset()

_iot = None
def OneNET_setup():
    global _iot
    _iot = MQTTClient("595250725", "183.230.40.39", 6002, "221628", "hiN7Cwz4Gd7d3ReMnNgtaEKBvv4=", keepalive=300)
    _iot.set_callback(OneNET_callback)
    if 1 == _iot.connect(): print('Successfully connected to MQTT server.')
    tim14.init(period=200, mode=Timer.PERIODIC, callback=timer14_tick)

image_picture = Image()

def OneNET_recv(_msg):
    try:
        _msgObj = ujson.loads(_msg)
        _name = _msgObj.get('name')
        _value = _msgObj.get('value')
    except: return
    if _name == "开灯":
        print(_value)
        music.play('B5:1')
        rgb.fill((int(255), int(0), int(0)))
        rgb.write()
        time.sleep_ms(1)
        oled.fill(0)
        oled.blit(image_picture.load('face/Objects/Light on.pbm', 0), 32, 0)
        oled.show()
    if _name == "关灯":
        print(_value)
        music.play('E5:1')
        rgb.fill( (0, 0, 0) )
        rgb.write()
        time.sleep_ms(1)
        oled.fill(0)
        oled.blit(image_picture.load('face/Objects/Light off.pbm', 0), 32, 0)
        oled.show()


rgb[1] = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
OneNET_setup()[/mw_shl_code]
 楼主| 发表于 2020-6-3 14:02 | 显示全部楼层
#MicroPython动手做(27)——物联网之微信小程序
#小程序控制RGB灯升级版(实验小视频)


https://v.youku.com/v_show/id_XN ... oneSokuUgc_1.dtitle


20.jpg
 楼主| 发表于 2020-6-3 14:10 | 显示全部楼层
mPython X 实验图形编程

19.jpg
 楼主| 发表于 2020-6-3 17:14 | 显示全部楼层

7、带输入框的RGB三色调光板

刚添加的小程序控制组件面板


21.jpg


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

本版积分规则

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

GMT+8, 2024-12-27 08:31 , Processed in 0.239401 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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