What this post is all about
This is a minor update to yesterdays post. After I had done my testing and written the post I decided to look for better ways to control the GPIOs without requiring that the executing user run the commands as ROOT. Through the magic that is google I stumbled upon the pigpio set of libraries and utilities.
Software
In the previous post I detailed the usage of the Python RPi.GPIO library. However, I was pretty dissatisfied with the fact that the Python needed to be executed with ROOT permissions, as this causes a significant security risk. As noted above: a bit of googling produced the fruitful pigpio. The benefit of pigpio (other than the fact that it’s cleaner) is that your python itself will no longer have to run under a root context. There will be a daemon (still running under a root context) that will accept commands via socket. In this way you can also remotely control GPIOs on other RasPis.
I am running Debian as my base distribution, so these steps may be specific to Debian (though they shouldn’t be). I also assume that you have the appropriate dependencies installed to build from source.
Step 1 - Get the tarball and install.
This is compiling on a RasPi, so keep in mind that it may take several minutes to build - don’t panic!
Code and Initial Testing
Some minor changes to test the boards in this section - everything other than the code is going to be similar.
Launch the daemon that controlls the GPIOs:
Quick Test - Launch a Pyton interactive shell (NOT AS ROOT) and issue some verification and testing commands. By issuing the following commands, assuming you are using the same RasPi and Relay Board as I am, you should see the LED and hear relay1 (IN1) physically switch over to the NO position when you set output to LOW and back to NC when you swithc to HIGH.
You can also see that there is a response from issuing these commands, useful for error handling etc…
I mentioned that this specific setup operates in reverse of what I expected. Normally, or normally according to google searches, OBJ.write(18,0) will turn OFF the relay. However, in the case of the noted relay board and raspi model it activates the NO side of the relay. Here are a few scripts that I took from GitHub and modified to work with this specific library, daemon, wiring and board model(s).
Turn EVERY relay on:
Turn EVERY relay off:
Cycle through 18,23,24,25 in a cylon style, ending with all in NC position:
Final Thoughts
Even with setups like this in potentially closed systems one should have security and extensibility kept well in-mind.