This project implements an IoT Gateway node using a XIAO ESP32-S3 microcontroller. Its main function is to receive telemetry data from field sensors via LoRa (915 MHz), display the information locally on an OLED screen, and forward the formatted JSON data packet to a secure backend (NestJS) through a Cloudflare HTTPS tunnel.
- Microcontroller: Seeed Studio XIAO ESP32-S3
- LoRa Module: Wio SX1262
- Display: OLED SSD1306 128x64 (I2C)
| Component | ESP32-S3 Pin |
|---|---|
| LoRa NSS | 41 |
| LoRa DIO1 | 39 |
| LoRa NRST | 42 |
| LoRa BUSY | 40 |
| OLED SDA | Default I2C |
| OLED SCL | Default I2C |
To compile this code in the Arduino IDE or PlatformIO, you need to install the following libraries:
RadioLib(SX1262 transceiver handling)U8g2(Graphic controller for the OLED display)WiFi,HTTPClient,WiFiClientSecure(Native ESP32 core libraries)
Before flashing the code to the ESP32, you must configure the credentials at the top of the main file:
-
WiFi Network:
- const char* ssid = "YOUR_WIFI_SSID";
- const char* password = "YOUR_WIFI_PASSWORD";
-
Backend Connection (Cloudflare / NestJS): Make sure to enter the exact domain provided by the Cloudflare tunnel, without the trailing port.
- const char* serverName = "https://your-tunnel.trycloudflare.com/telemetry";
-
Authentication and Device:
- const char* apiKey = "YOUR_SECRET_API_KEY";
- const char* deviceUid = "YOUR_NAME_DEVICE";
The Gateway constantly listens on the 915.0 MHz frequency (Bandwidth: 125.0, Spreading Factor: 7). The sending node is expected to transmit a strict comma-separated String format:
Voltage,BatteryPercentage(Example:"3.7,85")
Upon receiving a valid packet, the OLED screen updates to display:
- The raw received data.
- The signal strength (RSSI) in dBm.
The code extracts the voltage and percentage, builds a JSON object, and sends it using WiFiClientSecure (with flexible SSL validation to support Cloudflare development tunnels).
JSON Payload structure sent to the server: { "deviceUid": "SENSOR_ID_01", "battery_v": 3.7, "battery_p": 85 } (The x-simulator-key header is included for backend authorization).
- SSL Certificates: Currently, client.setInsecure() is enabled. This is ideal for testing with Cloudflare. For a strict production environment with a custom domain, it is recommended to provide the root CA certificate.
- Sync Word: The LoRa system uses the sync word 0x12 (Private network). Ensure that the sending nodes share this exact Sync Word.