I had a spare GPS unit lying in my box of old components, so I decided to put it to good use by installing it into my car together with a Particle Photon. This allows me to track its whereabouts when someone borrows it. Of course this project could be modified to track almost anything.
Although I used a Particle Photon which only connects via wifi instead of 3G, I used it for 2 reasons:
- I had a few spare Photons already.
- I had an old portable 3G router with a bad battery that I have installed permanently into the car providing a mobile hotspot. Many people have old mobile phones lying around which could be re-purposed to do the same, better than leaving them lying in the bottom of a drawer.
The only item I did purchase was a 2000mAh Li ion battery and a Li ion battery charger from Adafruit. This allows me to not only keep it plugged into the cars power socket permanently, but also to unplug it and have a portable unit if I need to track something else temporarily.
The CircuitThe following diagram shows the components connected together in the circuit. I started by connecting the GPS module to the hardware serial port on the Photon via pins:
- 1 = Vin (5V)
- 2 = Gnd
- 3 = Tx (connected to Rx on GPS)
- 4 = Rx (connected to Tx on GPS)
The Photon was mounted on a generic PCB - the Photon was the only component and the PCB was used simply to mount the Photon inside the housing.
The CodeThe online Particle code editor was used to write the code. The following snippet shows the declarations needed:
//Include necessary libraries
#include <TinyGPS++.h> //GPS library
#include <blynk.h> //Access to Blynk app
char auth[] = "xxxxx"; //Blynk app authorization code
TinyGPSPlus gps; //declare gps as a TinyGPS object
//declaration of variables
String msg5;
float spd;
float lat;
float lon;
float sats;
float hdop;
String bearing;
String slocation;
float startTime;
The setup routine is needed to initialize both the hardware serial port of the Photon and the connection to the Blynk server:
void setup()
{
Serial1.begin(4800); //hardware connection to gps
Blynk.begin(auth);
startTime = millis();
}
Finally, code was written to read in the string from the GPS module and send it to the Blynk app as well as the Particle cloud at certain intervals. Location data was updated every 2 seconds whereas the same location was written to the Particle cloud every 1 minute as a string. This string can be pasted directly into Google maps for a location view.
void loop()
{
Blynk.run();
//send the location data to Particle cloud every 1 minute
if (millis() > startTime + 60000) {
startTime = millis();
Particle.publish("GPS1 Location", slocation);
}
// This sketch displays information every time a new sentence is correctly encoded.
while (Serial1.available() > 0)
if (gps.encode(Serial1.read())) {
displayInfo();
delay(2000);
}
if (millis() > 5000 && gps.charsProcessed() < 10) {
msg5 = "No GPS detected";
Blynk.virtualWrite(V5, msg5);
while(true);
}
}
void displayInfo() {
if (gps.location.isValid()) {
lat = gps.location.lat(); //get latitude and longitude
lon = gps.location.lng();
Blynk.virtualWrite(V0, 1, lat, lon, "car"); //write to Blynk map
//create location string which can be used directly in Google maps
slocation = String(lat) + "," + String(lon);
spd = gps.speed.kmph(); //get speed
Blynk.virtualWrite(V1, spd);
bearing = TinyGPSPlus::cardinal(gps.course.value()); //direction
Blynk.virtualWrite(V2, bearing);
sats = gps.satellites.value(); //get number of satellites
Blynk.virtualWrite(V3, sats);
hdop = gps.hdop.value() / 10; //get accuracy of location
Blynk.virtualWrite(V4, hdop);
msg5 = "GPS Good";
Blynk.virtualWrite(V5, msg5);
}
else
{
msg5 = "Invalid Data";
Blynk.virtualWrite(V5, msg5);
}
}
Final TaskThe final task was to create a new project in the Blynk app on my Android phone. I created a new project, named it "GPS-Track" and selected the Particle Photon (WiFi) as the device:
The main component (widget) on the Blynk interface was the map widget. This was created with the connection to virtual pin V0:
The other labels were added and setup as follows:
- Speed label = V1
- Direction label = V2
- Number of Satellites = V3
- Accuracy = V4
The label V5 was used for status messages.
The final system allows easy tracking with an accuracy of around 20m indoors and 12m outside. I'm sure that this could be improved by using a newer more sensitive GPS receiver module.
Images
Comments