# IoT GPS Tracker ### Leandro Dinis Ferreira - ld222hp By following this tutorial you will end up building a Tracker device that sends location data, temperature and accelaration data to a easy to understand dashboard hosted on the cloud. ***It should take about 4 hours to put everything together*** ## Why My friend has a boat and we've talked about how he could find out in a timely matter if his boat was stollen and what he could do to retrieve it. I thought that building a tracking device that could transmit data reliably and that's out of sight would make a good project and solve his problem at the same time. ## How | Part | Function | Where To Get| Price| | -------- | -------- | -------- | -------- | | LoPy4 | The brains of the operation. |[Click](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/)| 949.00 SEK |Expansion Board 3.X| Brings the hardware together|[Click](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/)|(Comes included in LoPy4 kit) |MCP9700 (Temperatur Sensor)| Measures temperatur| [Click](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/)|(Comes included in LoPy4 kit) |Wires|Connect everything together|[Click](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) |(Comes included in LoPy4 kit) | Adafruit Ultimate GPS Breakout (MTK3339) | GPS Module with a breakout board for ease of use|[Click](https://www.elfa.se/en/mtk3339-gps-breakout-adafruit-746/p/30091220?q=adafruit+gps&pos=1&origPos=1&origPageSize=50&track=true)| 403,92 SEK | AXDL345 | Accelerometer to measure if the device is moving |[Click](https://www.elfa.se/sv/adxl345-med-tre-axlar-accelerometer-5v-adafruit-1231/p/30139029) | 167,00SEK |Generic LiPo Battery (1200mAh)| Battery to power the device without having to plug it in| [Click](https://www.aliexpress.com/item/32535344810.html) | $7.58 US |Active GPS Antenna| (Optional) Decreases the amount of time to get a fix| [Click](https://www.kjell.com/se/produkter/mobilt/gps/gps-tillbehor/navilock-gps-antenn-p95369) | 249 SEK |Generic Box big enough for the device|A water proof box that will hold the device|[Click](https://www.kjell.com/se/produkter/el-verktyg/elektronik/elektroniklador/inbyggnadslada-120x70x50-mm-p89028) | 59,90 SEK |Space Sleeves|Neat way to mount the experiment card and LoPy4 together|[Click](https://www.kjell.com/se/produkter/dator/datorkomponenter/montering/monteringsskruvar-for-pc-50-st-p98837) | 79,90 SEK |Experiment cards|Experiment card to mount the modules and battery|[Click](https://www.kjell.com/se/produkter/el-verktyg/elektronik/monsterkort/experimentkort/luxorparts-experimentkort-40x60-mm-10-pack-p90662) |89,90 SEK |CR1220 Battery|Backup battery for the GPS that saves the current time|[Click](https://www.kjell.com/se/produkter/el-verktyg/batterier/litiumbatterier/kjell-company-litiumbatteri-cr1220-p32230) |39,90 SEK ## Computer setup ### The brain (LoPy 4 & Expansion Board) Start by attaching the LoPy 4 to the expansion board like so: ![](https://i.imgur.com/xIaNufF.png) **The white RGB-Led should be on the same side as the white (battery) connector** Once the LoPy4 is attached to the expansion board you should attach a micro usb cable between your computer and the LoPy4. When this is done you should headover to [pybytes.pycom.io/](pybytes.pycom.io/), press the Sign-up link and create an account. After creating an account and logging in you can press the following [link](https://docs.pycom.io/pybytes/gettingstarted/) and follow the steps in that page in order to update the firmware on your device and connect it to pybytes (The pybytes IoT platform won't be used at all in the final product but it's nice to add a connection to your home Wifi if you want update it wirelessly in the future). #### Software Headover to [https://code.visualstudio.com/download](https://code.visualstudio.com/download) and download VSCode for your platform. After installing vscode. Next hold **ctrl+shift+x**(on windows) to open the extensions page in VScode, search and install the Pymakr extension. Installing the Pymakr extension adds a REPL shell to VScode that allows you to upload/download code between your computer and the LoPy4. Upon installing the Pymakr extension it will prompt you to install Node.js do so as it's required for pymakr to work. To upload the code to the device you should create a new empty folder in which you should place these files (from the github repository linked below): adxl345.py, main.py, micropyGPS.py, statistics.py. Afterwich you should open the folder via VSCode. When VSCode is open at that folder you can highlight all of the files, right-click and press "Pymakr > Upload project". After you uploaded the code to your LoPy4 you should headover to [thingsboard.cloud/home](https://thingsboard.cloud/home) and copy paste the code in data-decoder.js your Data Converter(which you created by following the guide linked above) Follow these guides to setup Sigfox and Thingsboard: [Sigfox](https://docs.pycom.io/pybytes/networks/sigfox/) [Thingsboard](https://thingsboard.io/docs/user-guide/integrations/sigfox/) ## Putting everything together #### Experiment card Step 1: Cut 2 PIN Headers to size (9 female connections) and place them on an experimentation card with the lipo battery between them like so (to know where to solder them): ![](https://i.imgur.com/DGDSKQf.jpg) After removing the battery you should now have the final position of the headers like so: ![](https://i.imgur.com/TcOvZ2h.jpg) Step 2: Solder the headers to the experiment card like so: (ignore the connected wires) ![](https://i.imgur.com/KtxRN3l.jpg) Step 3: Solder 2 Pins to the battery like so: ![](https://i.imgur.com/Mg7zrg8.jpg) ##### Adafruit GPS Ultimate Breakout & ADXL345 Step 1: Solder the pins to the 2 modules like so (My male pin headers came with the sensors/modules bought from elfa): ![](https://i.imgur.com/V2yDwbo.jpg) Step 2(Optional): The GPS module comes with a spot to hold a tiny battery. This battery is used internally in the module to hold the current time. The module always knowing the current time reduces the amount of time required to get a fix greatly. ***This step is optinal but highly recommended:*** ![](https://i.imgur.com/8Il4tw1.jpg) ##### MCP9700 Solder three wires to the three wires from the sensor like so: ![](https://i.imgur.com/5u4K9wV.jpg) I used electrical tape to wrap the wires ##### Box Make to holes in the side of the box like so and mount the Active GPS Antenna and LoraAntenna(should come provided if you bought the kit from electrokit). In the end you should have something like this. ![](https://i.imgur.com/ppZToyq.jpg) ### General Setup After following the steps above you should now have the LoPy4/Expansion Board and the Experiment Card ready to build. Step 1: Mount the Experiment card onto the expansion board using the spacer sleeves like so: ![](https://i.imgur.com/U0sYgna.jpg) You can follow this circut diagram and solder the wires. The wires #### Soldering / Circuit Diagram You can now follow this circuit diagram to solder all the wires ***on the bottom.*** ![](https://i.imgur.com/eiGbbZ4.jpg) In the end you should have something that looks like this: ![](https://i.imgur.com/Vd14f3c.jpg) Once everything is connected by wires you can mount the modules, sensors and battery like so: ![](https://i.imgur.com/1wIHUD0.jpg) ## Platform I chose thingsboard.cloud because it offers the ability to write a custom decoder for the Sigfox payload which in a way can "combine" the 3 different packets of data into the dashboard. I signed up using the free trial for 1 month but I would definetly pay for the subscription. Ubidots also offered the same features but I would have had to pay for a subscription from the get go. Linked here is a list of features that Thingboard provides: [Click](https://thingsboard.io/docs/#features) ## The Code All of the code used for this solution can be found here: [https://github.com/Mokas/IoT-Tracker](https://github.com/Mokas/IoT-Tracker) Adafruit much similarly to pycom also provides python libraries to all of it's sensors/modules/devices, the catch being that the way in which the libraries communicate with the hardware is different. This meant that I had to modify some of libraries to get it to work with the LoPy4. Here are the most important parts that were modified: #### Original ADXL345 Library ``` def _read_register(self, register, length): self._buffer[0] = register & 0xFF with self._i2c as i2c: i2c.write(self._buffer, start=0, end=1) i2c.readinto(self._buffer, start=0, end=length) return self._buffer[0:length] def _write_register_byte(self, register, value): self._buffer[0] = register & 0xFF self._buffer[1] = value & 0xFF with self._i2c as i2c: i2c.write(self._buffer, start=0, end=2) ``` This taken from the official Circuit python driver for the AXDL354 Module that could be found [here](https://github.com/adafruit/Adafruit_CircuitPython_ADXL34x/blob/main/adafruit_adxl34x.py). #### Modified ADXL345 Library ``` def _read_register(self, register, length): self.buffer = self.i2c.readfrom_mem(self.address, (register & 0xFF), length) return self.buffer[0:length] def _write_register_byte(self, register, value): self.i2c.writeto_mem(self.address, (register & 0xFF), (value & 0xFF)) ``` #### Other Libraries that were used I used a library for parsing NMEA GPS messages into a human readable format. This library could be found [here](https://github.com/inmcm/micropyGPS) There is also usage of a low memory statistics library that's tailor made for micropython devices. This library could be found [here](https://github.com/rcolistete/MicroPython_Statistics/blob/master/statistics.py) This library is used to get the mean average voltage of the temperature sensor and the battery voltage. #### Intresting Decode Functions in Thingsboard to decode Sigfox payload ``` //Turns a hex string to a byte array function hexToBytes(hex){ for(var bytes = [], c = 0; c < hex.length; c += 2) bytes.push(parseInt(hex.substr(c,2), 16)); return bytes } //Turns a byte array into a float function bytesToFloat(byteArray){ var buf = new ArrayBuffer(4); // Create a data view of it var view = new DataView(buf); // set bytes byteArray.forEach(function (b, i) { view.setUint8(i, b); }); // Read the bits as a float; note that by doing this, we're implicitly // converting it from a 32-bit float into JavaScript's native 64-bit double return view.getFloat32(0); } //Turns a Byte Array to an int function byteArrayToInt(byteArray) { var value = 0; for (var i = byteArray.length - 1; i >= 0; i--) { value = (value * 256) + byteArray[i]; } return value; } ``` ## Transmitting the data / connectivity I chose Sigfox as the means to transfer the data to the thingsboard platform. The main reason for this is because sigfox has really good coverage of the ocean outside of Gothenburg as could be seen here: ![](https://i.imgur.com/BHghFJp.png) The way that everything is set up is as following: LoPy4 -> (Via sigfox) Sigfox Network -> (Via Http Callback) Thingsboard A caviat of using Sigfox is that there is a 140 packet/day limit and a 12 Byte size limit/packet this meant that I had to be creative with how I sent the data and how I decoded it. Here is a visual representation of the packets that are send via Sigfox: #### Packet Structure | id | Main data | Total | -------- | -------- |-------- | |0x01 (1 Byte)|Latitude (4 bytes) + longitude(4 bytes) + speed (2 bytes)|11 Bytes| |0x02 (1 Byte)|X-Axis Accel (4 Bytes) + Y-axis Accel (4 Bytes) + Temperature (2 Bytes)| 11 Bytes |0x03 (1 Byte)|Z-Axis (4 Bytes) + Altitude (4 Bytes) + Battery Voltage (2 bytes)| 11 Bytes ***There are 3 packets being transmitted once an hour.*** ## Presenting the data #### Dashboard results The data is being kept for 30 days in thingsboard. But this being a GPS tracker I chose only to visualise the "latest" data. ![](https://i.imgur.com/WnYRhjq.png) By choosing different widgets you can also show graphs and even the GPS route for the day and so on. ## Finalizing the design #### Hardware results ![](https://i.imgur.com/NR6PQKe.jpg) The Adafruit GPS module has an ENable pin that could be used to turn the module "off" whilst the LoPy4 is in deepsleep, meaning that making use of this feature could increase the operating time of the tracker since the gps uses 20mAh during navitation. The reason I chose not to make use of this feature is simply down to the fact that the module usually takes an impractical amount of time for the gps module to get a fix (even with the backup time battery). Kjell didn't specify the inner dimensions of the electronics box that I bought meaning that the device doesn't fit flat in the fox. If I had a 3D-Printer I could have just printed my own box with the perfect dimensions. Since Corona is still going on I didn't have the chance to put install the device on my friend's boat and take pictures. It would have been cool seeing it used on where it was meant to be used, but since it's just a GPS tracker I just took it out for a spin here in Gothenburg to get some location data (that's not my house xD) I wish I LoRa had as much coverage here in Gothenburg as Sigfox since I like free and community driven services. It would also have allowed me to transmit even more accurate/real-time data. But sigfox has good coverage in the ocean beside Gothenburg making it the ideal communication platform, despite the size/rate limits.