🛠 Envoyer des données capteur vers un serveur (uplink)

Arduino MKR WAN 1300 · intermediaire · Schéma : IMG-mkr-wan-1300-intermediaire-2-mkrwan-uplink-lorawan.svg — capteur BME280 en I2C et uplink binaire vers le serveur réseau

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials