Learning Goals 5 min
Plain LoRa is two modules talking. LoRaWAN is the protocol that lets thousands of devices share a public network. The Things Network (TTN) is a free, community-run global LoRaWAN with gateways covering ~200 countries. By the end of this lesson you will:
- Explain the LoRaWAN three-layer architecture: end-device → gateway → network server.
- Register a device on TTN and pick its activation method (OTAA vs ABP).
- Decode an uplink message in TTN's console and route it to a webhook or MQTT integration.
Warm-Up 10 min
No hardware required — today is conceptual + a free TTN account.
Check coverage
Visit ttnmapper.org. Search your area. If you see gateway dots, you have free LoRaWAN coverage. If not, you'd need to deploy your own gateway (~£100) or use a different network like Helium / Senet.
New Concept · The LoRaWAN architecture 25 min
Three layers
- End-device: your battery-powered Arduino + LoRa module. Sends "uplinks" (data going up) periodically. Sleeps in between.
- Gateway: a more capable LoRa receiver, often roof-mounted, with internet connectivity. Forwards every packet it hears to a network server. Doesn't belong to any device — "public" gateway can hear any device near it.
- Network server: a cloud service that decrypts and routes messages. TTN is the most popular free one.
Plus the "application": your code or service that consumes the data. Could be a webhook, an MQTT integration, a Node-RED flow, an HTTP endpoint.
Flow of an uplink
- End-device packages 10 bytes of sensor data, encrypts with its session key, sends as LoRa packet.
- Every gateway in range hears it. Each one forwards to TTN, tagged with its own location + RSSI.
- TTN deduplicates (multiple gateways may have heard the same packet), decrypts using the device's key.
- TTN routes the decrypted payload to the configured destination — your webhook, MQTT broker, etc.
- Your application receives the payload + metadata (which gateways, when, how strong).
Activation: OTAA vs ABP
| Method | How | Security | Use |
|---|---|---|---|
| OTAA (Over The Air Activation) | Device joins on first boot with a known DevEUI + AppEUI + AppKey. Network gives it a fresh session key. | Strongest (session keys rotate). | Default. Always pick this unless you have a reason not to. |
| ABP (Activation By Personalisation) | Session keys are baked into the device's firmware. Skips the join procedure. | Weaker (keys never rotate). | Rare. Useful when you can't wait for the join, or testing. |
Device classes
| Class | Behaviour | Use |
|---|---|---|
| Class A | Mostly asleep. Tx then opens two short Rx windows. Lowest power. | Sensor nodes (default). |
| Class B | Class A plus periodic scheduled Rx slots. | Devices needing more frequent server commands. |
| Class C | Continuously listening except while transmitting. | Mains-powered actuators (door locks, valves). |
For battery-powered sensors, always Class A.
Setting up a device on TTN
- Create a free TTN account at
thethingsnetwork.org. - Console → Applications → Create application. Name it.
- Add an End Device → Manually (or pick a board if it's in the catalogue).
- Pick frequency plan (EU868 / US915 / AS923 etc.) — match your region.
- LoRaWAN version: latest (1.0.3 or 1.1).
- Activation mode: OTAA.
- Note the auto-generated DevEUI, AppEUI (or JoinEUI), AppKey. Copy these into your sketch.
Sketch — using MCCI LMIC library
The most popular Arduino LoRaWAN library. Install via Library Manager ("MCCI LoRaWAN LMIC library").
Configuration is more involved than plain LoRa — you need to set the DevEUI / AppEUI / AppKey, schedule transmissions (respecting duty cycle), handle the join, and parse downlinks. Adafruit and TTN publish example sketches; start from those.
// Skeleton — full LMIC sketches are 200+ lines; this is the shape.
#include <lmic.h>
#include <hal/hal.h>
static const u1_t DEVEUI[8] = { /* from TTN, LSB order */ };
static const u1_t APPEUI[8] = { /* from TTN, LSB order */ };
static const u1_t APPKEY[16] = { /* from TTN, MSB order */ };
void os_getDevEui(u1_t* buf) { memcpy_P(buf, DEVEUI, 8); }
void os_getArtEui(u1_t* buf) { memcpy_P(buf, APPEUI, 8); }
void os_getDevKey(u1_t* buf) { memcpy_P(buf, APPKEY, 16); }
void setup() {
os_init();
LMIC_reset();
LMIC_startJoining();
}
void loop() {
os_runloop_once();
}Once joined, you'd call LMIC_setTxData2(port, data, len, confirmed) to queue an uplink. The library handles channel hopping, duty-cycle enforcement, and downlink receipt.
Worked Example · Register and send your first uplink 20 min
Step 1 — set up the TTN application
Follow the steps in §3 above. Get DevEUI / AppEUI / AppKey.
Step 2 — wire and program
Same RFM95 wiring as L04-18. Use the "ttn-otaa-feather-us915-dht22" example (or the EU868 version) from the LMIC library and replace the keys with yours.
Step 3 — observe the join
Serial Monitor shows "Joining..." → "Joined!" within a minute (assuming a gateway is in range).
Step 4 — observe the first uplink
In TTN console → your application → your device → Live data. You'll see incoming packets, with the gateway that received them, RSSI, decoded bytes.
Step 5 — add an integration
TTN → Application → Integrations → MQTT or Webhooks. Configure to forward every uplink to your service. Now your TTN packets land in MQTT (Home Assistant!) or a custom HTTPS endpoint.
Step 6 — send a downlink
From TTN console → your device → Messaging → Downlink. Enter a hex payload. Send. Your device receives on its next Rx window.
Try It Yourself · Mostly paper 15 min
The TTN free tier gives you ~30 s of airtime per device per day. SF7 packet ≈ 50 ms. SF12 ≈ 1500 ms. Max daily packets at each SF?
Reveal
SF7: 30 / 0.05 = 600. SF12: 30 / 1.5 = 20. So the choice of SF directly limits your messaging budget.
Three gateways hear the same uplink. Does your application see it three times?
Reveal
No. TTN deduplicates server-side. You see one message, with a list of gateways and their RSSIs.
Your device sends 1 packet per hour at SF9 (~250 ms per packet). It runs on 2 × AA. Estimate years of life.
Reveal
Per packet: ~100 mA × 0.25 s = 7 µAh. Per hour: 7 µAh (transmit) + 5 µA × 1 h (deep sleep) = 12 µAh / h. Per year: 12 × 8760 ≈ 105 mAh. 2 × AA (~2500 mAh) → ~25 years of LoRa transmissions, limited in practice by battery shelf life (~7–10 years).
OTAA vs ABP: pick for (a) factory-deployed sensor (b) urgent prototype with no time for the join procedure.
Reveal
(a) OTAA — security + key rotation. (b) ABP — skips the join, simpler bring-up.
You want to send only a 1-bit "alarm" signal. What's the smallest packet payload you can use?
Reveal
1 byte (8 bits, smallest practical). LoRaWAN overhead adds ~13 bytes of headers. The total airtime is what matters; the payload size is mostly aesthetic. Just send {0x01} for alarm-on.
Mini-Challenge · Plan a real deployment 10 min
You want a TTN-connected pet-tracker on your dog's collar. Sends GPS once per 10 minutes. Battery lasts 6 months.
- Pick SF (range vs airtime).
- Pick activation (OTAA / ABP).
- Estimate the battery (capacity vs avg current).
- Identify which gateways near you would catch the uplinks (check ttnmapper.org).
Reveal one good answer
- SF: SF10 — ~7 km range, ~500 ms airtime. Fits the 6 packets per hour budget.
- OTAA — security matters.
- Per packet: ~50 µAh. Per day: 144 packets × 50 µAh = 7 mAh. Plus 5 µA × 24 h sleep = 120 µAh. Total ~7 mAh/day → 1500 mAh battery lasts ~200 days = 6+ months. Use an 18650 (3000 mAh) for headroom.
- ttnmapper.org will tell you. In most cities, multiple gateways. In rural areas, depends on volunteer deployments.
Recap 5 min
LoRaWAN = LoRa physical + a network layer with gateways + a network server. TTN is the free public option. Register a device on TTN, copy keys into your sketch, use LMIC library, see packets in the console. Tomorrow we leave radio and go industrial: Modbus on RS-485.
- LoRaWAN
- Network protocol on top of LoRa. Standardised by the LoRa Alliance. Adds encryption, addressing, gateway support.
- End-device
- Your sensor / actuator. Talks LoRa to gateways.
- Gateway
- Receiver with internet. Forwards packets to a network server. Doesn't process the contents.
- Network server
- Cloud service that authenticates devices, decrypts payloads, routes to applications. TTN is the most popular free one.
- OTAA / ABP
- Activation methods. OTAA = join procedure with key derivation. ABP = static keys.
- DevEUI / AppEUI / AppKey
- Identifiers and the root key used during OTAA join.
- Class A / B / C
- Power-vs-listening tradeoffs. Class A = always-sleep, briefly-listen; Class C = always listening.
- Uplink / downlink
- Device → server / server → device. Uplinks are frequent; downlinks are rare and limited.
- LMIC library
- The standard Arduino LoRaWAN library. Handles channels, duty cycle, joins.
- TTN integrations
- Outputs: MQTT, webhooks, Node-RED. Forward uplinks to your service.
Homework 5 min
- Make a free TTN account. Browse the gateway map for your area.
- If you have LoRa hardware: register a device. Don't worry about uploading the LMIC sketch yet — just the registration is the goal today.
- Read ahead to ARD-L04-20 (Modbus and RS-485). Bring an RS-485 transceiver module (MAX485, RS-485 USB adapter for testing) if you have one.