suivi-adr.ino
ADR is LoRaWAN's automatic energy/range tuning: enabled on the MKR WAN 1310, the server progressively lowers SF and power when the link is good. This sketch prints the current data rate at each send to watch convergence to the optimum.
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). Joined MKR WAN 1310, server with ADR active (default on TTN/ChirpStack).
Wiring
No wiring; move the board away from the gateway to watch the SF climb back in real time.
How it works
enableADR(true) allows server ADRLink commands; getDataRate() returns the installed DR. A nearby node converges to SF7/DR5 and divides its airtime by five.
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
Disable ADR (enableADR(false)) and pin the SF if your server does not handle ADRLink, or log the DR to audit coverage.
suivi-adr.ino
// Suivi ADR : la passerelle pilote SF/TX power, on l'affiche a bord.
#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;
unsigned long dernier = 0;
void setup() {
Serial.begin(9600);
lora.setDeviceEui(DEVICE_EUI);
lora.setAppEui(APP_EUI);
lora.setAppKey(APP_KEY);
lora.setRegion(EU_863_870_R1);
lora.enableADR(true); // laisse le serveur ajuster data rate + puissance
if (lora.join(OTAA, MODEL_TYPE_A, 30000, 8) == 0) while (true);
Serial.println(F("millis;upframes;sf_courant"));
}
void loop() {
lora.run();
if (millis() - dernier < 120000) return; // 2 min
dernier = millis();
uint8_t t[1] = { (uint8_t)(analogRead(A1) >> 2) };
lora.sendBuffer(t, 1, 3, 0);
// Le data rate courant est demande a la pile MAC
Serial.print(millis());
Serial.print(';');
Serial.print(lora.getUpFrameLinkCount());
Serial.print(';');
Serial.println(lora.getDataRate());
}