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

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

#通过小程序控制小车进、退、左和右

[mw_shl_code=python,true]#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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值