Raspberry Pi: Controlling your wall sockets

Black cat - light switchIn this post I will describe 2 methods to operate “remote controlled sockets” via the Raspberry Pi.

The methods differ in the used software. Both methods use C as development language and compile to native Raspberry Pi code (ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked).

The prerequisites and wiring are the same for both methods.

Prerequisites

Type: 972080 Imported by: Electro Cirkel B.V. - Rotterdam
Type: 972080
Imported by: Electro Cirkel B.V. – Rotterdam bought at Action.
433MHz - Receiver / Transmitter
433MHz Transmitter (top) / Receiver (bottom) kit.
  • RaspberryPi 2 – Model B – running Raspbian;
  • Working (WLAN-)connection;
  • 433MHz Transmitter/Receiver kit (buy for example at eBay or DealExtreme);
  • Remote controlled sockets.

Hardware

Raspberry Pi - B Rev 1, Raspberry Pi - A/B Rev 2 and Raspberry Pi 2 Model B GPIO pinouts.
Raspberry Pi – B Rev 1,
Raspberry Pi – A/B Rev 2 and Raspberry Pi 2 Model B GPIO pin outs (click to enlarge).

In this step you will learn how to electrically connect the transmitter and receiver to the Raspberry Pi.

Read at the printed circuit boards (PCBs) of the transmitter and the receiver the labels (VCC, GND and DATA).

Transmitter

Connect pin DATA to pin 11 (GPIO17), VCC to pin 2 (+5V) and GND to pin 6 (GND).

Receiver

Connect one of the DATA pins to pin 13 (GPIO27), VCC to pin 4 (+5V) and GND to pin 9 (GND).

After connecting the transmitter and receiver to the Raspberry Pi, it could look like this. Don’t be distracted by the plugged in RTL-SDR USB stick. During development this stick was used to check whether a 433MHz signal was transmitted.
Raspberry Pi 2 with connected 433MHz receiver and transmitter. As bonus you see a plugged in RTL-SDR USB stick.

Method 1 – Wiring Pi / 433Utils

This method is based on the projects: Wiring Pi library and 433Utils.

Software

  1. Login at Raspberry Pi (password: raspberry)
  2. Make sure Raspbian is up to date
  3. Install extra packages
  4. Create working directory
  5. Download / Build / Install Wiring Pi
  6. Download / Build 433Utils

Usage

  1. Listen for the codes transmitted by the remote control of the remote controlled sockets.
    Start the sniffer, press each key of the remote (one at a time) and write down the received (decimal) codes. Sometimes you have to press the same key several times because the signal was not received correctly.

    Received codes:
  2. Learn the remote controlled sockets to listen to your Raspberry Pi.
    1. Plug in a socket.
    2. Press and hold the button at the socket till the LED is blinking (+/- 5 seconds). Now the socket is ready to pair with your Raspberry Pi transmitter.
    3. Select one of the codes obtained using the sniffer to bind.

      The LED at the socket will blink fast to indicate it has been bound with the Raspberry Pi. When it doesn’t work the first time, just submit the code again. This is also a disadvantage of this kind of sockets, there is no feedback about whether the signal has been received in good condition. Good practice is to resend the same command several times after each other (i.e. 3x).
  3. Now switch the socket ON by the command:
  4. …and OFF again by:
  5. Pair the next socket with the Raspberry Pi. Plug in the socket, make sure the socket is in learning mode, select an unused code (i.e. 16115757) and submit it using codesend.

Enjoy playing with the sockets 🙂

Extra 1: Use the Wiring Pi command: gpio to see the pin out.

Extra 2: Use the Wiring command: gpio to see the exports.

Read more


Method 2 – Patrik Schmitz (no extra dependencies)

This method has been created by Patrik Schmitz (credits to him), I translated it to English and stored it here, mainly for own reference. It is based on recording the original 433MHz signal transmitted by the remote control and playing it back via the transmitter.

Software

  1. Login at Raspberry Pi (password: raspberry)
  2. Make sure Raspbian is up to date
  3. Install extra packages
  4. Create working directory
  5. Create sources files
    Now create in this directory three source files: receive.c, record.c and transmit.c. Make sure the path to the GPIOs is correct in the files.
    For the two receiving programs (receive.c and record.c) this must be: /sys/class/gpio/gpio27/value because the 433MHz receiver is connected to Raspberry Pi 2 – Model B pin 13 (GPIO27). For the sending part (transmit.c) this must be /sys/class/gpio/gpio17/value because the 433MHz sender is connected to pin 11 (GPIO17).
    receive.c

     

    record.c

     

    transmit.c

  6. Compile source files

Usage

  1. Now configure the GPIO.
    Tell the Raspberry Pi we like to use GPIO17 and 27:

    … and set GPIO17 as output:

  2. Record the signals
    Execute the command record, press a key on the remote control (+/- 2 seconds) and stop the record program (CTRL+C).

    Now rename the file which contains the recorded data (record.dat) to a name which corresponds to the name of the pressed button i.e. 1_on.dat Repeat these steps for each button.

  3. Pair the Raspberry Pi with the socket
    Plug in a socket, make sure it is in learning mode (press button on socket till it’s blinking 5 seconds). Now play back one of the recorded signals:

    The LED at the socket should start blinking fast.

  4. Switch on/off the socket
    Playback the key 1 ON signal to switch ON the socket:

    …playback the key 1 OFF signal to switch OFF the socket:

Enjoy playing with the sockets 🙂

Read more