The USR-R16-T is a 16-channel web relay sold on AliExpress and other places. I bought one, and spent hours building it into a box with 16 power outlets, all the while assuming that there was documentation available, and that it would be easy to control with a simple telnet-style protocol. Boy, was I wrong! I’ve wasted hours trying to follow broken links to get the Lonhand Protocol documentation… no dice.
Finally, now that I’ve gotten into openHAB and learned Python, I realized what I needed to do – so I reverse engineered the control protocol, with the help of wireshark. I’m posting this here hoping that google will help other people who are trying to find out how to control this relay.
I did not bother with reading the state of the relay – I was happy to set the state of each relay, one at a time. openHAB will be maintaining the state anyway.
Here is the solution.
Python script:
from core.rules import rule
from core.triggers import when
from core.log import logging, LOG_PREFIX
log = logging.getLogger(LOG_PREFIX + ".ruletest")
import socket
import time
def LonhandPower(strIP, strPassword, iChannelNumber, bPowerState):
command=bytearray.fromhex("55 AA 00 03 00")
command.append(2 if bPowerState else 1)
command.append(iChannelNumber)
iChecksum=0
for x in range(2,7):
iChecksum += command[x]
command.append(iChecksum)
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((strIP,8899))
sock.sendall(strPassword+"\r\n")
sock.recv(256)
sock.sendall(command)
sock.recv(256)
time.sleep(0.05)
sock.close()
del sock
@rule("Kvm1")
@when("Member of Kvm1 received update")
def KvmUpdateHandler(event):
iChannel=int(event.itemName[-2:])
bState=True if event.itemState==ON else False
LonhandPower("172.22.22.232","admin",iChannel,bState)
Items:
Group Kvm1 "KVM 1" <poweroutlet>
Switch Kvm1_01 "Unit 01" <switch> (Kvm1)
Switch Kvm1_02 "Unit 02" <switch> (Kvm1)
Switch Kvm1_03 "Unit 03" <switch> (Kvm1)
Switch Kvm1_04 "Unit 04" <switch> (Kvm1)
Switch Kvm1_05 "Unit 05" <switch> (Kvm1)
Switch Kvm1_06 "Unit 06" <switch> (Kvm1)
Switch Kvm1_07 "Unit 07" <switch> (Kvm1)
Switch Kvm1_08 "Unit 08" <switch> (Kvm1)
Switch Kvm1_09 "Unit 09" <switch> (Kvm1)
Switch Kvm1_10 "Unit 10" <switch> (Kvm1)
Switch Kvm1_11 "Unit 11" <switch> (Kvm1)
Switch Kvm1_12 "Unit 12" <switch> (Kvm1)
Switch Kvm1_13 "Unit 13" <switch> (Kvm1)
Switch Kvm1_14 "Unit 14" <switch> (Kvm1)
Switch Kvm1_15 "Unit 15" <switch> (Kvm1)
Switch Kvm1_16 "Unit 16" <switch> (Kvm1)
Hope it helps somebody.