基于Syntiant TinyML board的语音助手设计

funpack第二季第一期已圆满结束,参加活动的有89人之多,上线第一天就有四十多朋友下单,成功打破了去年funpack第一季第十二期的记录,也感谢大家对funpack活动和硬禾的信任和支持。
  本次共有44位朋友提交项目并全部审核通过,也恭喜这些上进的崽。第二期也已经上线,欢迎新老朋友前来参加,参加的期数越多,项目审核通过的越多,结束时可获得的奖品越丰厚。快来参加吧!
  从那么多优秀的项目中选择出更优秀的项目也是很苦恼的事情。今天先欣赏来自杭州的argon朋友做的基于syntiant tinyml board 设计一款可以和raspberry 4进行联动控制小米和涂鸦设备的语音助手,成功控制了家用的空气净化器。
任务目标
基于syntiant tinyml board 设计一款可以和raspberry 4进行联动控制小米和涂鸦设备的语音助手。
设计思路系统拓扑
详细描述
不管是tuya云还是米家云都是私有的,我们无法直接将自定义设备进行接入并进行交互。因此我们需要借助一个工具----home assistant来进行消息的转发。并且将树莓派作为其软件运行的载体。当tinyml开发板识别到对应语音指令之后通过io状态上报给esp8266芯片,esp8266通过局域网mqtt服务上报给树莓派上的emq服务器,hass去自动解析mqtt报文并且将相应指令上报给不同的云平台。
详细实施
1. docker安装
为了便于管理树莓派上的软件包我们统一使用docker进行软件的安装,这样可以保证每个软件都是独立运行在虚拟环境中的不会因为运行出错或者错删文件导致系统无法启动。
  下面提供两种安装方式:
脚本安装方式
只需要输入命令就可以安装好(省事 方便,初次学习可以试试第二种方法)
  sudo curl -ssl https://get.docker.com | sh
apt 安装方式
由于 raspbian 基于 debian,我们还可以使用 apt安装(其他发行版可做参考)
1.添加使用 https 传输的软件包以及 ca 证书。由于 apt 源使用 https 以确保软件下载过程中不被篡改。因此,我们首先需要添加使用 https 传输的软件包以及 ca 证书。
              $ sudo apt-get install apt-transport-https ca-certificates curl gnupg2 lsb-release software-properties-common  
1)添加 docker 的 gpg key
为了确认所下载软件包的合法性,需要添加软件源的 gpg 密钥。
        curl -fssl https://yum.dockerproject.org/gpg | sudo apt-key add - # 官方源 # $ curl -fssl https://download.docker.com/linux/raspbian/gpg | sudo apt-key add -  
2)设置稳定的 repository
                      sudo add-apt-repository deb https://apt.dockerproject.org/repo/ raspbian-$(lsb_release -cs) stable # 官方源 #$ sudo add-apt-repository #deb [arch=armhf] https://download.docker.com/linux/raspbian #$(lsb_release -cs) #stable  
3)安装 docker
更新 apt 软件包缓存,并安装 docker
    $ sudo apt-get update $ sudo apt-get -y install docker-engine  
4)测试 docker 是否安装正确
   sudo docker run hello-world  
2. homeassisatant 的安装
整个安装过程十分的简单这里不再赘述可以直接参考此贴:
树莓派4+ docker+ homeassistant停止测试的博客-csdn博客树莓派跑docker:https://blog.csdn.net/weixin_43990954/article/details/107330798
3. tuya设备接入
首先登陆涂鸦 iot 平台 (tuya.com) 官网,在云开发->云管理->我的云项目中 创建一个新的云项目。在创建过程中开放方式选择自定义开发即可。
在hass的配置->设备与服务中添加新的集成,在搜索框搜索涂鸦即可。
在配置框中的名称和tuya云平台上的名称对应关系如下图所示:
编号云平台名hass配置名1access id/client id涂鸦物联网设备接入id2access secret/client secret:涂鸦物联网设备接入密码
其中的用户名和密码对应tuya 手机app上登陆的用户名和密码,不是iot平台这里不要混淆。
注:access id和secret可以在概况中查询到。
在手机app上根据提示将设备添加到关联,之后在云平台的设备中手动去关联app账户并且添加关联的设备到此云服务,对于设备较多的用户建议手动添加所需设备,不要全部导入否则云平台在检索设备时会非常卡。
添加成功狗手动刷新hass面板即可看到新添加的设备
4. 米家设备接入
可以直接参考此篇博客:
米家设备接入 home assistant:https://zhuanlan.zhihu.com/p/400997679
5. emq安装和关联hass
emq是个轻量级的mqtt服务器,将其直接运行在树莓派上可以作为esp8266和hass沟通的桥梁
1) 安装
详细的安装教程可以参考我的博文:
树莓派安装配置使用emq教程_argon_ghost的博客:https://blog.csdn.net/argon_ghost/article/details/106033912
2) 集成
直接在集成选项中添加mqtt组件即可,然后输入对应的ip和端口(一般情况下mqtt emq 的端口为1883)
6. 数据转发
为使esp8266可以控制米家设备和涂鸦设备,可以通过hass平台的自动化功能来完成,自动化类似于iphone上的自动化功能,用户可以自定义触发源触发信号,在触发信号到来的时候将触发信号触发起到控制硬件的目的,这里以esp8266通过mqtt触发米家空气净化器开机为例来做讲解,其余设备类似:
1) 配置
在 配置->场景自动化-> 新建自动化来创建一个新的blueprint自动化场景
2) 配置触发条件
因为我们需要在收到mqtt特定报文时候进行触发,我配套代码中的触发条件配置为如下图所示即可:
3) 配置动作
配置动作就是在条件触发后的相应,这里我们需要相应的是开机,相应配置如下图所示:
4) 注
配套代码实现中的其他触发条件均通过office/sensor1 topic进行,详细的匹配关系参数如下表所示:
编号触发目的参数1关机02开机13自动模式24睡眠模式3
7. 语音数据训练
syntiant 提供了完整的从训练到部署的一站式支持,可以通过如下链接打开此平台:
[在线训练平台]  https://studio.edgeimpulse.com
整个训练过程分为以下几个部分,详细的使用方法可以在edge impulse网站的wiki上查看。
通过麦克风将数据集录入
新建识别内容和工程
设置采用方式、采样率、采样窗口等参数
配置数据分类方式(这边我们直接使用nn 经典模型)
数据训练
  训练完毕之后直接点击deployment即可对数据进行导出,我项目中训练的模型如下图所示:
8. 模型下载
对于音频模型可以直接将模型烧录在板卡的内置flash中,在官方提供的demo程序中已经集成了flash的烧录功能,通过tool中的
flash_windows.bat工具即可将模型和程序烧录到板卡中。
    9. 程序编译
在生成好的模型中可以得到如下三个文件,model-parameters是我们新生成模型的参数和匹配关键字名称的c函数,需要替换代码工程src目录下的model-parameters,ei_model.bin就是ndp101芯片所需要的模型数据了。
在替换完成之后使用arduino ide从新编译工程并且在on_classification_changed函数中填充自己的应用代码即可,如下所示。
                                                                                                        /* generated by edge impulse * * permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the software), to deal * in the software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the software, and to permit persons to whom the software is * furnished to do so, subject to the following conditions: * * the above copyright notice and this permission notice shall be included in * all copies or substantial portions of the software. * * the software is provided as is, without warranty of any kind, express or * implied, including but not limited to the warranties of merchantability, * fitness for a particular purpose and noninfringement. in no event shall the * authors or copyright holders be liable for any claim, damages or other * liability, whether in an action of contract, tort or otherwise, arising from, * out of or in connection with the software or the use or other dealings in the * software. */#ifndef _ei_classifier_model_variables_h_#define _ei_classifier_model_variables_h_#include #include model_metadata.h// #include edge-impulse-sdk/classifier/ei_model_types.hconst char* ei_classifier_inferencing_categories[] = { source_auto, source_close, source_open, source_sleep, z_openset };uint8_t ei_dsp_config_66_axes[] = { 0 };const uint32_t ei_dsp_config_66_axes_size = 1;ei_dsp_config_audio_syntiant_t ei_dsp_config_66 = { 1, 1, 0.032f, 0.024f, 40, 512, 0, 0, 0.96875f};// const ei_model_performance_calibration_t ei_calibration = {// 1, /* integer version number */// (int32_t)(ei_classifier_raw_sample_count / ((ei_classifier_frequency > 0) ? ei_classifier_frequency : 1)) * 1000, /* model window */// 0.8f, /* default threshold */// (int32_t)(ei_classifier_raw_sample_count / ((ei_classifier_frequency > 0) ? ei_classifier_frequency : 1)) * 500, /* half of model window */// 0 /* don't use flags */// };#endif // _ei_classifier_model_metadata_h_ ‍  
10. 板卡下载过程分析
官方demo中模型下载是个十分有意思的过程,我特地分析了他的代码并且绘制了下面的流程图。整个流程分为了两个部分,通过串口rst板子让板子引导进入程序下载模式下载我们的代码程序,在程序下载成功并且成功运行之后延时一段时间并且发送模型下载串口指令,串口相应之后就开始分块去搬移模型数据(每次搬移1k),在搬移的过程中不断的通过spi将数据写入flash中。搬移成功之后通过串口回复给上位机,上位机即显示下载成功。如果有有兴趣可以自己去了解下syntiant.cpp文件中的runmanagementcommand()函数,系统就是调用这个函数完成了模型的下载。
11. esp8266 部分代码
esp82666 主要是mqtt通信功能的代码,这里直接将代码副出:
                                                                                                                                                                                                                                      #include /*wifi*/#include #include #include /*wifi参数*/#define wlan_ssid jdcxxxxx89e#define wlan_pass 199xxxxx614/*服务器参数*/#define aio_server 192.168.68.210#define aio_serverport 1883#define aio_username #define aio_key #define aio_cid espwificlient client;/*实例化mqtt设备*/adafruit_mqtt_client mqtt(&client, aio_server, aio_serverport, aio_cid, aio_username, aio_key);/*发布topic报文*/adafruit_mqtt_publish mqtt_pub1 = adafruit_mqtt_publish(&mqtt, office/sensor1);/*订阅topic报文*/adafruit_mqtt_subscribe mqtt_sub1 = adafruit_mqtt_subscribe(&mqtt, /feeds, mqtt_qos_0);static void mqttcallback(char *data, uint16_t len);static void wifi_connect();static void mqtt_init();static void mqtt_connect();void setup(){ serial.begin(115200); // 设置串口波特率为115200 wifi_connect(); //wifi连接 //smartconfig(); mqtt_init(); //mqtt连接初始化}void loop(){ /*mqtt 连接*/ mqtt_connect(); mqtt.processpackets(1000); //获取主题 if(digitalread(14) == low) /*关闭模式*/ { mqtt_pub1.publish(0); serial.println(0); } if(digitalread(16) == low) /*开启模式*/ { mqtt_pub1.publish(1); serial.println(1); } if(digitalread(15) == low) /*微风模式*/ { mqtt_pub1.publish(2); serial.println(2); } if(digitalread(13) == low) /*大风模式*/ { mqtt_pub1.publish(3); serial.println(3); } delay(200);}void smartconfig(){ wifi.mode(wifi_sta); serial.println(wait for smartconfig); delay(2000); // 等待配网 wifi.beginsmartconfig(); while (1) { serial.print(.); delay(500); if (wifi.smartconfigdone()) { serial.println(smartconfig success); serial.printf(ssid:%s, wifi.ssid().c_str()); serial.printf(psw:%s, wifi.psk().c_str()); wifi.setautoconnect(true); // 设置自动连接 break; } } serial.println(); serial.println(wifi connected); serial.println(ip address: ); serial.println(wifi.localip());}void wifi_connect(){ serial.println(f(adafruit mqtt demo)); // connect to wifi access point. serial.println(); serial.println(); serial.print(connecting to ); serial.println(wlan_ssid); wifi.begin(wlan_ssid, wlan_pass); while (wifi.status() != wl_connected) { delay(500); serial.print(.); } serial.println(); serial.println(wifi connected); serial.println(ip address: ); serial.println(wifi.localip());}// function to connect and reconnect as necessary to the mqtt server.// should be called in the loop function and it will take care if connecting.void mqtt_connect(){ int8_t ret; // stop if already connected. if (mqtt.connected()) { return; } serial.print(connecting to mqtt... ); uint8_t retries = 3; while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected serial.println(mqtt.connecterrorstring(ret)); serial.println(retrying mqtt connection in 10 seconds...); mqtt.disconnect(); delay(10000); // wait 10 seconds retries--; if (retries == 0) { // basically die and wait for wdt to reset me while (1) ; } } serial.println(mqtt connected!);}void mqtt_init(){ mqtt_sub1.setcallback(mqttcallback); // setup mqtt subscription for time feed. mqtt.subscribe(&mqtt_sub1); // function definition further below. mqtt_connect(); mqtt.processpackets(1000);}/** * @brief mqtt接收回调 * * @param data * @param len */void mqttcallback(char *data, uint16_t len){ serial.print(hey we're in a onoff callback, the button value is: ); serial.println(data);}  
  总结
之前一直觉得tinyml是一个神秘莫测十分高大上的东西,通过这次活动对深度学习、tinyml等有了更深刻的了解。恰巧最近公司项目中有一个需要场景识别的需求,后续我也将采用esp32作为主控尝试引入tinyml。syntiant ndp101 板卡虽然功能强大,但是受限于资料不完全开源,目前我们还是不能了解到其完整的使用方法。希望后续能开放其低层资料,让大家来学习和使用,毕竟这么强一块板卡让更多geek用起来才能发挥其强劲的性能。目前来看syntiant 的arduino demo程序仅仅是一个框架性质的demo,没有办法真的用于项目,math匹配函数在实际运行时候,作为死循环运行在低层,这点对新手来说十分的不友好,希望后续官方更进一步优化arduino下的代码框架。


迎接第四次工业革命浪潮 NI祭出新版cRIO/myRIO
英国Pickering公司发布全新免费在线使用的微波开关设计工具
5G基础建设势在必行 基站芯片的前哨战已经开打
基于单片机的系统扩展
区块链在电子商务行业中的应用介绍
基于Syntiant TinyML board的语音助手设计
电容器充放电的原理与注意事项
小米新品笔记本Air发布,升级啊升级,厉害了我的雷军
千元机之战:你选红米Note4X还是360N5?
蓝牙Mesh推动“照明即平台”概念落实
电机内磁场的增磁和去磁控制
科大飞讯公布财报有大幅度上涨
中国电信5G试点首定六大城市,5G步入新阶段
PCI Express体系结构导读笔记之寄存器和配置
是USB也是摄像头的神奇“眼睛”
两岸共谋做大TD-SCDMA 时机成熟将在台测试
新能源的下一个风口,钛酸锂电池是锂电池里的“奇葩”
阿里云Teambition待办全新升级,增加了哪些新功能
Cadence将出席2020中国集成电路峰会
美国步步紧逼,华为被迫无奈推出鸿蒙系统