Particle Electron + GPS + AppEngine
by fr4gus in Circuits > Gadgets
3029 Views, 16 Favorites, 0 Comments
Particle Electron + GPS + AppEngine
![DSC_9951.jpg](/proxy/?url=https://content.instructables.com/FZE/OCM8/INUV0M1T/FZEOCM8INUV0M1T.jpg&filename=DSC_9951.jpg)
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.
Requirements:
- 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/
Create a Service to Receive the Locations Fixes
![Screen Shot 2016-05-04 at 11.26.00 AM.png](/proxy/?url=https://content.instructables.com/F5F/W0D7/INS15YTD/F5FW0D7INS15YTD.png&filename=Screen Shot 2016-05-04 at 11.26.00 AM.png)
![Screen Shot 2016-05-04 at 11.27.04 AM.png](/proxy/?url=https://content.instructables.com/FE0/E44P/INS15YTF/FE0E44PINS15YTF.png&filename=Screen Shot 2016-05-04 at 11.27.04 AM.png)
![Screen Shot 2016-05-04 at 11.33.38 AM.png](/proxy/?url=https://content.instructables.com/F43/GSAO/INS15YX0/F43GSAOINS15YX0.png&filename=Screen Shot 2016-05-04 at 11.33.38 AM.png)
![Screen Shot 2016-05-04 at 11.35.07 AM.png](/proxy/?url=https://content.instructables.com/FOB/ZBLH/INS15ZHQ/FOBZBLHINS15ZHQ.png&filename=Screen Shot 2016-05-04 at 11.35.07 AM.png)
![Screen Shot 2016-05-04 at 11.35.37 AM.png](/proxy/?url=https://content.instructables.com/FY2/2KSU/INS15ZI5/FY22KSUINS15ZI5.png&filename=Screen Shot 2016-05-04 at 11.35.37 AM.png)
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/
Make Your Electron Get GPS Fixes
![Screen Shot 2016-05-05 at 3.09.37 PM.png](/proxy/?url=https://content.instructables.com/F2H/84U9/INULTTAC/F2H84U9INULTTAC.png&filename=Screen Shot 2016-05-05 at 3.09.37 PM.png)
![Screen Shot 2016-05-05 at 4.03.26 PM.png](/proxy/?url=https://content.instructables.com/F86/TK3B/INULTTAF/F86TK3BINULTTAF.png&filename=Screen Shot 2016-05-05 at 4.03.26 PM.png)
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.
Setup the Webhook
![Screen Shot 2016-05-05 at 4.20.58 PM.png](/proxy/?url=https://content.instructables.com/FJJ/HYFD/INULTTQA/FJJHYFDINULTTQA.png&filename=Screen Shot 2016-05-05 at 4.20.58 PM.png)
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.