As anybody who has a garage knows, sometimes the door get left open inadvertently. Since the door is usually not visible from inside the house, that is easily done. Most of the time, there is no harm, but, if left overnight, raccoons and other undesirables may enter and create havoc.
Raspberry Pi to the rescue. In short, what I did was mount an ultrasonic sensor (HC-SR04, from eBay, $2.00, free shipping from China) on the ceiling in one of the bays of the garage (our garage is a double with a single 5 m door). When operating, if the sensor finds that is measure less than 20 cm, then the garage door is open. If the distance is between 50 and 150 cm, then the garage door must be closed with a car parked in the spot. If the distance is greater than 150 cm, the door is closed with the car gone. (Total distance from ceiling to floor is 244 cm)
My primary Pi does temperature logging every minute (see earlier posts), so I decided to make the the garage door check a part of this process.
Since I used the wiringpi library as part of the temperature logging process, I do not need to invoke ‘sudo’ before starting python. However, the sample ultrasonic sensor script did use sudo, so I was forced to find a way around. In the end, I used Richard Ryniker’s gpio_control C program to export the required GPIO pins. I found it gives great results. Now I can run my Python program without using sudo.
Sometime the ultrasonic sensor gives erroneous results. Therefore, I take 20 readings (only takes less than 200 millisecond the accomplish), then drop the highest 3 and the lowest 3 readings and average the rest. This gives very accurate results. Then I write the output, i.e. the distance and the time to a text file so I can use it when a web request comes in.
Here’s the final code:
This is how it is called:
Note that when the status is found to be open, GPIO Pin 11 is turned high. This I use to turn on an LED which I mounted next to the furnace thermostat in the living room. Now, in order to make this more attention grabbing, I wanted it to be a flashing LED. Of course the Pi could do this, but in this case that seemed like overkill. So I bought an LED that flashes all on its own, at about 2 Hz. (KLF-336HD-3P).
I also made it part of the web page that my Pi can produce (see earlier posts).
In case you are wondering, the first 7 lines show temperatures inside, outside and in my generation housing (see earlier posts).
Although it seems a rather trivial project, it was nonetheless very time consuming. Pulling wires from the utility room where the Pi is located to the garage, as well as to the living room next to the thermostat (doing it decently, that is) takes far more time than you wish it would. Then, finding a workaround for sudoless ultrasonic sensing, fitting the new into the existing Pi setup, testing a flashing LED on the Pi (ya never know what it might do) all took its sweet time. But then, hey, it is all worth it when in the end it works and it is useful.