Skip to content
Learni
View all tutorials
Internet des Objets

How to Set Up a LoRaWAN Network with TTN in 2026

18 minINTERMEDIATE
Lire en français

Introduction

LoRaWAN has become the essential standard for long-range, low-power IoT deployments. In 2026, network security and seamless integration with cloud platforms like The Things Network (TTN) are critical. This tutorial walks you through configuring a complete network: gateway setup, node registration, and data processing. You'll learn to avoid common MAC layer pitfalls and optimize the duty cycle. Each step includes functional code you can copy directly.

Prerequisites

  • Active The Things Network (TTN) account
  • Compatible LoRaWAN gateway (e.g., RAK7246)
  • Basic knowledge of Python and YAML
  • Access to a Linux environment or Raspberry Pi
  • LoRaWAN sensor (e.g., Arduino MKR WAN 1310)

Install TTN Tools

setup.sh
#!/bin/bash
sudo apt update && sudo apt install -y python3-pip mosquitto-clients
pip3 install paho-mqtt ttn

This script installs the required Python dependencies to communicate with TTN via MQTT. It also sets up Mosquitto clients to test local publications before remote deployment.

Gateway Configuration

The gateway acts as a bridge between LoRa sensors and the TTN network server. Use ChirpStack or your hardware's official software. Ensure the configuration file includes the correct European frequencies (868 MHz) and the proper TTN server address.

Gateway Configuration File

gateway-config.yml
gateway:
  id: "0011223344556677"
  server_address: "eu1.cloud.thethings.network"
  server_port_up: 1700
  server_port_down: 1700
  frequency_plan: "EU_863_870"

This YAML file defines the gateway's unique ID and points to the European TTN cluster. Only modify the ID and ports if using a different region.

Registering the Sensor on TTN

device.json
{
  "end_device": {
    "ids": {
      "device_id": "mon-capteur-01",
      "dev_eui": "0011223344556677"
    },
    "lorawan_version": "MAC_V1_0_4",
    "lorawan_phy_version": "PHY_V1_0_4",
    "frequency_plan_id": "EU_863_870",
    "root_keys": {
      "app_key": "00112233445566778899AABBCCDDEEFF"
    }
  }
}

This JSON is used via the TTN API to register an OTAA device. Replace the keys with randomly generated values and keep them secret.

Python Data Sending Script

send_data.py
import ttn
import time

app_id = "mon-application"
access_key = "NNSXS.XXX..."

def uplink_callback(msg, client):
    print("Données reçues:", msg.payload_raw)

handler = ttn.HandlerClient(app_id, access_key)
client = handler.data()
client.set_uplink_callback(uplink_callback)
client.connect()

while True:
    time.sleep(30)

This Python script connects to the TTN handler and listens for uplinks. It prints raw payloads. Adapt it to decode sensor data according to your format.

Payload Decoding

decoder.js
function decodeUplink(input) {
  var data = {};
  data.temperature = input.bytes[0] / 2;
  data.humidity = input.bytes[1];
  return { data: data };
}

This TTN JavaScript decoder converts received bytes into readable values. Place it in the TTN console to instantly visualize sensor measurements.

Best Practices

  • Always use OTAA instead of ABP for better key security
  • Respect the regional duty cycle (1% in Europe)
  • Encrypt payloads at the application layer
  • Monitor gateway metrics via the TTN dashboard
  • Regularly update node firmware

Common Errors to Avoid

  • Forgetting to configure the correct frequency plan (causes packet loss)
  • Reusing the same AppKey across multiple devices
  • Ignoring acknowledgments (Confirmed uplinks) for critical messages
  • Exceeding the limit of 10 messages per day without adaptive data rate

Go Further

Deepen your skills with our certified industrial IoT training courses. Discover our Learni courses.