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

 找回密码
 立即注册
查看: 4499|回复: 24

[经验分享] node-red实现tp路由器mesh节点室内手机定位

[复制链接]

6

主题

217

帖子

1321

积分

金牌会员

Rank: 6Rank: 6

积分
1321
金钱
1104
HASS币
0
发表于 2022-8-11 05:13:36 | 显示全部楼层 |阅读模式
本帖最后由 leonarddo 于 2022-8-13 06:18 编辑

由于家里使用的tp的易展mesh路由,研究了一下如何用路由实现一个室内定位的效果。做了一个demo出来,抛砖引玉,能实现基本需求。第一次使用node-red,语法或逻辑有什么问题还请指出。如图,画圈部分按需进行修改后即可使用。

图中画圈的三个部分根据实际情况修改

图中画圈的三个部分根据实际情况修改



所需额外组件:
homeassistant额外需要安装的组件
Node-RED Companion


node-red使用中额外需要安装的组件
node-red-contrib-loop
node-red-contrib-home-assistant-websocket


实现代码部分:
将该部分复制后导入,再根据实际情况修改即可。
[{"id":"6d323e565cdb008c","type":"function","z":"88f6c9b0caa7d564","name":"","func":"encrypt_password = encrypt_pwd(msg.password);\nheaders = {'Content-Type': 'application/json; charset=UTF-8'};\npayload = {'method':'do','login':{'password':encrypt_password}};\nurl='http://'+msg.ip+'/';\nmsg.url=url;\nmsg.headers = {};\nmsg.headers['Content-Type'] = 'application/json; charset=UTF-8';\nmsg.payload = payload;\n\nfor (const key in msg.room) {\n    msg.room[key]=msg.room[key].toLowerCase();\n    msg.room[key]=msg.room[key].replace(/:/g, '-')\n}\nfor (const key in msg.user) {\n    msg.user[key]=msg.user[key].toLowerCase();\n    msg.user[key]=msg.user[key].replace(/:/g, '-')\n}\n\nreturn msg;\n\nfunction encrypt_pwd(password){\n    input1 = \"RDpbLfCPsJZ7fiv\";\n    input3 = \"yLwVl0zKqws7LgKPRQ84Mdt708T1qQ3Ha7xv3H7NyU84p21BriUWBU43odz3iP4rBL3cD02KZciXTysVXiV8ngg6vL48rPJyAUw0HurW20xqxv9aYb4M9wK1Ae0wlro510qXeU07kV57fQMc8L6aLgMLwygtc0F10a0Dg70TOoouyFhdysuRMO51yY5ZlOZZLEal1h0t9YQW0Ko7oBwmCAHoic4HYbUyVeU3sfQ1xtXcPcf1aT303wAQhv66qzW\";\n    len1 = input1.length;\n    len2 = msg.password.length;\n    dictionary = input3;\n    lenDict = dictionary.length;\n    output = '';\n    if (len1 > len2){\n        length = len1;\n    }else{\n        length = len2;\n    }\n    index = 0;\n    while (index < length){\n        cl = 187;\n        cr = 187;\n        if (index >= len1){\n            cr = password[index].charCodeAt(0);\n        }else if (index >= len2){\n            cl = input1[index].charCodeAt(0);\n        }else{\n            cl = input1[index].charCodeAt(0);\n            cr = password[index].charCodeAt(0);\n        }\n        index += 1;\n        output = output + String.fromCharCode((dictionary[cl^cr]).charCodeAt(0) % lenDict);\n    }\n    return output;\n}\n\n\n","outputs":1,"noerr":51,"initialize":"","finalize":"","libs":[],"x":370,"y":220,"wires":[["84218d326015b8c4"]]},{"id":"aba4edad6372bed8","type":"inject","z":"88f6c9b0caa7d564","name":"","props":[{"p":"password","v":"你的密码","vt":"str"},{"p":"ip","v":"路由器ip","vt":"str"},{"p":"room","v":"{\"房间1\":\"房间1路由的mac\",\"房间2\":\"aa-bb-cc-dd-ee-ff\"}","vt":"json"},{"p":"user","v":"{\"设备1\":\"设备1的mac\",\"设备2\":\"设备2的mac\",\"设备3\":\"设备3的mac\"}","vt":"json"}],"repeat":"4","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":250,"y":220,"wires":[["6d323e565cdb008c"]]},{"id":"de8511021b63a935","type":"debug","z":"88f6c9b0caa7d564","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":480,"y":140,"wires":[]},{"id":"84218d326015b8c4","type":"http request","z":"88f6c9b0caa7d564","name":"stok","method":"POST","ret":"obj","paytoqs":"body","url":"","tls":"","persist":true,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"credentials":{"user":"","password":""},"x":490,"y":220,"wires":[["608954138b51145f"]]},{"id":"b978485d3e5bed1f","type":"function","z":"88f6c9b0caa7d564","name":"","func":"stok=msg.payload.stok;\npayload = {\"hosts_info\":{\"table\":\"host_info\"},\"method\":\"get\"};\nurl='http://'+msg.ip+'/stok='+stok+'/ds';\nmsg.url=url;\nmsg.headers = {};\nmsg.headers['Content-Type'] = 'application/json; charset=UTF-8';\nmsg.payload = payload\nreturn msg;","outputs":1,"noerr":6,"initialize":"","finalize":"","libs":[],"x":730,"y":220,"wires":[["d43f2cae1aec66a2"]]},{"id":"d43f2cae1aec66a2","type":"http request","z":"88f6c9b0caa7d564","name":"获取","method":"POST","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"credentials":{"user":"","password":""},"x":850,"y":220,"wires":[["de9dedbc7485f3df"]]},{"id":"608954138b51145f","type":"switch","z":"88f6c9b0caa7d564","name":"校验","property":"payload.error_code","propertyType":"msg","rules":[{"t":"else"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":610,"y":220,"wires":[[],["b978485d3e5bed1f"]]},{"id":"5163aec3ea30afad","type":"function","z":"88f6c9b0caa7d564","name":"","func":"for (const key in msg.payload) {\n    var obj=msg.payload[key];\n    for (const key2 in msg.room) {\n        if(obj.parent_mac==msg.room[key2]){\n            obj.room=key2;\n        }\n    }\n    msg.payload=obj;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":340,"wires":[["de9dedbc7485f3df","6bec9ac4d4dee396"]]},{"id":"6bec9ac4d4dee396","type":"switch","z":"88f6c9b0caa7d564","name":"","property":"payload.mac","propertyType":"msg","rules":[{"t":"eq","v":"user[\"设备1\"]","vt":"msg"},{"t":"eq","v":"user[\"设备2\"]","vt":"msg"},{"t":"eq","v":"user[\"设备3\"]","vt":"msg"}],"checkall":"true","repair":false,"outputs":3,"x":410,"y":340,"wires":[["de8511021b63a935","599d45cfb1e0d5be","bd3d10759739bcd5"],["2e78ae9e20e1529e","5070d379d2abf55e"],["71a21cf85e38771f","26b55712ea92ea85"]]},{"id":"599d45cfb1e0d5be","type":"function","z":"88f6c9b0caa7d564","name":"","func":"msg.payload.room='not_home';\nmsg.delay=5;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":280,"wires":[["03b6af81eb0170aa"]]},{"id":"03b6af81eb0170aa","type":"trigger","z":"88f6c9b0caa7d564","name":"","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"30","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":680,"y":280,"wires":[["bd3d10759739bcd5"]]},{"id":"2e78ae9e20e1529e","type":"function","z":"88f6c9b0caa7d564","name":"","func":"msg.payload.room='not_home';\nmsg.delay=5;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":320,"wires":[["0105021c6bc4635c"]]},{"id":"0105021c6bc4635c","type":"trigger","z":"88f6c9b0caa7d564","name":"","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"30","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":680,"y":320,"wires":[["5070d379d2abf55e"]]},{"id":"71a21cf85e38771f","type":"function","z":"88f6c9b0caa7d564","name":"","func":"msg.payload.room='not_home';\nmsg.delay=5;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":360,"wires":[["f6545760de247fe4"]]},{"id":"f6545760de247fe4","type":"trigger","z":"88f6c9b0caa7d564","name":"","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"30","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":680,"y":360,"wires":[["26b55712ea92ea85"]]},{"id":"de9dedbc7485f3df","type":"loop","z":"88f6c9b0caa7d564","name":"test","kind":"enum","count":"","initial":"1","step":"1","condition":"","conditionType":"js","when":"before","enumeration":"payload.hosts_info.host_info","enumerationType":"msg","limit":"","loopPayload":"loop-val","finalPayload":"final-orig","x":270,"y":280,"wires":[[],["5163aec3ea30afad"]]},{"id":"bd3d10759739bcd5","type":"ha-entity","z":"88f6c9b0caa7d564","name":"设备1","server":"59488b0fdf1eaf6e","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"设备1"},{"property":"device_class","value":""},{"property":"icon","value":"mdi-cellphone"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.room","stateType":"msg","attributes":[{"property":"room","value":"payload.room","valueType":"msg"},{"property":"ip","value":"payload.ip","valueType":"msg"},{"property":"mac","value":"payload.mac","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":810,"y":280,"wires":[[]]},{"id":"5070d379d2abf55e","type":"ha-entity","z":"88f6c9b0caa7d564","name":"设备2","server":"59488b0fdf1eaf6e","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"设备2"},{"property":"device_class","value":""},{"property":"icon","value":"mdi-cellphone"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.room","stateType":"msg","attributes":[{"property":"room","value":"payload.room","valueType":"msg"},{"property":"ip","value":"payload.ip","valueType":"msg"},{"property":"mac","value":"payload.mac","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":810,"y":320,"wires":[[]]},{"id":"26b55712ea92ea85","type":"ha-entity","z":"88f6c9b0caa7d564","name":"设备3","server":"59488b0fdf1eaf6e","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"设备3"},{"property":"device_class","value":""},{"property":"icon","value":"mdi-cellphone"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.room","stateType":"msg","attributes":[{"property":"room","value":"payload.room","valueType":"msg"},{"property":"ip","value":"payload.ip","valueType":"msg"},{"property":"mac","value":"payload.mac","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":810,"y":360,"wires":[[]]},{"id":"59488b0fdf1eaf6e","type":"server","name":"Home Assistant","version":4,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]

实现效果图
3[2DN2X)1[(TT3P(O49OA.png

========flows文件========
flow文件
flows.rar (2.9 KB, 下载次数: 20)





回复

使用道具 举报

6

主题

217

帖子

1321

积分

金牌会员

Rank: 6Rank: 6

积分
1321
金钱
1104
HASS币
0
 楼主| 发表于 2023-7-12 00:09:26 | 显示全部楼层
本帖最后由 leonarddo 于 2023-7-12 00:12 编辑

~9X1JZRZR~89JQG$A5SNF@R.png
新改的版本,不再需要频繁申请stok,修改红圈几个地方的数据。三人以内直接修改即可使用,三人以上自行往上增加。设置值时注意不要破坏json格式。

flows.rar

3.68 KB, 下载次数: 12

回复

使用道具 举报

8

主题

229

帖子

2002

积分

金牌会员

Rank: 6Rank: 6

积分
2002
金钱
1773
HASS币
0
发表于 2022-8-11 07:18:53 | 显示全部楼层
是根据手机连节点的信号强度来判断手机在某个房间的吗
回复

使用道具 举报

6

主题

217

帖子

1321

积分

金牌会员

Rank: 6Rank: 6

积分
1321
金钱
1104
HASS币
0
 楼主| 发表于 2022-8-11 09:14:01 | 显示全部楼层
xiaopi 发表于 2022-8-11 07:18
是根据手机连节点的信号强度来判断手机在某个房间的吗

根据手机连接的mesh路由器判断,应该不能作为准确的定位判断,能定位个大致范围,或者楼层定位。
回复

使用道具 举报

23

主题

660

帖子

3110

积分

论坛元老

Rank: 8Rank: 8

积分
3110
金钱
2450
HASS币
10
发表于 2022-8-12 14:45:00 | 显示全部楼层
提示代码错误,无法导入。。。
回复

使用道具 举报

14

主题

89

帖子

1065

积分

金牌会员

Rank: 6Rank: 6

积分
1065
金钱
966
HASS币
90
发表于 2022-8-12 16:03:47 | 显示全部楼层
建议上传flow文件,剪贴板导入有误
回复

使用道具 举报

6

主题

217

帖子

1321

积分

金牌会员

Rank: 6Rank: 6

积分
1321
金钱
1104
HASS币
0
 楼主| 发表于 2022-8-12 23:24:56 | 显示全部楼层
本帖最后由 leonarddo 于 2022-8-12 23:30 编辑
wshc1216 发表于 2022-8-12 14:45
提示代码错误,无法导入。。。

已上传flows文件,并重新粘贴代码。请重试。
回复

使用道具 举报

6

主题

217

帖子

1321

积分

金牌会员

Rank: 6Rank: 6

积分
1321
金钱
1104
HASS币
0
 楼主| 发表于 2022-8-12 23:26:09 | 显示全部楼层
本帖最后由 leonarddo 于 2022-8-12 23:30 编辑
hudan717 发表于 2022-8-12 16:03
建议上传flow文件,剪贴板导入有误

已上传flows文件,并重新粘贴代码。请重试。
回复

使用道具 举报

3

主题

69

帖子

825

积分

高级会员

Rank: 4

积分
825
金钱
756
HASS币
10
发表于 2022-8-16 12:50:06 | 显示全部楼层
好东西,之前试过TP的AP+AC环境下syslog发送到nodered,轻松就能定位人员在哪里。家用路由器没这类日志功能就得想想办法了,感谢楼主的思路
回复

使用道具 举报

1

主题

33

帖子

204

积分

中级会员

Rank: 3Rank: 3

积分
204
金钱
171
HASS币
0
发表于 2022-8-16 14:18:27 | 显示全部楼层
哇,这还是第一次玩?我的猪脑不够用了
回复

使用道具 举报

6

主题

217

帖子

1321

积分

金牌会员

Rank: 6Rank: 6

积分
1321
金钱
1104
HASS币
0
 楼主| 发表于 2022-8-17 01:48:21 | 显示全部楼层
本帖最后由 leonarddo 于 2022-8-17 01:55 编辑
killevery1 发表于 2022-8-16 12:50
好东西,之前试过TP的AP+AC环境下syslog发送到nodered,轻松就能定位人员在哪里。家用路由器没这类日志功能 ...

感觉代码还可以优化,主要对node-red组件不熟悉,有些想法还没实现。暂时忙,先不弄了
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-27 07:26 , Processed in 0.083190 second(s), 37 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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