驴友花雕 发表于 2020-6-2 15:40:49

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

1、微信小程序

小程序的一种,英文名Wechat Mini Program,是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或搜一下即可打开应用。全面开放申请后,主体类型为企业、政府、媒体、其他组织或个人的开发者,均可申请注册小程序。微信小程序、微信订阅号、微信服务号、微信企业号是并行的体系。




微信小程序是一种不用下载就能使用的应用,也是一项创新,经过将近两年的发展,已经构造了新的微信小程序开发环境和开发者生态。微信小程序也是这么多年来中国IT行业里一个真正能够影响到普通程序员的创新成果,已经有超过150万的开发者加入到了微信小程序的开发,与我们一起共同发力推动微信小程序的发展,微信小程序应用数量超过了一百万,覆盖200多个细分的行业,日活用户达到两个亿,微信小程序还在许多城市实现了支持地铁、公交服务。微信小程序发展带来更多的就业机会,2017年小程序带动就业104万人,社会效应不断提升。




2017年1月9日,张小龙在2017微信公开课Pro上发布的微信小程序正式上线。2018年2月,微信官方发布公告称:已对涉及假货高仿、SQ低俗和违规“现金贷”等超过2000个微信小程序,进行永久封禁处理。2019年8月9日,微信向开发者发布新能力公测与更新公告,微信PC版新版本中,支持打开聊天中分享的微信小程序。





驴友花雕 发表于 2020-6-5 14:58:21

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

#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 = (int(0), int(102), int(0))
      rgb.write()
      time.sleep_ms(1)
      Left()
    if _name == '右':
      print(_value)
      rgb = (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 = (int(102), int(0), int(0))
rgb.write()
time.sleep_ms(1)
music.play('G5:1')
OneNET_setup()

驴友花雕 发表于 2020-6-3 17:21:28

#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 = (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 = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
music.play('G4:1')
OneNET_setup()

驴友花雕 发表于 2020-6-3 13:48:34

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

#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 = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
OneNET_setup()

hnyzcj 发表于 2020-6-2 17:19:09

为啥后面一截没有了呢,总感觉少了点什么

驴友花雕 发表于 2020-6-2 17:23:18

hnyzcj 发表于 2020-6-2 17:19
为啥后面一截没有了呢,总感觉少了点什么

版主好!哪个帖子后面一截没有了?

KIKI 发表于 2020-6-2 17:45:45

hnyzcj 发表于 2020-6-2 17:19
为啥后面一截没有了呢,总感觉少了点什么

他的文章会一直在留言里更新的

驴友花雕 发表于 2020-6-2 19:06:20

KIKI 发表于 2020-6-2 17:45
他的文章会一直在留言里更新的

谢谢,学习了

驴友花雕 发表于 2020-6-2 19:42:38

2、打开微信搜索“掌控板物联网”小程序进行授权


驴友花雕 发表于 2020-6-2 19:47:34

微信授权并一键登录


驴友花雕 发表于 2020-6-2 19:53:24

添加掌控板,给掌控板取个名字,填上掌控板的mac地址




驴友花雕 发表于 2020-6-2 19:58:42

这里我添加了二块掌控板


驴友花雕 发表于 2020-6-2 21:12:39

3、登录mPython X 软件
编程软件登陆名要与手机微信登陆的帐号一致。曾经使用一个帐号登陆myphton X,用另一个手机号注册的微信号小程序查找,始终是设备不在线。说白了就是程序登陆的帐号要与微信号相同。


驴友花雕 发表于 2020-6-2 21:34:25

4、登录小程序“掌控板物联网”

#MicroPython动手做(27)——物联网之微信小程序
#登录小程序“掌控板物联网”

#MicroPython动手做(27)——物联网之微信小程序
#登录小程序“掌控板物联网”

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

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)


OneNET_setup()

hnyzcj 发表于 2020-6-3 05:55:12

KIKI 发表于 2020-6-2 17:45
他的文章会一直在留言里更新的

好吧,这也算是一种创新的更贴

驴友花雕 发表于 2020-6-3 09:03:29

mPython X 实验图形编程


驴友花雕 发表于 2020-6-3 09:13:27

尝试好多次,我的掌控板终于能够“在线”了。


pATAq 发表于 2020-6-3 09:21:39

但是阅读连续性会降低,还是建议直接在一楼更新完

驴友花雕 发表于 2020-6-3 10:18:44

添加应用——这里添加了“控制RGB灯”


驴友花雕 发表于 2020-6-3 10:22:58

本帖最后由 驴友花雕 于 2020-6-3 11:55 编辑

pATAq 发表于 2020-6-3 09:21
但是阅读连续性会降低,还是建议直接在一楼更新完
谢谢老师,级别太低,还是初级,要赚积分呢,呵呵

驴友花雕 发表于 2020-6-3 10:27:19

添加组件——这里添加了“开关”和“按钮”,计二个


驴友花雕 发表于 2020-6-3 11:00:16

组件调整为二个按钮,开灯键值设置为1,关灯键值设置为0


驴友花雕 发表于 2020-6-3 11:47:44

拖动按钮至合适位置并保存



驴友花雕 发表于 2020-6-3 11:54:51

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

#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 = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
OneNET_setup()

页: [1] 2
查看完整版本: MicroPython动手做(27)——物联网之微信小程序