Controlling a ESP8266 H801 Wifi controller through Python

These controllers can be bought for about ~$10 from Aliexpress which makes it possible to light an entire room with LEDs for about ~$20 if you wish. Controlling them is pretty easy too, besides the available Android application, the protocol is actually fairly simple.

The controller: RGBWW LED Strip Light WiFi Controller Dimmer ESP8266 (Android WLAN Control) 1 Port Control 200 Lights Output 5 Routes PWM Data
led-dimmer

Here’s a simple Python script to generate a rainbow:

import time
import socket
import colorsys

# Only change the 192.168.0 part, the 255 is the broadcast address which is
# what we want
IP = '192.168.0.255'
PORT = 30977
# Light IDs (hint, the last few characters of the wifi name: HCX_<id>). Don't
# forget to add the 0x before
IDS = 0x235817,
# Wait for 10ms between setting colours
DELAY = 10
# Rainbow steps
N = 0xAFF


def set_color(r=0, g=0, b=0, w0=0, w1=0):
    message = [0xFB, 0xEB, r, g, b, w0, w1]
    for id_ in IDS:
        # The mac is in reverse order
        message.append(id_ >> 0)
        message.append(id_ >> 8)
        message.append(id_ >> 16)
        message.append(0x00)

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    # Convert to characters and cut the numbers to 0xFF
    sock.sendto(''.join(chr(c & 0xFF) for c in message), (IP, PORT))

    time.sleep(DELAY / 1000.)


r = g = b = 0
# I like rainbows
for i in range(N):
    # Generate HSV colours
    h, s, v = i * 1. / N, 1., 1.
    # Convert HSV to RGB integers
    r, g, b = [int(0xFF * c) for c in colorsys.hsv_to_rgb(h, s, v)]
    # Set the colour
    set_color(r, g, b)

# Turn the lights off again
set_color()

Bookmark and Share

About Rick van Hattem

Rick van Hattem is a Dutch Internet entrepreneur and co-founder of Fashiolista.com
No comments yet.

Leave a Reply