Anth's Computer Cave

AAIMI GPS Mapper


Using AAIMI GPS Mapper

12th May 2017

In our first AAIMI GPS Mapper tutorial we covered the basic program features and downloaded, then set up the system on your server.

Today I'll show you several ways to embed the AAIMI GPS Mapper into your programs for seamless operation.

How I use AAIMI GPS Mapper

The first of my programs to employ the new GPS Mapper is our AAIMI Home Automation system.

AAIMI Home Automation already had a web server as an integral part of its system, and all the existing functions to handle calls from that server and react accordingly. It was already monitoring home-occupants' locations, but it was using an IP address-based system to decide whether they were home(via the LAN) or away(via the WAN).

If you have your own communication system in place, you can import aaimi_perimeter.py directly into your main Python program file like I have. This method uses just the live user-location detection functions and keeps the configuration and area setup separate from your program.

If you are just starting out you can build your own functions into the AAIMI GPS Mapper code. We'll cover this option first.

Build inside AAIMI code

Open aaimi_mapper.py and scroll down to line 114 in the check_if_within_perimeter() function. You'll see three commented blocks headed "Your Code Here". These are the three sections of code where a decision has been made about a user's location.

You just need to add your own actions to suit your needs into each block. For complex actions you can create new functions above the check_if_within_perimeter() function and call them from inside these blocks.

That's the Python stuff sorted. You can also customize and re-work the various web files to suit your needs.

You can create new sections in the aaimi_mapper.html file or add your own webpages that utilize the system. You can modify the aaimi.css file to change the appearance and layout of the webpages.

Importing into your existing program

The best way to import the system into your program is to import aaimi_perimeter.py. This file contains just the raw location detector.

import aaimi_perimeter

If you don't already have one you'll need to add a list of your program's users to match the list created by the configuration web interface

users = ["yourName"]

Add a call to load the GPS coordinates list to memory.

aaimi_perimeter.load_gps_coordinates()

That loads the file created in the configuration web interface in the last article.

I have a phone_home() function in my AAIMI Home program. AAIMI Home Automation receives commands from the web interface as a space-separated string containing the user's name, the latitude and longitude of the device. The phone_home() function is called to check and update the users' current location status, with the username, longitude and latitude as parameters.

Here's what you'll need in your own phone_home() function in your program:

First, add a line to check if the user is in the official home perimeter from the main user's list.

if aaimi_perimeter.check_if_in_perimeter(latitude, longitude, aaimi_perimeter.main_user) == "home":
    users[user] = "home"
    # Put your home reactions here

If the user is "home", the function will update the status and quit. If they are "away" the function will check the user against any places in their own list of perimeters.

users[user] = aaimi_perimeter.check_if_in_perimeter(latitude, longitude, user, scope="full")
    if users[user] == "home":
        # Put your home reactions here
    elif users[user] == "away":
        # Put your away reactions here
    else:
        # Put your away/but/known reactions here

By this time the user will either be home, in one of their own perimeters or away, and your function can react accordingly.

Your full function might look something like this:

def phone_home(user, latitude=0.0, longitude=0.0):
    if aaimi_perimeter.check_if_in_perimeter(latitude, longitude, aaimi_perimeter.main_user) == "home":
        users[user] = "home"
        # Put your home reactions here
    else:
        users[user] = aaimi_perimeter.check_if_in_perimeter(latitude, longitude, user, scope="full")
            if users[user] == "home":
                # Put your home reactions here
            elif users[user] == "away":
                # Put your away reactions here
            else:
                # Put your away/but/known reactions here

That should be all you need to do with AAIMI GPS Mapper. You can now focus on what your program can do with its newfound location awareness.

Phone app

You can get a user's location when they visit the webpage, but you may want more frequent contact from a user's phone to more-accurately monitor their location.

For this we created a simple Python phone app to say hello to the server at set intervals.

This is not a traditional Android app, it's a Python script that runs in the QPython interpreter app. You can download QPython here.

You may need to adjust some wireless setting on your phone to keep the program working in the background. I had to set the performance setting to full and select not to turn off wireless after periods of inactivity. I won't go into how to do that because it will be different on different Android versions.

Open the phoneapp.py file in the mapper folder. Change the user on line 6 to your username, or the name of the user who will use the phone. On line 35, change "yourIP" to the URL for your web server then save the file.

Move the phoneapp.py file from the aaimi_mapper folder to the QPython/scripts folder on your phone then start the QPython app. Click Programs then select phoneapp.py. You'll be asked for a password for your server

The program will then contact the server to submit the GPS location, then sleep for the default time of two minutes. It will repeat this over and over. Press back on your phone and a popup will appear asking if you wish to keep the program running in the background. Select Yes. The window will close and you'll see an icon for your Python session in the notification bar. The program will run for as long as your phone is on.

That's it for now. Leave a comment and let us know how you go.

Cheers

Anth


Previous: AAIMI GPS Mapper Features

Next:

_____________________________________________


Comments

Leave a comment on this article