11

(Marcin) #1

Build your own ANPR home automation setup


TUTORIAL


sqlite> BEGIN;
sqlite> CREATE TABLE anpr_cars(id INTEGER PRIMARY
KEY AUTOINCREMENT, car TEXT, isHome INTEGER,
lastChangeDate DATE, lastChangeTime TIME);
sqlite> COMMIT;
Finally, write an INSERT query to populate the table
with the data you’ll be using:
sqlite> BEGIN;
sqlite> INSERT INTO anpr_cars(car, isHome,
lastChangeDate, lastChangeTime) values(“PA11LFP”,
0, date(‘now’), time(‘now’));
sqlite> COMMIT;
sqlite> .quit

IF THIS, THEN THAT...
You’re nearly ready to run the script, but first it’s time
to set up the action(s) you want to happen each time
you park your car on your driveway. In my case, it’s
going to turn on the hallway light by my front door,
which is a TP-link WiFi-enabled smart light bulb
controlled by the Kasa app. Kasa is supported by the
IFTTT service, which will act as a bridge between the
Raspberry Pi and the light bulb by using webhooks.
To configure webhooks on your IFTTT account,
log in and click on My Applets, then Services. Type

‘webhooks’ in the search box, and then click on the
Webhooks icon that comes up. Click Settings in the
top-right corner and click Connect. This will generate
a unique URL for your account with a key which
authorises your script to use your IFTTT account. Copy
and paste the URL into your address bar to bring up
the instructions page, which briefly explains how to
use the webhooks as various triggers in Applets you
can create. Essentially the Python script will make a
POST request to the IFTTT servers, and specify an
{event} followed by the key which both associates the
action with your account and authorises the request.
Make a note of the key from this page, as we’ll need
it in a moment, but first we need to create the Applets


  • one to turn the light on when the car arrives, and one
    to turn it off when the car leaves.
    Go back to your My Applets page and click on New
    Applet, then click on the big ‘+this’ to add your ‘if this
    happens...’ scenario. Type ‘webhook’ in the search
    field and click on the Webhooks icon that comes up.
    Click on ‘Receive a web request’ for the trigger and
    then type your event name. This must match the event
    name in the script exactly, so type in ‘car_arrived’
    (without the quotes) then click create trigger.
    Next, click on the ‘+that’ and search for ‘Kasa’.
    If you’ve not already connected your TP-Link Kasa
    account to IFTTT, then you’ll be asked to do so. For the
    action, choose ‘Turn on’ and all your Kasa devices will
    be available in a drop-down; select the device you want
    to turn on – in my case it’s Hallway Light. Click Create
    Action and then Finish. Then, do exactly the same and
    create an Applet with the event name ‘car_gone’ and
    set this to Turn off Hallway Light instead.
    Once that’s done, install the main Python script:
    sudo apt-get install git
    git clone https://github.com/paulfp/ANPR-Home-
    Automation.git


This will download the script into a new directory
called ‘ANPR-Home-Automation’ within the pi user’s
home directory. Once that’s done, open up the Python
script with the following command:
cd ~/ANPR-Home-Automation
nano anpr.py

Look for the line which sets the ifttt_webhook_key
variable, and paste in the key you noted previously:
ifttt_webhook_key = “__YOUR_IFTTT_KEY__”

Save and exit the file. You can now run the Python
script like this:
python ~/ANPR-Home-Automation/anpr.py

Above
Using the calibration
utility, you can
artificially adjust
the perspective of
the image which is
given to OpenALPR,
to make up for
less-than-perfect
camera angles

If you don’t already
have your smart WiFi
LED bulb set up and
working, you can
watch a quick video
guide on how to do
this at:
hsmag.cc/uhoCic.

QUICK TIP


You’re nearly ready to run the script, but first it’s
time to set up the action(s) you want to happen
each time you park your car on your driveway


Free download pdf