Lots of people around the world are using bicycles as transport vehicles, a hobby or just to stay healthy. Bicycles are not cheap and thieves know that. Bikes are easy to steal and then sell, so these are primary targets in many countries.
If someone steals your bike, your chances to get it back is very low. I wanted to change that by creating an SMS based tracking system. It will give you a pretty good chance to retrieve your bike. It can also be used for tracking other devices like drones, cars or even humans.
HardwareI used a Raspberry Pi, a Hologram Nova and a power bank. That's it. This is a great example that shows the power of Hologram. No external hardware is used in this project, besides the Raspberry Pi.
This is an asset tracking system, so using GPS seems obvious, but I wanted to try something different. In movies the police can catch the bad guys by tracking their phones using the cellular network.
I can do the same thing with Hologram Nova! I can get its coordinates without GPS! I liked the idea and I wanted to test the accuracy of cellular tracking. It also resulted a very compact system that easily fits into this mini box on my bike:
Start your Raspberry. At first you have to install the Hologram Python SDK and then get my lost-and-found.py script from the Lost-and-Found repo. You might need to change it's permissions. The script needs to be executable.
sudo chmod +x lost-and-found.py
Use Cron to start the script right after boot in the background:
sudo crontab -e
Add:
@reboot python3 [path]/lost-and-found.py $
Asset tracking can work in numerous ways. I didn't want continuously stream the bike's position, because that would kill the battery in no time and the communication through Hologram is not free. The Raspberry Pi will only send the coordinates to the Hologram Cloud when it receives a request by the user. Here's how it works:
1.) I send an SMS to the Nova. The message should be either an 's' (reply with SMS) or an 'e' (reply with e-mail). You can do this in two ways:
- You can send an SMS to the Raspberry using Hologram Dashboard . (Dashboard / Devices / {select a device] / via SMS)
- Or if you purchased a phone number for your Hologram SIM then you can send an SMS directly to the Raspberry from a regular phone.
The SMS based control is great because it works with every phone, even with the old ones.
Sending messages from the Dashboard is free. I used about 125KB for about 50 messages. The basic plan provides us 1MB/month. It is unlikely that you will run out of this 1MB.
2.) As soon as the Raspberry receives the SMS, it will send some data back to the Hologram Cloud. At first it will get the longitude and latitude data from the modem:
latitude = hologram.network.location.latitude
longitude = hologram.network.location.longitude
Then it can respond in two ways, depending on the content of the original SMS:
if RESPOND_WITH == 's':
response = 'GPS coordinates of your bike: https://maps.google.com/maps?q='+str(latitude)+','+str(longitude)
topic = 'sms'
else:
response = '{"latitude":"'+str(latitude)+'","longitude":"'+str(longitude)+'"}'
topic = 'email'
As you can see, when the message is 's', it will reply with "GPS coordinates of your bike:" and a Google Maps link as a String or else it will respond with a JSON, containing the longitude and latitude data. I'll talk about topics later.
The Google Maps have a great library for Python. At first I used that but then I moved on to use just a simple Google Maps URL. I wanted to try the JSON parsing method of the Hologram Cloud so it was easier this way to demonstrate how it works. I'll also talk about that later.
hologram.sendMessage(response, topic)
3.) We are back at the Hologram Dashboard. In the Console part you can see the received messages:
4.) This is the part where topics are used. When the topic of the message is "sms" then it triggered the "Coordinates in SMS" route, but when the topic is "email" then the triggered route is "Coordinates in e-mail".
5.) Routes handle the incoming messages.
A route can be triggered by one or more topics. Here I used only one topic for each of them. You need to select an action to execute when the route is triggered:
I used SMS and Email actions. Configuring them is pretty straightforward:
:
The only interesting thing here is the <<decdata.latitude>> and <<decdata.longitude>>. This is how you can insert data from the JSON into the message.
At the SMS configuration you can only give a delivery phone number. If you wish you can also get the phone number from the Raspberry, using JSON. At this point you can't edit the text of the SMS. This is the reason of the differently structured messages of "sms" and "email" topics.
AccuracyLets continue with the results. I liked this idea but my expectations were quite low from the beginning. I mean if it would be accurate then we wouldn't need GPS on land, right?
I went to walk to collect some data. Here are some of the results. My actual position is blue and the received position is red:
The difference can be more than 100m. I have to say that it is not that bad. Obviously not as accurate as GPS, but it can be usable. However here are some strange results:
These are 3 different measurements with the same results from 3 different real positions. That's strange. It occurred quite often, with different places, too.
After examining this place, I saw a cell phone station on the top of that building. I guess all the received coordinates are indicating the position of the cell tower that the Nova is connected to at that moment.
5 days laterAfter seeing the results I was sure that I also need proper GPS capability. The cellular positioning is useful but it is not enough if I want to protect my bike.
I used an Ublox Neo 7M GPS module. The Raspberry is communicating with it directly though UART so I had to enable it, first.
This is a one-way communication. The GPS module will stream the data to the Raspi. It means that we only need to connect the Raspberry's RX pin with the Neo 7M's TX pin besides the power lines. The Neo 7M modules need 3.3V to work but mine has an onboard voltage regulator so I fed it with 5V.
Most GPS modules are using NMEA sentences to communicate and mine is not an exception. You can find more information about it here. I'll be using pynmea2 to parse this data into a usable format. Use pip to install pynmea2:
sudo pip install pynmea2
We only need GPGGA sentences from the received data. The others are irrelevant.
x=ser.readline()
if x[:6] == "$GPGGA":
position = pynmea2.parse(x)
if position.longitude != 0.0 and position.latitude != 0.0:
gpsLatitude = position.latitude
gpsLongitude = position.longitude
The pynmea2.parse(msg) part is doing the hard work here. It is parsing the GPGGA sentences into usable positions. After that we get standard coordinates and we can use them directly in Google Maps.
I want to keep the cellular positioning, too, so I am using a second character in the request SMS, indicating the desired response data. The first character is still 's' or 'e'. The second characters can be:
- 'g' --> The requested data is the GPS position.
- 'c' --> The requested data is the cellular position
So for example the response for the 'sg' request is the GPS position in SMS.
if RESPOND_WITH[1] == 'g' and gpsLatitude != 0.0:
latitude, longitude = gpsLatitude, gpsLongitude
origin = 'GPS'
else:
latitude, longitude = getCoordinates()
origin = 'Cellular'
The "gpsLatitude != 0.0" is used to detect if the GPS had a fix since the boot. If not then the program will respond with cellular position, even if GPS position is requested. The "origin" string is used to indicate the origin of the position data to the user in the response. To use origin in e-mail, you have to modify the subject in its route's action:
Another cool feature is keeping the last GPS position in the memory. It can be very useful in cases when the thief puts the bike in a place where there's no proper GPS signal, but there is cellular reception. For examle into his cellar. The last position will be just outside his house. If the Raspberry ever had a fix GPS position since booting but there's no GPS signal at the moment, then it will send the last known position when GPS position is requested.
Everything is ready, everything is working perfectly, the code is robust, so let's conclude this project!
ConclusionHowever we don't get the exact position of the Nova by itself, the closest cell phone tower to our stolen or lost object is still not bad. Cellular positioning can also be handy when you can't get a GPS reception from the beginning. A GPS tracker is worth nothing if it is in a building without signal. However outside of the city, a cellular only tracker would be less accurate due to the lower number of cell towers.
You usually lose GPS reception a lot sooner than you lose cellular reception in a building. Simply knowing the cell phone towers' position in your area can also be handy in some cases (e.g. to find the best mobil internet reception in the building). Combining the two can really help you to recover your stolen assets!
I really like that the Hologram Nova can do so much without any extra sensors. The Hologram Python SDK was also a huge help for me. No low level coding is required, only calling simple functions to use complex features. I feel that I'll use Hologram's services in lots of projects in the future!
You are almost finished reading this project. Here's a well deserved puppy for you:
I hope you like my project! Thank you for reading and have a nice day! :)
Comments