找回密码
 立即注册

微信扫码登录

搜索
楼主: idreamshen

[经验分享] Esphome Uart Debugging 使用分享(远程串口抓包)

[复制链接]

0

主题

5

回帖

27

积分

新手上路

积分
27
金钱
22
HASS币
0
发表于 7 小时前 | 显示全部楼层
这个打个记号.
回复

使用道具 举报

0

主题

5

回帖

27

积分

新手上路

积分
27
金钱
22
HASS币
0
发表于 7 小时前 | 显示全部楼层
chaoself 发表于 2024-3-3 13:04
同问,怎么才能把数据往外传

esp32 做个 TEXT sendor 开个MQTT 从 NODE RED 读是不是就可以?
给您一个
[
    {
        "id": "8696d997d6705a77",
        "type": "mqtt in",
        "z": "dc7ab9e9cd303614",
        "name": "ew11-in",
        "topic": "ew11/data",
        "qos": "0",
        "datatype": "buffer",
        "broker": "9e9d11862399ce26",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 70,
        "y": 680,
        "wires": [
            [
                "d0afcc36a99fdea7"
            ]
        ]
    },
    {
        "id": "d0afcc36a99fdea7",
        "type": "function",
        "z": "dc7ab9e9cd303614",
        "name": "解析4个客厅灯开关",
        "func": "let payload = msg.payload;\n\n// 如果是 Buffer,先转为十六进制字符串\nif (Buffer.isBuffer(payload)) {\n    // 每个字节转成两位十六进制,用空格分隔\n    payload = Array.from(payload).map(b => b.toString(16).padStart(2,'0')).join(' ').toUpperCase();\n}\n\n// 如果是字符串,去掉尖括号\nelse if (typeof payload === 'string') {\n    payload = payload.replace(/^<|>$/g, '').trim().toUpperCase();\n}\n\n// 否则不是可处理的格式\nelse {\n    node.warn("Unsupported payload type: " + typeof payload);\n    return null;\n}\n\n// 转为数组\nlet data = payload.split(/\\s+/).map(b => parseInt(b,16));\n\n// 定义报文头(16字节示例)\nconst headerPattern = [0x02,0x19,0x01,0x08,0x00,0xC2,0x3D,0x03,\n                       0x02,0x19,0x01,0x0D,0x40,0xC2,0x00,0x00,0x04];\n\n// 判断报文是否匹配头部\nlet isMatch = true;\nfor (let i = 0; i < headerPattern.length; i++) {\n    if (data[i] !== headerPattern[i]) {\n        isMatch = false;\n        break;\n    }\n}\n\nif (!isMatch) return null;\n\n// 状态字节在 0x04 后\nlet statusByte = data[headerPattern.length];\n\n// 灯1~4按位解析\nlet lights = {\n    dingning_light_1: (statusByte & 0x80) ? "turn_on" : "turn_off",\n    dingning_light_2: (statusByte & 0x40) ? "turn_on" : "turn_off",\n    dingning_light_3: (statusByte & 0x20) ? "turn_on" : "turn_off",\n    dingning_light_4: (statusByte & 0x10) ? "turn_on" : "turn_off"\n};\n\nmsg.payload = lights;\n\nreturn msg;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 150,
        "y": 620,
        "wires": [
            [
                "bb5f964b6f17dcc8"
            ]
        ]
    },
    {
        "id": "9e9d11862399ce26",
        "type": "mqtt-broker",
        "name": "",
        "broker": "192.168.1.2",
        "port": 1883,
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": 4,
        "keepalive": 60,
        "cleansession": true,
        "autoUnsubscribe": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthRetain": "false",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closeRetain": "false",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willRetain": "false",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    }
]
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian ( 晋ICP备17001384号-1 )

GMT+8, 2025-11-1 17:44 , Processed in 0.048398 second(s), 10 queries , MemCached On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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