3927浏览
楼主: 驴友花雕

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

[复制链接]

驴友花雕  中级技神
 楼主|

发表于 2020-6-3 12:09:11

尝试通过小程序来控制板载RGB灯

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-3 12:30:03

mPython X 实验图形编程

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-3 13:48:34

6、小程序控制RGB灯升级版

[mw_shl_code=python,false]#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:01:26

#MicroPython动手做(27)——物联网之微信小程序
#小程序控制RGB灯升级版(实验小视频)


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



回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-3 14:11:19

mPython X 实验图形编程

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-3 17:15:09

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

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


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


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-3 17:21:28

[mw_shl_code=python,false]#MicroPython动手做(27)——物联网之微信小程序
#带输入框的RGB三色调光板

from mpython import *
import network
import time
import music
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)
        music.play('B3:1')
        rgb[1] = (int(51), int(102), int(255))
        rgb.write()
        time.sleep_ms(1)
        oled.fill(0)
        oled.DispChar(_value, 0, 23, 1)
        oled.show()
    if _name == "R":
        print(_value)
        rgb.fill( (int(_value), int((_value + 20)), int((_value - 20))) )
        rgb.write()
        time.sleep_ms(1)
    if _name == "G":
        print(_value)
        rgb.fill( (int((_value + 20)), int(_value), int((_value - 20))) )
        rgb.write()
        time.sleep_ms(1)
    if _name == "B":
        print(_value)
        rgb.fill( (int((_value - 20)), int((_value + 20)), int(_value)) )
        rgb.write()
        time.sleep_ms(1)


rgb[1] = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
music.play('G4:1')
OneNET_setup()[/mw_shl_code]
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-3 17:35:41

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-3 17:48:01

mPython X 实验图形编程

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-3 18:47:28

8、实时监控光线值和声音值
GX与SY 小程序控制组件面板

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-3 18:51:20

[mw_shl_code=python,false]#MicroPython动手做(27)——物联网之微信小程序
#实时监控光线值和声音值

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

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 pubdata(_dic):
    print(_dic)
    _list = []
    for _key in list(_dic.keys()):
        _d = {'id':_key,'datapoints':[{'value':_dic[_key]}]}
        _list.append(_d)
    _data = {'datastreams': _list}
    j_d = json.dumps(_data)
    j_l = len(j_d)
    arr = bytearray(j_l + 3)
    arr[0] = 1
    arr[1] = int(j_l / 256)
    arr[2] = j_l % 256
    arr[3:] = j_d.encode('ascii')
    return arr


rgb[1] = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
music.play('G4:1')
OneNET_setup()
while True:
    _iot.publish('$dp', pubdata({"xx":light.read()}))
    _iot.publish('$dp', pubdata({"yy":sound.read()}))
    time.sleep(1)[/mw_shl_code]

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-3 18:55:21

mPython X 实验图形编程

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-3 19:32:10

小程序上实时监控光线值和声音值


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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-5 12:39:05

9、通过小程序控制小车进、退、左和右
调整好的小程序控制组件面板

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

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-5 14:58:21

#MicroPython动手做(27)——物联网之微信小程序
#通过小程序控制小车进、退、左和右

[mw_shl_code=python,false]#MicroPython动手做(27)——物联网之微信小程序
#通过小程序控制小车进、退、左和右

from mpython import *

import network

my_wifi = wifi()

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

import time

import music

from umqtt.simple import MQTTClient

from machine import Timer

import machine

import ubinascii

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)

import ujson

import parrot

def forward():
    global _name, _value
    parrot.set_speed(parrot.MOTOR_1, 80)
    parrot.set_speed(parrot.MOTOR_2, 80)
    oled.fill(0)
    oled.blit(image_picture.load('face/Information/Forward.pbm', 0), 32, 0)
    oled.show()

def retreat():
    global _name, _value
    parrot.set_speed(parrot.MOTOR_1, -80)
    parrot.set_speed(parrot.MOTOR_2, -80)
    oled.fill(0)
    oled.blit(image_picture.load('face/Information/Backward.pbm', 0), 32, 0)
    oled.show()

def right():
    global _name, _value
    parrot.set_speed(parrot.MOTOR_1, 80)
    parrot.set_speed(parrot.MOTOR_2, -0)
    oled.fill(0)
    oled.blit(image_picture.load('face/Information/Right.pbm', 0), 32, 0)
    oled.show()

def Left():
    global _name, _value
    parrot.set_speed(parrot.MOTOR_1, -0)
    parrot.set_speed(parrot.MOTOR_2, 80)
    oled.fill(0)
    oled.blit(image_picture.load('face/Information/Left.pbm', 0), 32, 0)
    oled.show()

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(0), int(102), int(0)))
        rgb.write()
        time.sleep_ms(1)
        forward()
    if _name == '退':
        print(_value)
        rgb.fill((int(51), int(51), int(255)))
        rgb.write()
        time.sleep_ms(1)
        retreat()
    if _name == '左':
        print(_value)
        rgb[0] = (int(0), int(102), int(0))
        rgb.write()
        time.sleep_ms(1)
        Left()
    if _name == '右':
        print(_value)
        rgb[2] = (int(0), int(102), int(0))
        rgb.write()
        time.sleep_ms(1)
        right()
    time.sleep(3)
    parrot.set_speed(parrot.MOTOR_1, 0)
    parrot.set_speed(parrot.MOTOR_2, 0)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)

image_picture = Image()
rgb[1] = (int(102), int(0), int(0))
rgb.write()
time.sleep_ms(1)
music.play('G5:1')
OneNET_setup()[/mw_shl_code]

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-5 15:04:13

后来改为延时3秒关闭电机,小程序组件面板为

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

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-5 15:33:21

mPython 实验图形编程

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-6-5 17:17:04

MicroPython动手做(27)——物联网之微信小程序
通过小程序控制小车进、退、左和右(实验视频)

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




回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail