# Tutorial on how to build a simple location tracker by **Julija Rukanskaite, jr223ha** ## Project overview This tutorial describes how to make a simple location tracking IoT device and visualize the location of a thing or a person on a map using Pycom harware and software together with the Ubidots platform. :clock1: **Estimated project lenght**: max ~7 hours, depending on how familiar you are with the software used. ## Objective I have chosen to build this device to familiarize myself with location tracking in IoT: the sensors, how data is read, and how it's turned into a visualization on the map. The purpose of this device is to act as a single-handled location sensor device, to be attached to moving things or to be carried around, to send location data to the cloud. It can be used for utilitarian or playful purposes, like drawing on the map or simply looking for patterns in how you yourself or something moves; patterns not easily noticeable without this data visualization. ## Material #### Lopy 4 ![](https://pycom.io/wp-content/uploads/2018/08/lopySide-1.png) ###### Image: Pycom A micropython-programmable micro-processor with embedded Bluetooth, LoRa, and WiFi connectivity. Read more about Lopy4 [here](https://pycom.io/product/lopy4/). Price: **43,31 €** #### Pytrack V1.1 ![](https://cdn-blog.adafruit.com/uploads/2017/06/3506_iso_ORIG.jpg) ###### Image: Adafruit industries A Pycom expansion board with several embedded sensors, including a GPS module. Seems to be out of production on Pycom's site, but still available from some electronics distributors. Can be replaced with the newest version, [pytrack V2.2](https://pycom.io/product/pytrack-2-0-x/) Price: **45,78 €** #### USB cable A simple, data-transfering USB cable for connecting the hardware to a the computer. I already had one lying around. ***** Both Lopy and Pytrack were purchased from a Lithuania-based electronics distributor [Lemona electronics](https://www.lemona.lt/). ## Computer setup instructions The device is programmed via the Pycom plugin on the Atom IDE. Here is a step-by-step guide to seting things up. ### 1. Flashing the firmware Before you connect your device to the computer, check for updates on firmware on both Lopy and Pytrack. The Lopy firmware update is done through the Pycom upgrade program; the tutorial can be found [here](https://docs.pycom.io/updatefirmware/device/).The Pytrack firmaware update tutorial can be found [here](https://docs.pycom.io/updatefirmware/expansionboard/). ### 2. Atom setup To write code, you need a development environment. Here I am using Atom. To set it up, go to [atom.io](https://atom.io/) , download a version for your computer, install, and run it. ### 3. Installing the Pycom plugin To upload code to your device, you need to use the Pymaker plugin. Dowload it [here](https://atom.io/packages/pymakr). Once installed, the plugin will open the command line interface at the bottom of the Atom ID. ## Wiring To put your device together, place the Lopy pins-down in the Pytrack expansion board, so that the Lopy LED is facing the same way as the Pytrack USB port, and connect them. No need for any additional wiring. The power is supplied through the USB cable connected to your computer. This same straighforward wiring of the Lopy and Pytrack can also be used in production. However, you'd need to connect a batery instead of leaving it attached to a pc. ## Ubidots connection To visualize data, I have chosen the [Ubidots](https://ubidots.com/) platform. Ubidots is a cloud-based IoT platform that can be easily connected to a Pycom device via WiFi, Bluetooth, Lte, LoRa, and Sigfox. In this tutorial I am using a WiFi connection. While there are several cloud platforms available for this use case, I have chosen Ubidots because of their dashboard's map widget. The widget allows to easily display location data from the device and to customize it without any coding. Moreover, you can use simple CSS to style the dashboard, which makes it a good option if you want to get creative. A free Ubidots plan is enough for starting the project. However, if the project scales and, for example, more data per day needs to be received, that would require a paid subscription. ## Code ### File structure Once you have Atom running, click *open project folder* on the upper left corner of the interface and create/select an empty folder for your project in the file explorer. Once you have the main folder, create a lib folder in it, and create new empty files in both. Your folder structure and the files names should look like this: ![](https://i.imgur.com/ySBWOmG.png) ### Establishing a Ubidots conection To establish a connection to Ubidots, follow [this tutorial](https://help.ubidots.com/en/articles/961994-connect-any-pycom-board-to-ubidots-using-wi-fi-over-http). It guides you through the process and provides example code for the main, boot and urequests files. Paste the code to the files in your folder with the same names. Make sure to replace the dummy text with your Wi-Fi credentials. Insert your Ubidots Token, as shown in the Ubidots tutorial. Once the connection is established, your device should appear on your Ubidots account under "Devices". Although the code in main.py will change, it is important to run this tutorial before trying something else. This way you can be sure the connection to Ubidots is working. ### Reading location data with L76GNSV4 and Pycoproc_1 The main.py code provided by Ubidots does not read the pytrack sensor values. That's why a GPS sensor library is needed. I used the [L76GNSV4 location module](https://github.com/andrethemac/L76GLNSV4) and the [pycoproc module for pytrack](https://github.com/pycom/pycom-libraries/blob/master/shields/lib/pycoproc_1.py) to read them. To use these modules, simply copy both file codes from the libraries' linked and paste them into the empty files of the same names in Atom. Save the files and import them to the main.py file under other modules: ```javascript=20 from L76GNSV4 import L76GNSS from pycoproc_1 import Pycoproc ``` Then, in the same file, before the JSON build function , create a L76GNSS object. ```javascript=41 py = Pycoproc(Pycoproc.PYTRACK) GPS = L76GNSS(py, timeout = 20) ``` Then edit the JSON build function provided by Ubidots to send only two variables with one value each. ```javascript=44 # Builds the json to send the request def build_json(variable1, value1, variable2, value2): try: data = {variable1: {"value": value1}, variable2: {"value": value2}} return data except: return None ``` Adjust the request sending function to only send one value - location. ```javascript=53 # Sends the request. Please reference the REST API reference https://ubidots.com/docs/api/ def post_var(device, value): try: url = "https://industrial.api.ubidots.com/" url = url + "api/v1.6/devices/" + device headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"} data = build_json("location", value) if data is not None: print(data) req = requests.post(url=url, headers=headers, json=data) return req.json() else: pass except: pass ``` Finally, edit the function call. Remove the unused variables and replace them with the location value (running the .coordinates() method on the GPS object returns a dictionary with latitude and longitude). ```javascript=68 while True: location = GPS.coordinates(debug = False) # (lng, lat) post_var("pycom", location) time.sleep(DELAY) ``` ## Transmitting data As shown above, the data is transmitted to the Ubidots API via WiFi through the HTTP protocol. The data request is sent in a JSON format once every second. Each data sending request is approved by checking the user's Ubidots token. ## Presenting the data To visualize the data, open Ubidots, click "Data" and then "Dashboards". Add a new map widget: ![](https://i.imgur.com/o3FdQYE.png) Select your device and customize your map widget as you wish. Here is the mine, in satelite view with a blue location pin. ![](https://i.imgur.com/E4G091y.png) ## Reflection ![](https://i.imgur.com/Je51ojG.png) ![](https://i.imgur.com/VWSfaN0.jpg) ###### The final result of the project Because I struggled a lot with accessing and transmitting the location data, I did not have time to connect my device to a batery. However, if I continued this project that would be the first thing I'd do. Furthermore, although preferred for fast data transmission, WiFi might not be best for location tracking. The device might not stay close enough to the WiFi source to continuously send data. That is why alternatives, such as LoRa, would be important to try. Finally, however the device is connected, I would still want to have the data saved to an SD card, so, once connection, WiFi or other, is lost, the data is still being collected. When I began this project I wanted to be able to draw the movement of the device on the map. However, not being able to get away from the WiFi source or the power from my laptop's USB port meant the movement was too suttle to be shown on the map. Hopefully, with the improvements described here I can make it work in the future.