passerelle-usb.ino

Arduino MKR WAN 1300 · File : passerelle-usb.ino · Language : Arduino · Use case : Relayer les paquets LoRa reçus vers le port USB

The single gateway for the station network: this MKR WAN 1300 bridges the LoRa air and the USB port, replaying each frame with RSSI, SNR and timestamp. Plugged into a Raspberry Pi or laptop, it becomes the gateway of a complete private network.

Hardware

An Arduino MKR WAN 1300 (SAMD21 microcontroller, Semtech SX1272 LoRa modem for point-to-point links, no built-in LoRaWAN stack, 868/915 MHz antenna). MKR WAN 1300, 868 MHz antenna, USB cable to a computer or SBC.

Wiring

No wiring; place the gateway high up (attic, mast) to maximise range for all stations.

How it works

The loop polls the radio FIFO and formats each packet as semicolon CSV, readable by a host script. A radio error (-1) triggers a full SX1272 reset.

Running it

Install Arduino SAMD Boards and the MKRWAN library, select the board, upload. You need TWO LoRa boards (two MKR WAN 1300s, or a 1300 and a 1310) to see exchanges; set the same frequency plan in the code.

Variants

Add a whitelist of station ids, or forward frames to MQTT/UDP from the computer to centralise them remotely.

passerelle-usb.ino

// Passerelle : toute trame LoRa recue est rejouee sur Serial USB.
// Format : horodatage carte, RSSI, SNR, charge utile.
#include <MKRWAN.h>

LoRaModem modem;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  if (!modem.begin(EU868)) {
    Serial.println(F("modem KO"));
    while (true);
  }
  modem.setFrequency(868.5);
  modem.setSpreadingFactor(9); // comme les stations
  Serial.println(F("millis;rssi;snr;trame"));
}

void loop() {
  int n = modem.available();
  if (n > 0) {
    String trame = "";
    while (modem.available()) trame += (char)modem.read();
    Serial.print(millis());
    Serial.print(';');
    Serial.print(modem.readRssi());
    Serial.print(';');
    Serial.print(modem.readSnr());
    Serial.print(';');
    Serial.println(trame);
  }
  if (n < 0) { // erreur radio : remise a zero du modem
    Serial.println(F(";;;ERREUR_RADIO"));
    modem.begin(EU868);
    modem.setFrequency(868.5);
    modem.setSpreadingFactor(9);
  }
}

← Back to the board