The time for the review is coming to an end. According to the review plan, there is one last voice control review. Let's take a look at the implementation video first.
语音控灯带
1. Implementation ideas
The Xingkong board is connected to Baidu Cloud via WiFi, and the API can be called to achieve voice recognition, and the effect is very good. For details, please refer to my previous review, [Xingkong Board Python Programming Learning Main Control Board] 5. Comparison of Offline Voice Recognition and Cloud Voice Recognition
By detecting the text information after voice recognition, voice control output can be achieved.
This time we choose the light strip output, and you can use voice to set the output color. The initial setting is "turn on the light, turn off the light, more red, less red, more green, less green, more blue, less blue" such voice commands to control the light strip.
2. Voice Recognition
This code is implemented based on the previous two evaluations, including voice recognition and WYSIWYG light strip output.
Since the voice recognition of the iFlytek platform is still free, I chose to use iFlytek to implement voice recognition. Baidu's voice recognition is still charged. It was used 24 times in the previous evaluation and required a payment of 0.08 yuan. It was in arrears and the call always failed. After recharging, it could be successfully called.
The call of the iFlytek platform is also very convenient. After applying on the iFlytek platform, create an application
Fill in APPID, APISecret, APIKey and you can use it
In the code section, "iFLYTEK Speech Recognition" needs to load the user library, enter the URL: https://gitee.com/liliang9693/ext-xunfeiyuyin
Then select Load
The specific process of speech recognition is similar to Baidu
3. Code Implementation
After switching to code mode, first load the module
from pinpong.extension.unihiker import *
from pinpong.board import Board,Pin
from pinpong.board import NeoPixel
from unihiker import GUI
from unihiker import Audio
import xunfeiasr
import time
xunfeiasr mainly implements speech recognition
xunfeiasr.xunfeiasr_set(APPID="...",APISecret="...",APIKey="...")
print("开始进行录音")
u_audio.record("record.wav",3)
print("录音结束")
ShiBieNeiRong = xunfeiasr.xunfeiasr(r"record.wav")
if (ShiBieNeiRong == ""):
ShiBieNeiRong = "未能识别"
else:
ParseShiBieNeiRong()
yanse_update()
Chinese.config(text=ShiBieNeiRong)
After getting the recognized text, it is relatively simple. Just compare the text and implement the corresponding function.
def ParseShiBieNeiRong():
global BoFangBiaoShi
global ShiBieNeiRong
global HongZhi
global LanZhi
global LvZhi
global YanSe
global YanSeZhi
if (ShiBieNeiRong == "关闭灯光"):
BoFangBiaoShi = 0
elif (ShiBieNeiRong == "打开灯光"):
BoFangBiaoShi = 1
elif (ShiBieNeiRong == "偏红一点"):
HongZhi = HongZhi + 16
if (HongZhi > 255):
HongZhi = 255
YanSe[0]= HongZhi
YanSeZhi = tuple(YanSe)
elif (ShiBieNeiRong == "红少一点"):
if (HongZhi < 16):
HongZhi = 0
else:
HongZhi = HongZhi - 16
YanSe[0]= HongZhi
YanSeZhi = tuple(YanSe)
elif (ShiBieNeiRong == "偏绿一点c"):
LvZhi = LvZhi + 16
if (LvZhi > 255):
LvZhi = 255
YanSe[1]= LvZhi
YanSeZhi = tuple(YanSe)
elif (ShiBieNeiRong == "绿少一点"):
if (LvZhi < 16):
LvZhi = 0
else:
LvZhi = LvZhi - 16
YanSe[1]= LvZhi
YanSeZhi = tuple(YanSe)
elif (ShiBieNeiRong == "偏蓝一点"):
LanZhi = LanZhi + 16
if (LanZhi > 255):
LanZhi = 255
YanSe[2]= LanZhi
YanSeZhi = tuple(YanSe)
elif (ShiBieNeiRong == "蓝少一点"):
if (LanZhi < 16):
LanZhi = 0
else:
LanZhi = LanZhi - 16
YanSe[2]= LanZhi
YanSeZhi = tuple(YanSe)
The idea is to change the color little by little, but the actual effect has no intermediate transition. Due to time constraints, this problem will not be solved for the time being.
The output of the light strip is still pin P24
p_p24_out=Pin(Pin.P24, Pin.OUT)
p_p24_out.write_digital(0)
np1 = NeoPixel(p_p24_out,10)
for XuHao in range(0, 10):
np1[XuHao] = (0,0,0)
Update after color change
def yanse_update():
global YanSeZhi
global DanTiaoDengDaiYanSe
global BoFangBiaoShi
XuHao = 0
if (BoFangBiaoShi == 1):
for XuHao in range(10):
Deng[XuHao].config(color=YanSeZhi)
np1[XuHao] = YanSeZhi
DanTiaoDengDaiYanSe[XuHao]=YanSeZhi
XuHao = XuHao + 1
else:
for XuHao in range(10):
Deng[XuHao].config(color="#000000")
np1[XuHao] = (0,0,0)
DanTiaoDengDaiYanSe[XuHao]=(0, 0, 0)
XuHao = XuHao + 1
Since I am a beginner in Python, I am still confused about many codes.
In addition, the light strip of the LCD interface in this evaluation was not updated at the same time, and can only be improved slowly in the future.
4. Bluetooth music output
Today I saw in the forum that music can be output via Bluetooth. The specific implementation is as follows:
1. Bluetooth pairing
1) Enter the Bluetooth configuration through the command line, PowerShell is recommended
- ssh root@10.1.2.3
- password:dfrobot
2) Type the following commands in sequence to turn on the Bluetooth function of the Xingkong board
bluetoothctl
default-agent
power on
3) Turn on the Bluetooth speaker and set the pairing mode
4) Type the following command to scan the Bluetooth speaker device ID, find and record the device ID of the Bluetooth device
scan on
5) Type the following commands in sequence to pair the devices:
trust xx:xx:xx:xx:xx:xx (Device ID)
pair xx:xx:xx:xx:xx:xx (Device ID)
connect xx:xx:xx:xx:xx:xx (Device ID)
6) Pairing is complete, exit settings
exit
You can then play the Bluetooth speaker, it’s proven to be effective!