Introduction: Particle Electron + GPS + AppEngine
This idea started for my interest of tracking my motorcycle rides, without the hassle of draining my mobile phone battery. I'm one of the backers that supported Particle Electron campaign so I needed to make good use for it, therefore, having a necessity I could tackle it with my Electron.
I'm not going to publish the moto track project yet, but based on the experience of building my first prototype using Electron and App Engine, I would like to share what I found either the good as the bad things.
- External GPS Antenna + SMA to uFL adapter
- Particle Electron
- Asset Tracker
- Android Studio
- Programming and backend knowledge.
All code is published at Github here: https://github.com/fr4gus/electronappengine/
Step 1: Create a Service to Receive the Locations Fixes
We will use App Engine for Java, but you could use any other "flavor" based on your coding experience.
I like to use Android Studio because is based on IntelliJ and it has already plug-ins integrated to work with App Engine. We need to create a new project which will create an Android module that we won't be using for this example.
After defining the project name and package name, keep selecting "Next" until "Finish", this will create your project and also an Android Module (that we will ignore)
Now you can create a new module for your App Engine project.
Once the project is set up, we can code our "backend" service. This can be anything you want. For this example purpose, we will just create a service that will receive a string containing latitude and longitude separated by a comma (I'll explain later why this way). You can make your service in any other technology, only if it is jus a REST API.
At the end, is the way you create the webhook the one that makes this magic work, therefore, it really doesn't matter what your REST API looks and was built. In my case, the easier way to build and deploy in a real server was using Google AppEngine, due to my Java programming skills.
If you are intertested on see how the code looks like, please see my code ate Github here: https://github.com/fr4gus/electronappengine/
Step 2: Make Your Electron Get GPS Fixes
Now that we have were to "store" our location fixes, is time to program what is going to send them.
First step is to include the AssetTracker library. It's important to inform that the library must be included using the IDE. If you copy the include sentence from another code source, it won't work. I hope they (Particle) can fix this.
Next step is just to ask the GPS unit to start up on the setup lines 15,16 (is set off on boot to avoid unnecessary battery consumption) and keep asking for fixes. For example sake, we will set a really short delay (30 seconds), but for a real world solution it should be higher. Keep in mind that any data sent to anywhere, is going to account against your quota.
On the loop method, we will be updating the GPS (line 26) and if there is any available plus the last publish was made 30 seconds ago, we publish again. The method "publishLocation" (line 37) contains all the magic. It basically publishes an "event" called "SL" which is the name of the webhook we will setup on the next step. It receives a JSON data containing the parameters we need to pass to it, in our cases, the location fix from the GPS.
Step 3: Setup the Webhook
Last but not least, the most importan part, the glue!
All you need is to create a JSON like these (more details at https://docs.particle.io/guide/tools-and-features/...
The event that will trigger the webhook will be "SL" that is the one called during Particle.publish on the previous step. The url sets the destination and a placeholder for a variable called fix. This is the variable sent as JSON when publishing the event.
Go to your dashboard at particle (dashboard.particle.io) and in the Integrations menu, select a new WebHook using a Custom JSON. It should look as in the attached image.
We have a be nice policy.
Please be positive and constructive.