9.4 KiB
TTN Gateway for Raspberry Pi
Resin Dockerfile & scripts for The Things Network gateways based on the Raspberry Pi. This updated version uses the gateway connector protocol, not the old packet forwarder. See the TTN documentation on Gateway Registration.
Currently two gateway setups are supported:
- Raspberry Pi + IMST IMST iC880A-SPI configured as described by TTN-ZH. You do not need to follow the Setting up the software step, as the setup scripts in this repository does it for you.
- "linklabs-dev", a LinkLabs Raspberry Pi "Hat"
PREREQUISITES
- Build your hardware.
- Create a new gateway that uses
gateway connector
on the TTN Console. Also set the location and altitude of your gateway. We will come back to this console later to obtain the gateway ID and access key. - Create and sign into an account at http://resin.io, which is the central "device dashboard". By using this dashboard you will be able to create the "boot SD" for your Raspberry pi. When you use that SD card, Resin will recognize that Raspberry Pi as belonging to you, will then deliver the gateway software to your RPi, and will subsequently keep your RPi up-to-date with future updates to the gateway software.
- Clone this github repo to your mac/pc. That desktop repo will be subsequently used to instruct resin.io what software to place on your gateway(s).
PREPARING YOUR FIRST GATEWAY DEVICE FOR RESIN.IO
- On resin.io, create an "Application" for managing your TTN gateway devices. I'd suggest that you give it the name "ttngw", select the appropriate device type (i.e. Raspberry Pi 2 or Raspberry Pi 3), and click "Create New Application". You only need to do this once, after which you'll be able to manage one or many gateways of that type.
- You'll then be brought to the Device Management dashboard for that Application. Follow the instructions to create a boot SD for your Raspberry Pi. (Pro Tip: Use a fast microSD card and a USB 3 adapter if you can, because it can take a while to copy all that data. Either that, or be prepared to be very patient.) Note that during the long process of waiting for the "sudo dd" command, if you're using a Mac you can press "Ctrl T" which will give you an interim progress report.
- When the (long) process of writing the image to the SD card completes, insert it into your Raspberry Pi, connect it to the network with Ethernet, and power it up.
- After several minutes, on the resin.io Devices dashboard you'll now see your device - first in a "Configuring" state, then "Idle". Click it to open the Devices control panel.
- If you like, enter any new Device Name that you'd like, such as "gateway-1".
PREPARING YOUR FIRST GATEWAY DEVICE FOR TTN
Click the "Environment Variables" section at the left side of the screen. This will allow you to configure environment parameters for this and only this device, and which will be appropriately inserted into this device's "local_conf.json" with lowercase tuple names derived from the appropriate uppercase environment names beginning with "GW_". For example, for an IMST gateway with no GPS, the MINIMUM environment variables that you should configure at this screen should look something like this:
DEVICE ENVIRONMENT VARIABLES
Name | Value |
---|---|
GW_TYPE | imst-ic880a-spi |
GW_CONTACT_EMAIL | yourname@yourdomain.com |
GW_ID | The gateway ID from the TTN console |
GW_KEY | The gateway KEY from the TTN console |
GW_RESET_PIN | 22 |
On the other hand, for the LinkLabs gateway, which has a built-in GPS, you need:
DEVICE ENVIRONMENT VARIABLES
Name | Value |
---|---|
GW_TYPE | linklabs-dev |
GW_CONTACT_EMAIL | yourname@yourdomain.com |
GW_ID | The gateway ID from the TTN console |
GW_KEY | The gateway KEY from the TTN console |
GW_GPS | true |
GW_RESET_PIN | 29 |
For a more complete list of possible environment variables, see CONFIGURATION.
Reset pin values
Depending on the way you connect the concentrator board to the Raspberry Pi, the reset pin of the concentrator might be on a different GPIO pin of the Raspberry Pi. Here follows a table of the most common backplane boards used, and the reset pin number you should use in the GW_RESET_PIN
environment variable.
Note that the reset pin you should define is the physical pin number on the Raspberry Pi. To translate between different numbering schemes you can use pinout.xyz.
Backplane | Reset pin
Gonzalo Casas backplane<br >https://github.com/gonzalocasas/ic880a-backplane<br >https://www.tindie.com/stores/gnz/ | 22 ch2i<br >https://github.com/ch2i/iC880A-Raspberry-PI | 11 Linklabs Rasberry Pi Hat | 29 (untested) Rising HF Board | 26 (untested)
If you get the message
ERROR: [main] failed to start the concentrator
after resin.io is finished downloading the application, or when restarting the gateway, it most likely means the GW_RESET_PIN
you defined is incorrect.
SPECIAL Note for using the LinkLabs gateway on a Raspberry Pi 3
There is a backward incomatibility between the Raspberry Pi 1 and 2 hardware, and Raspberry Pi 3. For Raspberry Pi 3, it is necessary to make a small additional configuration change.
Click <- to go back to the Device List, and note that on the left there is an option called "Fleet Configuration". Click it.
Add a New config variable as follows:
APPLICATION CONFIG VARIABLES
Name | Value |
---|---|
RESIN_HOST_CONFIG_core_freq | 250 |
TRANSFERRING TTN GATEWAY SOFTWARE TO RESIN SO THAT IT MAY BE DOWNLOADED ON YOUR DEVICES
-
On your Mac or PC, in terminal, change your working directory to the directory that is the clone of this git repo:
git clone https://github.com/jpmeijers/ttn-resin-gateway-rpi.git
-
Now, type the command that you'll see displayed in the edit control in the upper-right corner of the Resin devices dashboard for your device. This command "connects" your local directory to the resin GIT service, which uses GIT to "receive" the gateway software from TTN, and it looks something like this:
git remote add resin youraccount@git.resin.io:youraccount/yourapplication.git
-
Type the following commands into terminal to "push" the TTN files up to resin.io:
git add . git commit -m "first upload of ttn files to resin" git push -f resin master
-
What you'll now see happening in terminal is that this "git push" does an incredible amount of work:
-
It will upload a Dockerfile, a "build script", and a "run script" to resin
-
It will start to do a "docker build" using that Dockerfile, running it within a QEMU ARM virtual machine on the resin service.
-
In processing this docker build, it will run a "build.sh" script that downloads and builds the packet forwarder executable from source code, for RPi+iC880A-SPI.
-
When the build is completed, you'll see a unicorn 🦄 ASCII graphic displayed in your terminal.
-
Now, switch back to your device dashboard, you'll see that your Raspberry Pi is now "updating" by pulling the Docker container from the resin.io service. Then, after "updating", you'll see the gateway's log file in the window at the lower right corner. You'll see it initializing, and will also see log output each time a packet is forwarded to TTN. You're done!
PRO TIPS
-
At some point if you would like to add a second gateway, third gateway, or a hundred gateways, all you need to do is to add a new device to your existing Application. You needn't upload any new software to Resin, because Resin already knows what software belongs on the gateway. So long as the environment variables are configured correctly for that new device, it'll be up and running immediately after you burn an SD card and boot it.
-
Resin will automatically restart the gateway software any time you change the environment variables. You'll see this in the log. Also, note that Resin restarts the gateway properly after power failures. If the packet forwarder fails because of an error, it will also automatically attempt to restart.
-
If you'd like to update the software across all the gateways in your device fleet, simply do the following:
Edit the Dockerfile to bump the TTN_GATEWAY_VERSION number git add . git commit -m "Updated gateway version" git push -f resin master"
-
For devices without a GPS, the location that is configured on the TTN console is used. This location is only read at startup of the gateway. Therefore, after you set or changed the location, restart the application from the resin.io console.
Credits
Gonzalo Casas on the iC880a-based gateway Ruud Vlaming on the Lorank8 installer Jac Kersing on the Multi-protocol packet forwarder The Team at resin.io