DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Stop Running Two Data Systems for One Agent Query
  • Setting Up a Data Catalog With Azure Purview and Collibra: What Three Attempts Taught Me
  • Beyond Partitioning and Z-Order: A Deep Dive into Liquid Clustering for Unity Catalog Managed Tables
  • Why Google Data Migration Gets Stuck at 99%: Causes and Proven Fixes

Trending

  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel
  • LLM Integration in Enterprise Applications: A Practical Guide
  • Self-Hosted Inference Doesn’t Have to Be a Nightmare: How to Use GPUStack
  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  1. DZone
  2. Data Engineering
  3. Data
  4. Create Your Own GPS-Tracker Using LoRa

Create Your Own GPS-Tracker Using LoRa

This post describes how to create your own GPS tracker using Pycom LoPy microcontrollers.

By 
Yves Debeer user avatar
Yves Debeer
·
Oct. 20, 20 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
19.3K Views

Join the DZone community and get the full member experience.

Join For Free

This post describes how to create your own GPS tracker using Pycom LoPy microcontrollers. 

You will also learn how to build your own single-channel LoRa Nano Gateway.

Do you prefer guided details and explanations? There is a video recording available from my technical talk at the IBM Developer Europe Crowdcast channel.

The architecture diagram below shows the different components we will cover in our setup.

A LoPy Node, used as a GPS tracking device, sending data to a LoRa Nano-Gateway.

The gateway itself is connected to 'TheThingsNetwork' via WiFi.

Finally, we'll have Node-Red running in IBM Cloud which will pick up the MQTT messages from TheThingsNetwork, store those into a Cloudant database, and visualize the GPS data points on a map.

Hardware Needed:

  • 1 LoPy as LoRa node + Antenna (868MHz/915MHz) + 3.7V LiPo Battery
  • 1 uBlox NMEA Serial GPS Module with Antenna
  • 1 LoPy as LoRa Nano-Gateway + Antenna (868MHz/915MHz)
  • 2 Pycom Expansion boards

References to the hardware used can be found at https://pycom.io/products/hardware

Software Needed:

In order to program the LoPy microcontrollers we will use Pymakr — a plugin for Atom or Visual Studio Code.

The LoPy from Pycom.io is a multi-network hardware module based on the ESP32 microcontroller. Most of the modules (functions and libraries) are built into MicroPython.

1. Setup a LoRaWAN Nano Gateway and Connect to TheThingsNetwork

The source code for the LoRaWAN Nano-Gateway can be found here.

Unzip the download file and then find the lorawan-nano-gateway folder under examples.

All details for setting up the Nano-Gateway can be found at: 

https://pycom.io/lopy-lorawan-nano-gateway-using-micropython-and-ttn/

This will guide you to defining a gateway on TheThingsNetwork, set the configuration parameters according to your region, uploading the code to the board and verifying it's operational within TheThingsNetwork console.

You can get an account for free at TheThingsNetwork. It serves as a registration point for our gateway as well as an integration point for our data packets we will send from our LoRa GPS Tracker node.

2. Setup a Lora Node With GPS Tracker

Getting started with The Things Network:

https://core-electronics.com.au/tutorials/pycom/getting-started-on-the-things-network-tutorial.html

Follow the instructions on TheThingsNetwork to register and activate your LoPy device. 

https://www.thethingsnetwork.org/docs/devices/lopy/usage.html

There are two ways to activate the LoPy via either OTAA (Over the Air Activation) or ABP (Activation By Personalization). OTAA is the preferred method, certainly if you have a lot of devices, but doesn't seem to be working with the LoRa Nano-Gateway.

The following sample code uses ABP activation and once the LoRa communication is active it will send 10 bytes as 10 separate packets.

C
 




xxxxxxxxxx
1
67


 
1
from network import LoRa
2
import binascii
3
import struct
4

          
5
lora = LoRa(mode=LoRa.LORAWAN)
6

          
7
dev_addr = struct.unpack(">l", binascii.unhexlify('XXXXXXX'))[0]
8
nwk_swkey = binascii.unhexlify('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
9
app_swkey = binascii.unhexlify('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
10

          
11
# remove all the non-default channels
12
for i in range(3, 16):
13
    lora.remove_channel(i)
14

          
15
# set the 3 default channels to the same frequency
16
lora.add_channel(0, frequency=config.LORA_FREQUENCY, dr_min=0, dr_max=5)
17
lora.add_channel(1, frequency=config.LORA_FREQUENCY, dr_min=0, dr_max=5)
18
lora.add_channel(2, frequency=config.LORA_FREQUENCY, dr_min=0, dr_max=5)
19

          
20
# join a network using ABP (Activation By Personalization)
21
lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey))
22

          
23
# create a LoRa socket
24
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
25

          
26
# set the LoRaWAN data rate
27
s.setsockopt(socket.SOL_LORA, socket.SO_DR, config.LORA_NODE_DR)
28

          
29
# make the socket non-blocking
30
s.setblocking(False)
31

          
32
for i in range (10):
33
    pkt = b'PKT #' + bytes([i])
34
    print('Sending:', pkt)
35
    s.send(pkt)
36
    time.sleep(4)
37
    rx, port = s.recvfrom(256)
38
    if rx:
39
        print('Received: {}, on port: {}'.format(rx, port))
36
    time.sleep(4)
40
    time.sleep(6)



Now we have configured a LoRa node we should see the packets coming in via the console of the Nano-Gateway as well as within the console of TheThingsNetwork.

Next, we can add the code for reading the fixed GPS location data.

The sample code can be found here.

In the example, we make use of the 'adafruit_gps' library and we only send a LoRa packet once we have a fixed GPS location.

This is all great, but the data we receive are all bytes! So how do we get this into a usable format? We have to decode it!

Goto TheThingsNetwork-console and select your application. Next, select the tab Payload Formats and define a decoder. Don't forget to save it.

JavaScript
 




x
11
9


 
1
function Decoder(bytes, port) { 
2
  console.log(bytes)
3
  return {
4
      GPSCoordinates: String.fromCharCode.apply(null, bytes)
5
  };
6
}



Now that we have a GPS tracker connected via LoRa, wouldn't it be nice to store this into a database and show the GPS data onto a map?

For this, I chose to work with Node-Red within the IBM Cloud environment.

Easy to install, and completely free using a Lite account. You can signup for an account here.

By default the Node-Red flows are stored within a Cloudant database, so we can easily use that same database to store our GPS data.

You can find the example Node-Red flow here.

The GPS data is gathered from TheThingsNetwork via MQTT and stored into Cloudant.

In order to get the data via MQTT from TheThingsNetwork you will need to create/generate a new access key. Goto TheThingsNetwork-console and select your application. You can find the access keys within the settings tab of your application. The generated key can be found at the bottom of the application overview page.

More details on usage of MQTT with 'TheThingsNetwork' can be found here.

Conclusion

This article should get you started with the basics of IoT using Lora communication.

If you want to see all this in action, there is a video recording available from my technical talk at the IBM Developer Europe Crowdcast channel.

There I also show how to connect the Lora GPS tracker with a public Lora communication provider Proximus in Belgium.

Are you thinking of other use cases for IoT using Lora?

Node-RED Data (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Stop Running Two Data Systems for One Agent Query
  • Setting Up a Data Catalog With Azure Purview and Collibra: What Three Attempts Taught Me
  • Beyond Partitioning and Z-Order: A Deep Dive into Liquid Clustering for Unity Catalog Managed Tables
  • Why Google Data Migration Gets Stuck at 99%: Causes and Proven Fixes

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook