【丁丁历险记】ESP8266做微信遥控语音识别

  

  1. 【丁丁历险记】Arduino配置esp8266/esp32开发环境
  2. 【丁丁历险记】ESP8266配置microPython
  3. 【丁丁历险记】ESP8266花式连接wifi 
  4. 【丁丁历险记】ESP8266做微信遥控语音识别

  • 本节教大家如何用esp8266做高精度的语音识别服务,实现微信语音远程遥控单片机。请先保证自己安装成功了Arduino的开发环境或者是microPython的开发环境。请关注"如易科技"公众平台获取Key.

一、Arduino开发语音识别

开发流程主要分为三步:

  1. 安装开发环境请参考【丁丁历险记】Arduino配置esp8266/esp32开发环境选择开发板型号为esp8266我的配置如下
  2. 复制代码详情参考 Github :https://github.com/Vulcan-YJX/RYKJ
    #include <Arduino.h>
    #include <ESP8266WiFi.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 = "";
        }
    
    }
  3. 修改wifi名称和密码,将Key替换成公众号获取的Key下载如芯片即可。现在您可以使用公众号发送语音到ESP8266设备上了。ESP8266会将最终的识别结果通过串口打印出来。

二、microPython开发语音识别

继续坚持3步入坑战略:

  1. 安装开发环境请参考【丁丁历险记】ESP8266配置microPython 并下载microPython File upLoader进行调试
  2. 复制代码详情参考 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会上电自启动 
发布了14 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/T_infinity/article/details/104485244
今日推荐