AWS Public Sector Blog

Creating a serverless GPS monitoring and alerting solution

To help solve a few challenges I faced with my family, including the need to track the location of a child, I needed a serverless global positioning system (GPS) monitoring solution. Commercial geographic monitoring GPS solutions are not cost-effective because of the cost of digital map licenses from third parties and running servers around the clock. Existing GPS systems work with proprietary GPS devices that lock-in the users’ vendors’ devices. Alerts and geofencing are normally limited to static geofencing, but I needed to track the distance between two moving points – like two people moving at the same time.

My solution? Build my own serverless GPS monitoring and alerting solution. The system I built can ingest GPS location information from GPS-enabled devices and display their position on a digital map, which uses open data to visualize the position of each device and provides geofencing alerts. Given the serverless approach, GPS servers do not need to run around the clock. Instead, the core of the solution uses Amazon API Gateway and AWS Lambda.

Architecture of the project

Figure 1: Architecture of the project

How to build it

First, ingest and decode the location data coming from the GPS devices via HTTP. Devices capable of posting their location data using HTTP are compatible with the system, including GPS devices with GPRS/3G capabilities, Internet of Things (IoT) devices with GPS modules, or any mobile applications capable of using a mobile phones’ GPS module to post the device’s location.

The devices publish their locations to the HTTP endpoint provided by Amazon API Gateway, which receives the HTTP requests from GPS devices containing location data. Amazon API Gateway invokes an AWS Lambda function to deal with the data and extract the coordinates, storing them as longitude and latitude degrees into an Amazon DynamoDB table.

Next, consume the location information in real time through a digital map view. Use a simple web application, written in JavaScript and hosted on Amazon Simple Storage Service (Amazon S3). The map visualization uses Mapbox GL Javascript. The API is based on the open data OpenStreetMap dataset.

Finally, compute the distance between different devices’ locations and send an alert call through Amazon Connect – even if points are moving.

The Amazon DynamoDB table to store the location data is essential. I chose to use the deviceID (serial, imei or deviceid depending on the device) as the primary key, then the longitude and latitude as the attributes. You may choose to include additional attributes.

Amazon DynamoDB table

Figure 2: Amazon DynamoDB table

To provide the app with permissions to read from the table, create an Amazon Cognito identity pool. When deploying this solution, you need to authorize your users via username and password. You can do this using Amazon Cognito. Use Amazon DynamoDB fine-grained access control, so each authorized user is able to only read their own data.

Lastly, create a MapBox’s API key and compile the sources. Create your API key – we’ll need it to display the maps.

There are multiple GPS applications that can be used to post the phone’s GPS location, like GPS Logger. To configure the application, provide the HTTP endpoint for it to post its location and the query parameters.

GPS Logger

Figure 3: GPS Logger

To obtain the HTTP endpoint, we configure and deploy an API in Amazon API Gateway. In figure 4 below, I deployed an API named “GPSData” with a resource called “log” under GET method to ingest the data.

Note, the API is now public, but in a production environment, so we should create the API as private with one of the multiple methods available in Amazon API Gateway.

Amazon API Gateway configuration

Figure 4: Amazon API Gateway configuration

We will use Amazon API Gateway with AWS Lambda Proxy integration to handle the HTTP parameters in our AWS Lambda function. See figure 5 for an example of the API being integrated with “GPSIngest.”

Method Execution Configuration

Figure 5: Method Execution Configuration

The “GPSIngest” AWS Lambda function will extract location information and will store it in the database. Check out an example of “GPSIngest” code in Python 3.6.

GPS devices normally use the NMEA standard with GPRMC sentences embedded into the HTTP messages to send their GPS location.

GPS stand-alone device GPRS 3G

Figure 6: GPS stand-alone device GPRS 3G

In NMEA format, each piece of information sent – called a sentence – is self-contained. For example:

““

query=imei=357xxxxxxxx12&rmc=$GPRMC,002347.00,V,4032.35096,N,00354.04761,W,0.023,,210818,,,A*69,AUTO,3661,45,9,0,99.9,C0,0,0,0,M1

““

The parameter “rmc” contains the NMEA sentence (full decoding information available here). In the “rmc” parameter, the device location, longitude, and latitude are the 3-6th comma separated values. To ingest location information from these devices, you will create an API via Amazon API Gateway and integrate it with an AWS Lambda function. See below for a sample code for this function in Python 3.6 that will decode the information and store them in degrees format.

For IoT devices, expect NMEA GPRMC sentences. In other cases, it can be formatted in longitude and latitude degrees.

You have already created your Mapbox GL Javascript API key. The application using Mapbox GL will be a simple html document that includes a JS frame where the map will be displayed.

Map visualization of two points with Mapbox JS GL API

Figure 7: Map visualization of two points with Mapbox JS GL API

Serve this html file from Amazon S3. See below for an example of code of the html map. You may choose to create a CNAME in your DNS to the URL provided by Amazon S3, if you want to use your domain.

To have portable geofences and track the distance between points (even if those are moving), we use the Haversine formula (the AWS Lambda function code suggested in the previous section already includes the Haversine formula implementation).

When the distance computed in the AWS Lambda function is bigger than the threshold set, the Amazon Connect API starts outbound voice contact to initiate an outgoing call to the number specified. We need to have an instance of Amazon Connect running and a contact flow.

Amazon Connect Contact Flow

Figure 8: Amazon Connect Contact Flow

By building a traditional GPS monitoring and alerting environment under the serverless compute model, we can help in a number of use cases such as in healthcare to monitor Alzheimer patients and alert if they go far from home or in the logistics industry to track fleets and locations. This solution can be cost effective and scalable.

Read some of my other blog posts on the AWS Public Sector Blog, including, “Grandma Emergency Button – A simple emergency alert solution with AWS IoT Button,” “Using a serverless architecture to collect and prioritize citizen feedback,” and “Develop and extract value from open data.”

And register to come to my Chalk Talk “Building a serverless GIS/geo-positioning and alerting solution” (WPS307) on Monday, 2 December at re:Invent 2019.

Subscribe to the AWS Public Sector Blog newsletter to get the latest in AWS tools, solutions, and innovations from the public sector delivered to your inbox, or contact us.