Remote Control Power Sockets: Part 9: Switching with librsswitch

In the first three parts we took the remote control apart on paper, with the oscilloscope and with an SDR. In Part 4, Part 5 and Part 6 we switched the same sockets from an Arduino Uno, an STM32F0DISCOVERY and an STM32F429I-DISCO with Zephyr, each time with a small firmware of its own. In Parts 7 and 8 we set up a Raspberry Pi and a BeagleBone Black so that they can drive a 433 MHz transmitter. What is still missing is the piece that turns "switch socket 1 on" into the right pulses on the data pin. That piece is librsswitch, a small C library that emulates the encoder chip of the remote control on a GPIO line. It knows two devices: the Pollin 2605 set of this series, whose remote control has the PT2262 (HX2262) encoder, and the REV 008342 with a PT2260.

Wiring

As the transmitter we use an FS1000A. We connect its data pin to the pin that toggle switched in Part 7 and Part 8. The module gets its supply from a 5 V pin of the pin header, never from a GPIO pin. According to its datasheet, it works from 3 V to 12 V and draws up to about 40 mA. With 5 V it reaches further than with 3.3 V, but the 3.3 V signal of both boards is enough for the data input. We switch the board off before wiring.

A wire of about 17 cm on the antenna pad ANT, a quarter of the wavelength at 433 MHz, improves the range considerably.

FS1000A 433 MHz transmitter module with the wires for data, VCC and ground

Raspberry Pi

  • DATA to pin 11 (GPIO17)

  • VCC to pin 2 (5 V)

  • GND to pin 9

BeagleBone Black

  • DATA to P9_23

  • VCC to P9_7 (SYS_5V)

  • GND to P9_1

P9_7 carries 5 V whether the board gets its power from USB or from the barrel jack; P9_5 only with the barrel jack.

Getting the code

librsswitch lives on GitHub. We clone it where we build: on the board or, when cross-compiling, on the host:

git clone https://github.com/graznik/librsswitch.git
cd librsswitch

The root directory holds the library, and examples/basic holds the command-line program rsssend, which sends exactly one command: rsssend DEV GROUP SOCKET DATA. DEV is 1 for the Pollin 2605 and 0 for the REV 008342. For the Pollin 2605, GROUP and SOCKET are numbers from 0 to 31; for the REV 008342, the group goes from 0 to 3 and the socket from 0 to 2. DATA is 1 for on and 0 for off.

How the codeword comes together

In Part 1 we found out that the encoder turns the DIP switches and the pressed button into a codeword of twelve tri-state bits. In Part 2 we measured these codewords, and in Part 3 we read the same words from the radio signal. librsswitch assembles such a word from three parts:

Part

Bits

Meaning

Group

5

DIP switches of the remote control (0 to 31)

Socket

5

Button pair of the remote control (0 to 31)

Data

2

0F for on, F0 for off

Group and socket are binary numbers with the lowest bit first. librsswitch writes a cleared bit as F and a set bit as 0, exactly the pattern from Part 2. Socket 1 is therefore 0FFFF. librsswitch then turns the word into pulses. A short pulse lasts 350 µs, which is four oscillator periods of 87.5 µs from Part 2, and a long pulse is three times as long:

  • A 0 bit is short high, long low, short high, long low.

  • An F bit is short high, long low, long high, short low.

  • A 1 bit is long high, short low, long high, short low.

  • After the twelve bits comes a sync pulse, short high and then a low of 31 short pulses.

librsswitch sends the whole word ten times, which takes about half a second.

We can follow all of this without hardware. For this, librsswitch comes with a stub board: it sits in place of the GPIO line and prints the codeword instead of sending it. make BOARD=stub builds the library with this board into build/stub, and make -C examples/basic BOARD=stub builds rsssend against it:

make BOARD=stub
make -C examples/basic BOARD=stub

rsssend links the library dynamically and has no built-in search path. When we run it from the repository, we therefore tell the loader the directory with LD_LIBRARY_PATH:

$ LD_LIBRARY_PATH=build/stub examples/basic/rsssend 1 0 1 1
FFFFF0FFFF0F
$ LD_LIBRARY_PATH=build/stub examples/basic/rsssend 1 0 1 0
FFFFF0FFFFF0
$ LD_LIBRARY_PATH=build/stub examples/basic/rsssend 1 1 1 1
0FFFF0FFFF0F

The first call is group 0, socket 1, on: FFFFF for the group, 0FFFF for the socket and 0F for on. That is the codeword we measured first in Part 2, FFFFF 0FFFF 0F. librsswitch counts the sockets the same way: socket 1 is the socket that this first codeword from Part 2 belongs to. The second call changes only the last two characters to F0, which is off. The third call sets group 1 and turns the first five characters into 0FFFF, as in the group 1 example from Part 2.

Raspberry Pi

On the Pi, librsswitch needs the libgpiod headers from Part 7. Without BOARD, make builds the library for libgpiod into build/gpiod, and make -C examples/basic builds rsssend against it:

sudo apt install build-essential libgpiod-dev
make
make -C examples/basic

The same build serves a 32-bit and a 64-bit Raspberry Pi OS and the BeagleBone Black as well; only the compiler determines the architecture. On the 64-bit system, file examples/basic/rsssend shows ELF 64-bit LSB and ARM aarch64. rsssend links librsswitch.so.1, and the library links libgpiod.so.3, the library of libgpiod 2.x. From the repository we start rsssend with LD_LIBRARY_PATH=build/gpiod. After sudo make install, the library is in /usr/local/lib, and the loader finds it without this variable too.

To cross-compile on the Debian 13 host from Part 7, we install the multiarch libgpiod package for the target architecture, libgpiod-dev:arm64 for a 64-bit and libgpiod-dev:armhf for a 32-bit Raspberry Pi OS. We pass the prefix of the cross-compiler to both calls in CROSS_COMPILE, for a 64-bit system like this:

make CROSS_COMPILE=aarch64-linux-gnu-
make -C examples/basic CROSS_COMPILE=aarch64-linux-gnu-
file examples/basic/rsssend
scp examples/basic/rsssend user@raspberrypi.local:

For a 32-bit system the prefix is arm-linux-gnueabihf-, and file shows ELF 32-bit LSB and ARM, EABI5. An rsssend built this way needs librsswitch.so.1 on the Pi. So there we build the library with make and install it with sudo make install.

The default line is gpiochip0:17, that is GPIO17, the pin of the wiring above. We choose a different pin with --gpio in the form chip:line, for example --gpio gpiochip0:17.

BeagleBone Black

On the BeagleBone Black we build the same way, natively on the board:

make
make -C examples/basic

To cross-compile, we pass CROSS_COMPILE=arm-linux-gnueabihf- to both calls and use the armhf compiler and libgpiod-dev:armhf from Part 8; then, too, the library must be installed on the board. file examples/basic/rsssend shows ELF 32-bit LSB and ARM, EABI5. The default line is gpiochip0:17, which on Debian 13 is P9_23, the pin of the wiring above; we do not need --gpio there. On other kernels P9_23 can belong to a different chip. gpioinfo P9_23 from Part 8 tells us which one, and --gpio takes the chip in the form chip:line. If it is gpiochip1, the call looks like this:

LD_LIBRARY_PATH=build/gpiod examples/basic/rsssend 0 3 0 1 --gpio gpiochip1:17

rsssend rejects a bare number such as 49, the format of the earlier sysfs version: the program exits with status 1 and prints malformed --gpio '49'.

Switching a socket

Now for the real thing. We tested with the REV socket from Part 4, its dial set to D 1, that is group 3, socket 0. With the transmitter connected, the first call switches it on, and the second switches it off again:

LD_LIBRARY_PATH=build/gpiod examples/basic/rsssend 0 3 0 1
LD_LIBRARY_PATH=build/gpiod examples/basic/rsssend 0 3 0 0

An rsssend that we cross-compiled and copied to the board runs there as ./rsssend 0 3 0 1 once the library is installed. For socket 1 of group 0 of the Pollin set, the arguments are 1 0 1 1 and 1 0 1 0. These calls send the codewords that we measured on the remote control in Part 2. We have not tried librsswitch on a Pollin socket for this part, though.

rsssend exits with status 0 when it has sent the codeword, with 1 when the library could not send, and with 2 for wrong arguments. The radio link goes one way only: status 0 says that a codeword has left the transmitter, not that the socket has switched. If a socket does not react, we run the command again: librsswitch times the pulses with sleeps on a normal Linux kernel, and a receiver may ignore a slightly stretched word.

On a Raspberry Pi 3 Model B Rev 1.2 with the 64-bit Raspberry Pi OS and on a BeagleBone Black revision A5C with the Debian 13 image from Part 8, rsssend switched the lamp on the REV socket on and off again, each time with status 0, without sudo and without --gpio. The oscilloscope in the background shows the pulses of a codeword:

The Raspberry Pi 3 in its acrylic case, the FS1000A transmitter on a breadboard, behind them the oscilloscope with the pulses of a codeword; the lamp on the REV socket is onThe same setup with the BeagleBone Black instead of the Raspberry Pi; the lamp on the REV socket is on

Stopping safely

While rsssend is sending, we never stop it with SIGKILL (kill -9). After that, no code runs any more that could pull the data pin low. A 433 MHz transmitter whose data pin stays high transmits its carrier continuously and blocks the band for everyone else until the next transmission starts, because every transmission first pulls the line low. A crash or a power cut in the middle of a transmission has the same effect. rsssend has no signal handler, so Ctrl-C is hardly better. A transmission takes about half a second; we wait that long.

What comes next

Next we switch the sockets over the network: with the rsswitchd daemon and a small web app from the librsswitch repository.