Remote Control Switches via 433 Mhz transmitter using gpiod - instead of gpiozero , RPi.GPIO , WiringPi

This page shows the way to control your lights and other devices in your house by using a simple 433 Mhz transmitter connected to the Raspberry Pi.

For more than 10 years I used the WiringPi software from www.wiringpi.com (which was standard software to use at that time and now is deprecated).

I created a script (Bash) which are independent of the Raspberry Pi model used and OS/Debian version!

I am using the C-library of gpiod (5-10 times faster), which has better performance with sending than Python-library!

The scripts will still work after updates/upgrades of your Raspberry Pi hardware/software! So, this will work for RPi model 1 to 5.

It's using the gpiod software which is the best to be installed and the standard nowadays.

Note: Raspberry Pi 5 model will NO longer support NOR work with RPI.GPIO or WiringPi libraries/software.

 

Supported remote switches switches (which will work with this instruction):

- ELRO AB440 S
- Brennenstuhl RCS 1000 N Comfort
(Not tested: BAT RC-3500-A, Intertechno (Silvercrest) PA3-1000, Vivanco FSS 31000W)

NOTE: The ELRO switches are no longer available.. Order (Amazon/Ebay) and use the "Brennenstuhl RCS 1000 N Comfort" which are exactly the same as the ELRO's !

 

Hardware:

- Raspberry Pi

- RF-DRA887TX or DRA888TX or DRA889TX DORJI 433MHZ 10/17dBm ASK TRANSMITTER , DIP PACKAGE => http://www.dorji.com/products.php?CateId=36

PIN used on Raspberry Pi:
4 => 5V ----> +5V RF-Transmitter
6 => GND ----> 0V/GND RF-Transmitter
8 => GPIO for TX ----> Data out RF-Transmitter ==> This is Pin 14 !! See "TRANSMIT_PIN = 14" (in my case this is 14) in the elro.py script.

# make sure you install at least the libgpiod-dev for the gpiod.h file (for C-library support)
apt-get install gpiod libgpiod-dev


# Compile the binary with the following command (also make sure you installed gcc compiler)
gcc -Wall -o gpiod_elro gpiod_elro.c -lgpiod

 

mkdir -p /wiringpi/lights/

Scripts to create elro (To give all the credit in the past to WiringPi the directory is still named /wiringpi  and 'chmod 755' ):

-------------------------- name it: elro ------------------------------------------------------------

#!/bin/bash
#set -x

if [ -z "$3" -o -n "$4" ]
then
        echo "Usage: $0 <SwitchID> <SwitchNUM> <on|off>"
        echo "Example: $0 24 B on"
        exit
fi

DT=`date +%d-%m-%Y-%H-%M`

export PULSE=302
export ARG1=$1
export ARG2=$2
export ARG3=$3

if [ "${ARG1}" -lt "1" -o "${ARG1}" -gt "31" ]
then
        echo "Not a valid argument for SwitchID (valid values between: 1-31)!"
        echo "Usage: $0 <SwitchID> <SwitchNUM> <on|off>"
        exit
fi
D2B=({0..1}1{0..1}1{0..1}1{0..1}1{0..1}1)
PART1=`echo -n ${D2B[$ARG1]}|rev`

case $ARG2 in
        A|a )
                PART2=1110101010
        ;;
        B|b )
                PART2=1011101010
        ;;
        C|c )
                PART2=1010111010
        ;;
        D|d )
                PART2=1010101110
        ;;
        E|e )
                PART2=1010101011
        ;;
        * )
                echo "Not a valid argument for SwitchNUM (valid values: A|B|C|D|E)!"
                echo "Usage: $0 <SwitchID> <SwitchNUM> <on|off>"
                exit
        ;;
esac

case $ARG3 in
        ON|On|on )
                PART3=11101
        ;;
        OFF|Off|off )
                PART3=10111
        ;;
        * )
                echo "Not a valid argument for <on|off>!"
                echo "Usage: $0 <SwitchID> <SwitchNUM> <on|off>"
                exit
        ;;
esac

/wiringpi/lights/gpiod_elro ${PULSE} ${PART1}${PART2}${PART3}

exit

#Address(10 bits):
#31: 1111111111
#15: 1111111110
#1 : 1110101010
#10: 1011101110
#16: 1010101011
#24: 1010101111
1111101010101110101010111
#bitpos: 10 => 16
#bitpos: 8 => 8
#bitpos: 6 => 4
#bitpos: 4 => 2
#bitpos: 2 => 1
#
#Letters(10 bits):
#A on: 1110101010
#B on: 1011101010
#C on: 1010111010
#D on: 1010101110
#E on: 1010101011
#bitpos: 20 => E
#bitpos: 18 => D
#bitpos: 16 => C
#bitpos: 14 => B
#bitpos: 12 => A
#
#Letters(5 bits):
#on:  11101
#off: 10111
#bitpos: 24 => off
#bitpos: 22 => on
#

-------------------------- end of: elro ------------------------------------------------------------
 
 
-------------------------- name it: elro.c ------------------------------------------------------------
 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <gpiod.h>

#define NUM_ATTEMPTS 10
#define TRANSMIT_PIN 14

void transmit_code(const char* code, double short_delay, double long_delay, double extended_delay) {
    const char* path = "/dev/gpiochip4";
    int check_file = access(path, F_OK);
    const char* GPIOCHIPSET = (check_file == 0) ? "gpiochip4" : "gpiochip0";

    struct gpiod_chip* chip = gpiod_chip_open_by_name(GPIOCHIPSET);
    struct gpiod_line* line = gpiod_chip_get_line(chip, TRANSMIT_PIN);
    gpiod_line_request_output(line, "pulsecodesend", 0);

    for (int t = 0; t < NUM_ATTEMPTS; ++t) {
        for (const char* i = code; *i != '\0'; ++i) {
            if (*i == '1') {
                gpiod_line_set_value(line, 1);
                usleep(1000 * short_delay);
                gpiod_line_set_value(line, 0);
                usleep(1000 * long_delay);
            } else if (*i == '0') {
                gpiod_line_set_value(line, 1);
                usleep(1000 * long_delay);
                gpiod_line_set_value(line, 0);
                usleep(1000 * short_delay);
            }
        }
        gpiod_line_set_value(line, 0);
        usleep(1000 * extended_delay);
    }

    gpiod_line_release(line);
    gpiod_chip_close(chip);
}

int main(int argc, char* argv[]) {
    if (argc != 3) {
        printf("no arguments passed\n");
        printf("usage: elro <pulselength> <code>\n");
        printf("usage example: elro 316 1010111111010101011\n");
        return 1;
    }

    double short_delay = atof(argv[1]) / 1000.0;
    double long_delay = 3 * short_delay;
    double extended_delay = 31 * short_delay;

    transmit_code(argv[2], short_delay, long_delay, extended_delay);

    exit(0);
}

 

----------------------------------------------------------------------------------

#Compile the C-code with:

gcc -Wall -o gpiod_elro elro.c -lgpiod

cp -p gpiod_elro /wiringpi/lights/

#Now you can use the elro command to control a switch:

f.e.: ./elro 24 B on

./elro 18 C off

 

  Donate now via the Paypal button on the top in US Dollar and on the bottom in Euro