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

 找回密码
 立即注册
查看: 44645|回复: 11

[经验分享] 蓝牙网关 TTL->MQTT->Node-RED 接入米家门锁

[复制链接]

24

主题

356

帖子

4721

积分

元老级技术达人

积分
4721
金钱
4340
HASS币
120
发表于 2020-12-16 16:07:30 | 显示全部楼层 |阅读模式
本帖最后由 121665460 于 2020-12-16 16:14 编辑

效果:
door2.png

为什么要通过Node-RED接入?


1.接入更加方便
2.重启HA后状态可以保持
3.可以通过Node-RED推送开门信息到微信
4.调试更加方便
5.无需配置YAML
6.还可以干很多事情。。。

door.png


[
    {
        "id": "ae8f6ee1.4f4d",
        "type": "tab",
        "label": "门锁",
        "disabled": false,
        "info": ""
    },
    {
        "id": "703fd0c.9239f3",
        "type": "mqtt in",
        "z": "ae8f6ee1.4f4d",
        "name": "HA-MQTT",
        "topic": "mqttl/#",
        "qos": "2",
        "datatype": "auto",
        "broker": "630cffdb.b6934",
        "x": 80,
        "y": 300,
        "wires": [
            [
                "4aad887.af91c78"
            ]
        ]
    },
    {
        "id": "5682ddf1.12e694",
        "type": "debug",
        "z": "ae8f6ee1.4f4d",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 730,
        "y": 20,
        "wires": []
    },
    {
        "id": "4aad887.af91c78",
        "type": "switch",
        "z": "ae8f6ee1.4f4d",
        "name": "门锁",
        "property": "topic",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "mqttl/1011415348",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 230,
        "y": 300,
        "wires": [
            [
                "ed581fd9.73fe5"
            ]
        ]
    },
    {
        "id": "ed581fd9.73fe5",
        "type": "function",
        "z": "ae8f6ee1.4f4d",
        "name": "",
        "func": "const topic = msg.topic\nconst val = msg.payload\nconst val2 = parseInt(val,16)\n\nif(topic.indexOf("4110",topic.length - 4) != -1 ){\n    msg.type = "shuoshe"\n    switch(val2){\n        case 0:\n            msg.payload = "门未关"\n            break;\n        case 4:\n            msg.payload = "已关,未上锁"\n            break;\n        case 5:\n            msg.payload = "已上锁"\n            break;\n        case 6:\n            msg.payload = "已反锁"\n            break;\n        case 7:\n            msg.payload = "已上锁和反锁"\n            break;\n    }\n}\n\nif(topic.indexOf("7",topic.length - 1) != -1 ){\n    msg.type = "status"\n    var temp = val.substring(9,10)\n    temp = parseInt(temp,16)\n    switch(temp){\n        case 0:\n            msg.payload = "门开了"\n            break;\n        case 1:\n            msg.payload = "门关了"\n            break;\n        case 2:\n            msg.payload = "门超时未关"\n            break;\n        case 3:\n            msg.payload = "敲门"\n            break;\n        case 4:\n            msg.payload = "撬门"\n            break;\n        case 5:\n            msg.payload = "门卡住"\n            break;\n    }\n}\n\nif(topic.indexOf("11",topic.length - 2) != -1 ){\n    msg.type = "way"\n    var keyID = val.substring(8,16)\n    switch(keyID){\n        case "c0de0000":\n            msg.payload = "错误密码频繁开锁"\n            break;\n        case "c0de0001":\n            msg.payload = "错误指纹频繁开锁"\n            break;\n        case "c0de0002":\n            msg.payload = "操作超时(密码输入超时)"\n            break;\n        case "c0de0003":\n            msg.payload = "撬锁"\n            break;\n        case "c0de0004":\n            msg.payload = "重置按键按下"\n            break;\n        case "c0de0005":\n            msg.payload = "错误钥匙频繁开锁"\n            break;\n        case "c0de0006":\n            msg.payload = "钥匙孔异物"\n            break;\n        case "c0de0007":\n            msg.payload = "钥匙未取出"\n            break;\n        case "c0de0008":\n            msg.payload = "错误NFC频繁开锁"\n            break;\n        case "c0de0009":\n            msg.payload = "超时未按要求上锁"\n            break;\n        case "c0de0010":\n            msg.payload = "电量低于10%"\n            break;\n        case "c0de0011":\n            msg.payload = "电量低于5%"\n            break;\n        case "c0de0012":\n            msg.payload = "指纹传感器"\n            break;\n        case "ffffffff":\n            msg.payload = ""\n            break;\n        case "00000000":\n            msg.payload = "管理员"\n            break;\n        case "80010000":\n            msg.payload = "指纹0"\n            break;\n        case "80010001":\n            msg.payload = "指纹1"\n            break;\n    }\n    var way = val.substring(16,17)\n    way = parseInt(way,16)\n    switch(way){\n        case 0:\n            msg.payload += "蓝牙"\n            break;\n        case 1:\n            msg.payload += "密码"\n            break;\n        case 2:\n            msg.payload += "指纹"\n            break;\n        case 3:\n            msg.payload += "应急钥匙"\n            break;\n        case 4:\n            msg.payload += "转盘方式"\n            break;\n        case 5:\n            msg.payload += "NFC"\n            break;\n        case 6:\n            msg.payload += "一次性密码"\n            break;\n        case 7:\n            msg.payload += "双重验证"\n            break;\n        case 10:\n            msg.payload += "手动"\n            break;\n        case 11:\n            msg.payload += "自动"\n            break;\n    }\n    var action = val.substring(17,18)\n    action = parseInt(action,16)\n    switch(action){\n        case 0:\n            msg.payload += "门外开锁"\n            break;\n        case 1:\n            msg.payload += "上提把手锁门"\n            break;\n        case 2:\n            msg.payload += "门内反锁"\n            break;\n        case 3:\n            msg.payload += "解除反锁"\n            break;\n        case 4:\n            msg.payload += "门内开锁"\n            break;\n        case 5:\n            msg.payload += "门内上锁"\n            break;\n        case 6:\n            msg.payload += "开启童锁"\n            break;\n        case 7:\n            msg.payload += "关闭童锁"\n            break;\n        case 15:\n            msg.payload += "异常"\n            break;\n    }\n}\n\nif(topic.indexOf("4106",topic.length - 4) != -1 ){\n    msg.type = "battery"\n}\n\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 370,
        "y": 300,
        "wires": [
            [
                "5682ddf1.12e694",
                "a621be17.82d97"
            ]
        ]
    },
    {
        "id": "a621be17.82d97",
        "type": "switch",
        "z": "ae8f6ee1.4f4d",
        "name": "",
        "property": "type",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "status",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "way",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "battery",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 3,
        "x": 530,
        "y": 300,
        "wires": [
            [
                "5638e5bc.515f9c"
            ],
            [
                "3a254e37.1cd942"
            ],
            [
                "2f5ee3bf.60212c"
            ]
        ]
    },
    {
        "id": "5638e5bc.515f9c",
        "type": "ha-entity",
        "z": "ae8f6ee1.4f4d",
        "name": "门锁状态",
        "server": "8ed410d3.e4c06",
        "version": 1,
        "debugenabled": false,
        "outputs": 1,
        "entityType": "sensor",
        "config": [
            {
                "property": "name",
                "value": "door_status"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": "mdi:door-closed-lock"
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "x": 720,
        "y": 220,
        "wires": [
            []
        ]
    },
    {
        "id": "2f5ee3bf.60212c",
        "type": "ha-entity",
        "z": "ae8f6ee1.4f4d",
        "name": "门锁电量",
        "server": "8ed410d3.e4c06",
        "version": 1,
        "debugenabled": false,
        "outputs": 1,
        "entityType": "sensor",
        "config": [
            {
                "property": "name",
                "value": "door_battery"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": "mdi:battery-charging"
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "x": 720,
        "y": 360,
        "wires": [
            []
        ]
    },
    {
        "id": "3a254e37.1cd942",
        "type": "ha-entity",
        "z": "ae8f6ee1.4f4d",
        "name": "门锁操作记录",
        "server": "8ed410d3.e4c06",
        "version": 1,
        "debugenabled": false,
        "outputs": 1,
        "entityType": "sensor",
        "config": [
            {
                "property": "name",
                "value": "door_use"
            },
            {
                "property": "device_class",
                "value": ""
            },
            {
                "property": "icon",
                "value": "mdi:door"
            },
            {
                "property": "unit_of_measurement",
                "value": ""
            }
        ],
        "state": "payload",
        "stateType": "msg",
        "attributes": [],
        "resend": true,
        "outputLocation": "",
        "outputLocationType": "none",
        "inputOverride": "allow",
        "x": 740,
        "y": 300,
        "wires": [
            []
        ]
    },
    {
        "id": "630cffdb.b6934",
        "type": "mqtt-broker",
        "name": "",
        "broker": "172.30.32.1",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": false,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "uuid",
        "birthQos": "1",
        "birthRetain": "true",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    },
    {
        "id": "8ed410d3.e4c06",
        "type": "server",
        "name": "Home Assistant",
        "addon": true
    }
]
感谢XCray提供相关固件
TTL->MQTT 固件见:
https://bbs.hassbian.com/thread-10434-1-1.html

其它蓝牙设备同理:
WX20201216-160211@2x.png




评分

参与人数 1金钱 +20 HASS币 +20 收起 理由
+ 20 + 20 感谢楼主分享!

查看全部评分

回复

使用道具 举报

15

主题

438

帖子

4348

积分

论坛元老

Rank: 8Rank: 8

积分
4348
金钱
3910
HASS币
0
发表于 2020-12-16 16:21:32 | 显示全部楼层
蓝牙设备现在的最佳接入方案是 多模网关 + 自定义固件
回复

使用道具 举报

40

主题

3056

帖子

1万

积分

超级版主

Nero

Rank: 8Rank: 8

积分
11149
金钱
8042
HASS币
182
发表于 2020-12-17 14:52:52 | 显示全部楼层
米家的蓝牙貌似有问题,还是会丢消息~

米家自己的日子也丢
Nero
回复

使用道具 举报

24

主题

356

帖子

4721

积分

元老级技术达人

积分
4721
金钱
4340
HASS币
120
 楼主| 发表于 2020-12-17 15:22:27 | 显示全部楼层
neroxps 发表于 2020-12-17 14:52
米家的蓝牙貌似有问题,还是会丢消息~

米家自己的日子也丢

是的,很蛋疼。传感器我改用esphome了,就是它不支持门锁。。
回复

使用道具 举报

0

主题

19

帖子

108

积分

注册会员

Rank: 2

积分
108
金钱
89
HASS币
0
发表于 2020-12-18 01:57:31 | 显示全部楼层
整了这么多,请问楼主,可以实现homekit远程开锁吗?
回复

使用道具 举报

40

主题

3056

帖子

1万

积分

超级版主

Nero

Rank: 8Rank: 8

积分
11149
金钱
8042
HASS币
182
发表于 2020-12-18 09:01:27 | 显示全部楼层
kai666 发表于 2020-12-18 01:57
整了这么多,请问楼主,可以实现homekit远程开锁吗?

分享无罪

不可以,锁本身不支持 homekit 的话就无法支持远程开锁。

但是我们这种前期买的米家的锁都不支持 homekit 的只能这样曲线救国了。
Nero
回复

使用道具 举报

1

主题

78

帖子

330

积分

中级会员

Rank: 3Rank: 3

积分
330
金钱
252
HASS币
0
发表于 2021-1-2 19:49:19 | 显示全部楼层
好贴,学习
回复

使用道具 举报

6

主题

259

帖子

2998

积分

金牌会员

Rank: 6Rank: 6

积分
2998
金钱
2739
HASS币
0
发表于 2021-1-4 13:51:25 | 显示全部楼层
提示错误
ST`4$32_SG_IM[B7PQX8L@4.png
回复

使用道具 举报

6

主题

259

帖子

2998

积分

金牌会员

Rank: 6Rank: 6

积分
2998
金钱
2739
HASS币
0
发表于 2021-2-20 17:30:08 来自手机 | 显示全部楼层
添加以后怎么没有实体呢?也没有报错
回复

使用道具 举报

6

主题

259

帖子

2998

积分

金牌会员

Rank: 6Rank: 6

积分
2998
金钱
2739
HASS币
0
发表于 2021-2-20 17:36:09 来自手机 | 显示全部楼层
能不能打包发一下。错误无法导入
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-23 05:42 , Processed in 1.248662 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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