Anth's Computer Cave

AAIMI GPIO Tutorial Hub

AAIMI GPIO is a web-based control interface for the Raspberry Pi and Arduino. Check out the tutorials below.


Using AAIMI GPIO Notifications

24th November 2017

Today we'll configure AAIMI GPIO to provide notifications on input pin events.

There are several notification methods available, Email and HTTP POST request.

Email notifications are perfect if you are creating AAIMI GPIO based security or monitoring gadgets and you wish to know their status while away.

You can use HTTP notifications to send details of pin changes to a wesite. You could use this, for example, to create a Raspberry Pi weather staion with AAIMI GPIO and have the weather details automatically POST live to your website. In the next version you can use the HTTP feature to send details to other AAIMI GPIO systems in your home to create Raspberry Pi swarms.

We'll cover email notifications first, then move on to HTTP requests.

Install libraries

Before we start we'll install some libraries.

AAIMI uses web requests for HTTP communication. The Python Requests library takes care of that. You may have to install the Requests library. In your Raspberry Pi terminal type:

sudo pip install requests.

Strangely, the system will probably state that the latest version of Requests is already installed, but I have found Requests won't work until you have ran the command anyway.

Next we'll update some libraries for AAIMI's communication. Type:

sudo apt-get install python-dev libffi-dev libssl-dev

This may take a few minutes. When it's finished, type:

sudo pip install --upgrade ndg-httpsclient


Email notifications

AAIMI GPIO uses another AAIMI module called aaimi_email_out.py, which runs a Python Gmail client to send emails

AAIMI sends emails from its own dedicated Gmail account. You'll need to get a Gmail account for your AAIMI GPIO system and change it's settings to Allow 3rd Party Apps. If you are running multiple AAIMI GPIO systems you can use the same account for all of them.

Enable Email notifications

Next you'll need to enable email notifications in the main AAIMI GPIO code.

Open aaimi_gpio.py and scroll down to line 38.

Change the email_enabled variable to "yes". Next go to line 130 and enter your system's Gmail account.

Leave the password variable on line 134 blank. You shouldn't have the email password written in plain text in your code.

You enter the password via your phone after you have started the Raspberry Pi.

If you wish instead to have the system email password in the Python code (not recommended), enter your password on line 134, then change the pass_entered variable on line 124 to 'yes'.

Save and close the aaimi_gpio.py file, restart the program and open the AAIMI GPIO GUI on your phone.

Enter email password

The email pass entry field in AAIMI GPIO

Click the On/Off tab and click Set Email Pass.

The email pass entry field in AAIMI GPIO

Enter your system's email password and click submit.

You can now use the email notifications with your digital and analog input pins, let's make something!

Example email gadget

For this demo we'll make a simple PIR-based one-room security system to tell you if anyone enters your home while you are out.

We've already hooked one of these up in an earlier article, but here's the diagram again for a PIR sensor attached to a Raspberry Pi.

A PIR sensor connected to a Raspberry Pi. Picture: Anthony Hartup.

As before, I'll use GPIO17, so I click the GPIO17 button and click Change Setting.

In the initial form I enter a name and select Input from the drop-down menu.

In the next section set the trigger for the pin. For a PIR sensor this is High.

The email option in AAIMI GPIO

Enter a heading for your email alert. I'll call mine Intruder Alert.

The email heading entry field in AAIMI GPIO

Click next, and you're done.

Test your system by moving past the PIR sensor and waiting for an email.

Once you know things are working, turn the AAIMI GPIO loop off from your phone and the system will stop monitoring the sensor. When you go out, simply start the AAIMI GPIO loop after you leave and the system will start monitoring. When you return just stop the program loop from your browser before you enter your home. The system will remain in dormat mode until the next time you restart the program loop.


HTTP POST requests (Advanced)

I have a few plans for the HTTP notification method, one of which is to use it to join multiple AAIMI GPIO systems together. I also plan to build methods to post to some of the popular online services and social media platforms. For today I'll provide a simple demo to have AAIMI send an input event to a website to log the pin name, state and time.

First, let's talk about security and other rquirements.

You should only let AAIMI GPIO send HTTP requests to HTTPS-enabled websites and servers, or servers on your own secured LAN. Otherwise other people may be able to see communications and credentials in transit.

You'll need a web server to send data to, and you'll need a reasonable grasp of HTML, Javascript and PHP.

Enable in code

As with the Email method, you'll need to enable HTTP in the Python code and add your details.

Go to line 41 in aaimi_gpio.py and change the http_enabled variable to 'yes'. Enter the Url to the PHP script on your website on line 145.

If you require a username and password to POST to the website, enter your username on line 149. If not, change the pass_required variable on line 148 to 'no'.

Once again I don't recommend storing your password within the code, so you can enter that via you phone just like the email password method I mentioned earlier.

If you are just playing on your home LAN and don't care about security you can once again hard code the password. Enter your pass on line 154 and change the website_pass_entered variable on line 153 to 'yes'.

Save and close the file and start the AAIMI GPIO program. Start the main loop from the On/Off tab in your browser then click the Set Website Pass button and enter your password.

Example HTTP gadget

You now have HTTP notifications enabled, let's look at our HTTP example.

Let's say you own a store and you wish to guage customer traffic. You could use AAIMI GPIO's Count Input Events action to monitor the numbers while you were there, but what about while you are away?

You can create a simple private PHP page on your site to receive pin data from a PIR movement sensor on your AAIMI GPIO system, then embed the data into your website and view it from anywhere. This could work with multiple GPIO systems, so you could create multiple traffic-monitoring units.

We'll set up a movement sensor input on GPIO17.

A PIR sensor connected to a Raspberry Pi. Picture: Anthony Hartup.

As before give the pin a name and this time select Send HTTP Request from the drop-down Action menu.

Using HTTP reuests in AAIMI GPIO

For digital pins with just a low and High state, this web request will only fire if the pin goes into event. For analog pins the web request will fire any time the analog reading changes.

You'll be asked to provide two key/value pairs to send the data in.

Setting HTTP arguments for web request

I'll briefly explain how requests to websites work. The data is sent to a PHP script as key/value pairs appended after a question mark at the end of the URL. The PHP file extracts the values and executes any tasks required.

I'm going to use two keys called 'pin' and 'status'. I want the value for 'pin' to be the name of the GPIO pin the data relates to, and the 'status value to be the status of that pin. My request to the server would look something like:

https://mywebsite/myphp_script.php?pin=gpio17&status=High

So to use those values in AAIMI GPIO I'll change the first key to 'pin' and the second pin to 'status'.

Setting HTTP arguments for web request

There are drop-down options for default values, and my choices are included in those. I select Pin Name for key 1 and Pin State for key 2.

If I wished to use custom values I could instead select 'custom' and enter my values in the pop-out custom fields. For example, if the pin is monitoring my Lounge I could have entered 'Lounge' in the pop out custom field for key 1 instead of using the default GPIO pin name.

Before the program sends the request it creates another key called 'time' with a value of current day:hour:min.

Receiving pin data

Now we'll create a basic PHP script to put on your web server that extracts the data and writes it to file for your web-pages to access.

This script gets the value of the pin, status and time keys (Note that it needs PHP tags at the start and finish. I couldn't get them to display properly on this page).

$pin = $_GET['pin'];
$status = $_GET['status'];
$time = $_GET['time'];
$params = $time . " " . $pin . " " . $status . "\n";
$fh = fopen('pins.txt', 'a') or die("can't open file");
fwrite($fh, $params);
fclose($fh);
echo "done";

It then combines them into a space-separated string and appends them to a new line in a text file called pins.txt.

To view your log of pin events you can go to https://yourWesite.com/pins.txt.

This is a really simple implementation but you can build on that.

Next

In the next article we'll connect and operate a distance sensor using AAIMI GPIO.

Cheers

Anth


Previous: Using stepper motors with AAIMI GPIO

Next: Using distance sensors with AAIMI GPIO.

_____________________________________________


Comments

Leave a comment on this article