uplink-confirme.ino
Reliable LoRaWAN transmission: the MKR WAN 1310 sends two sensor bytes on port 2 in confirmed mode and waits for the server MAC acknowledgement. On failure the frame is simply retried next cycle — the simple strategy that avoids queues.
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 (see join-otaa example), analog sensor on A1, active LoRaWAN server.
Wiring
Sensor on A1/GND/3.3 V; nothing else, the board handles the radio itself.
How it works
sendBuffer(..., confirmed=1) schedules an RX2-window retransmission if the server does not acknowledge. The frame counter increments on every send, protecting against replays.
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
Store the frame in non-volatile RAM to replay after a reboot, or switch to unconfirmed for loss-tolerant readings.
uplink-confirme.ino
// Uplink confirme + suivi du compteur de frame sur MKR WAN 1310.
#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;
uint8_t trame[2];
void setup() {
Serial.begin(9600);
lora.setDeviceEui(DEVICE_EUI);
lora.setAppEui(APP_EUI);
lora.setAppKey(APP_KEY);
lora.setRegion(EU_863_870_R1);
if (lora.join(OTAA, MODEL_TYPE_A, 30000, 8) == 0) while (true);
Serial.println(F("JOIN OK"));
}
void loop() {
lora.run();
if (millis() - dernier < 300000) return; // 5 min
dernier = millis();
int brut = analogRead(A1);
trame[0] = brut >> 8;
trame[1] = brut & 0xFF;
// sendBuffer(donnees, taille, port, confirme)
if (lora.sendBuffer(trame, 2, 2, 1))
Serial.println(F("ACK recu du serveur"));
else
Serial.println(F("pas d'ACK (reessai au prochain cycle)"));
}