- 【丁丁歷險記】全篇索引
- 本節教大家如何用esp8266做高精度的語音識別服務,實現微信語音遠程遙控單片機。請先保證自己安裝成功了Arduino的開發環境或者是microPython的開發環境。請關注"如易科技"公衆平臺獲取Key.
- 視頻教程
一、Arduino開發語音識別
開發流程主要分爲三步:
- 安裝開發環境請參考【丁丁歷險記】Arduino配置esp8266/esp32開發環境選擇開發板型號爲esp8266我的配置如下
- 複製代碼詳情參考 Github :https://github.com/Vulcan-YJX/RYKJ
#include <Arduino.h> #include <ESP8266WiFi.h> #如果使用的是ESP32請將此處替換爲“WIFI.h” #include <WiFiClient.h> #include <Ticker.h> #define WIFISSID "YOUR WIFI SSID" #define PASSWD "YOUR WIFI PASSWD" String Key = "微信獲取Key進行替換"; const char *host = "www.rykj.xyz"; String comdata = ""; String KeyMark = "<key>"+Key+"</key>"; Ticker flipper; WiFiClient client; void flip() { //間隔一段時間發送一次數據用來維持連接 client.print(KeyMark); } void setup() { Serial.begin(115200); Serial.println("Connecting...\n"); WiFi.mode(WIFI_STA); WiFi.begin(WIFISSID,PASSWD); // change it to your ussid and password while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } if (!client.connect("www.rykj.xyz", 443)) { Serial.println("Connection to host failed"); delay(1000); return; } else{ client.print(KeyMark); } flipper.attach(180, flip);//每隔180秒執行一次回調函數 } void loop() { while (Serial.available() > 0) //檢測串口是否有用戶數據 { comdata += char(Serial.read()); delay(2); } while (client.available() > 0) //打印服務器返回的數據 { char c = client.read(); Serial.write(c); } if (comdata.length() > 0) { String SendMsg = comdata + KeyMark; //Serial.println(SendMsg); //選擇是否打印出用戶輸入 client.print(SendMsg); comdata = ""; } }
- 修改wifi名稱和密碼,將Key替換成公衆號獲取的Key下載如芯片即可。現在您可以使用公衆號發送語音到ESP8266/ESP32設備上了。ESP8266/ESP32會將最終的識別結果通過串口打印出來。
二、microPython開發語音識別
繼續堅持3步入坑戰略:
- 安裝開發環境請參考【丁丁歷險記】ESP8266配置microPython 並下載microPython File upLoader進行調試
- 複製代碼詳情參考 Github :https://github.com/Vulcan-YJX/RYKJ
- 創建名稱爲main.py的文件並粘貼下列代碼,修改其中的wifi名稱和密碼以及<key>標籤中的十九位key
-
#coding:utf-8 import socket import time SSID = 'your wifi SSID' PASSWD = 'your wifi passwd' usrkey = "<key>kEy*************</key>" def connectWifi(): import network wlan = network.WLAN(network.STA_IF) wlan.active(True) if not wlan.isconnected(): print('connecting to network...') wlan.connect(SSID,PASSWD) while not wlan.isconnected(): pass print('network config:', wlan.ifconfig()) def doConnect(): client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: connectMsg = socket.getaddrinfo('www.rykj.xyz', 443)[0][4] client.connect(connectMsg) except: pass return client client = doConnect() client.send(usrkey.encode('utf-8')) print('[+] connect succesfull') while True: try: data = client.recv(1024).decode('utf-8') print('from server:',data) except socket.error: print('socket.error doConnect') time.sleep(2) client = doConnect() client.send(usrkey.encode('utf-8')) print('[+] connect close...') client.close()
- 創建名稱爲boot.py的文件並粘貼下列代碼
#import esp #esp.osdebug(None) import uos, machine #uos.dupterm(None, 1) # disable REPL on UART(0) import gc #import webrepl #webrepl.start() gc.collect() import main
3. 使用ampy將代碼上傳到esp8266中
-
ampy --port 端口號 put main.py #上傳main.py ampy --port 端口號 put boot.py #上傳boot.py
- 使用 microPython File upLoader打開端口即可查看數據(由於此工具不支持中文所以顯示的均爲???,但是並不影響代碼中對中文關鍵字的判斷)
- 因爲esp8266在上電後會自動運行boot.py文件,所以import main 以後 main.py會上電自啓動