Run your LoRaWAN gateway with your own The Things Stack network server on a Raspberry Pi and balena
Tags: balena, lorawan, thethingsnetwork, raspberrypi
Published: 2021-08-27 14:57:01

Running this project you can have all the LoRaWAN stack (Basics Station gateway + The Things Stack network server) running together on a single Raspberry Pi, LoRa concentrator and balena. Using containers on balena means that it can run alongside with other services such as the balena LoRaWAN BasicsTM Station gateway and others.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efth4rdpw296627ykrc4.png)

## What is LoRa and LoRaWAN

I love the concept of the Low-Power Wide Area Networks (LPWAN). LPWAN enable to set up tiny sensors running with batteries far away from the base stations and still send data during a lot of time. 

LoRa is a radio modulation meant for Long Range communication using very little power to send data.

LoRaWAN is a Media Acces Layer (MAC) protocol. A software layer that defines how devices use LoRa to transmit and receive messages on a network.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cechj4uxy2yqar4u20vc.png)

LoRaWAN (LoRa Wide Area Network) is an example of LPWAN. Using LoRa (Long Range) radio-frequencies, you can send tiny packages of data over long distances (even miles away depending on the topology) with simple battery-powered devices. The radio packets are captured by gateways which relay over the Internet the LoRa packages to network servers.
The Things Stack is an example of a LoRaWAN network server. The Things Stack (previously The Things Network) is being run on the The Things Industries clusters that are distributed worldwide. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5td5s00rx50llmqxgel.png)

## Why is this Running on a Raspberry Pi instead of a cloud?

If you want to manage your own data without sharing it with any cloud provider, the data collected can’t leave your country, or other similar requirements, then the solution is to deploy The Things Stack LoRaWAN Network Server using balena on a Raspberry Pi.

If you are a professional that needs to build your ad-hoc LoRaWAN private network for tests or others, this is a great and unexpensive solution. And if you are a community or company who need a tiny private LoRaWAN network server, this could be just enough.

Some people says that a Raspberry Pi is to small for this type of job, but it has been running for weeks with good performance with tiny amount of LoRa nodes. Needs to be tested on a bigger environment. Feel free to bring your feedback here.


## Hardware and Software required

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/325hntapzouzlrfnl0do.png)

* Raspberry Pi 3 or 4 (or similar Compute Module carrier boards).
* SD card
* Power supply
* LoRa concentrator (SX1301 or SX1302) (e.g. [RAK 2245](https://store.rakwireless.com/products/rak2245-pi-hat), [RAK 2287](https://www.rakwireless.com/en-us/products/lpwan-gateways-and-concentrators/rak2287) among others)

* [balenaCloud](https://dashboard.balena-cloud.com/) account
* [balena Etcher](https://balena.io/etcher) to flash the SD card.


## Deploy the Basics Station gateway with The Things Stack network server

To deploy the Basics(TM) Station gateway service along TTS local network server on your Raspberry Pi, click the [Deploy with balena here](https://dashboard.balena-cloud.com/deploy?repoUrl=https://dashboard.balena-cloud.com/deploy?repoUrl=https://github.com/xoseperez/the-things-stack-basicstation-balena) or go to the [Github repo](https://github.com/xoseperez/the-things-stack-basicstation-balena) to click the Deploy with balena button to automatically deploy the application on balenaCloud.

[![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/or4c4wpa91kz427lt6xk.png)](https://dashboard.balena-cloud.com/deploy?repoUrl=https://dashboard.balena-cloud.com/deploy?repoUrl=https://github.com/xoseperez/the-things-stack-basicstation-balena)

Clicking the deploy button will create a fleet with all the necessary code to deploy your The Things Stack plus basicstation service.

It’s time to connect your LoRa concentrator compatible with your Raspberry Pi or balena Fin (SX1301, or SX1302 and SX1303 such as RAK2245 or RAK 2287), to run the LoRaWAN gateway running alongside with The Things Stack network server.

Remember to add or modify the variable `MODEL` depending on the LoRa concentrator model you have (SX1301, SX1302 or SX1303). By default is set to SX1301.

You should see something similar on balenaCloud as below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lg6j069euyymu3dhke9j.png)

You will notice that the services will reboot a few times, this is normal behaviour to set up all the services properly.

## Register the gateway at your private The Things Stack

Copy your local IP address and paste it into the browser with https. Accept the self-signed certificate that the device has generated automatically for your local IP address. Alternatevaly feel free to use the public URL generated by balenaCloud.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pes3wm3j9ptck7ft7pe9.png)

Login using the credentials User ID `admin` and the password `changeme`. Feel free to change your Password after you log in.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3eyoh4tsoneflgkj0von.png)

Click `Register a gateway` and then `Add new Gateway`. Copy the EUI that the basicstation service introduces as a TAG of on the balenaCloud dashboard. Paste it as the EUI of the gateway when registering.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1q416345mgm99x60701m.png)

Now the gateway is registered and it's time to generate an API Key. Go to `Add API Key`, then select `Link as a Gateway to a Gateway Server for traffic exchange, i.e. write uplink and red downlink`. Click the button `Create API key`. Remember to copy the key.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nqlbra067y02orvmzyi6.png)

Paste the key on the balenaCloud `Device Variables` of the device on balenaCloud as `TC_KEY`.

The gateway registered will be online on your private The Things Stack network server and able to receive data from your LoRa nodes.


This project involves a lot of services running together. If there is any issue not reported here, feel free to report it at the [balena forums](https://forums.balena.io/).

And remember that this project is open source, [feel free to contribute to improve it](https://github.com/xoseperez/the-things-stack-basicstation-balena).