join-otaa.ino
The MKR WAN 1310 first LoRaWAN connection: provisioning the three identifiers (Device EUI, App EUI, App Key), then OTAA join with eight attempts over 30 seconds. The server validates the device and the encryption session is set — the prerequisite for everything else.
Hardware
An Arduino MKR WAN 1310 (SAMD21 microcontroller, LoRa modem driven by an onboard ESP32 that also exposes Wi-Fi, LoRa antenna and Wi-Fi antenna). MKR WAN 1310, 868 MHz antenna, a covering LoRaWAN gateway, a server account (TTN, ChirpStack, LaCocotte).
Wiring
No wiring; note the App Key generated by the server, it will not be shown again.
How it works
join(OTAA,...) sends a Join request over EU channels, waits for Accept on RX1/RX2 and installs the NwkSKey/AppSKey session keys. lora.run() spins the MAC state machine in the loop.
Running it
Install Arduino SAMD Boards and the MKRWAN 2.x library (MKRWAN1310), select the board, upload. The 1310 is driven through AT commands over its internal ESP32; for LoRa, plan a second LoRa board on the other end.
Variants
Also test MODEL_TYPE_C for an always-on node, or enable confirmed mode right after join to validate the full RX window.
join-otaa.ino
// Jointure OTAA de la MKR WAN 1310 (bibliothèque MKRWiSOLAR).
// Renseignez les cles provisionnees sur votre serveur (TTN, ChirpStack).
#include <MKRWiSOLAR.h>
const uint8_t DEVICE_EUI[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED,0xC0,0xFF};
const uint8_t APP_EUI[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
const uint8_t APP_KEY[16] = {0}; // a provisionner
LoRaWAN lora;
void setup() {
Serial.begin(9600);
while (!Serial);
lora.setDeviceEui(DEVICE_EUI);
lora.setAppEui(APP_EUI);
lora.setAppKey(APP_KEY);
lora.setRegion(EU_863_870_R1);
// join(OTAA, MODEL_TYPE_A, timeout ms, tentatives max)
if (lora.join(OTAA, MODEL_TYPE_A, 30000, 8) == 0) {
Serial.println(F("JOIN refuse"));
while (true);
}
Serial.println(F("JOIN accepte !"));
Serial.print(F("Adresse : 0x"));
Serial.println(lora.getDevAddr(), HEX);
}
void loop() {
lora.run(); // traite la couche MAC (retransmissions, downlinks)
}