====== SR201 network controlled module ======
{{:ha:sr-201.jpg?200 |}}The SR-201 module is a widely available ethernet connected relay module. It is advertised as being "web controlled" in many outlets, but as I found out, it's not. I have written some python to allow it to be controlled via MQTT, this is available on Git Hub here:-[[https://github.com/andrewjstringer/sr201-mqtt/tree/dev]]. The link points to the dev branch which is working, but should probably get a code review before merging to master.
The purpose of this project was to override a bedroom light switch, so when it was "dark hours", children cannot turn the bedroom light back on.
This code listens for messages from an MQTT topic and sends the appropriate command to the SR-201 module, it also reads the module status and writes it back to an MQTT topic. These are definable in a config file.
{{:ha:sr-201-relay-module-sm.jpg?400 |Constructed SR201 device}}
===== Factory Defaults =====
Default IP Address: 192.168.1.100
Port 6722: TCP control
Port 6723: UDP control
Port 5111: TCP Configuration
The device can be reset to these defaults by shorting the CLR pins
on the header next to the RJ45 connector. CLR is adjacent to the +5V
and P30 pins.
===== Testing from BASH =====
Below is a quick play about from BASH to test the relay with raw TCP. ''nc'' is Netcat, a really useful utility to communicate with raw TCP/IP, either to send packets as here, but also you can use it to set up a listener on a port. See the repo link above to work out the data fed to nc from the printf. Using printf as echo by default sends a newline / CR which will upset the module.
If you're stuck on windows you probably don't have netcat, yet another reason to upgrade to Linux.
# Commands are ASCII strings that must be sent in one packet
# (even for TCP):
#
# 0R No operation (but return status).
#
# 1R* Close relay if it's open, wait approx 1/2 a second, open
# relay.
#
# 1R Close relay if it's open.
#
# 1R:0 Close relay if it's open.
#
# 1R:n Close relay if it's open, then in n seconds (1 <= n <= 65535)
# open it.
#
# 2R Open relay if it's closed.
#
# Where:
#
# R is the relay number, '1' .. '8'. The main board has relay's
# '1' and '2', the extension board (if present) has the rest.
# If R is 'X' all relays are effected.
#
# If the command is sent over TCP (not UDP, TCP only), the relay will
# reply with a string of 8 0's and 1's, representing the "before" command
# was executed" state of relay's 1..8 in that order. A '0' is sent if the
# relay is open, '1' if closed.
$ printf "11:" | nc 192.168.1.91 6722
10000000
$ printf "12:" | nc 192.168.1.91 6722
11000000
$ printf "02:" | nc 192.168.1.91 6722
11000000
$
This page has been accessed:- \\
Today: {{counter|today}} \\
Yesterday: {{counter|yesterday}} \\
Until now: {{counter|total}} \\