🛠 Envoyer des données capteur vers un serveur (uplink)
Objectives
LoRaWAN equivalent of the "HTTP GET client": send data to a server. We build a compact binary frame (temperature/pressure from a BME280 over I²C), send it as a confirmed uplink, and read RSSI/SNR — the radio equivalent of an HTTP status code.
Hardware
- 1 Arduino MKR WAN 1300 (successful OTAA join, see previous tutorial)
- 1 BME280 sensor over I²C (SDA/SCL pins, 3.3 V)
- MKRWAN + Adafruit BME280 libraries
Complete code
#include <MKRWAN.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
LoRaModem modem;
Adafruit_BME280 bme;
void setup {
Serial.begin(115200);
if (!bme.begin(0x76)) { Serial.println("BME280 absent"); while (1) {} }
if (!modem.begin(EU868)) while (1) {}
if (!modem.joinOTAA("0080000000000001",
"00112233445566778899AABBCCDDEEFF")) while (1) {}
modem.minPollInterval(60);
}
void loop {
modem.beginPacket;
int16_t t = (int16_t)(bme.readTemperature * 100); // 0,01 degC
uint32_t p = (uint32_t)bme.readPressure; // Pa
modem.write((uint8_t *)&t, 2);
modem.write((uint8_t *)&p, 4);
if (modem.endPacket(true) > 0) { // confirme : attend ACK
Serial.print("OK rssi="); Serial.print(modem.readRSSI);
Serial.print(" snr="); Serial.println(modem.readSNR);
} else {
Serial.println("Envoi echoue : duty cycle ou pas de gateway");
}
delay(120000); // throttle firmware : 1 msg / 2 min max
}
Expected verification
The server (TTN) decodes the 6 bytes (temperature int16, pressure uint32); the console displays RSSI/SNR after each ACK. One frame every 2 minutes maximum.
Common errors
- Verbose text payload: the LoRaWAN payload is tiny, send binary bytes.
- Sending too frequently: the firmware enforces 1 message every 2 minutes (European regulation), not modifiable.
- Forgetting the server-side decoder: raw bytes arrive without a schema.
Other tutorials for this board
Code examples for this board
emission-p2p.inoArduinoreception-p2p.inoArduinoreglage-sp.inoArduinometeo-lora.inoArduinotrame-crc.inoArduinorelais-radio.inoArduino