🛠 Rejoindre The Things Network (OTAA)

Arduino MKR WAN 1310 · intermediaire · Schéma : IMG-mkr-wan-1310-intermediaire-3-mkrwan1310-otaa-the-things-network.svg — MKR WAN 1310 envoyant un uplink LoRaWAN vers une antenne de passerelle puis le cloud The Things Network, clés OTAA représentées par des cadenas.

Objectives

Join a public LoRaWAN network (The Things Network) using OTAA with the MKRWAN library: register the device with its deviceEUI, send uplinks and receive downlinks, read RSSI/SNR. This is the primary purpose of the board — no Wi-Fi, no HTTP here, only LoRaWAN Class A.

Hardware

Full code

// OTAA The Things Network — MKR WAN 1310
#include <MKRWAN.h>

LoRaModem modem;
String appEui = "0000000000000000"; // EUI de l'application TTN
String appKey = "00000000000000000000000000000000"; // APP_KEY OTAA

void setup {
  Serial.begin(115200);
  while (!Serial);
  if (!modem.begin(EU868)) { Serial.println("Module KO"); while (1); }
  Serial.print("deviceEUI : "); Serial.println(modem.deviceEUI);
  if (!modem.joinOTAA(appEui, appKey)) {   // blocage jusqu'au join
    Serial.println("Join echoue : rapprochez-vous d'une fenetre");
    while (1) {}
  }
  Serial.println("Joint !");
  modem.minPollInterval(60);
}

void loop {
  while (!Serial.available);
  String msg = Serial.readStringUntil('\n');
  modem.beginPacket;
  modem.print(msg);
  int err = modem.endPacket(true);   // true = uplink confirmé (ACK)
  Serial.println(err > 0 ? "Envoye !" : "Echec (duty cycle ?)");
  if (modem.available) {           // downlink reçu en fenetre RX
    char recu[64]; int i = 0;
    while (modem.available && i < 63) recu[i++] = modem.read;
    recu[i] = '\0';
    Serial.printf("Downlink: %s\n", recu);
  }
  Serial.printf("RSSI=%d SNR=%d\n", modem.rssi, modem.snr);
  delay(120000); // le firmware impose 1 émission / 2 min max (EU)
}

Expected verification

"Joined!" appears after a few seconds to a few minutes; uplinks appear in the TTN live data with RSSI/SNR. A downlink scheduled on TTN returns in the RX window following the uplink.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials