Wifi mode
502748957@qq.com(讨论 | 贡献)2018年12月12日 (三) 08:04的版本
WiFi音频播放/语音识别
所需硬件
电路搭建 将Battery、Core+、hub堆叠在一起,通过MicroUSB数据线接入电脑,使用传感器线连接MicroAI的UART(串口)。将耳机根据模块上的耳机标志插入,并下载如下程序至Core+。将两个碰撞传感器接在D4和D6口
代码 #include <Microduino_Key.h>
#include <Microduino_Ai.h>
#define WIFI_SSID "SSID" //路由器名称
#define WIFI_PWD "PW" //路由器密码
#define WEB_NUM 4
const char* webRadioUrl[WEB_NUM] = { //网络广播地址,也可换成文件名.mp3并将文件存在SD卡中
"https://http.qingting.fm/386.mp3",
"https://http.qingting.fm/387.mp3",
"https://http.qingting.fm/388.mp3",
"https://http.qingting.fm/389.mp3",
};
static uint8_t webRadioIndex = 0xFF;
DigitalKey touch1(D4); //按键接口
DigitalKey touch2(D6); //按键接口
MicroAi mAi(&Serial1);
char strBuf[64] = {0};
void setup() {
Serial.begin(115200);
touch1.begin(INPUT_PULLUP); //按键设置为上拉输入,用于防止没有接传感器导致的电平识别错误
touch2.begin(INPUT_PULLUP); //按键设置为上拉输入,用于防止没有接传感器导致的电平识别错误
Serial.print("MicroAi set mode: WIFI.");
Serial.println("start initiation. ");
if(mAi.begin(MODE_WIFI)){
Serial.println("init OK!");
}else{
Serial.println("init failed!");
while(1);
}
if(mAi.setWifi(WIFI_SSID, WIFI_PWD)){
Serial.println("set wifi ssid and password OK!");
}else{
Serial.println("set wifi ssid and password failed!");
while(1);
}
Serial.print("wifi connecting");
while(mAi.getRssi()==0){
Serial.print(".");
delay(1000);
}
Serial.println("\r\nwifi connected.");
delay(1000);
}
void loop() {
if(touch1.readEvent() == SHORT_PRESS) {
if(webRadioIndex < WEB_NUM-1)
webRadioIndex++;
else
webRadioIndex = 0;
mAi.playFile(webRadioUrl[webRadioIndex]);
}
if(touch2.readEvent() == SHORT_PRESS) {
delay(500);
Serial.print("AT start Rest: ");
if(mAi.startRest()){ //启动语音识别
Serial.println("OK!");
int32_t code = mAi.waitResult(strBuf);
if(code){
Serial.print("rest code: ");
Serial.print(code);
Serial.print(", result: ");
Serial.println(strBuf);
}else{
Serial.println("rest none ");
}
}else{
Serial.println("Failed!");
}
}
delay(50);
}
运行结果
播放时MicroAI点阵显示音乐播放特效
观察串口返回识别结果: |