Google Maps beta

Particle and Google Maps can now be used in tandem to easily find the location of Particle devices without the need for any additional hardware. The Geolocation API from Google Maps returns a location and accuracy radius from visible Wi-Fi access points or cell towers collected by Particle devices.

Particle + Azure IoT Hub

Geolocate Particle devices using the Google Maps Geolocation API

Geolocating devices has many potential applications in an IoT deployment, including:

  • Improve operating efficiency via remote tracking of assets in a company's supply chain, including improving demand forecasting and reducing operating expenses. For more details, please check out our whitepaper on supply chain management
  • Delight customers via providing visibility into the location of connected devices to end-users in real-time
  • Discover business insights via collecting and identifying device location data

This tutorial will explore how to get started with the Particle + Google Maps integration.

Geolocation basics

There are four common ways to find a location of a device, described below. It is important to note that while these methods are distinct from one another, it is possible (and often preferrable) to use multiple geolocation methods together to enhance end-user experience and/or increase the likelihood of successfully locating a device.

GPS (Global Positioning System)

The Global Positioning System (GPS) is generally the most accurate and it can provide device location continuously. Under normal conditions, GPS can locate a device with an accuracy of ~4 meters.

While very accurate, the disadvantage is that GPS requires additional hardware like the Particle Asset Tracker module. It may also take some time to establish a satellite fix, and may have trouble establishing a location where the view is obstructed, such as downtown in cities.

The Particle + Google Maps integration does not use GPS, but rather cell towers or Wi-Fi networks to geolocate a device. Read on for a description of these two geolocation methods.

Cellular tower location

Cell tower geolocation is ideal for cellular IoT deployments that require approximate locations and do not wish to include additional hardware to support GPS.

Every cell tower has an identifier, and this can be looked up using the Google Geolocation API to find its location. This process is fast and provides a general location, usually within 4000 meters or a couple miles.

The Google Maps integration can be used in tandem with a Particle Electron to geolocate devices using visible cellular networks.

Wi-Fi location

For the Particle Photon and P1, Wi-Fi geolocation is another option. Google maintains a database of known Wi-Fi base stations and can use this to determine a device's location by what networks it can see.

The database is generated by information from Android smart phones with location services enabled. They periodically record both the visible Wi-Fi access points and GPS locations when location services is enabled. (Apple uses the same technique with iPhones for their Wi-Fi location database, but it's a different database than Google's database.) Not all Wi-Fi access points are included in these databases, but if a device is near one, it can provide accurate location information.

The Google Maps integration also supports this method of geolocation. Particle Photon and P1 (Wi-Fi) devices can collect visible Wi-Fi access points, and send these to the geolocation API in exchange for its location. Often times, Wi-Fi geolocation is more precise than cellular geolocation, often with an accuracy radius of 50 meters or less.

IP Address location

The Google Maps Device Locator does not use this technique, but for home Wi-Fi networks it's often possible to get an approximate location (city or nearby city), based on the public IP address. Particle Wi-Fi devices report their last IP address to the cloud, which could be used for this geolocation method. This is most often the least accurate of the 4 methods described in this section.

Preconfiguration steps

Sign up for a Google account

If you do not already have an account, visit the Google signup page. Follow the instructions to register for a new account. A Google account is needed to successfully authenticate with the Google Maps Geolocation API.

Get a Google Geolocation API Key

In order to use the Google Geolocation API, you need an API key. In many cases, you'll be able to take advantage of the free usage tier.

  • Go to the Google Geolocation API page.

  • If you are not signed into Google already, sign in (1).

  • Use the Get a Key button (2) to request a new API key.

Get a key

  • Select or create a project.

Select or create project

  • In many cases you'll want to Create a new project. Every key is associated with one project, though a project can have a number of different keys, and also be enabled for multiple services.

Create a new project

  • I created a new project location and use the Enable API button to enable it.

Enable API

  • And then you'll be presented with your API key.

API Key

Copy your key to the clipboard because you'll need it when you enable your integration.

To view, edit or delete your credentials later, you can use the Google Developer Credentials Console.

Run the Google Maps firmware library on your devices

The final preconfiguration step is the firmware that you flash to your Particle device. In order for Google Maps to geolocate a device, the device must collect a list of currently visible networks (access points for Wi-Fi, and towers for Cellular) and send information about these networks to the cloud.

A verified Particle library must be added to a device's firmware to collect and send network info to successfully fetch its location from Google Maps.

Using Particle Build (Web IDE)

The quickest way to add the Google Maps firmware library is to start with the sample firmware application in the Web IDE. This sample app will locate the device periodically, every 30 seconds. The Google Maps firmware library is automatically added to your project when using the sample app.

Use Sample Firmware App

This example publishes the location every 30 seconds. It works on the Particle Photon, P1, and Core (Wi-Fi) and also the Electron (cellular).

Navigate to the sample app, then click Copy this App to fork the sample code into a new app associated with your account, as shown below:

Copy app

By changing a few options you can have it publish once at boot, which might be appropriate for Wi-Fi devices that are not battery powered. You can also post location on demand, when you make a call from your code.

For more information on how to customize the library, or integrate the library in an existing firmware application, check out the firmware library API.

Enabling the Integration

Particle Console

Now that you've done all of the pre-configuration, you are now ready to enable the Google Maps integration on the Particle Console.

Start by going to the integrations hub by clicking on the integrations icon in the sidebar (), or by simply following this link. If you'd like to enable the integration for a product, you'll need to visit the integrations hub for the desired product. Do this by clicking the products icon () in the sidebar, finding your product, then clicking on the integrations icon () in the product context.

Once in the integrations hub, click on the "New Integration" button. From the list of available integrations, click on "Google Maps."

You'll see a reminder that setup is required before continuing to enable the integration. If you have followed the steps above, you should be good to go. Click the "I have done all these things" button to advance.

The next step is configuring the integration. Fill out the following fields:

  • API Key: Enter the API key you created above.

  • Device: Select which of your devices will trigger publishing to Google. If you'd like the publish to trigger from any of the devices you own, select 'Any.'

Integration Configuraton

If you expand Advanced Settings the following option appears:

  • Override Default Event Name: The name of the event that will trigger publishing an event to Google Maps. This is the name of your event set using locator.withEventName("differentEventName").

Testing it out

Once you have the firmware installed on your device and the integration enabled, you can check the Logs in the console.

Event Log

From the bottom to the top:

  • The deviceLocator event is what was sent from the Particle device
  • The hookSent/deviceLocator is what the Particle cloud sent to the Google Geolocation service. The data is always undefined; that's the normal behavior.
  • The hook- is the response data: latitude,longitude,uncertainty. The uncertainty radius is in meters.

If the location is not known by the Google geolocation service, a 404 error appear in the logs instead:

Event Log 404

Example: Google Maps Visualization

For most of us, latitudinal and longitudinal coordinates don't give us a clear understanding of where something is located in the world. Seeing those coordinates plotted on a map helps provide that clarity.

Google Maps Visualization

This example demonstrates how to visualize your Particle devices' locations using Node.js in a Google App Engine Flexible Environment and the Google Maps Javascript API. This sample also uses the Express web framework, websockets, and the Particle Javascript API.

Devices that are running the proper firmware and trigger the Google Maps integration will have its returned location displayed with a marker on a map. An radius will also be drawn around the reported coordinates to represent the precision of the reading.

The Google Maps team has created an open-source visualization application, which you can find here:

Check out the repo

The README of the app has all the information you need to both run the application locally, or deploy it to Google Cloud App Engine.

Example: Location OLED Display

OLED display

This example displays your current location on a small SSD1306-compatible OLED display to show your latitude, longitude, and uncertainty radius (in meters).

The connections are:

  • VCC to 3V3 red
  • GND to GND black
  • SCL to D1 (SCL) blue
  • SDA to D0 (SDA) green

Firmware

The firmware to power displaying a device's location on an OLED display can be found in the Google Maps device locator examples repository.

To use this firmware example, you should downlod the repo, either by clone or download zip. Then, cd into the oled-locator directory.

Using the Particle CLI

Using the Particle CLI is an easy to flash this firmware to a device.

For example, if you wanted to flash the oled-locator example to the device you named "test2" then you'd use the commands:

cd oled-locator
particle flash test2 .

That will automatically include the necessary libraries for you.

For an Electron, you'll probably want to do:

cd oled-locator
particle compile electron . --saveTo firmware.bin
particle flash --usb firmware.bin

This will flash the firmware to your electron locally (over USB) to prevent consuming data from over-the-air firmware updates.

Using Particle Dev (Local IDE)

It's also easy to open this example using Particle Dev (Local IDE).

Particle DEV

Makes sure you have a recent version of Particle Dev, as older versions don't know how to automatically load libraries.

Also, you want to make sure you close all open windows, then open the .cpp file for the example you want to use. For example, the oled-locator.cpp file. You want to make sure that only a single project is visible in the left-hand folder navigation.

Using Particle Build (Web IDE)

If you want to use the examples in Particle Build (Web IDE), you need to copy and paste the example source into a new project and then add each of the libraries that it needs.

For example, the oled-locator project has this project.properties.

name=oled-locator
dependencies.google-maps-device-locator=0.0.2
dependencies.Adafruit_SSD1306_RK=1.1.2

You would have to add the libraries google-maps-device-locator and Adafruit_SSD1306_RK to your project in order to run the sample code.

Firmware Library API

Creating an object

You normally create an locator object as a global variable in your program:

GoogleMapsDeviceLocator locator;

Operating modes

There are three modes of operation:

If you want to only publish the location once when the device starts up, use withLocateOnce from your setup function.

locator.withLocateOnce();

To publish every n seconds while connected to the cloud, use withLocatePeriodic. The value is in seconds.

locator.withLocatePeriodic(120);

To manually connect, specify neither option and call publishLocation when you want to publish the location

locator.publishLocation();

The loop

With periodic and locate once modes, you must call:

locator.loop();

from your loop. It doesn't hurt to always call it, even in manual location mode. It gives the library time to process the data.

Customizing the event name

The default event name is deviceLocator. You can change that in setup using:

locator.withEventName("myEventName");

This also must be updated in the integration, since the eventName is what triggers the webhook.

Event Name Configuration

Subscription

You can also have the library tell your firmware code what location was found. Use the withSubscribe option with a callback function.

This goes in setup() for example:

locator.withSubscribe(locationCallback).withLocatePeriodic(120);

The callback function looks like this:

void locationCallback(float lat, float lon, float accuracy)

One possibility is that you could display this information on a small OLED display, for example.

Debugging

The library uses the logging feature of system firmware 0.6.0 or later when building for 0.6.0 or later. Adding this line to the top of your .ino file will enable debugging messages to the serial port.

SerialLogHandler logHandler;