LoRa weather station with acknowledgments on an Arduino Uno
A BME280 node sends temperature, humidity and pressure over LoRa with RadioHead, and a gateway acknowledges each reading and shows it on an I2C LCD. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Advanced Runs in your browser. Free, and no account needed.
The parts retail for about $72. On Mokxi they're free.
The note on the canvas
Word for word, as it sits beside the circuit in the editor.
Try it
1. Press Run. Every 5 seconds the node sends its BME280 reading, and the gateway's LCD shows it.
2. Drag the BME280's sliders: the next reading on the LCD follows.
3. The node's serial monitor says "acknowledged" for every reading the gateway got.
4. Hold the gateway's reset button: the node retries 3 times, then says no acknowledgment came. Let go and it is acknowledged again.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// LoRa weather station, the sensor node: a BME280 read every 5 seconds and
// sent over an SX1278 (Ra-02) LoRa module to the gateway with RadioHead's
// RHReliableDatagram. The gateway acknowledges every reading; with no
// acknowledgment the library sends again, up to 3 retries, and the sketch
// says so if all of them go unanswered.
//
// Ra-02 NSS -> 10 DIO0 -> 2 RST -> 9 SCK -> 13 MOSI -> 11
// MISO -> 12 3.3V -> 3.3V GND -> GND
// BME280 SDA -> A4 SCL -> A5 VIN -> 5V GND -> GND
//
// The SX1278 runs on 3.3 V and its pins are not 5 V tolerant: on the bench,
// put a level shifter on NSS, SCK, MOSI and RST, or use a 3.3 V board.
//
// Things to try. Drag the BME280's sliders. Hold the gateway's reset button
// and watch the retries run out. Change INTERVAL_MS for slower reports,
// which a battery node would use.
#include <SPI.h>
#include <RH_RF95.h>
#include <RHReliableDatagram.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
const uint8_t NODE_ADDRESS = 1;
const uint8_t GATEWAY_ADDRESS = 2;
const float FREQUENCY = 433.0; // MHz: the Ra-02 is a 433 MHz module
const unsigned long INTERVAL_MS = 5000;
const uint8_t RESET_PIN = 9;
RH_RF95 rf95(10, 2); // NSS, DIO0
RHReliableDatagram manager(rf95, NODE_ADDRESS);
Adafruit_BME280 bme;
// What goes on the air: 8 bytes, the same layout on both boards.
struct __attribute__((packed)) Reading {
int16_t centiC; // temperature, hundredths of a degree C
uint16_t deciHum; // humidity, tenths of a percent
uint16_t deciHpa; // pressure, tenths of a hectopascal
uint16_t count;
};
uint16_t count = 0;
unsigned long lastSent = 0;
void setup() {
Serial.begin(9600);
// Reset the radio, as the module's datasheet asks, before talking to it.
pinMode(RESET_PIN, OUTPUT);
digitalWrite(RESET_PIN, LOW);
delay(10);
digitalWrite(RESET_PIN, HIGH);
delay(10);
if (!bme.begin(0x76)) {
Serial.println("no BME280 at 0x76: check SDA and SCL");
while (1) {}
}
if (!manager.init()) {
Serial.println("LoRa radio not found: check NSS, DIO0 and the 3.3 V supply");
while (1) {}
}
rf95.setFrequency(FREQUENCY);
rf95.setTxPower(13);
manager.setRetries(3);
manager.setTimeout(500); // ms to wait for an acknowledgment
Serial.println("LoRa weather node 1 ready");
}
void loop() {
if (millis() - lastSent < INTERVAL_MS && lastSent != 0) return;
lastSent = millis();
float t = bme.readTemperature();
float h = bme.readHumidity();
float p = bme.readPressure() / 100.0;
Reading r = {(int16_t)lround(t * 100), (uint16_t)lround(h * 10), (uint16_t)lround(p * 10), ++count};
Serial.print("reading ");
Serial.print(count);
Serial.print(": ");
Serial.print(t, 1);
Serial.print(" C, ");
Serial.print(h, 0);
Serial.print(" %, ");
Serial.print(p, 1);
Serial.print(" hPa: ");
if (manager.sendtoWait((uint8_t *)&r, sizeof(r), GATEWAY_ADDRESS)) {
Serial.println("acknowledged");
} else {
Serial.println("no acknowledgment after 3 retries");
}
}
// LoRa weather station, the gateway: receives the sensor node's readings
// over an SX1278 (Ra-02) with RadioHead's RHReliableDatagram, acknowledges
// each one, and shows the latest on a 16x2 I2C LCD. Every reading also goes
// to the serial monitor with its RSSI, ready for a computer to log.
//
// Ra-02 NSS -> 10 DIO0 -> 2 RST -> 9 SCK -> 13 MOSI -> 11
// MISO -> 12 3.3V -> 3.3V GND -> GND
// LCD SDA -> A4 SCL -> A5 VCC -> 5V GND -> GND
//
// The SX1278 runs on 3.3 V and its pins are not 5 V tolerant: on the bench,
// put a level shifter on NSS, SCK, MOSI and RST, or use a 3.3 V board.
//
// Things to try. Change FREQUENCY here only and nothing arrives. Change
// STALE_MS to flag a quiet node sooner.
#include <SPI.h>
#include <RH_RF95.h>
#include <RHReliableDatagram.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const uint8_t GATEWAY_ADDRESS = 2;
const float FREQUENCY = 433.0; // MHz, the same as the node
const unsigned long STALE_MS = 20000;
const uint8_t RESET_PIN = 9;
RH_RF95 rf95(10, 2); // NSS, DIO0
RHReliableDatagram manager(rf95, GATEWAY_ADDRESS);
LiquidCrystal_I2C lcd(0x27, 16, 2);
struct __attribute__((packed)) Reading {
int16_t centiC;
uint16_t deciHum;
uint16_t deciHpa;
uint16_t count;
};
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
unsigned long lastHeard = 0;
bool heard = false;
bool stale = false;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(RESET_PIN, OUTPUT);
digitalWrite(RESET_PIN, LOW);
delay(10);
digitalWrite(RESET_PIN, HIGH);
delay(10);
if (!manager.init()) {
Serial.println("LoRa radio not found: check NSS, DIO0 and the 3.3 V supply");
lcd.print("No LoRa radio");
while (1) {}
}
rf95.setFrequency(FREQUENCY);
rf95.setTxPower(13);
Serial.println("LoRa gateway listening");
lcd.print("Waiting for node");
}
void loop() {
uint8_t len = sizeof(buf);
uint8_t from;
// recvfromAck() sends the acknowledgment before it returns.
if (manager.recvfromAck(buf, &len, &from)) {
if (len != sizeof(Reading)) return;
Reading r;
memcpy(&r, buf, sizeof(r));
heard = true;
stale = false;
lastHeard = millis();
lcd.clear();
lcd.print(r.centiC / 100.0, 1);
lcd.print(" C ");
lcd.print(r.deciHum / 10.0, 0);
lcd.print(" %");
lcd.setCursor(0, 1);
lcd.print(r.deciHpa / 10.0, 1);
lcd.print(" hPa");
Serial.print("node ");
Serial.print(from);
Serial.print(" #");
Serial.print(r.count);
Serial.print(": ");
Serial.print(r.centiC / 100.0, 1);
Serial.print(" C, ");
Serial.print(r.deciHum / 10.0, 0);
Serial.print(" %, ");
Serial.print(r.deciHpa / 10.0, 1);
Serial.print(" hPa, RSSI ");
Serial.println(rf95.lastRssi());
}
if (heard && !stale && millis() - lastHeard > STALE_MS) {
stale = true;
lcd.setCursor(0, 1);
lcd.print("node quiet 20 s ");
Serial.println("no reading for 20 s");
}
}
Parts list
8 parts, plus the jumper wires. They retail for about $72; on Mokxi they're free.
How it is wired
24 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 (1) pin GND; LoRa module, SX1278 (Ra-02) (1) pin GND
- Arduino Uno R3 (1) pin 13; LoRa module, SX1278 (Ra-02) (1) pin SCK
- Arduino Uno R3 (1) pin 12; LoRa module, SX1278 (Ra-02) (1) pin MISO
- Arduino Uno R3 (1) pin 11; LoRa module, SX1278 (Ra-02) (1) pin MOSI
- Arduino Uno R3 (1) pin 10; LoRa module, SX1278 (Ra-02) (1) pin NSS
- Arduino Uno R3 (1) pin 9; LoRa module, SX1278 (Ra-02) (1) pin RST
- Arduino Uno R3 (1) pin 2; LoRa module, SX1278 (Ra-02) (1) pin DIO0
- Arduino Uno R3 (1) pin 3V3; LoRa module, SX1278 (Ra-02) (1) pin 3.3V
- Arduino Uno R3 (1) pin A4; Pressure, humidity and temperature, BME280 pin SDA
- Arduino Uno R3 (1) pin A5; Pressure, humidity and temperature, BME280 pin SCL
- Ground: Arduino Uno R3 (2) pin GND; LoRa module, SX1278 (Ra-02) (2) pin GND
- Arduino Uno R3 (2) pin 13; LoRa module, SX1278 (Ra-02) (2) pin SCK
- Arduino Uno R3 (2) pin 12; LoRa module, SX1278 (Ra-02) (2) pin MISO
- Arduino Uno R3 (2) pin 11; LoRa module, SX1278 (Ra-02) (2) pin MOSI
- Arduino Uno R3 (2) pin 10; LoRa module, SX1278 (Ra-02) (2) pin NSS
- Arduino Uno R3 (2) pin 9; LoRa module, SX1278 (Ra-02) (2) pin RST
- Arduino Uno R3 (2) pin 2; LoRa module, SX1278 (Ra-02) (2) pin DIO0
- Arduino Uno R3 (2) pin 3V3; LoRa module, SX1278 (Ra-02) (2) pin 3.3V
- Arduino Uno R3 (2) pin A4; Character LCD with I2C backpack pin SDA
- Arduino Uno R3 (2) pin A5; Character LCD with I2C backpack pin SCL
- 5 V: Pressure, humidity and temperature, BME280 pin VIN
- Ground: Pressure, humidity and temperature, BME280 pin GND
- Ground: Character LCD with I2C backpack pin GND
- 5 V: Character LCD with I2C backpack pin VCC
Questions and answers
What does RHReliableDatagram add over plain LoRa?
An acknowledgment for every message. The gateway's recvfromAck() replies before it returns; if no reply comes within the timeout (500 ms here), sendtoWait() sends again, up to 3 retries, and then returns false.
What is sent over the air?
An 8-byte packed struct with the same layout on both boards: the temperature in hundredths of a degree, humidity and pressure in tenths, and a count.
Which frequency should I use?
The Ra-02 is a 433 MHz module, so both sketches call setFrequency(433.0). Use a frequency that is legal where you live, and the same one at both ends, or nothing arrives.
Can I wire the Ra-02 straight to a 5 V Uno?
It runs on 3.3 V and its pins are not 5 V tolerant. Put a level shifter on NSS, SCK, MOSI and RST, or use a 3.3 V board.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.