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

 找回密码
 立即注册
查看: 5057|回复: 14

[基础教程] 使用TCP通信方式控制小米电视

[复制链接]

20

主题

247

帖子

1405

积分

金牌会员

Rank: 6Rank: 6

积分
1405
金钱
1153
HASS币
40
发表于 2021-11-14 22:12:34 | 显示全部楼层 |阅读模式
本帖最后由 zxlch 于 2021-11-14 22:17 编辑

家里的小米电视坏了,之前写的一个控制方式本来要删除的,可是想想之前也是自己辛辛苦苦做出来的,就发上来给大家共享一下。
之前写过一个索尼全系列家用设备TCP控制的:索尼家电TCP控制(使用NODERED实现),控制码及控制流程 - 『HomeAssistant』综合讨论区 - 『瀚思彼岸』» 智能家居技术论坛 - Powered by Discuz! (hassbian.com)

废话不多说,直接上代码: 屏幕截图 2021-11-14 221706.png
Nodered上的代码
[
    {
        "id": "ff3b62cf.74686",
        "type": "inject",
        "z": "85c4a17a.2912d",
        "name": "",
        "props": [
            {
                "p": "payload",
                "v": "enter",
                "vt": "str"
            },
            {
                "p": "topic",
                "v": "",
                "vt": "string"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "enter",
        "payloadType": "str",
        "x": 230,
        "y": 120,
        "wires": [
            [
                "3f0e44a6.179bcc"
            ]
        ]
    },
    {
        "id": "3f0e44a6.179bcc",
        "type": "function",
        "z": "85c4a17a.2912d",
        "name": "format",
        "func": "var a = new Buffer([71,69,84,32,47,99,111,110,116,114,111,108,108,101,114,63,97,99,116,105,111,110,61,107,101,121,101,118,101,110,116,38,107,101,121,99,111,100,101,61]);\n\nvar c = new Buffer([32,72,84,84,80,47,49,46,49,13,10,104,111,115,116,58,32,49,57,50,46,49,54,56,46,50,49,54,46,51,48,58,54,48,57,53,13,10,67,111,110,110,101,99,116,105,111,110,58,32,99,108,111,115,101,13,10,13,10]);\n\nvar b = msg.payload;\n\nmsg.payload = a + b + c;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 470,
        "y": 80,
        "wires": [
            [
                "2a6bd5d5.92bb7a"
            ]
        ]
    },
    {
        "id": "2a6bd5d5.92bb7a",
        "type": "tcp request",
        "z": "85c4a17a.2912d",
        "server": "192.168.88.99",
        "port": "6095",
        "out": "time",
        "splitc": "0",
        "name": "TCP(MITV4)",
        "x": 670,
        "y": 80,
        "wires": [
            []
        ]
    },
    {
        "id": "c80755ba.fd9588",
        "type": "mqtt in",
        "z": "85c4a17a.2912d",
        "name": "",
        "topic": "control/mitv4/send",
        "qos": "2",
        "datatype": "auto",
        "broker": "b095aef8.978e9",
        "x": 190,
        "y": 80,
        "wires": [
            [
                "3f0e44a6.179bcc"
            ]
        ]
    },
    {
        "id": "b095aef8.978e9",
        "type": "mqtt-broker",
        "name": "",
        "broker": "127.0.0.1",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": false,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    }
]


需要更改的两个位置:
"server": "192.168.88.99",
        "port": "6095",
应该来说,port是不需要更改的,只更改server的IP地址即可。


接下来是HA的代码:
XiaoMi TV4:
    cards:
      - cards:
          - color: rgb(113, 113, 198)
            color_type: card
            icon: mdi:power
            name: 电源
            styles:
              card:
                - height: 80px
            tap_action:
              action: call-service
              service: mqtt.publish
              service_data:
                payload: power
                topic: control/mitv4/send
            type: custom:button-card
          - default: 'off'
            entity: binary_sensor.mi_tv4
            states:
              'off':
                color: rgb(255, 0, 0)
                color_type: label-card
                name: XiaoMi TV4
                styles:
                  card:
                    - height: 80px
                type: custom:button-card
              'on':
                color: rgb(255, 255, 255)
                color_type: label-card
                name: XiaoMi TV4
                styles:
                  card:
                    - height: 80px
                type: custom:button-card
            type: custom:state-switch
          - color: rgb(255, 255, 255)
            color_type: card
            icon: mdi:home
            name: 主页
            styles:
              card:
                - height: 80px
            tap_action:
              action: call-service
              service: mqtt.publish
              service_data:
                payload: home
                topic: control/mitv4/send
            type: custom:button-card
        type: horizontal-stack
      - cards:
          - styles:
              card:
                - filter: opacity(0%)
            type: custom:button-card
          - color: rgb(119, 136, 153)
            color_type: card
            icon: mdi:arrow-up-bold
            name: 上
            styles:
              card:
                - height: 80px
            tap_action:
              action: call-service
              service: mqtt.publish
              service_data:
                payload: up
                topic: control/mitv4/send
            type: custom:button-card
          - styles:
              card:
                - filter: opacity(0%)
            type: custom:button-card
        type: horizontal-stack
      - cards:
          - color: rgb(119, 136, 153)
            color_type: card
            icon: mdi:arrow-left-bold
            name: 左
            styles:
              card:
                - height: 80px
            tap_action:
              action: call-service
              service: mqtt.publish
              service_data:
                payload: left
                topic: control/mitv4/send
            type: custom:button-card
          - color: rgb(119, 136, 153)
            color_type: card
            icon: mdi:power-off
            name: 确认
            styles:
              card:
                - height: 80px
            tap_action:
              action: call-service
              service: mqtt.publish
              service_data:
                payload: enter
                topic: control/mitv4/send
            type: custom:button-card
          - color: rgb(119, 136, 153)
            color_type: card
            icon: mdi:arrow-right-bold
            name: 右
            styles:
              card:
                - height: 80px
            tap_action:
              action: call-service
              service: mqtt.publish
              service_data:
                payload: right
                topic: control/mitv4/send
            type: custom:button-card
        type: horizontal-stack
      - cards:
          - color: rgb(255, 255, 255)
            color_type: card
            icon: mdi:keyboard-backspace
            name: 返回
            styles:
              card:
                - height: 80px
            tap_action:
              action: call-service
              service: mqtt.publish
              service_data:
                payload: back
                topic: control/mitv4/send
            type: custom:button-card
          - color: rgb(119, 136, 153)
            color_type: card
            icon: mdi:arrow-down-bold
            name: 下
            styles:
              card:
                - height: 80px
            tap_action:
              action: call-service
              service: mqtt.publish
              service_data:
                payload: down
                topic: control/mitv4/send
            type: custom:button-card
          - color: rgb(255, 255, 255)
            color_type: card
            icon: mdi:menu
            name: 菜单
            styles:
              card:
                - height: 80px
            tap_action:
              action: call-service
              service: mqtt.publish
              service_data:
                payload: menu
                topic: control/mitv4/send
            type: custom:button-card
        type: horizontal-stack
      - cards:
          - color: rgb(255, 255, 255)
            color_type: card
            icon: mdi:volume-minus
            name: 音量减
            styles:
              card:
                - height: 80px
            tap_action:
              action: call-service
              service: mqtt.publish
              service_data:
                payload: volumedown
                topic: control/mitv4/send
            type: custom:button-card
          - color: rgb(255, 255, 255)
            color_type: card
            icon: mdi:volume-plus
            name: 音量增
            styles:
              card:
                - height: 80px
            tap_action:
              action: call-service
              service: mqtt.publish
              service_data:
                payload: volumeup
                topic: control/mitv4/send
            type: custom:button-card
        type: horizontal-stack
    type: vertical-stack


注意,HA的代码不能直接用,要修改一下,我太懒了,希望有人改好了发上来我复制过来。
具体样式如下:
Screenshot_20211114_221026_io.homeassistant.companion.android.jpg
如果设备在线,电源旁边的设备名称会变成白色的底,不在线就是红色的低。


评分

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

查看全部评分

回复

使用道具 举报

20

主题

247

帖子

1405

积分

金牌会员

Rank: 6Rank: 6

积分
1405
金钱
1153
HASS币
40
 楼主| 发表于 2021-11-14 22:16:24 | 显示全部楼层
原理是,可以用TCP发送一个数据(好像是json?)给小米电视,小米电视能够解析信息然后执行对应功能。我把变化的部分提取出来在NR上进行拼接,然后HA通过MQTT发送需要拼接的可变部分,再由NR拼接完成后发给小米电视。
例如拼接的内容power就是电源键,home就是主页键,menu就是菜单键。
回复

使用道具 举报

0

主题

176

帖子

2472

积分

金牌会员

Rank: 6Rank: 6

积分
2472
金钱
2296
HASS币
0
发表于 2021-11-14 22:32:09 | 显示全部楼层
能控制开机吗?
回复

使用道具 举报

20

主题

247

帖子

1405

积分

金牌会员

Rank: 6Rank: 6

积分
1405
金钱
1153
HASS币
40
 楼主| 发表于 2021-11-14 22:41:56 | 显示全部楼层

不太清楚,可以试试
回复

使用道具 举报

16

主题

167

帖子

1365

积分

金牌会员

Rank: 6Rank: 6

积分
1365
金钱
1198
HASS币
0
发表于 2021-11-14 23:13:23 来自手机 | 显示全部楼层
小米电视api是http协议的,用http resquest节点呀,比tcp直观
回复

使用道具 举报

20

主题

247

帖子

1405

积分

金牌会员

Rank: 6Rank: 6

积分
1405
金钱
1153
HASS币
40
 楼主| 发表于 2021-11-14 23:15:17 | 显示全部楼层
pikaq 发表于 2021-11-14 23:13
小米电视api是http协议的,用http resquest节点呀,比tcp直观

http resquest不能拼接协议,因为他封装了
回复

使用道具 举报

0

主题

143

帖子

524

积分

高级会员

Rank: 4

积分
524
金钱
381
HASS币
0
发表于 2021-11-15 16:53:15 | 显示全部楼层
厉害!!!!!!!!!!!!
回复

使用道具 举报

19

主题

329

帖子

2321

积分

论坛积极会员

积分
2321
金钱
1992
HASS币
0
发表于 2021-11-17 17:19:12 | 显示全部楼层
connect failed 192.168.0.12:6095,连不上tcp,我是小米大师82,不知道为什么。。。。。
回复

使用道具 举报

175

主题

2956

帖子

7555

积分

超级版主

我就是六神

Rank: 8Rank: 8

积分
7555
金钱
4574
HASS币
398

活跃会员教程狂人灌水之王

QQ
发表于 2021-11-20 23:49:45 | 显示全部楼层
好东西~~
回复

使用道具 举报

15

主题

303

帖子

2233

积分

金牌会员

Rank: 6Rank: 6

积分
2233
金钱
1930
HASS币
0
发表于 2021-11-25 19:43:51 来自手机 | 显示全部楼层
BobbyCoon 发表于 2021-11-17 17:19
connect failed 192.168.0.12:6095,连不上tcp,我是小米大师82,不知道为什么。。。。。 ...

format那个node里的第二个数组里还有个ip地址也需要改下,数组里的数据全部是字符ascii码,自己转换下。建议复制到excel中整理下,用chr函数求得对应的字符就能看明白了。
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-4-26 05:30 , Processed in 0.068079 second(s), 39 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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