# Feather + Raspberry Pi Weather Monitoring Network with LoRa or LoRaWAN

## Overview

Danger: 

![](https://cdn-learn.adafruit.com/assets/assets/000/070/308/medium800thumb/adafruit_io_io_gtwy.jpg?1548954922)

In this guide, we'll be connecting a sensor to two Feathers and pairing them with a gateway to bring all the data collected into the cloud. **We're going to build our own network** to send data from&nbsp; **devices** to a&nbsp; **router.** The router will then&nbsp; **decode&nbsp;** the data packets and route them to Adafruit IO or The Things Network.&nbsp;

Here's a network map (also known as _a network topology_) of the connections between the nodes/gateways/protocols of our network.

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/070/337/medium800/adafruit_io_updated_topology_%282%29.png?1548969064)

## Selecting a Transport

Devices need a&nbsp;way to communicate their data - the first step in building a network is _selecting_ a mode of transport. As humans, we talk to each other in our native language(s). Electronic devices can also talk to each other, and there's a _plethora_ of ways for them to communicate (for more information on&nbsp;_transports_, [check out our All the Internet of Things - Part 1, which explains more about selecting a transport for your project](https://learn.adafruit.com/alltheiot-transports))

## LoRa to adafruit.io

In the first part of this guide, we're going to employ an Internet-of-Things transport protocol called&nbsp; **LoRa** to have our Feathers communicate with the Raspberry Pi Gateway. While we _could_ use WiFi/BTLE/Cellular, we're not sending a large amount of data (a few bytes). We also do not need to send this data quickly, our sensors will transmit their data to the router every 15 minutes or so.&nbsp;

By sacrificing transmit speed and the amount of data which we're able to send, LoRa radios can transmit much farther than Bluetooth Low-Energy and WiFi with lower power utilization.

## LoRaWAN to The Things Network

The second part of this guide uses&nbsp; **LoRa_WAN_** _.&nbsp;_LoRaWAN is a Low Power Wide Area Network (LPWAN) specification which allows connection of LoRa devices to the internet. LoRaWAN differs from LoRa by the inclusion of a _physical_ layer.

### Selecting a Device
To begin building a sensor network, we'll _specify_ what we want the **sensor&nbsp;device** to do:

- Read a sensor
- Send data over LoRa (it'll need a LoRa radio)
- Connect to a LiPo battery (wireless operation!)
- Run&nbsp;[CircuitPython](https://learn.adafruit.com/welcome-to-circuitpython/overview)

A board which meets all of these specifications is the Feather M0 with RFM95 LoRa Radio. This microcontroller runs CircuitPython, has a built-in LoRa radio, and a connector for LiPo batteries.&nbsp;

### Adafruit Feather M0 with RFM95 LoRa Radio - 900MHz

[Adafruit Feather M0 with RFM95 LoRa Radio - 900MHz](https://www.adafruit.com/product/3178)
This is the **Adafruit Feather M0 RFM95 LoRa Radio (900MHz)****. **We call these _RadioFruits** ,**_ our take on an microcontroller with a "[Long Range (LoRa)](https://www.lora-alliance.org/)" packet radio transceiver with built...

In Stock
[Buy Now](https://www.adafruit.com/product/3178)
[Related Guides to the Product](https://learn.adafruit.com/products/3178/guides)
![Angel shot Adafruit Feather M0 with RFM95 LoRa Radio - 900MHz](https://cdn-shop.adafruit.com/640x480/3178-07.jpg)

_Next up,_ we'll need a sensor to gather and send data. The BME280 is a fantastic environmental sensor&nbsp;- it can read a location's temperature, barometric pressure, and humidity.&nbsp;

- _Need less data? Just want the humidity from a location?_ [We have&nbsp; **_lots_** of sensors to help build a sensor node.&nbsp;](https://www.adafruit.com/?q=environmental%20)

### Adafruit BME280 I2C or SPI Temperature Humidity Pressure Sensor

[Adafruit BME280 I2C or SPI Temperature Humidity Pressure Sensor](https://www.adafruit.com/product/2652)
Bosch has stepped up their game with their new BME280 sensor, an environmental sensor with temperature, barometric pressure&nbsp;and&nbsp;humidity! This sensor is great for all sorts of indoor environmental sensing and can even be used in both I2C and SPI!

This precision sensor from...

In Stock
[Buy Now](https://www.adafruit.com/product/2652)
[Related Guides to the Product](https://learn.adafruit.com/products/2652/guides)
![small, rectangle-shaped, BME280 temperature humidity pressure sensor breakout board.](https://cdn-shop.adafruit.com/640x480/2652-04.jpg)

### Selecting a Router/Gateway
We're going to use the LoRa Radio Bonnet and a Raspberry Pi Zero W to act as a **_gateway_** - converting incoming packets (from the feathers) to readable values. After it converts this data, it'll&nbsp;_forward&nbsp;_it to either Adafruit IO or The Things Network.

### Adafruit LoRa Radio Bonnet with OLED - RFM95W @ 915MHz

[Adafruit LoRa Radio Bonnet with OLED - RFM95W @ 915MHz](https://www.adafruit.com/product/4074)
The latest Raspberry Pi computers come with WiFi and Bluetooth®, and now you can add even more radio options with the Adafruit Radio Bonnets! Upgrade your Raspberry Pi with a LoRa/LoRaWAN radio so it can communicate over very long distances. These bonnets plug right into your Pi and give...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4074)
[Related Guides to the Product](https://learn.adafruit.com/products/4074/guides)
![A rectangular microcontroller with OLED screen. A blue-manicured index finger presses buttons below the OLED screen, triggering different texts.](https://cdn-shop.adafruit.com/product-videos/640x480/4074-03.jpg)

### Part: Rasperry Pi Zero
quantity: 1
Raspberry Pi Zero with Headers
[Rasperry Pi Zero](https://www.adafruit.com/product/3708)

## Parts
You'll want to grab the following parts for this project:

### Part: FeatherWing Proto
quantity: 2
FeatherWing Proto - Prototyping Add-on For All Feather Boards
[FeatherWing Proto](https://www.adafruit.com/product/2884)

### Part: 8GB SD Card with NOOBS
quantity: 1
8 GB MicroSD Card with full PIXEL desktop NOOBS - v2.8
[8GB SD Card with NOOBS](https://www.adafruit.com/product/3259)

### Part: MicroUSB Power Supply
quantity: 1
5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable
[MicroUSB Power Supply](https://www.adafruit.com/product/1995)

### Part: MicroUSB Cable
quantity: 1
USB cable - USB A to Micro-B - 3 foot long
[MicroUSB Cable](https://www.adafruit.com/product/592)

# Feather + Raspberry Pi Weather Monitoring Network with LoRa or LoRaWAN

## Wiring

## Feather Wiring
You can connect the BME280 directly to a feather, or to a breadboard. But, we'd like to be able to remove it (and possibly add it to a Featherwing Doubler, or even Tripler!)

![](https://cdn-learn.adafruit.com/assets/assets/000/070/342/medium800/adafruit_io_Pasted_Image_1_31_19__5_29_PM.png?1548973833)

Make the following connections between the LoRa Radio Feather and the BME280:

- **Board 3V** &nbsp;to&nbsp; **sensor VIN**
- **Board GND** &nbsp;to&nbsp; **sensor GND**
- **Board SCL** &nbsp;to&nbsp; **sensor SCK**
- **Board SDA** &nbsp;to&nbsp; **sensor SDI**

## Raspberry Pi Wiring
![](https://cdn-learn.adafruit.com/assets/assets/000/070/343/medium800/adafruit_io_adafruit_products_LoRaBonnetTop.jpg?1548973917)

Since we're using an Adafruit LoRa Radio Bonnet with OLED, we don't need to wire anything. Connect the 20x2 header socket on the bonnet the header on your Pi and you're ready to roll!

# Feather + Raspberry Pi Weather Monitoring Network with LoRa or LoRaWAN

## Feather Setup

Some of the CircuitPython compatible boards come with CircuitPython installed. Others are&nbsp;_CircuitPython-ready_, but need to have it installed.&nbsp;

**If you're using a&nbsp;[Feather M0 RFM9x](https://www.adafruit.com/product/3178), you'll need to install CircuitPython on your board.&nbsp;**

- [**To install CircuitPython on your Feather M0 RFM9x, follow this page**](https://learn.adafruit.com/using-lorawan-and-the-things-network-with-circuitpython/circuitpython-installation)

We'll need to install a few CircuitPython libraries on the Feather. To download them all at once, download the CircuitPython Library bundle.&nbsp;

- _Don't have the latest CircuitPython Bundle installed?_ [Click here to navigate a guide which will explain the process of downloading and installing the bundle](https://learn.adafruit.com/welcome-to-circuitpython/circuitpython-libraries#installing-the-circuitpython-library-bundle-12-5)

[Click here for the latest CircuitPython Library Bundle Release](https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/latest)
Once the bundle is downloaded and unzipped, drag and drop the following files over to the **CIRCUITPY** drive&nbsp; **lib** folder:

- **If you're building a LoRa Device** &nbsp;for communicating with [Adafruit IO](io.adafruit.com), you'll need the following libraries:
  - **adafruit\_rfm9x**
  - **adafruit\_bme280**
  - **adafruit\_bus\_device**

- **If you're building a LoRaWAN Device** &nbsp;for communicating with [TheThingsNetwork](https://thethingsnetwork.org), you'll need the following libraries:
  - **adafruit\_tinylora**
  - **adafruit\_bme280**
  - **adafruit\_bus\_device**

After the libraries are dragged over to the **lib** folder, we'll move on to setting up the Raspberry Pi to act as a gateway.

# Feather + Raspberry Pi Weather Monitoring Network with LoRa or LoRaWAN

## LoRa Raspberry Pi Setup

This guide assumes that you've gotten your Raspberry Pi up and running, have CircuitPython installed, and are using a virtual environment.&nbsp;

If you have not done this, please follow the steps in this guide below,

- [https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi)

## Installing CircuitPython Libraries

We're running CircuitPython on the Raspberry Pi, installing the libraries for radio communication is simple.

If you on Raspberry Pi OS Bookworm or newer, please start by configuring a virtual environment (VENV). If you do not know how to do this, follow this link:&nbsp;[https://learn.adafruit.com/python-virtual-environment-usage-on-raspberry-pi/basic-venv-usage](https://learn.adafruit.com/python-virtual-environment-usage-on-raspberry-pi/basic-venv-usage)

Once the virtualenv is set up, to&nbsp; **install the library for the display** , **enter the following into the terminal:**

**`sudo pip3 install adafruit-circuitpython-ssd1306`**

You'll also need to **install the framebuf module** in order to write to the display.&nbsp;

`sudo pip3 install adafruit-circuitpython-framebuf`

To install the library for the **RFM9x** Module, **enter the following into the terminal:**

**`sudo pip3 install adafruit-circuitpython-rfm9x`**

You'll also want to download the font file, **`font5x8.bin`,** and copy it into the same directory as the script

` wget https://github.com/adafruit/Adafruit_CircuitPython_framebuf/raw/main/examples/font5x8.bin`

[Click to download font5x8.bin](https://github.com/adafruit/Adafruit_CircuitPython_framebuf/blob/master/examples/font5x8.bin)
Make sure the font file is 1282 bytes long, if not something went wrong with the download

![](https://cdn-learn.adafruit.com/assets/assets/000/096/258/medium800/raspberry_pi_image.png?1603405641)

### RFM9x Connection Test!
Warning: 

The following code is for checking if the RFM9x radio is set up for transmitting and receiving. **Save the code on your Pi** (save this to a location you can remember, like your Desktop or Downloads folder)&nbsp;as **`rfm9x_check.py`**.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/pi_radio/rfm9x_check.py

To use the code, copy and paste the following command into your terminal:

`python3 rfm9x_check.py`

Now to check the setup:

If the RFM9x/RFM69 is **detected** , the OLED will display **Detected**. You can test&nbsp; the buttons by pressing them.

![raspberry_pi_bonnet_test.gif](https://cdn-learn.adafruit.com/assets/assets/000/069/466/medium640thumb/raspberry_pi_bonnet_test.jpg?1547664932)

 **If the wiring of the radio module is incorrect** &nbsp;-&nbsp;the display will show&nbsp; **ERROR**. Check over your wiring on the Wiring Page and re-run the test. You may also need to ensure the correct CircuitPython library is installed for the module.&nbsp;

&nbsp;

**If the OLED does not turn on** &nbsp;-&nbsp;first check that it is wired correctly. Then, make sure you enabled I2C from&nbsp;`raspi-config`&nbsp;and installed the required libraries (`adafruit-circuitpython-framebuf`_and_&nbsp;`adafruit-circuitpython-ssd1306`).

![pi_a___b___2__3_IMG_0773.jpg](https://cdn-learn.adafruit.com/assets/assets/000/068/581/medium640/pi_a___b___2__3_IMG_0773.jpg?1546449242)

With everything working, let's move on to using the radio.

Warning: If you are using a Pi 5 and receive a "lgpio.error: 'GPIO busy" error try the following.

## Pi 5 GPIO busy&nbsp;

disable one-wire:

&nbsp;

```terminal
sudo raspi-config nonint do_onewire 1
```

disable CE0 / CE1:

```terminal
curl -fsSL https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/main/raspi-spi-reassign.py -o raspi-spi-reassign.py
sudo -E env PATH=$PATH python3 raspi-spi-reassign.py --ce0=disabled --ce1=disabled
```

# Feather + Raspberry Pi Weather Monitoring Network with LoRa or LoRaWAN

## Adafruit IO Setup

_If you do not already have an Adafruit IO account set up_, head over to [io.adafruit.com](http://io.adafruit.com/) to link your Adafruit.com account to Adafruit IO.

We'll need to create a new feed to hold data for the first feather's BME280 sensor. The BME280 can output Temperature, Humidity,&nbsp;_and_&nbsp;Pressure data, so we'll need to create&nbsp; **three feeds** &nbsp;to hold data from each node's sensor.

To do this, navigate to the&nbsp;[feeds page](https://io.adafruit.com/feeds)&nbsp;on Adafruit IO.

&nbsp;

Then click&nbsp; **Actions&nbsp;-\> Create New Feed** , and name this feed&nbsp; **`feather-1-temp`.**

- If you do not already know how to create a feed, head over to&nbsp;[Adafruit IO Basics: Feeds](https://learn.adafruit.com/adafruit-io-basics-feeds).

![adafruit_io_feather-1-temp.png](https://cdn-learn.adafruit.com/assets/assets/000/070/138/medium640/adafruit_io_feather-1-temp.png?1548713368)

Next, we'll **create two more feeds** for the first feather:

- `feather-1-humid`
- `feather-1-pressure`

Now that the first feather has feeds to store its data in, we'll **create two additional feeds** for the second feather:

- `feather-2-humid`
- `feather-2-pressure`
- `feather-2-temp`

We're also going to need our Adafruit IO _username_ and our _secret API key_.

Navigate to your profile and click the **View AIO Key** button to retrieve them. Write them down in a safe place, we'll need them for later.

![](https://cdn-learn.adafruit.com/assets/assets/000/070/038/medium800thumb/3d_printing_adafruit_io_username_and_password_%281%29.jpg?1548457371)

Now that the feeds are set up - if you wish to visualize incoming data, you'll want to set up a new dashboard to display feed data from the sensors.

- If you do not know how to create a new dashboard, or use the dashboard feature of Adafruit IO - [click here to learn&nbsp;more about Adafruit IO Dashboards.](https://learn.adafruit.com/adafruit-io-basics-dashboards)

I set my dashboard up with three gauge blocks connected to each of the feeds, and a text box to identify the gauge cluster for each sensor node.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/070/667/medium800/adafruit_io_Untitled.png?1549375100)

With Adafruit IO set up, let's move on to using the sensor network we've created.

# Feather + Raspberry Pi Weather Monitoring Network with LoRa or LoRaWAN

## Using with Adafruit IO

## Feather Code Usage
We'll want to start by loading the code onto the Feather sensor nodes. To do this, copy the code below and rename it as&nbsp; **code.py**.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/lorawan_sensing_network/lora_device/code.py

We'll also want to set the frequency which we are transmitting on. **This value _must be the same_ between the Feathers (nodes) and the Raspberry Pi (gateway)**, otherwise the gateway will not receive the packet, or visa-versa.&nbsp;

To do this, set&nbsp;`RADIO_FREQ_MHZ`&nbsp;to a frequency value in MHz. For example, I'll set it to 905.5MHz.

```auto
# Define radio frequency, must match device.
RADIO_FREQ_MHZ = 905.5
```

We also need a way for the gateway to identify each Feather. To do this, we'll set the&nbsp;`FEATHER_ID`. If you're preparing the first sensor node, leave this variable alone as its already set to 1.

```auto
# Device ID
FEATHER_ID = 1
```

Save the file (CTRL/CMD+S). This will reload and run the code from **code.py**.

Using the Mu editor, there is a built in REPL/Serial monitor. This is the recommended way to view output from your Python code. Alternatively you can use a terminal program to connect to the Feather's USB serial port.

You should see the following output from the REPL:

```auto
code.py output:
Temperature: 23.8 C
Humidity: 21.5 %
Pressure: 1005.2 hPa
Sending data..
Data sent!
```

Whenever the Feather node(s) send data using the radio, they'll blink an onboard LED (located on Pin #13) to show that the packet has been sent.

## Feather Code Overview
One of the advantages of using LoRa over another type of transport (like WiFi) is that the radio is only turned on during transmission. Doing this reduces the amount of power consumption by the node.

We're going to **build a packet to transmit the data to the gateway**. This packet comprises&nbsp;of 7 bytes (the RFM9x library currently supports sending packets \<= 252 bytes in length) which will hold all of our required sensor data, along with a unique identifier for our node, set in the code as `FEATHER_ID`.

The temperature values read by the BME280 are _floating point (decimal)_ values (this is a value like 3.14 instead of 3). To send these values, we'll assemble the packet by sending the values as integers (which will later be re-assembled by the gateway)

Once assembled, the LoRa packet is sent using the radio.&nbsp;

```auto
rfm9x.send(bme280_data)
```

To modify how frequently the packet is sent, change the variable&nbsp;`SENSOR_SEND_DELAY`, keeping in mind that the delay is in minutes.

That's it! Then, load the code onto the other feather(s) sensor nodes, changing the `FEATHER_ID` as you go.&nbsp;

## Pi Code Usage
Next, we'll want to set up the Raspberry Pi with the code to use the RFM9x as a LoRa gateway. **Save the code below** on your Pi as **lora\_gateway.py**.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/lorawan_sensing_network/lora_gateway/code.py

We'll need to **install the [Adafruit IO Python library](https://github.com/adafruit/Adafruit_IO_Python)** to forward the messages by the LoRa radio to Adafruit IO. To do this, enter the following in your terminal:

`sudo pip3 install adafruit-io`

Once the Adafruit IO library is installed, we'll need to set the username and the key in the code:

Set the&nbsp;`ADAFRUIT_IO_USERNAME` to your **Adafruit IO Username**.

Then, set the&nbsp;`ADAFRUIT_IO_KEY` to your **Adafruit IO Key**.

And **save the file**.

Now that the Adafruit IO username and password are configured, run the code by typing the following into your terminal:

`python3 lora_gateway.py`

The display should show&nbsp; **Adafruit IO GTWY&nbsp;** and&nbsp; **- Waiting for PKT -**. This is the idle state, the gateway is listening for a new packet from the Feather node.

![adafruit_io_waiting_for_pkt_io.gif](https://cdn-learn.adafruit.com/assets/assets/000/070/306/medium640thumb/adafruit_io_waiting_for_pkt_io.jpg?1548952894)

When a new packet is received, the display will change to show that a new packet from the feather was received. It'll also forward the packet to Adafruit IO.

![adafruit_io_io_rx_pkts.gif](https://cdn-learn.adafruit.com/assets/assets/000/070/307/medium640thumb/adafruit_io_io_rx_pkts.jpg?1548953063)

The terminal will change when a new packet is received:

```auto
- New Packet! -
Device ID: LoRa Feather # 1
Temp: 22.83 C
Humid: 17.90 %
Pressure: 370.11 hPa
- New Packet! -
Device ID: LoRa Feather # 2
Temp: 22.89 C
Humid: 10.48 %
Pressure: 370.11 hPa
```

To verify Adafruit IO has received the data,&nbsp;[navigate to the monitor page on Adafruit IO](https://io.adafruit.com/monitor). This page shows a live-view of all incoming data (and error messages).&nbsp;

![adafruit_io_live_data.png](https://cdn-learn.adafruit.com/assets/assets/000/070/202/medium640/adafruit_io_live_data.png?1548796523)

Your Adafruit IO Dashboard should look like the following as it receives data from the LoRa devices:

![](https://cdn-learn.adafruit.com/assets/assets/000/070/305/medium800thumb/adafruit_io_io_dash.jpg?1548951984)

 **If you want this code to run when the Pi boots up** , you'll need to make an entry in rc.local.

To do this, **type the following into the terminal**

`sudo nano /etc/rc.local`

Then, **add the following line of code the file**

`(sleep 30; python3 home/pi/rfm9x_gateway.py) &`

and **save the file**.

**Reboot the Pi** by entering the following into the terminal

`sudo shutdown -r now`

When the Pi restarts, it'll run the gateway code.

 **Congrats! You've built your first LoRa network, and forwarded data to the internet.**

## Code Overview
Since the network topology can seem complicated at a glance, we'll take it piece by piece and explain how the code relates.

![](https://cdn-learn.adafruit.com/assets/assets/000/070/303/medium800/adafruit_io_top_lora_radiob.png?1548947535)

First, the gateway receives a new packet.

```auto
packet = rfm9x.receive()
```

Then, it checks if the packet's value is empty or a retransmission. If it's a new packet value, it'll decode the packet.

Since we converted the float sensor value into an integer for transmission, and know the order of the packet, we can decode the temperature, humidity and air pressure. We also know the first byte of the packet (the header) is the the Feather's unique identifier.&nbsp;

```auto
# Decode packet
feather_id = packet[0]
temp_val = pkt_int_to_float(packet[1], packet[2])
humid_val = pkt_int_to_float(packet[3], packet[4])
pres_val = pkt_int_to_float(packet[5], packet[6])
```

Now that the data has been decoded, we're going to tackle the second role of the Radio Bonnet - sending the data as a&nbsp; **transmission&nbsp;** to Adafruit IO.

![](https://cdn-learn.adafruit.com/assets/assets/000/070/304/medium800/adafruit_io_top_radio_io.png?1548947568)

If the decoded&nbsp;`feather_id`&nbsp;is 1, we'll send&nbsp; the values to Adafruit IO feeds `temperature_feed_1`,&nbsp;`humidity_feed_1`, and&nbsp;`pressure_feed_1`.

```auto
if feather_id is 1:
  display.fill(0)
  display.text('Feather #1 Data RX''d!', 15, 0, 1)
  display.text('Sending to IO...', 0, 20, 1)
  display.show()
  aio.send(temperature_feed_1.key, temp_val)
  aio.send(humidity_feed_1.key, humid_val)
  aio.send(pressure_feed_1.key, pres_val)
  display.text('Sent!', 100, 20, 1)
  display.show()
```

We have code for detecting a second feather, but if you have a third (or fourth!), you'll want to add another&nbsp;`elif` statement identical to the second one.

# Feather + Raspberry Pi Weather Monitoring Network with LoRa or LoRaWAN

## Things Network Raspberry Pi Setup

Danger: 

This guide assumes that you've gotten your Raspberry Pi up and running, and have CircuitPython installed.

- [If you have not done this yet, visit the installation guide **here** and come back when you're set up.](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi)

## Installing CircuitPython Libraries

We're running CircuitPython on the Raspberry Pi, installing the libraries for radio communication is simple.

To **install the library for the display** , **enter the following into the terminal:**

**`pip3 install adafruit-circuitpython-ssd1306`**

You'll also need to **install the framebuf module** in order to write to the display.&nbsp;

`sudo pip3 install adafruit-circuitpython-framebuf`

To install the library for the **RFM9x** Module, **enter the following into the terminal:**

`sudo pip3 install adafruit-circuitpython-rfm9x`

## Testing the Setup

There have been a _lot_ of steps so far - let's verify that everything is wired up and all the libraries are installed properly before moving on to sending and receiving data.&nbsp;

The following code is for checking if the RFM9x radio is set up for transmitting and receiving. **Save the code on your Pi** as **`rfm9x_check.py`**.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/pi_radio/rfm9x_check.py

To use the code, enter the following in your terminal:

`python3 rfm9x_check.py`

You'll also want to download the font file, **`font5x8.bin`,** and copy it&nbsp;into the same directory as the script:

[font5x8.bin](https://github.com/adafruit/Adafruit_CircuitPython_framebuf/blob/master/examples/font5x8.bin)
Then, let's check the setup:

If the RFM9x is&nbsp; **detected** , the&nbsp;OLED will display&nbsp;_Detected!_&nbsp;

&nbsp;

You can test&nbsp; the buttons by pressing them and watching the display for changes.

![raspberry_pi_bonnet_test.gif](https://cdn-learn.adafruit.com/assets/assets/000/069/541/medium640thumb/raspberry_pi_bonnet_test.jpg?1547750875)

 **If the wiring of the radio module is incorrect** &nbsp;-&nbsp;the display will show&nbsp; **ERROR**. Check over your wiring on the Wiring Page and re-run the test. You may also need to ensure the correct CircuitPython library is installed for the module.&nbsp;

&nbsp;

**If the OLED does not turn on** &nbsp;-&nbsp;first check that it is wired correctly. Then, make sure you enabled I2C from&nbsp;`raspi-config`&nbsp;and installed the required libraries (`adafruit-circuitpython-framebuf`_and_&nbsp;`adafruit-circuitpython-ssd1306`).

![raspberry_pi_pi_a___b___2__3_IMG_0773.jpg](https://cdn-learn.adafruit.com/assets/assets/000/069/384/medium640/raspberry_pi_pi_a___b___2__3_IMG_0773.jpg?1547586798)

## Install the Single Channel Pi Gateway

[WiringPi](http://wiringpi.com/)&nbsp;is&nbsp; **PRE-INSTALLED** &nbsp;with standard Raspbian systems. To check if it is pre-installed on your system, run:

` gpio -v`

If you get something, then you have it already installed. Otherwise, install WiringPi by running:

`sudo apt-get install wiringpi`

Then, **clone the packet forwarder repository** &nbsp;

`git clone https://github.com/adafruit/single_chan_pkt_fwd.git`

**Navigate** into the packet forwarder directory

`cd single_chan_pkt_fwd/`

and **compile** the code for the gateway

`sudo make all`

You'll also want to download the font file, **`font5x8.bin`,** and copy it&nbsp;into the same directory as the script:

[font5x8.bin](https://github.com/adafruit/Adafruit_CircuitPython_framebuf/blob/master/examples/font5x8.bin)
 **If you are using a Raspberry Pi ZERO,&nbsp;** you will need to change a value within the configuration file. Using a text editor, open the `global_conf.json` file. Replace `"is_pi_zero": false` with `"is_pi_zero": true`. Your configuration file should look like the following.

```python
{
  "SX127x_conf": {
    "freq": 905100000,
    "spread_factor": 7,
    "pin_nss": 11,
    "pin_dio0": 3,
    "pin_rst": 25
  },
  "gateway_conf": {
    "ref_latitude": 0.0,
    "ref_longitude": 0.0,
    "ref_altitude": 10,
    "name": "Radio Pi Bonnet",
    "email": "contact@email.com",
    "desc": "Adafruit Radio Pi LoRa 1-Ch Gateway",
    "is_pi_zero": true,
    "servers": [
      {
        "address": "router.us.thethings.network",
        "port": 1700,
        "enabled": true
      },
      {
        "address": "router.eu.thethings.network",
        "port": 1700,
        "enabled": false
      }
    ]
  }
}
```

## Gateway Usage

From within the `single_chan_pkt_fwd/` folder, run the Python program by entering the following into the terminal:

`python3 lorawan_gateway.py`

The display should show the Gateway EUI. Keep this screen open, we'll need it for the next step.

![raspberry_pi_gateway_eui.jpeg](https://cdn-learn.adafruit.com/assets/assets/000/069/540/medium640/raspberry_pi_gateway_eui.jpeg?1547750785)

# Feather + Raspberry Pi Weather Monitoring Network with LoRa or LoRaWAN

## Configuring The Things Network

Danger: 

## Registering a Gateway with The Things Network
Before a Gateway can be used with [The Things Network](https://www.thethingsnetwork.org/)&nbsp;(TTN), it needs to be registered.

First, **register an account with TTN**.&nbsp;

- [Navigate to their account registration page](https://account.thethingsnetwork.org/register)&nbsp;to set up an account.

Once logged in,&nbsp;**[navigate to the The Things Network Console](https://console.thethingsnetwork.org/).** This page is where you can register applications and add new devices or gateways.

**Click&nbsp;_Gateways_&nbsp;**

![](https://cdn-learn.adafruit.com/assets/assets/000/068/682/medium800/raspberry_pi_The_Things_Network_Console_Gateways.png?1546635645)

 **Click _register gateway_**

![](https://cdn-learn.adafruit.com/assets/assets/000/068/683/medium800/raspberry_pi_The_Things_Network_Console_Reg_gatewy.png?1546635671)

 **Tick the&nbsp;_I'm using the legacy packet forwarder&nbsp;_** checkbox.

Then, enter the Gateway EUI displayed on the gateway.

![raspberry_pi_gateway_eui.jpeg](https://cdn-learn.adafruit.com/assets/assets/000/069/561/medium640/raspberry_pi_gateway_eui.jpeg?1547756159)

![raspberry_pi_The_Things_Network_Console_Leg_Packet_fwd_2.png](https://cdn-learn.adafruit.com/assets/assets/000/068/684/medium640/raspberry_pi_The_Things_Network_Console_Leg_Packet_fwd_2.png?1546635714)

 **Fill out a&nbsp;description** &nbsp;of what the gateway is.

&nbsp;

**Set the frequency plan to** your region's frequency, we set ours to **United States**.&nbsp;

&nbsp;

**Set the Router&nbsp;to the location closest to you**.&nbsp;

- Adafruit is located in New York City, so I set the router to _ttn-router-us-west.&nbsp;_

![raspberry_pi_The_Things_Network_Console_Gateway_Config.png](https://cdn-learn.adafruit.com/assets/assets/000/068/686/medium640/raspberry_pi_The_Things_Network_Console_Gateway_Config.png?1546635961)

 **Click Register Gateway**. TTN will register the gateway and redirect you to the Gateway Overview page.

![raspberry_pi_The_Things_Network_Console.png](https://cdn-learn.adafruit.com/assets/assets/000/068/687/medium640/raspberry_pi_The_Things_Network_Console.png?1546636095)

The Things Network does **not** list Single Channel gateways on their map - these gateways are not great at handling multiple devices or accurately receiving a packets every time they're sent by a device. However, they're great for hacking on, experimenting, and using in a staging/lab setup.&nbsp;

We'll want to **set the Gateway Privacy to Private** and **set the location to Unlisted**.&nbsp;

From the&nbsp; **Gateway Overview&nbsp;** page, **click** _&nbsp; **Edit Info** _.

On the Privacy Page, **_untick_ all of the options&nbsp;** and&nbsp; **click _Save Settings_**.

![](https://cdn-learn.adafruit.com/assets/assets/000/068/693/medium800/raspberry_pi_The_Things_Network_Console_Privacy_2.png?1546636845)

Before starting up the gateway, we'll optionally set the gateway information.

&nbsp;

From the **Gateway Overview** page, **click** _Edit Info__&nbsp;_

&nbsp;

For brand, **select** _Single-Channel DIY Gateway_

&nbsp;

For model, **type** _Raspberry Pi_

![raspberry_pi_The_Things_Network_Console_Information.png](https://cdn-learn.adafruit.com/assets/assets/000/068/690/medium640/raspberry_pi_The_Things_Network_Console_Information.png?1546636489)

![raspberry_pi_The_Things_Network_Console_Info_2.png](https://cdn-learn.adafruit.com/assets/assets/000/068/691/medium640/raspberry_pi_The_Things_Network_Console_Info_2.png?1546636504)

It's time to test! Run the Python program by entering the following into the terminal:

`python3 lorawan_gateway.py`

and **press the middle button** &nbsp;to launch the gateway.

You should see the **Status&nbsp;** of the gateway on your console switch from from _Not Connected_ to _Connected_. If you've had previously connected the gateway: the&nbsp;_Last Seen_ label should update to&nbsp;_Now_.&nbsp;

![raspberry_pi_last_seen.gif](https://cdn-learn.adafruit.com/assets/assets/000/069/388/medium640thumb/raspberry_pi_last_seen.jpg?1547589287)

Our gateway is now listening for new packets, it'll refresh whenever it's received a new packet.

Next, we're going to set up a device to send data to the gateway.

## Usage with Pi Zero

If you are using a Pi Zero, you will need to modify the&nbsp; **global\_conf.json&nbsp;** file prior to running the command above.

**Change the following line** in the JSON configuration from:

`"is_pi_zero": false,`

to

`"is_pi_zero": true,`

And **save the file.**

https://github.com/adafruit/single_chan_pkt_fwd/blob/master/global_conf.json

Then, run the Python program by entering the following into the terminal:

`python3 lorawan_gateway.py`

## Troubleshooting your Gateway
### 

Double check the configuration steps on this page. Make sure the Pi is connected to the internet and that the **Gateway EUI matches the Gateway EUI displayed on the Pi's display**.&nbsp;&nbsp;

# Feather + Raspberry Pi Weather Monitoring Network with LoRa or LoRaWAN

## Using with The Things Network

Danger: 

We've sent data from our sensor devices to Adafruit IO over LoRa, but what about sending data over a different protocol - **LoRaWAN**?&nbsp;

The [Adafruit\_TinyLoRa](https://github.com/adafruit/Adafruit_CircuitPython_TinyLoRa) library allows the RFM9x to be used as a LoRa_WAN_ device capable of communicating its data over longer distance with less power utilization.&nbsp;

## Feather Usage
The code below will use the&nbsp;`adafruit_tinylora`&nbsp;library which provides high-level access to your radio transceiver features.&nbsp;

The code for sending data using a BME280 sensor, along with a unique feather identifier, to a Things Network Gateway is shown below:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/lorawan_sensing_network/ttn_feather_node/code.py

While we can send data to The Things Network, and our gateway might notice it, it isn't registered to a device yet.

- To register your feather(s) with The Things Network, [follow these steps and come back when you're done](https://learn.adafruit.com/using-lorawan-and-the-things-network-with-circuitpython/using-tinylora#setting-up-the-code-for-the-things-network-6-10).

## Running the Code using Mu Editor

Upon saving ( **ctrl/cmd +s** ) your code, the board will refresh.&nbsp;

_But where is the output?&nbsp;_The serial monitor is hidden by default in Mu Editor.

In the Mu Editor, click the&nbsp; **Serial&nbsp;** button on the top icon-bar to bring up the Serial REPL.

The REPL should output the following:&nbsp;

```auto
code.py output:
Sending packet...
Packet sent!
```

We still need to check that the Raspberry Pi Gateway receives the packet from the Feather. Let's set that up next.

## Pi Usage
Run the Python program by entering the following into the terminal:

`python3 lorawan_gateway.py`

 **Press the first button** &nbsp;to display statistics about the Pi, such as its IP, CPU load, and available memory.

![adafruit_io_raspberry_pi_3_show_pi_stats.gif](https://cdn-learn.adafruit.com/assets/assets/000/070/316/medium640thumb/adafruit_io_raspberry_pi_3_show_pi_stats.jpg?1548956126)

 **Press the third button** &nbsp;to display the name of the gateway along with the frequency, spreading factor, and The Things Network router.

![adafruit_io_raspberry_pi_2_show_gateway_info.gif](https://cdn-learn.adafruit.com/assets/assets/000/070/311/medium640thumb/adafruit_io_raspberry_pi_2_show_gateway_info.jpg?1548955850)

 **Press the second button** &nbsp;to launch the gateway. It'll display the current status (if a packet is received or not) and update the timestamp every minute.

![adafruit_io_raspberry_pi_5_no_pkt_rx_yet_(1).gif](https://cdn-learn.adafruit.com/assets/assets/000/070/317/medium640thumb/adafruit_io_raspberry_pi_5_no_pkt_rx_yet_%281%29.jpg?1548956207)

When a LoRa Packet is received by the gateway, the terminal will display that a packet has been received, and it'll print out useful data coming from the packet :

```auto
incoming pkt...
{"rxpk":[{"tmst":4067850409,"freq":905.1,"chan":0,"rfch":0,"stat":1,"modu":"LORA","datr":"SF7BW125","codr":"4/5","rssi":-35,"lsnr":10.0,"size":24,"data":"QLERAiYAKAABE0NCX+3lkSnK09WVlk8N"}]}
```

The display will also refresh to display that a packet has been received.

![](https://cdn-learn.adafruit.com/assets/assets/000/070/336/medium800thumb/adafruit_io_raspberry_pi_4_send_pkt.jpg?1548968609)

We can't read the incoming data since it's _encrypted_. After all, device data shouldn't be readable by a gateway operator. We'll need to check our&nbsp; **device** &nbsp;on The Things Network Console to read the decrypted data.

## Checking the data on The Things Network Console
We want to make sure the data has been received on the other end. To do this, we'll navigate to the The Things Network Console and select the&nbsp; **application**. From the menu on the right hand side of the page,&nbsp; **Click&nbsp;Data**.

![](https://cdn-learn.adafruit.com/assets/assets/000/070/211/medium800/adafruit_io_wireless_feather_Pasted_Image_11_7_18__3_46_PM.png?1548798478)

If everything worked correctly, you'll see the payload from your device streaming into the page, in real time.&nbsp;

Neat,&nbsp; **right**? But while we received a payload, we still don't understand what it means. It's been sent to The Things Network and&nbsp;_decoded_&nbsp;on the client (Gateway) side, so it's not an AES-encrypted payload anymore. It's just not readable by humans.

![adafruit_io_uplink_data_only.png](https://cdn-learn.adafruit.com/assets/assets/000/070/332/medium640/adafruit_io_uplink_data_only.png?1548968517)

If you're sending packets in strange formats or encodings (like we are!), The Things Network Console has a&nbsp;[programmable data decoder](https://www.thethingsnetwork.org/docs/devices/bytes.html)&nbsp;to decode the packets, and assign useful labels to the data.

**Copy and paste the decoder script** &nbsp;below into the decoder's integrated text editor and&nbsp; **click save**.&nbsp;

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/lorawan_sensing_network/ttn_decoder_bme280.js

Then,&nbsp; **click the data tab**. Next to the raw payload data, you should see the decoded data for humidity and temperature, along with the unique ID of the feather.

![](https://cdn-learn.adafruit.com/assets/assets/000/070/333/medium800/adafruit_io_TTN_SCREEN.png?1548968527)


## Featured Products

### Adafruit LoRa Radio Bonnet with OLED - RFM95W @ 915MHz

[Adafruit LoRa Radio Bonnet with OLED - RFM95W @ 915MHz](https://www.adafruit.com/product/4074)
The latest Raspberry Pi computers come with WiFi and Bluetooth®, and now you can add even more radio options with the Adafruit Radio Bonnets! Upgrade your Raspberry Pi with a LoRa/LoRaWAN radio so it can communicate over very long distances. These bonnets plug right into your Pi and give...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4074)
[Related Guides to the Product](https://learn.adafruit.com/products/4074/guides)
### Adafruit BME280 I2C or SPI Temperature Humidity Pressure Sensor

[Adafruit BME280 I2C or SPI Temperature Humidity Pressure Sensor](https://www.adafruit.com/product/2652)
Bosch has stepped up their game with their new BME280 sensor, an environmental sensor with temperature, barometric pressure&nbsp;and&nbsp;humidity! This sensor is great for all sorts of indoor environmental sensing and can even be used in both I2C and SPI!

This precision sensor from...

In Stock
[Buy Now](https://www.adafruit.com/product/2652)
[Related Guides to the Product](https://learn.adafruit.com/products/2652/guides)
### Adafruit Feather M0 with RFM95 LoRa Radio - 900MHz

[Adafruit Feather M0 with RFM95 LoRa Radio - 900MHz](https://www.adafruit.com/product/3178)
This is the **Adafruit Feather M0 RFM95 LoRa Radio (900MHz)****. **We call these _RadioFruits** ,**_ our take on an microcontroller with a "[Long Range (LoRa)](https://www.lora-alliance.org/)" packet radio transceiver with built...

In Stock
[Buy Now](https://www.adafruit.com/product/3178)
[Related Guides to the Product](https://learn.adafruit.com/products/3178/guides)
### Raspberry Pi Zero WH (Zero W with Headers)

[Raspberry Pi Zero WH (Zero W with Headers)](https://www.adafruit.com/product/3708)
If you didn't think that the Raspberry Pi Zero W could possibly get any better, then boy do we have a pleasant surprise for you!&nbsp;The new **Raspberry Pi Zero WH&nbsp;** offers all the benefits of the Pi Zero W, but with one big difference – a **pre-soldered GPIO...**

In Stock
[Buy Now](https://www.adafruit.com/product/3708)
[Related Guides to the Product](https://learn.adafruit.com/products/3708/guides)
### FeatherWing Proto - Prototyping Add-on For All Feather Boards

[FeatherWing Proto - Prototyping Add-on For All Feather Boards](https://www.adafruit.com/product/2884)
A Feather board without ambition is a Feather board without FeatherWings!

This is the **FeatherWing Proto** - a prototyping add-on for all Feather boards. Using our [Feather Stacking Headers](https://www.adafruit.com/products/2830) or <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/2884)
[Related Guides to the Product](https://learn.adafruit.com/products/2884/guides)
### 8 GB MicroSD Card with full PIXEL desktop NOOBS

[8 GB MicroSD Card with full PIXEL desktop NOOBS](https://www.adafruit.com/product/3259)
NooBs is the fastest way to have a variety of operating systems on your Pi. Available on an 8G microSD card, you can now boot multiple OS's such as Raspbian, Kodi, OSMC, Windows 10 IoT, etc. There's a boot up menu for selecting which one you like. **This card has PIXEL on it with...**

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/3259)
[Related Guides to the Product](https://learn.adafruit.com/products/3259/guides)
### 5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable

[5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable](https://www.adafruit.com/product/1995)
Our all-in-one 5V 2.5 Amp + MicroUSB cable power adapter is the perfect choice for powering single-board computers like Raspberry Pi, BeagleBone, or anything else that's power-hungry!

This adapter was specifically designed to provide 5.25V, not 5V, but we still call it a 5V USB...

In Stock
[Buy Now](https://www.adafruit.com/product/1995)
[Related Guides to the Product](https://learn.adafruit.com/products/1995/guides)
### USB cable - USB A to Micro-B

[USB cable - USB A to Micro-B](https://www.adafruit.com/product/592)
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or microcontroller

Approximately 3 feet / 1 meter long

In Stock
[Buy Now](https://www.adafruit.com/product/592)
[Related Guides to the Product](https://learn.adafruit.com/products/592/guides)

## Related Guides

- [Adafruit Radio Bonnets with OLED Display - RFM69 or RFM9X](https://learn.adafruit.com/adafruit-radio-bonnets.md)
- [LoRa and LoRaWAN Radio for Raspberry Pi](https://learn.adafruit.com/lora-and-lorawan-radio-for-raspberry-pi.md)
- [3D Printed Case for Adafruit Feather](https://learn.adafruit.com/3d-printed-case-for-adafruit-feather.md)
- [MicroPython Displays: Drawing Shapes](https://learn.adafruit.com/micropython-displays-drawing-shapes.md)
- [I2C Addresses and Troublesome Chips](https://learn.adafruit.com/i2c-addresses.md)
- [RPi Stock Alert Alarm](https://learn.adafruit.com/rpi-stock-alert-alarm.md)
- [PyPortal IoT Weather Station](https://learn.adafruit.com/pyportal-iot-weather-station.md)
- [Blahaj Alarm and Lamp](https://learn.adafruit.com/blahaj-alarm.md)
- [CircuitPython Hardware: ILI9341 TFT & FeatherWing](https://learn.adafruit.com/micropython-hardware-ili9341-tft-and-featherwing.md)
- [IoT Filament Sensor](https://learn.adafruit.com/iot-filament-sensor.md)
- [Homefruit FeatherWing Tester](https://learn.adafruit.com/homefruit-featherwing-tester.md)
- [CircuitPython Hardware: PCA9685 PWM & Servo Driver](https://learn.adafruit.com/micropython-hardware-pca9685-pwm-and-servo-driver.md)
- [No-Code Offline Data Logger with WipperSnapper](https://learn.adafruit.com/no-code-offline-data-logging-with-wippersnapper.md)
- [Coffee Detonator: The TNT Plunger Grinder](https://learn.adafruit.com/coffee-detonator.md)
- [FeatherWing Proto, Doubler  and Tripler](https://learn.adafruit.com/featherwing-proto-and-doubler.md)
- [Textable Sensor with FONA and CircuitPython](https://learn.adafruit.com/textable-sensor-with-fona-and-circuitpython.md)
