『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 203162|回复: 215

[新奇玩法] 【20190614,修复日志路径】让小爱支持控制任意自制.....

  [复制链接]

9

主题

228

帖子

2052

积分

超级版主

Rank: 8Rank: 8

积分
2052
金钱
1799
HASS币
100

突出贡献

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


官方升级了日志文件以及格式,已调整日志路径以及对应的自动拦截词的判定






                               
登录/注册后可看大图


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

一键安装包测试版
在小爱上执行(注意,会验证nodered服务是否正常,需要先导入后文里的流程进行)
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流程,可以自己导入进去玩


[{"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


// @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);
}




关于putty中,程序显示乱码问题可以参考的@windgo 解决方案:
參考連結:https://my.oschina.net/yuyang/blog/176002

打开putty主程序
选择window-〉Appearance-〉Font settings-〉Change...
选择Fixedsys字体,字符集选择CHINESE_GB2312。
在window-〉Appearance -〉Translation中,
Received data assumed to be in which character set 中
把Use font encoding改为UTF-8



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






评分

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

查看全部评分

回复

使用道具 举报

22

主题

359

帖子

3763

积分

元老级技术达人

积分
3763
金钱
3369
HASS币
100

教程狂人

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

使用道具 举报

40

主题

3056

帖子

1万

积分

超级版主

Nero

Rank: 8Rank: 8

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

使用道具 举报

14

主题

245

帖子

3536

积分

论坛元老

Rank: 8Rank: 8

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

使用道具 举报

8

主题

220

帖子

1330

积分

金牌会员

Rank: 6Rank: 6

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

使用道具 举报

0

主题

54

帖子

297

积分

中级会员

Rank: 3Rank: 3

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

使用道具 举报

12

主题

545

帖子

2041

积分

金牌会员

Rank: 6Rank: 6

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

使用道具 举报

2

主题

171

帖子

2494

积分

金牌会员

Rank: 6Rank: 6

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

使用道具 举报

7

主题

138

帖子

931

积分

高级会员

Rank: 4

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

使用道具 举报

0

主题

55

帖子

2234

积分

论坛积极会员

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

使用道具 举报

9

主题

228

帖子

2052

积分

超级版主

Rank: 8Rank: 8

积分
2052
金钱
1799
HASS币
100

突出贡献

 楼主| 发表于 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, 2024-12-22 16:44 , Processed in 0.074143 second(s), 39 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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