【项目由来】
在思考这次的麦昆脑洞任务时,想好好用用麦昆两个轮子的便利性和多个接口的扩展性,因此想用"管家"这一个身份重定义麦昆。但既然时“管家”那么应该有个命令输入接口和命令执行模块。
我们知道妨碍麦昆和外面的世界联系的问题主要是物联网设备的问题,因此本文用了个集成度较高的esp32设备用来语音等信息的接收,并用麦昆进行命令的执行。
【准备事项】
【软件平台】
mind+
步骤1 连接Gravity: UART MP3语音模块和无源音箱小喇叭 (8Ω3W)
基本上根据mp3模块的说明文档就可以的,要注意的是因为需要用到uart,所以需要拔掉麦昆的hc-sr04超声波测距模块,并将MP3
模块的tx和rx口分别连到p1和p0口。
步骤2 连接某esp32板
因为涉及到信息的传递,所以需要用esp32板来做为转接站。鉴于麦昆裸露可用的编程口不是很多,所以准备复用麦昆的p0,p1,p2做为数字口。这样总共可以用的信息传递有7种状态,足够完成基本的内容。(每个口有两种状态,除掉全为0的状态总共有2^3-1=7种)
步骤3 连接载物台
这么多设备不好放,所以用开源的lasorblock模板和柱型螺丝连接了载物台,放置MP3模块、喇叭和某esp32板、用来供电的电池组等。来个完整的照片。
大概的执行过程就如上图所示。
以下是某esp32板的代码,因为涉及到语音的识别,和天气信息的接口读取,所以在时间消耗上会多点。
代码
# -*- coding: utf-8 -*-
import haloboard
import time
import event
import urequests as requests
import ujson
def get_weather_info(city_code=2161853, data_type=4):
if not haloboard.wifi.is_connected():
return ''
request_url = 'http://mweather.makeblock.com/' + 'getweather?woeid=' + str(city_code) + '&type=' + str(data_type)
resp = requests.get(request_url)
text = resp.text
if int(data_type) <= 3:
return int(text)
return text
@event.start
def use_code():
haloboard.wifi.start(ssid = "", password = "", mode = haloboard.wifi.WLAN_MODE_STA)
while(True):
if haloboard.wifi.is_connected() == True:
print("wifi is connected!")
break;
while True:
if haloboard.button.is_pressed():
haloboard.led.show_all(0, 0, 50)
haloboard.speech_recognition.start(haloboard.speech_recognition.SERVER_BAIDU, haloboard.speech_recognition.LAN_DEFAULT, 1)
if haloboard.speech_recognition.get_error_code() != 0:
str = haloboard.speech_recognition.get_error_message()
haloboard.led.show_all(0, 0, 0)
print("error_message:" + str)
else:
result = haloboard.speech_recognition.get_result_code()
print("result:" + result)
if '天气' in result:
data = get_weather_info(2161853,4)
print("data:" + data)
if 'Cloudy' in data:
print('Cloudy')
haloboard.pin0.write_digital(1)
haloboard.led.show_all(0, 0, 0)
time.sleep(2)
haloboard.pin0.write_digital(0)
else:
print('weather report error!')
haloboard.led.show_all(0, 0, 0)
haloboard.pin0.write_digital(0)
time.sleep(0.5)
mind+麦昆的基本信息,这里只判断了p0的状态。
虚无缥缈2020.01.16
好像很好玩