Building an ESP32 LoRa gateway packet forwarding TTN solution is the bridge between low-power field sensors and the internet. LoRa (Long Range) radio technology can carry sensor data over 5-15 km in open terrain, making it ideal for Indian agricultural applications, smart city deployments, and remote industrial monitoring. The Things Network (TTN) is a global, community-driven LoRaWAN network server that is free to use. In this tutorial, you’ll learn how to build a working LoRa gateway using an ESP32 and a LoRa radio module that forwards received packets to TTN over the internet.
LoRa vs LoRaWAN: Understanding the Difference
LoRa and LoRaWAN are related but distinct technologies. Understanding this distinction is crucial before building your gateway.
LoRa (Long Range) is the physical radio modulation technique developed by Semtech. It uses Chirp Spread Spectrum (CSS) modulation to achieve extraordinary range and obstacle penetration at very low data rates (250 bps to 50 kbps). LoRa defines how bits are transmitted over the air — it’s the physical (PHY) layer.
LoRaWAN is the network protocol built on top of LoRa. It defines how devices are authenticated, how data is routed through gateways to a network server, how encryption works, and how acknowledgements are handled. The Things Network is a LoRaWAN network server.
An ESP32 with a LoRa module can act as a single-channel gateway — it listens on one frequency and one spreading factor and forwards received packets to TTN. This is simpler than a full multi-channel gateway (which requires 8-channel hardware like the RAK7258) but is excellent for learning and for covering a small area with your own end devices.
Important note: TTN’s fair use policy limits each application to 30 seconds of downlink airtime per day and 10 messages per device per day. For commercial deployments in India, consider TTN Cloud (paid) or a private network server like ChirpStack.
Hardware Requirements
For this project you need:
- ESP32 development board (any standard board works)
- SX1276 or SX1278 LoRa module (Ra-02 is the most common in India)
- Antenna appropriate for your frequency band (865-867 MHz for India)
- Jumper wires and breadboard
- Stable internet connection (wired Ethernet via an Ethernet shield is preferred for gateways)
India uses the IN865 frequency plan for LoRaWAN, which operates in the 865-867 MHz band. Make sure your LoRa module and antenna are rated for this band. The Ra-02 module (based on SX1278) is rated for 433 MHz and can also operate at 868 MHz. For Indian deployments, configure the frequency to 865.0625 MHz, 865.4025 MHz, or 865.9850 MHz — the three channels defined in the IN865 plan.
Ai Thinker NodeMCU-32S-ESP32 Development Board – IPEX Version
This ESP32 board with IPEX antenna connector provides excellent WiFi range for reliable gateway connectivity — perfect for remote LoRa gateway deployments.
Setting Up The Things Network Console
Before writing any code, register your gateway on TTN. Here’s the process:
Step 1: Create a TTN Account
Go to thethingsnetwork.org and create a free account. During registration, select your region. For India, choose the Community Edition under the eu1, au1, or nam1 cluster — TTN does not yet have a dedicated Indian cluster, but au1 (Australia) typically provides the lowest latency from India.
Step 2: Register the Gateway
In the TTN console, go to Gateways → Register Gateway. You need to provide:
- Gateway EUI: An 8-byte unique identifier. Use your ESP32’s MAC address with FF:FE inserted in the middle to create a proper EUI-64. Example: if MAC is
24:6F:28:AA:BB:CC, the EUI is24:6F:28:FF:FE:AA:BB:CC. - Frequency Plan: Select India 865-867 MHz (IN865).
- Gateway Server Address: This will be shown after registration.
Step 3: Note the Gateway Credentials
After registration, TTN generates the gateway server address. For the au1 cluster, it is typically au1.cloud.thethings.network. Note this down — you’ll need it in the firmware.
Wiring the ESP32 and LoRa Module
The LoRa Ra-02 module communicates with the ESP32 via SPI. Connect the pins as follows:
| LoRa Ra-02 Pin | ESP32 Pin | Notes |
|---|---|---|
| 3.3V | 3.3V | Do NOT use 5V — will damage the module |
| GND | GND | Common ground |
| SCK | GPIO18 | SPI clock |
| MISO | GPIO19 | SPI MISO |
| MOSI | GPIO23 | SPI MOSI |
| NSS (CS) | GPIO5 | Chip select |
| RESET | GPIO14 | LoRa reset pin |
| DIO0 | GPIO26 | Interrupt for RX done |
Always connect an antenna before powering the LoRa module. Transmitting without an antenna can damage the SX1278 chip. For indoor testing, even a 17.3 cm wire antenna cut for 865 MHz works. For outdoor deployments, use a proper 865 MHz omni-directional antenna.
Gateway Firmware: Single-Channel Packet Forwarder
The single-channel packet forwarder (SCPF) is a well-established open-source firmware for ESP32-based LoRa gateways. The most popular implementation is by Thomas Telkamp and Matthijs Kooijman, with ESP32 adaptations available on GitHub.
The key configuration parameters you need to set:
// WiFi credentials
#define SSID "YOUR_WIFI_SSID"
#define PASS "YOUR_WIFI_PASSWORD"
// TTN Gateway server
#define SERVER "au1.cloud.thethings.network"
#define PORT 1700
// Gateway EUI (your ESP32 MAC with FFFE inserted)
uint8_t GatewayEUI[] = { 0x24, 0x6F, 0x28, 0xFF, 0xFE, 0xAA, 0xBB, 0xCC };
// LoRa frequency for India (IN865 channel 1)
#define FREQ 865062500 // 865.0625 MHz in Hz
// Spreading factor (SF7 = fastest, SF12 = longest range)
#define SF_CHECK SF7
// LoRa module SPI pins
#define SCK 18
#define MISO 19
#define MOSI 23
#define SS 5
#define RST 14
#define DI0 26
The SCPF listens on one frequency and spreading factor. When a LoRa packet is received, it wraps the payload in the UDP Semtech packet forwarder protocol and sends it to the TTN server over UDP. TTN then routes the data to the appropriate application based on the device EUI in the packet header.
The firmware handles NTP time synchronization (required by TTN for packet timestamps), LED status indication, and automatic reconnection to WiFi if the connection drops. Download the complete firmware from the GitHub repository and configure the settings file before compiling.
2 x 18650 Lithium Battery Shield V8 – 5V/3A for ESP32
Deploy your ESP32 LoRa gateway in locations without wall power — this battery shield provides hours of reliable operation from two 18650 cells.
Setting Up an End Device Node
Your gateway needs at least one end device (sensor node) to forward packets for. The end device is a second ESP32 (or Arduino) with a LoRa module that sends sensor data. Here is a minimal end device sketch using the LMIC library:
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
// Device credentials from TTN console (ABP activation)
static const PROGMEM u1_t NWKSKEY[16] = { /* Network Session Key */ };
static const PROGMEM u1_t APPSKEY[16] = { /* App Session Key */ };
static const u4_t DEVADDR = 0x260BXXXX; // Device address from TTN
const lmic_pinmap lmic_pins = {
.nss = 5,
.rxtx = LMIC_UNUSED_PIN,
.rst = 14,
.dio = {26, 25, LMIC_UNUSED_PIN}
};
void do_send(osjob_t* j) {
if (LMIC.opmode & OP_TXRXPEND) return;
uint8_t payload[2];
float temp = 28.5; // Replace with actual sensor reading
int16_t t = (int16_t)(temp * 10);
payload[0] = t >> 8;
payload[1] = t & 0xFF;
LMIC_setTxData2(1, payload, sizeof(payload), 0);
}
Register this device in TTN under Applications → Add Application → Add End Device. Use ABP (Activation By Personalisation) for simplicity, which gives you the Device Address, Network Session Key, and Application Session Key directly without an OTAA join procedure.
Real-World Applications in India
The ESP32 LoRa gateway + TTN combination is being used across India for innovative projects:
Precision Agriculture
Farmers in Maharashtra and Punjab use LoRa sensor nodes measuring soil moisture, temperature, and pH across large fields. A single gateway on a water tower or elevated building can cover an entire 100-acre farm. Data flows to TTN and then to a farmer’s smartphone app, enabling irrigation decisions based on real data rather than guesswork.
Smart City Waste Management
Municipal corporations in cities like Surat and Bengaluru are piloting LoRa-based bin fill level sensors. Each dustbin has an ultrasonic sensor and a LoRa node. The gateway forwards fill-level data to TTN, and a dashboard routes garbage collection trucks only to full bins, reducing fuel consumption by 30-40%.
Water Level Monitoring
With India’s monsoon floods affecting millions annually, LoRa-based water level sensors on rivers and reservoirs provide early warning. The gateway at a local utility office forwards readings to TTN, which triggers SMS alerts through a connected service when water levels reach danger thresholds.
A86 JSN-SR04T Waterproof Ultrasonic Rangefinder Module Version 3.0
A weatherproof ultrasonic sensor for water level or distance measurement — perfect for LoRa end devices deployed outdoors in Indian weather conditions.
Frequently Asked Questions
Is LoRa legal to use in India?
Yes. The WPC (Wireless Planning and Coordination Wing) under India’s Department of Telecommunications has allocated the 865-867 MHz band for short range devices without a license, subject to a maximum transmit power of 1 Watt ERP and a duty cycle limit. LoRaWAN end devices typically transmit at 14-20 dBm (25-100 mW), well within this limit. Commercial LoRaWAN network operators still require WPC authorization for their gateway infrastructure, but individual experimenter use falls under the de-licensed category.
How far can a single-channel ESP32 gateway reach?
In open terrain with a proper rooftop antenna, an ESP32 SCPF gateway can receive LoRa packets from 3-8 km away. In dense urban areas with buildings, the range drops to 500 metres to 2 km. Using a higher gain directional antenna or installing the gateway at an elevated location significantly improves coverage.
Can the ESP32 gateway handle multiple end devices simultaneously?
A single-channel gateway can receive one packet at a time. If two end devices transmit simultaneously on the same channel and spreading factor, there will be a collision and one or both packets will be lost. In practice, since LoRa devices transmit infrequently (every few minutes), collisions are rare for small networks of up to 10-15 devices.
What is the difference between a single-channel gateway and a multi-channel gateway?
A single-channel gateway listens on one frequency at one spreading factor. A commercial multi-channel gateway (8 channels) listens on 8 frequencies simultaneously and can handle all spreading factors. Commercial gateways are required for production LoRaWAN deployments. The ESP32 SCPF is suitable for learning, prototyping, and small private networks.
Build Your LoRa Gateway Today
Get genuine ESP32 boards, sensors, and accessories for your LoRa gateway project from Zbotic — trusted by Indian makers and engineers nationwide.
Add comment