Even before getting the RPi I made some jokes about making it control my Christmas-lights. They are not the heaviest lights around; it is just 30 LED’s in a row running on 4.5v (three AA).
The easiest way to get started with the GPIO would be by connecting an LED. I followed this guide, and just connected the Christmas-lights instead. Pretty simple.
The computer is now controlling something physical. How fun! :)
However, why stop there? Would it not be fun if my neighbors could control them too? I have the lights in my window so they are already visible for everyone.
I am of course not the first one with this thought, so there are a lot of tutorials and blogs about how to do this. However, that would ruin the fun!
My solution is the following: A PHP page allowing you to turn on or off by sending a get-parameter, and then returning the current state – are they on or off?
$what = $_GET['turnem'];
if ("on" == $what) {
//turn on
exec("gpio write 0 1 > /dev/null &");
}
else if ("off" == $what) {
//turn off
exec("gpio write 0 0 > /dev/null &");
}
// get state
passthru ("gpio read 0", $gpio);
print $gpio;
Visiting https://thepi/?turnem=on would execute the command “gpio write 0 1” and not caring for any results, and then return the state. Visiting the page without any parameters would just return the state.
The command “gpio write” doesn’t return anything, so the “> /dev/null &” isn’t actually needed, but is good for further references. ;) Passthru allows for keeping any binary result from a command. In this case it will fill the variable $gpio with 1 or 0 depending whether the lights are on or off. Please note that this solution does require you to run “gpio mode 0 out” one time manually, which suits my needs.
Just having an empty page viewing a number isn’t very user friendly, but it allows you to make a fancy looking Norwegian page for your neighbors!