python-broadlink

repository·master·Indexed 23 days ago

https://github.com/mjg59/python-broadlink

A Python module and CLI for local network control of Broadlink devices, including remotes, smart plugs, switches, and sensors. Features include IR/RF code learning and sending, sensor data querying, power and nightlight control for smart plugs, and network discovery via the broadlink_discovery tool. The documentation also provides detailed protocol specifications for packet construction, checksum calculation, device authorization, and Wi-Fi configuration.

Tokens
3.8K
Snippets
10
Records
32
Agent score
81%

What's inside python-broadlink

  1. Install the python-broadlink CLI

    master

    To use the command line interface, you must first install the core broadlink module via pip, then obtain the broadlink_cli and broadlink_discovery programs.

    1. Install the Python module: pip3 install broadlink
    2. Download the broadlink_cli and broadlink_discovery binaries/programs.
    pip3 install broadlink
  2. Use learning mode to capture IR/RF codes

    master

    Broadlink devices support learning modes to capture remote signals.

    1. Enter Learning Mode

    Send a 16-byte payload with command value 0x006a:

    • 0x00: 0x03
    • 0x01-0x0f: 0x00

    2. Read Back Captured Data

    Send a 16-byte payload with command value 0x006a:

    • 0x00: 0x04
    • 0x01-0x0f: 0x00

    Processing the Response:

    • Check byte 0x22 (little-endian 16-bit error code). If 0, a code was successfully captured.
    • Decrypt the response from byte 0x38 onwards using the device's encryption key.
    • The captured data starts at byte 0x04 of the decrypted payload.
  3. Set up a new Broadlink device in AP Mode

    master

    To configure a new Broadlink device while it is in Access Point (AP) Mode, send a 136-byte packet as a UDP broadcast to 255.255.255.255 on port 80.

    Packet Structure:

    OffsetContents
    0x00-0x1900
    0x20-0x21Checksum (little-endian 16-bit integer)
    0x2614 (Always 14)
    0x44-0x63SSID Name (zero padded)
    0x64-0x83Password (zero padded)
    0x84Character length of SSID
    0x85Character length of password
    0x86Wireless security mode (00=none, 01=WEP, 02=WPA1, 03=WPA2, 04=WPA1/2)
    0x87-8800
  4. Discover Broadlink devices on the local network

    master

    To discover Broadlink devices, send a 48-byte UDP broadcast packet to 255.255.255.255 on port 80.

    Request Packet Structure:

    OffsetContents
    0x00-0x0700
    0x08-0x0bCurrent GMT offset (little-endian 32-bit integer)
    0x0c-0x0dCurrent year (little-endian 16-bit integer)
    0x0eSeconds past the minute
    0x0fMinutes past the hour
    0x10Hours past midnight
    0x11Day of the week (Monday = 1, etc.)
    0x12Day of the month
    0x13Month
    0x14-0x1700
    0x18-0x1bLocal IP address
    0x1c-0x1dSource port (little-endian 16-bit integer)
    0x1e-0x1f00
    0x20-0x21Checksum (little-endian 16-bit integer)
    0x22-0x2500
    0x2606
    0x27-0x2f00

    Response Packet Structure:

    OffsetContents
    0x34-0x35Device type (little-endian 16-bit integer)
    0x3a-0x3fMAC address of the target device
  5. Learn and send IR/RF codes with Universal Remotes

    master

    Universal remotes allow you to capture and replay IR or RF signals.

    Learning Codes

    • IR (Console): Use --learn to capture an IR code and display it in the console.
    • RF (Console): Use --rfscanlearn to capture an RF code and display it in the console.
    • IR (File): Use --learnfile <FILENAME> to save a captured IR code to a file.
    • RF (File): Use --rfscanlearn --learnfile <FILENAME> to save a captured RF code to a file.

    Sending Codes

    • Raw Data: Use --send <DATA> to send a specific code string.
    • From File: Use --send @<FILENAME> to send a code stored in a file.
  6. Send IR or RF data packets

    master

    To replay a captured signal, send a payload with command byte 0x006a using the following structure:

    OffsetContents
    0x000x02
    0x01-0x030x00
    0x04Signal Type: 0x26 (IR), 0xb2 (RF 433Mhz), or 0xd7 (RF 315Mhz)
    0x05Repeat count (0 = no repeat, 1 = send twice, etc.)
    0x06-0x07Length of following data (little-endian)
    0x08...Pulse lengths in $2^{-15}$ s units. (Formula: $\mu s \times 269 / 8192$)

    Note on IR Codes:

    • Pulse lengths must be paired as ON, then OFF.
    • Captured IR codes typically end with a constant OFF value of 0x00 0x0d 0x05 (~102ms).
  7. Learn RF codes with a Universal Remote

    master

    RF learning is a multi-step process involving frequency sweeping and two-stage button presses.

    1. Call sweep_frequency().
    2. Point the remote and long press the button.
    3. Verify frequency with check_frequency().
    4. Call find_rf_packet().
    5. Point the remote and short press the button.
    6. Retrieve the packet with check_data().

    You can exit the process early using cancel_sweep_frequency().

  8. Connect a device to your local network

    master

    If the device is not already configured via the Broadlink app, you must put it into AP Mode (long press reset until blue LED blinks quickly, then long press again until it blinks slowly) and connect to the BroadlinkProv SSID. Then, use broadlink.setup() to connect it to your WiFi.

    Security mode options:

    • 0: none
    • 1: WEP
    • 2: WPA1
    • 3: WPA2
    • 4: WPA1/2
    import broadlink
    # Connect to SSID with WPA2 (mode 3)
    broadlink.setup('myssid', 'mynetworkpass', 3)
    
    # Advanced: specify broadcast address if setup fails
    broadlink.setup('myssid', 'mynetworkpass', 3, ip_address='192.168.0.255')
  9. Learn IR codes with a Universal Remote

    master

    To capture an IR code, enter learning mode, point your remote at the Broadlink device, and press the button. Then, retrieve the packet using check_data().

    Note: Universal remotes with product ID 0x2712 do not require frequency sweeping; simply use enter_learning() and check_data().

  10. Authorize a Broadlink device

    master

    To communicate with a device, you must first obtain an authorization key.

    1. Generate an 80-byte packet. The payload must include a 15-digit value (the device IMEI) at offset 0x04-0x12, a command value of 0x0065, and a NULL-terminated ASCII device name at offset 0x30.
    2. Send the packet. The response will contain an encrypted payload starting at byte 0x38.
    3. Decrypt the response using the default AES key and IV.

    Decrypted Authorization Payload:

    OffsetContents
    0x00-0x03Device ID
    0x04-0x13Device encryption key

    Use this Device ID and Device encryption key for all subsequent command packets.