请选择 进入手机版 | 继续访问电脑版

『瀚思彼岸』» 智能家居第一站

 找回密码
 立即注册
查看: 4133|回复: 103

[新奇玩法] 【1027更新,无需拦截词】让小爱支持控制任意自制设备

  [复制链接]

5

主题

161

帖子

1092

积分

超级版主

Rank: 8Rank: 8

积分
1092
金钱
926
HASS币
20

突出贡献

发表于 2018-10-17 14:45:02 | 显示全部楼层 |阅读模式
本帖最后由 flashsoft 于 2018-10-27 22:46 编辑

前提是已经拥有一台SSH过的小爱
重启就会丢SSH的绿板小爱可以见后面的物理解决方案

一键安装包测试版
在小爱上执行(注意,会验证nodered服务是否正常,需要先导入后文里的流程进行)
[AppleScript] 纯文本查看 复制代码
curl -s "https://raw.githubusercontent.com/FlashSoft/mico/master/install.sh?`date +%s`" > /root/install.sh
sh /root/install.sh


一键安装包截图

屏幕快照 2018-10-26 下午9.33.11.png

与之匹配的nodered的流程如下, 自己可以发挥自己的想象力去写自己的流

屏幕快照 2018-10-26 下午9.29.27.png

NodeRed流程,可以自己导入进去玩

[AppleScript] 纯文本查看 复制代码
[{"id":"57fc0f09.83c4c","type":"http in","z":"2a168bed.f914f4","name":"","url":"/miai","method":"post","upload":false,"swaggerDoc":"","x":80,"y":225,"wires":[["9813ba6d.7261f8","b79563dc.c75a9"]]},{"id":"9813ba6d.7261f8","type":"debug","z":"2a168bed.f914f4","name":"全局调试","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":275,"y":140,"wires":[]},{"id":"aa970ca6.ec15a","type":"http in","z":"2a168bed.f914f4","name":"","url":"/miai","method":"get","upload":false,"swaggerDoc":"","x":75,"y":95,"wires":[["58f84781.1c1e68"]]},{"id":"42c15019.d03be","type":"http response","z":"2a168bed.f914f4","name":"","statusCode":"200","headers":{},"x":450,"y":95,"wires":[]},{"id":"58f84781.1c1e68","type":"function","z":"2a168bed.f914f4","name":"小爱拦截词","func":"// 多个关键词以竖线分割\nmsg.payload = `没有|未知`\nreturn msg","outputs":1,"noerr":0,"x":280,"y":95,"wires":[["42c15019.d03be"]]},{"id":"b79563dc.c75a9","type":"function","z":"2a168bed.f914f4","name":"处理捕获指令","func":"// 指令词列表\n// 一行一个设备,对于一个设备的多个指令词可用,分割\nconst list = [\n  ['打开新风'],\n  ['关闭新风'],\n  ['室内空气质量']\n]\n\n\n// === 以下内容大多数情况不用改 ====\nlet queries = []\ntry { queries = JSON.parse(msg.payload.asr).response.queries.map(it => it.query) } catch (e) { }\nmsg.queries = queries\nmsg.list = list\nmsg.matched = !!list.filter((item, index) => {\n  item.matched = !!queries.filter(it => ~item.indexOf(it)).length\n  item.matched && (msg.matched_index = index)\n  return item.matched\n}).length\n\nreturn msg","outputs":1,"noerr":0,"x":290,"y":225,"wires":[["a26ebc66.1c77f","672bee8b.a306"]]},{"id":"4110603.07d34a","type":"switch","z":"2a168bed.f914f4","name":"","property":"matched_index","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"},{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"2","vt":"str"},{"t":"eq","v":"3","vt":"str"},{"t":"eq","v":"4","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":630,"y":385,"wires":[["4e1a7bcb.af07c4","46fae169.a3aa2"],["42de8cd6.db79e4","231a209b.5a602"],["e8994c49.c91b5","8941d489.2d0158","60d41877.24a8d8"],[],[]]},{"id":"4e1a7bcb.af07c4","type":"api-call-service","z":"2a168bed.f914f4","name":"打开所有FFU","server":"4ccda836.aef688","service_domain":"switch","service":"turn_on","data":"{\"entity_id\":\"switch.original_xiaomi_mi_smart_wifi_socket,switch.xiaomi_mi_smart_wifi_usb_socket\"}","mergecontext":"","x":860,"y":360,"wires":[["fec8efed.c3c45"]]},{"id":"46fae169.a3aa2","type":"api-call-service","z":"2a168bed.f914f4","name":"打开所有Airx","server":"4ccda836.aef688","service_domain":"fan","service":"turn_on","data":"{\"entity_id\":\"fan.airx,fan.airx2\"}","mergecontext":"","x":860,"y":411,"wires":[["fec8efed.c3c45"]]},{"id":"42a42cc6.e51e74","type":"function","z":"2a168bed.f914f4","name":"TTS文本","func":"msg.payload = '新风打开成功'\nreturn msg","outputs":1,"noerr":0,"x":1160,"y":360,"wires":[["13bb5f0.c37b7a1"]]},{"id":"42de8cd6.db79e4","type":"api-call-service","z":"2a168bed.f914f4","name":"关闭所有FFU","server":"4ccda836.aef688","service_domain":"switch","service":"turn_off","data":"{\"entity_id\":\"switch.original_xiaomi_mi_smart_wifi_socket,switch.xiaomi_mi_smart_wifi_usb_socket\"}","mergecontext":"","x":860,"y":525,"wires":[["447d706a.41662"]]},{"id":"231a209b.5a602","type":"api-call-service","z":"2a168bed.f914f4","name":"关闭所有Airx","server":"4ccda836.aef688","service_domain":"fan","service":"turn_off","data":"{\"entity_id\":\"fan.airx,fan.airx2\"}","mergecontext":"","x":860,"y":575,"wires":[["447d706a.41662"]]},{"id":"e8994c49.c91b5","type":"api-current-state","z":"2a168bed.f914f4","name":"PM2.5","server":"4ccda836.aef688","halt_if":"","override_topic":true,"override_payload":true,"entity_id":"sensor.m1_pm25","x":845,"y":695,"wires":[["442c8451.3029ac"]]},{"id":"8941d489.2d0158","type":"api-current-state","z":"2a168bed.f914f4","name":"温度","server":"4ccda836.aef688","halt_if":"","override_topic":true,"override_payload":true,"entity_id":"sensor.m1_temperature","x":845,"y":750,"wires":[["442c8451.3029ac"]]},{"id":"60d41877.24a8d8","type":"api-current-state","z":"2a168bed.f914f4","name":"湿度","server":"4ccda836.aef688","halt_if":"","override_topic":true,"override_payload":true,"entity_id":"sensor.m1_humidity","x":845,"y":805,"wires":[["442c8451.3029ac"]]},{"id":"442c8451.3029ac","type":"join","z":"2a168bed.f914f4","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1015,"y":694,"wires":[["47e7f395.074fec"]]},{"id":"430c565c.8c74b8","type":"comment","z":"2a168bed.f914f4","name":"在这里统一配置响应词以及TTS文案","info":"","x":350,"y":185,"wires":[]},{"id":"a26ebc66.1c77f","type":"debug","z":"2a168bed.f914f4","name":"捕获调试","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":510,"y":140,"wires":[]},{"id":"672bee8b.a306","type":"switch","z":"2a168bed.f914f4","name":"","property":"matched","propertyType":"msg","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":480,"y":225,"wires":[["d81bcfed.b26de"],["4110603.07d34a"]]},{"id":"447d706a.41662","type":"join","z":"2a168bed.f914f4","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1025,"y":525,"wires":[["cea11ca1.25103"]]},{"id":"fec8efed.c3c45","type":"join","z":"2a168bed.f914f4","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1020,"y":360,"wires":[["42a42cc6.e51e74"]]},{"id":"90f763d2.6e01f","type":"comment","z":"2a168bed.f914f4","name":"在这里配置小爱的拦截词","info":"","x":320,"y":50,"wires":[]},{"id":"47e7f395.074fec","type":"function","z":"2a168bed.f914f4","name":"TTS文本","func":"msg.payload = `\n当前室内PM2.5为${msg.payload['sensor.m1_pm25']},\n温度${msg.payload['sensor.m1_temperature']}摄氏度,\n湿度${msg.payload['sensor.m1_humidity']}\n`\nreturn msg","outputs":1,"noerr":0,"x":1160,"y":694,"wires":[["4fdc4b2b.e28ff4"]]},{"id":"9e028a2b.92f5f8","type":"comment","z":"2a168bed.f914f4","name":"打开净化器","info":"","x":850,"y":320,"wires":[]},{"id":"149bebb6.7f1b14","type":"comment","z":"2a168bed.f914f4","name":"关闭净化器","info":"","x":850,"y":485,"wires":[]},{"id":"a3d5f3e0.112ac","type":"comment","z":"2a168bed.f914f4","name":"查询空气质量","info":"","x":865,"y":655,"wires":[]},{"id":"d81bcfed.b26de","type":"function","z":"2a168bed.f914f4","name":"TTS文本","func":"// 如果是被nodered捕获了,则在小爱提示基础上补了一个嘿嘿嘿,便于辨识\ntry{\n  const res = JSON.parse(msg.payload.res)\n  msg.payload = `嘿嘿嘿,${res.answer[0].text}`\n}\ncatch(e){\n  msg.payload = '嘿嘿嘿,你说的是啥'    \n}\n\nreturn msg","outputs":1,"noerr":0,"x":640,"y":218,"wires":[["bce07803.b56008"]]},{"id":"bce07803.b56008","type":"http response","z":"2a168bed.f914f4","name":"","statusCode":"200","headers":{},"x":840,"y":218,"wires":[]},{"id":"13bb5f0.c37b7a1","type":"http response","z":"2a168bed.f914f4","name":"","statusCode":"200","headers":{},"x":1305,"y":360,"wires":[]},{"id":"cea11ca1.25103","type":"function","z":"2a168bed.f914f4","name":"TTS文本","func":"msg.payload = '新风关闭成功'\nreturn msg","outputs":1,"noerr":0,"x":1160,"y":525,"wires":[["4b41eae3.ab1c54"]]},{"id":"4b41eae3.ab1c54","type":"http response","z":"2a168bed.f914f4","name":"","statusCode":"200","headers":{},"x":1305,"y":525,"wires":[]},{"id":"4fdc4b2b.e28ff4","type":"http response","z":"2a168bed.f914f4","name":"","statusCode":"200","headers":{},"x":1305,"y":693,"wires":[]},{"id":"4ccda836.aef688","type":"server","z":"","name":"HASS","legacy":false}]




绿板小爱的不用老连着TTL开启dropbear的方案
供大家参考,直接使用ESP8266或其他单片机,直发串口指令给小爱,开启dropbear


[AppleScript] 纯文本查看 复制代码
// @author FlashSoft

// ESP8266 定时重启dropbear程序
// 用于解决不拆机开启绿板小爱的SSH功能
// 默认密码为空

// 操作流程: 从小爱的12V口接电,用AMS1117或更小的更省电的HT7333降压成3.3V给ESP8266供电
// 按照如下线序接到小爱上,把固件烧写进ESP8266即可,可以使用arduino ide或platformio进行烧写

// 接线顺序:
// rxd GPIO14 (D5) ==> 小爱 TXD
// txd GPIO12 (D6) ==> 小爱 RXD
// gnd ==> 小爱GND
#include <Arduino.h>
#include <SoftwareSerial.h>
SoftwareSerial swSer(14, 12, false, 256);

void setup() {
  Serial.begin(115200);
  swSer.begin(115200);
}

void loop() {
  Serial.println("执行指令");
  // 向串口发送指令
  // 判断dropbear进程个数,如果是0个,则启动重新生成key并启动dropbear
  swSer.println("[[ \"`ps|grep 'dropbear -r /etc/dropbear/dropbear_rsa_host_key'|grep -v grep|wc -l`\" -eq \"0\" ]] && rm /etc/dropbear/dropbear_rsa_host_key && dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key && dropbear -r /etc/dropbear/dropbear_rsa_host_key");
  // 10秒执行一次
  delay(10000);
}




================ 历史内容 ====================
在大神们已经研究完成的基础上,简单写了个shell程序,在小爱上运行
主要逻辑是:
1、检测小米服务器响应的日志变化
2、捕获响应日志中,如果是未知设备(也等于你自己的自制设备)
3、则让小爱停止播放找到未知设备的播报
4、通过curl转发asr和res日志内容给自己的远端接口
5、远端接口去跟HA通讯来控制自制设备(这个等大神们写HA插件)
6、远端接口返回需要小爱播报的文本内容
7、如果之前小爱在播放音乐的话就接着播放音乐



评分

参与人数 9金钱 +95 HASS币 +20 收起 理由
Lee + 6 膜拜大神!感谢分享!
hhyhxc13 + 5 在下对你的景仰犹如滔滔长江之水,连绵不绝.
windgo + 10 大神666!
bilubin + 5 大神666!分都加不过来了
mars.z + 4 无法用言语表达仰慕之情~总之一个字“溜”.
baisetiankong + 5 膜拜大神!
lidicn + 20 已搭建成功,真香!
Jones + 20 + 20 给力,f大辛苦了!
neroxps + 20 大神666!

查看全部评分

回复

使用道具 举报

13

主题

210

帖子

1830

积分

论坛技术达人

积分
1830
金钱
1605
HASS币
0

教程狂人

发表于 2018-10-28 09:00:55 | 显示全部楼层
提供一个节点例子哈,可以自动从ha获取设备并控制
TIM图片20181028085920.png 小爱控制-自动获取HASS设备.txt (14.97 KB, 下载次数: 79)
回复

使用道具 举报

26

主题

991

帖子

3630

积分

超级版主

Nero

Rank: 8Rank: 8

积分
3630
金钱
2619
HASS币
40
发表于 2018-10-17 15:24:22 | 显示全部楼层
最难的是服务器端口部分~····
Nero
回复

使用道具 举报

7

主题

141

帖子

658

积分

高级会员

Rank: 4

积分
658
金钱
517
HASS币
0
发表于 2018-10-17 15:31:02 | 显示全部楼层
先点赞在看教程
回复

使用道具 举报

4

主题

159

帖子

762

积分

高级会员

Rank: 4

积分
762
金钱
598
HASS币
20
发表于 2018-10-17 15:32:46 | 显示全部楼层
准备收一个黑板的小爱~一起折腾
回复

使用道具 举报

0

主题

32

帖子

148

积分

注册会员

Rank: 2

积分
148
金钱
116
HASS币
0
发表于 2018-10-17 15:35:01 | 显示全部楼层
先评论在看教程
回复

使用道具 举报

12

主题

453

帖子

1450

积分

金牌会员

Rank: 6Rank: 6

积分
1450
金钱
997
HASS币
0
发表于 2018-10-17 15:35:01 | 显示全部楼层
太牛逼 了。我服我服~
回复

使用道具 举报

0

主题

125

帖子

806

积分

高级会员

Rank: 4

积分
806
金钱
681
HASS币
0
发表于 2018-10-17 15:41:33 | 显示全部楼层
下了很大功夫了,点赞
回复

使用道具 举报

4

主题

92

帖子

469

积分

论坛分享达人

积分
469
金钱
377
HASS币
0
发表于 2018-10-17 15:50:24 | 显示全部楼层
看样子,要去买一个小爱同学了,要是能改变唤醒的语音就更好玩了
回复

使用道具 举报

0

主题

42

帖子

342

积分

论坛积极会员

积分
342
金钱
300
HASS币
0
发表于 2018-10-17 15:56:31 | 显示全部楼层
大神利害
回复

使用道具 举报

5

主题

161

帖子

1092

积分

超级版主

Rank: 8Rank: 8

积分
1092
金钱
926
HASS币
20

突出贡献

 楼主| 发表于 2018-10-17 16:01:16 | 显示全部楼层
本帖最后由 flashsoft 于 2018-10-18 13:22 编辑
neroxps 发表于 2018-10-17 15:24
最难的是服务器端口部分~····

更正:如果是设备找不到的asr里是没有意图字段的


认同,不过转发给服务端的时候,带上了小米生成asr和res,里面毕竟都已经做过语义分析以及意图处理,至少现在服务端不需要做分词等一些基础的事情了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2018-11-22 04:54 , Processed in 0.247592 second(s), 46 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表