🛠 Rejoindre The Things Network (OTAA)
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
- 1 MKR WAN 1310 board + 868 MHz antenna
- A The Things Network account (or TTN Stack) and a created application
- Reference example: MKRWAN > LoraSendAndReceive
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
joinOTAAfailing even when everything is correct: known bug with false errors (MKRWAN issue #139) — retry, place the board near a window, verify that a TTN gateway covers the area.- Sending more than one message every 2 minutes: the firmware imposes this duty cycle floor, which cannot be changed; transmissions are rejected.
- Inverted keys (APP_KEY in appEui) or in text format instead of hexadecimal.
- ABP: regression reported since MKRWAN 1.0.15 (issue #85) — prefer OTAA.
- After ~255 transmissions, TX blocking (frame counter bug, issue #141): see the advanced tutorial on workarounds.
Other tutorials for this board
Code examples for this board
join-otaa.inoArduinouplink-confirme.inoArduinodownlink.inoArduinoactivation-abp.inoArduinosuivi-adr.inoArduinoclasse-c.inoArduino