Calculate Broadlink packet checksums
master0xbeaf, allowing the value to wrap around after 0xffff.repository·master·Indexed 23 days ago
https://github.com/mjg59/python-broadlinkA 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.
0xbeaf, allowing the value to wrap around after 0xffff.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.
pip3 install broadlinkbroadlink_cli and broadlink_discovery binaries/programs.pip3 install broadlinkBroadlink devices support learning modes to capture remote signals.
Send a 16-byte payload with command value 0x006a:
0x00: 0x030x01-0x0f: 0x00Send a 16-byte payload with command value 0x006a:
0x00: 0x040x01-0x0f: 0x00Processing the Response:
0x22 (little-endian 16-bit error code). If 0, a code was successfully captured.0x38 onwards using the device's encryption key.0x04 of the decrypted payload.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:
| Offset | Contents |
|---|---|
0x00-0x19 | 00 |
0x20-0x21 | Checksum (little-endian 16-bit integer) |
0x26 | 14 (Always 14) |
0x44-0x63 | SSID Name (zero padded) |
0x64-0x83 | Password (zero padded) |
0x84 | Character length of SSID |
0x85 | Character length of password |
0x86 | Wireless security mode (00=none, 01=WEP, 02=WPA1, 03=WPA2, 04=WPA1/2) |
0x87-88 | 00 |
To discover Broadlink devices, send a 48-byte UDP broadcast packet to 255.255.255.255 on port 80.
Request Packet Structure:
| Offset | Contents |
|---|---|
0x00-0x07 | 00 |
0x08-0x0b | Current GMT offset (little-endian 32-bit integer) |
0x0c-0x0d | Current year (little-endian 16-bit integer) |
0x0e | Seconds past the minute |
0x0f | Minutes past the hour |
0x10 | Hours past midnight |
0x11 | Day of the week (Monday = 1, etc.) |
0x12 | Day of the month |
0x13 | Month |
0x14-0x17 | 00 |
0x18-0x1b | Local IP address |
0x1c-0x1d | Source port (little-endian 16-bit integer) |
0x1e-0x1f | 00 |
0x20-0x21 | Checksum (little-endian 16-bit integer) |
0x22-0x25 | 00 |
0x26 | 06 |
0x27-0x2f | 00 |
Response Packet Structure:
| Offset | Contents |
|---|---|
0x34-0x35 | Device type (little-endian 16-bit integer) |
0x3a-0x3f | MAC address of the target device |
Universal remotes allow you to capture and replay IR or RF signals.
--learn to capture an IR code and display it in the console.--rfscanlearn to capture an RF code and display it in the console.--learnfile <FILENAME> to save a captured IR code to a file.--rfscanlearn --learnfile <FILENAME> to save a captured RF code to a file.--send <DATA> to send a specific code string.--send @<FILENAME> to send a code stored in a file.To replay a captured signal, send a payload with command byte 0x006a using the following structure:
| Offset | Contents |
|---|---|
0x00 | 0x02 |
0x01-0x03 | 0x00 |
0x04 | Signal Type: 0x26 (IR), 0xb2 (RF 433Mhz), or 0xd7 (RF 315Mhz) |
0x05 | Repeat count (0 = no repeat, 1 = send twice, etc.) |
0x06-0x07 | Length of following data (little-endian) |
0x08... | Pulse lengths in $2^{-15}$ s units. (Formula: $\mu s \times 269 / 8192$) |
Note on IR Codes:
0x00 0x0d 0x05 (~102ms).RF learning is a multi-step process involving frequency sweeping and two-stage button presses.
sweep_frequency().check_frequency().find_rf_packet().check_data().You can exit the process early using cancel_sweep_frequency().
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: none1: WEP2: WPA13: WPA24: WPA1/2import 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')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().
Use pip3 to install the latest version of the broadlink module.
pip3 install broadlinkTo communicate with a device, you must first obtain an authorization key.
0x04-0x12, a command value of 0x0065, and a NULL-terminated ASCII device name at offset 0x30.0x38.Decrypted Authorization Payload:
| Offset | Contents |
|---|---|
0x00-0x03 | Device ID |
0x04-0x13 | Device encryption key |
Use this Device ID and Device encryption key for all subsequent command packets.