11

(Marcin) #1
FORGE

Now, park your car in your driveway and watch as
your light switches on, all by itself! Drive away again
to see it turn off.


HOW THE SCRIPT WORKS
The script starts by first connecting to the SQLite
database to retrieve the number-plate to watch
for, as well as the last recorded home/away state.
This is saved in a local variable, as well as being
represented by switching on or off the LED which
is connected to the Pi’s GPIO ports. The script then
constantly checks the Beanstalk queue for new Jobs
which are inserted by the OpenALPR daemon each
time it spots car number-plates in the frame. When
processing each Job, it compares the plate found
to the one it’s looking for. If a match occurs when
the car’s home/away status is currently ‘away’,
the system considers the car to have just arrived
home and switches on the LED, updates the local
variable, and makes a POST request to the IFTTT
servers triggering the ‘car_arrived’ event. This in
turn, switches on the hallway light via the Kasa web
service to your smart light bulb.
The Python script then deletes the current job,
clears the queue of any others and sleeps for ten
seconds – the ten-second sleep introduces a crude
rate-limit into the setup, meaning the lights could
never flash on/off irritatingly if something goes
wrong, and clearing the queue ensures there’s not
a huge backlog of results to work through once the
script wakes up again.
When the car drives away, the Beanstalk queue
should pretty quickly become – and stay – empty,
because there’s nothing in frame for OpenALPR to
find a number-plate in. This is handled in the section
of code which starts with the line if job is None:
where if the current home/away status is set to
home, the system considers the car to have just left
and the opposite occurs, eventually triggering the
‘car_gone’ event via IFTTT which turns the hallway
light back off again.


Above
All you need is a
cheap and cheerful
IP camera like this
one. Connect it to
your home network
with a cable rather
than WiFi, and make
sure it’s an outdoor
one if you’re installing
it outside

STAYING IN HOUSE
You could, of course, make this completely self-contained
without the need for an internet connection, and the use
of external services like IFTTT and a Kasa light bulb, if
you’re comfortable doing a bit more electronics. Rather
than using the GPIO ports to turn on the little LED, you
could instead turn on/off a relay switch and use that to
control lighting or anything else in your house. However,
please only attempt this if you’re competent and
experienced working with mains voltages!

For the sake of simplicity and readability, I’ve
left out important things like logging, proper error
handling, and resilience from the script I’ve shared
on GitHub, but if you’re building this system for
yourself, I would recommended enhancing the
code appropriately.
Finally, set the script to auto-run each time
your Pi boots. To do this, edit the Pi’s autostart
configuration file:
sudo nano ~/.config/lxsession/LXDE-pi/autostart

Add this line at the end of the file:
sudo service openalpr-daemon start
python /home/pi/ANPR-Home-Automation/anpr.py

The anpr.py script will now load each time the Pi
boots up, and the ALPR daemon will also be started,
just in case it doesn’t start automatically.
Now you know how to set up your own automatic
number-plate recognition system and tie it in with
your own home automation and IoT devices, what
are you going to set to happen when you drive your
car onto your driveway? We’d love to see what this
inspires you to make – get in touch via Twitter to
show off your creations!
Free download pdf