小米台灯控制

小米台灯控制文档输出

0. 起源

  • 之前研究了一下小米台灯局域网控制,现在直接将总结的文档发出来分享一下

1. 参考

  • python-miio:https://github.com/rytilahti/python-miio
  • 注意事项:不要升级台灯,不要升级台灯,不要升级台灯,容易导致接口变动,从而软件出错

2. 软件控制

2.1 下载米家APP,给小米台灯联网

  • 下载米家APP,并注册账号
  • 在米家APP中找到对应的台灯,并添加
  • 台灯被其他人已经添加过,需要长按台灯底下的复位按钮5s以上,台灯恢复出厂设置

2.2 安装python-miio

  • 这里本人使用的python为3.11.3版本

  • 直接命令行安装

    pip install git+https://github.com/rytilahti/python-miio.git
    

在这里插入图片描述

2.3 测试miiocli

指令:miiocli cloud

输入账号密码

然后就能得到设备列表

记录下设备列表中台灯对应的IP和Token,后面局域网控制需要用到

在这里插入图片描述

2.4 直接指令控制

  • 查询台灯信息

    miiocli.exe genericmiot --ip 192.168.3.188 --token d6d90289cde043638e7bf89d02f683e8 status
    
    miiocli.exe genericmiot --ip 192.168.3.188 --token d6d90289cde043638e7bf89d02f683e8 status
    Running command status
    Service Light (light)
            Switch Status (light:on, access: RW): False (<class 'bool'>, )
            Brightness (light:brightness, access: RW): 40 % (<class 'int'>, min: 1, max: 100, step: 1)
            Color Temperature (light:color-temperature, access: RW): 4080 None (<class 'int'>, min: 2600, max: 5100, step: 1)
            Default Power On State (light:default-power-on-state, access: RW): False (<class 'bool'>, )
            Mode (light:mode, access: RW): Free (value: 0) (<class 'int'>, choices: Computer (5), Warmth (6), Leisure (7), Office (8), Reading (9), Entertainment (10), Free (0), MyMode1 (1), MyMode2 (2), MyMode3
    (3), MyMode4 (4))
    Service yeelight (yeelight)
            save-state (yeelight:save-state, access: RW): True (<class 'bool'>, )
            delay-time (yeelight:delay-time, access: RW): 0:15:00 (<class 'int'>, min: 1, max: 60, step: 1)
            delay-off-countdown (yeelight:delay-off-countdown, access: R): 0:00:00
            delay-off-enable (yeelight:delay-off-enable, access: RW): False (<class 'bool'>, )
            kid-mode (yeelight:kid-mode, access: RW): False (<class 'bool'>, )
            double-click-enable (yeelight:double-click-enable, access: RW): False (<class 'bool'>, )
            double-click (yeelight:double-click, access: RW): Start Pomodoro Mode (value: 0) (<class 'int'>, choices: Start Pomodoro Mode (0), Start Delay Off (1), Start Mode (2))
            scene-mode (yeelight:scene-mode, access: RW): Free Mode (value: 0) (<class 'int'>, choices: Free Mode (0), My Mode 1 (1), My Mode 2 (2), My Mode 3 (3), My Mode 4 (4), Computer (5), Warmth (6), Leisure
    (7), Office (8), Reading (9), Entertainment (10))
            pomodoro-enable (yeelight:pomodoro-enable, access: RW): False (<class 'bool'>, )
            work-minutes (yeelight:work-minutes, access: RW): 0:45:00 (<class 'int'>, min: 1, max: 120, step: 1)
            break-minutes (yeelight:break-minutes, access: RW): 0:10:00 (<class 'int'>, min: 1, max: 120, step: 1)
            work-countdown-sec (yeelight:work-countdown-sec, access: R): 0:00:00
            break-countdown-sec (yeelight:break-countdown-sec, access: R): 0:00:00
            mode-one-bright (yeelight:mode-one-bright, access: RW): 50 % (<class 'int'>, min: 1, max: 100, step: 1)
            mode-one-ct (yeelight:mode-one-ct, access: RW): 4000 None (<class 'int'>, min: 2600, max: 5100, step: 1)
            mode-two-bright (yeelight:mode-two-bright, access: RW): 100 % (<class 'int'>, min: 1, max: 100, step: 1)
            mode-two-ct (yeelight:mode-two-ct, access: RW): 2700 None (<class 'int'>, min: 2600, max: 5100, step: 1)
            mode-three-bright (yeelight:mode-three-bright, access: RW): 30 % (<class 'int'>, min: 1, max: 100, step: 1)
            mode-three-ct (yeelight:mode-three-ct, access: RW): 5000 None (<class 'int'>, min: 2600, max: 5100, step: 1)
            mode-four-bright (yeelight:mode-four-bright, access: RW): 60 % (<class 'int'>, min: 1, max: 100, step: 1)
            mode-four-ct (yeelight:mode-four-ct, access: RW): 3000 None (<class 'int'>, min: 2600, max: 5100, step: 1)
            double-click-mode (yeelight:double-click-mode, access: RW): My Mode 1 (value: 1) (<class 'int'>, choices: My Mode 1 (1), My Mode 2 (2), My Mode 3 (3), My Mode 4 (4), Computer (5), Warmth (6), Leisure
    (7), Office (8), Reading (9), Entertainment (10))
    
  • 控制台灯

    开灯:

    miiocli.exe -d device --ip 192.168.3.188 --token d6d90289cde043638e7bf89d02f683e8 raw_command set_properties "[{'did': 'MYDID', 'piid': 1, 'siid': 2, 'value': True}]"
    
    PS E:\Works_XH\19_小米台灯控制研究\2_code>  miiocli.exe -d device --ip 192.168.3.188 --token d6d90289cde043638e7bf89d02f683e8 raw_command set_properties "[{'did': 'MYDID', 'piid': 1, 'siid': 2, 'value': True}]"
    Running command raw_command
    DEBUG    Unknown model, trying autodetection                                                                                                                    click_common.py:184DEBUG    Got a response: Container:                                                                                                                             miioprotocol.py:128             data = Container:
                     data = b'' (total 0)
                     value = b'' (total 0)
                     offset1 = 32
                     offset2 = 32
                     length = 0
                 header = Container:
                     data = b'!1\x00 \x00\x00\x00\x001&zO\x00\x02\xf5\xf1' (total 16)
                     value = Container:
                         length = 32
                         unknown = 0
                         device_id = unhexlify('31267a4f')
                         ts = 1970-01-03 05:53:53+00:00
                     offset1 = 0
                     offset2 = 16
                     length = 16
                 checksum = b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff' (total 16)
    DEBUG    Discovered 31267a4f with ts: 1970-01-03 05:53:53+00:00, token: b'ffffffffffffffffffffffffffffffff'                                                      miioprotocol.py:90DEBUG    192.168.3.188:54321 >>: {'id': 1, 'method': 'miIO.info', 'params': []}                                                                                 miioprotocol.py:182DEBUG    192.168.3.188:54321 (ts: 1970-01-03 05:53:53+00:00, id: 1) << {'exe_time': 10,                                                                         miioprotocol.py:212          'id': 1,
              'result': {'ap': {'bssid': 'C4:17:0E:5A:23:70',
                                'primary': 11,
                                'rssi': -33,
                                'ssid': 'XIANGHANGSZ'},
                         'fw_ver': '2.1.7_0001',
                         'hw_ver': 'esp32',
                         'ipflag': 1,
                         'life': 194033,
                         'mac': '84:46:93:BA:DE:74',
                         'miio_ver': '0.0.9',
                         'mmfree': 60156,
                         'model': 'yeelink.light.lamp28',
                         'netif': {'gw': '192.168.3.1',
                                   'localIp': '192.168.3.188',
                                   'mask': '255.255.255.0'},
                         'uid': 969792160,
                         'wifi_fw_ver': '7687a56'}}
    DEBUG    Detected model yeelink.light.lamp28                                                                                                                          device.py:140DEBUG    192.168.3.188:54321 >>: {'id': 2,                                                                                                                      miioprotocol.py:182          'method': 'set_properties',
              'params': [{'did': 'MYDID', 'piid': 1, 'siid': 2, 'value': True}]}
    DEBUG    192.168.3.188:54321 (ts: 1970-01-03 05:53:53+00:00, id: 2) << {'exe_time': 0,                                                                          miioprotocol.py:212          'id': 2,
              'result': [{'code': 0, 'did': 'MYDID', 'piid': 1, 'siid': 2}]}
    [{'did': 'MYDID', 'siid': 2, 'piid': 1, 'code': 0}]
    

    关灯:

    miiocli.exe -d device --ip 192.168.3.188 --token d6d90289cde043638e7bf89d02f683e8 raw_command set_properties "[{'did': 'MYDID', 'piid': 1, 'siid': 2, 'value': False}]"
    
    PS E:\Works_XH\19_小米台灯控制研究\2_code>  miiocli.exe -d device --ip 192.168.3.188 --token d6d90289cde043638e7bf89d02f683e8 raw_command set_properties "[{'did': 'MYDID', 'piid': 1, 'siid': 2, 'value': False}]"
    Running command raw_command
    DEBUG    Unknown model, trying autodetection                                                                                                                    click_common.py:184DEBUG    Got a response: Container:                                                                                                                             miioprotocol.py:128             data = Container:
                     data = b'' (total 0)
                     value = b'' (total 0)
                     offset1 = 32
                     offset2 = 32
                     length = 0
                 header = Container:
                     data = b'!1\x00 \x00\x00\x00\x001&zO\x00\x02\xf5\xfb' (total 16)
                     value = Container:
                         length = 32
                         unknown = 0
                         device_id = unhexlify('31267a4f')
                         ts = 1970-01-03 05:54:03+00:00
                     offset1 = 0
                     offset2 = 16
                     length = 16
                 checksum = b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff' (total 16)
    DEBUG    Discovered 31267a4f with ts: 1970-01-03 05:54:03+00:00, token: b'ffffffffffffffffffffffffffffffff'                                                      miioprotocol.py:90DEBUG    192.168.3.188:54321 >>: {'id': 1, 'method': 'miIO.info', 'params': []}                                                                                 miioprotocol.py:182DEBUG    192.168.3.188:54321 (ts: 1970-01-03 05:54:03+00:00, id: 1) << {'exe_time': 0,                                                                          miioprotocol.py:212          'id': 1,
              'result': {'ap': {'bssid': 'C4:17:0E:5A:23:70',
                                'primary': 11,
                                'rssi': -33,
                                'ssid': 'XIANGHANGSZ'},
                         'fw_ver': '2.1.7_0001',
                         'hw_ver': 'esp32',
                         'ipflag': 1,
                         'life': 194043,
                         'mac': '84:46:93:BA:DE:74',
                         'miio_ver': '0.0.9',
                         'mmfree': 59632,
                         'model': 'yeelink.light.lamp28',
                         'netif': {'gw': '192.168.3.1',
                                   'localIp': '192.168.3.188',
                                   'mask': '255.255.255.0'},
                         'token': 'd6d90289cde043638e7bf89d02f683e8',
                         'uid': 969792160,
                         'wifi_fw_ver': '7687a56'}}
    DEBUG    Detected model yeelink.light.lamp28                                                                                                                          device.py:140DEBUG    192.168.3.188:54321 >>: {'id': 2,                                                                                                                      miioprotocol.py:182          'method': 'set_properties',
              'params': [{'did': 'MYDID', 'piid': 1, 'siid': 2, 'value': False}]}
    DEBUG    192.168.3.188:54321 (ts: 1970-01-03 05:54:03+00:00, id: 2) << {'exe_time': 0,                                                                          miioprotocol.py:212          'id': 2,
              'result': [{'code': 0, 'did': 'MYDID', 'piid': 1, 'siid': 2}]}
    [{'did': 'MYDID', 'siid': 2, 'piid': 1, 'code': 0}]
    

    查询信息:

    miiocli device --ip 192.168.3.188 --token d6d90289cde043638e7bf89d02f683e8 raw_command miIO.info
    
    miiocli device --ip 192.168.3.188 --token d6d90289cde043638e7bf89d02f683e8 raw_command miIO.info
    Running command raw_command
    {'life': 24526, 'uid': 969792160, 'model': 'yeelink.light.lamp28', 'token': 'd6d90289cde043638e7bf89d02f683e8', 'ipflag': 1, 'fw_ver': '2.1.7_0001', 'miio_ver': '0.0.9', 'hw_ver': 'esp32', 'mmfree': 60380,
    'mac': '84:46:93:BA:DE:74', 'wifi_fw_ver': '7687a56', 'ap': {'ssid': 'XIANGHANGSZ', 'bssid': 'C4:17:0E:5A:23:70', 'rssi': -40, 'primary': 11}, 'netif': {'localIp': '192.168.3.188', 'mask': '255.255.255.0',
    'gw': '192.168.3.1'}}
    

2.5 代码控制

  • 整体参考代码,控制台循环灯亮1s灭1s,注意台灯是渐亮渐灭的

    import time
    from miio import DeviceFactory
    
    dev = DeviceFactory.create("192.168.3.188", "d6d90289cde043638e7bf89d02f683e8")
    print("Status-----------------")
    print(dev.status())
    print("settings:---------------")
    print(dev.settings())
    dev.change_setting("light:on", True)
    dev.change_setting("light:brightness", 100)
    dev.change_setting("light:color", 6500)
    
    
    
    brightness = 1
    lightstatus = True
    while True:
        lightstatus = not lightstatus
        dev.change_setting("light:on", lightstatus)
        time.sleep(1)
    
  • 可控制的部分详解,可以控制灯的亮灭、亮度、色温

    settings:---------------
    {
    'light:on': PropertyDescriptor(id='light:on', name='Power', type=<class 'bool'>, unit=None, status_attribute='is_on', access=<AccessFlags.Read|Write: 3>, constraint=<PropertyConstraint.Unset: 1>), 
    
    'light:brightness': RangeDescriptor(id='light:brightness', name='Brightness', unit='%', status_attribute='brightness', access=<AccessFlags.Read|Write: 3>, min_value=0, max_value=100, step=1, range_attribute=None, type=<class 'int'>, constraint=<PropertyConstraint.Range: 2>), 
    
    'light:color': PropertyDescriptor(id='light:color', name='Color', type=<class 'int'>, unit=None, status_attribute='rgb_int', access=<AccessFlags.Read|Write: 3>, constraint=<PropertyConstraint.Unset: 1>), 
    
    'light:color-temperature': RangeDescriptor(id='light:color-temperature', name='Color temperature', unit='K', status_attribute='color_temp', access=<AccessFlags.Read|Write: 3>, min_value=1700, max_value=6500, step=1, range_attribute='color_temperature_range', type=<class 'int'>, constraint=<PropertyConstraint.Range: 2>), 
    
    'YeelightStatus.developer_mode': PropertyDescriptor(id='YeelightStatus.developer_mode', name='Developer mode enabled', type=<class 'bool'>, unit=None, status_attribute='developer_mode', access=<AccessFlags.Read|Write: 3>, constraint=<PropertyConstraint.Unset: 1>),
    
    'YeelightStatus.save_state_on_change': PropertyDescriptor(id='YeelightStatus.save_state_on_change', name='Save state on change enabled', type=<class 'bool'>, unit=None, status_attribute='save_state_on_change', access=<AccessFlags.Read|Write: 3>, constraint=<PropertyConstraint.Unset: 1>)}
    
  • 可用的代码控制

    亮灯

    dev.change_setting("light:on", True)
    

    灭灯

    dev.change_setting("light:on", False)
    

    亮度,可调范围为1~100,设为其他状态代码会出错

    dev.change_setting("light:brightness", brightness)
    dev.change_setting("light:brightness", 1)
    dev.change_setting("light:brightness", 100)
    

    色温,可调范围为1700~6500,1700为最暖色温,6500为最冷色温,设为其他值代码会报错

    dev.change_setting("light:color", color)
    dev.change_setting("light:color", 1700)
    dev.change_setting("light:color", 6500)
    

    未知指令,light:color-temperature也是调色温的,但是具体参数不清楚,低于2000会报错

    dev.change_setting("light:color-temperature", 3000)
    
  • 灯的状态读取

    print(dev.status().is_on)
    print(dev.status().brightness)
    print(dev.status().color_mode)
    print(dev.status().rgb_int)
    print(dev.status().color_temp)
    print(dev.status().developer_mode)
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值