It is perfectly possible to control the GPIO lines from the command line. This will allow you to control Dave's relays by a simple shell script. But you'll have to make some preparations first.
First of all we must ensure that normal users can control the GPIO pins of the Raspberry Pi, which control the relay outputs. Enter the next command to make this happen:
sudo usermod -aG gpio pi
This line assumes that you're user pi. Replace the user name pi with your own if it is not. You'll have to log out and log in again to make this change effective.
Now we must export the 7 GPIO pins which drive the relays. Exporting these pins will free them from direct control by the kernel, which will enable them to be controlled by a non-root user. After exporting the lines we make them all outputs.
We can export these GPIO lines manually, but we'd have to do that again after every reboot. Therefore it's better to have the system do it automatically at reboot. Start editing the /etc/rc.local file with the command sudo nano /etc/rc.local and add the following lines to this file, just before the line containing the exit 0 command.
# Export GPIO relay lines for GPIO in 17 18 22 23 24 25 27 do echo $GPIO > /sys/class/gpio/export echo "out" > /sys/class/gpio/gpio$GPIO/direction done
If you don't want to reboot your Raspberry Pi to make the changes effective you can simply run the rc.local file with sudo /etc/rc.local.
After that, or after a reboot, this will be the output of the ls /sys/class/gpio directory:
export gpio17 gpio18 gpio22 gpio23 gpio24 gpio25 gpio27 gpiochip0 unexport
If you don't want to control the GPIO lines through the shell any more simply remove the lines above from the /etc/rc.local file . Reboot after that or give a command like this:
echo 17 > /sys/class/gpio/unexport
Repeat this command, replacing the 17 with the appropriate GPIO port number to unexport all the other ports as well.
Warning: If you have built in a micro controller like I did, you'll have to make sure that the micro controller doesn't drive the I/O lines together with the Raspberry Pi.
Otherwise you're bound to have electrical conflicts which may harm the Raspberry Pi and/or the micro controller.
Driving the Reset line to the micro controller to keep it in Reset state is no solution, as per default the reset pin of the micro controller is programmed to be just an other I/O pin.
After these preparations you can easily control Dave's relays. Controlling a relay is simply a matter of writing a 0 (relay off) or a 1 (relay on) to any of the files /sys/class/gpio/gpioNN, where NN is the GPIO number. The table below will tell you what the GPIO numbers are for each of the relays.
Relay | GPIO |
1 | 17 |
2 | 18 |
3 | 22 |
4 | 23 |
5 | 24 |
6 | 25 |
7 | 27 |
Suppose I want to turn relay 4 on and relay 6 off, this is what you would have to do:
echo 1 > /sys/class/gpio/gpio23/value echo 0 > /sys/class/gpio/gpio25/value
That's all there is to it to switch relays now. You may control the relays this way with just about any program now. All you have to do is write a 0 or a 1 into the value file of the appropriate GPIO pin.
Similarly, if you want to know the current status of a particular relay, simply read the appropriate value file of the GPIO pin. An output of 0 means the relay is turned off, a 1 means it is on.
cat /sys/class/gpio/gpio23/value