🛠 Commander une LED par downlink LoRaWAN
Objectives
Equivalent of the "LED web server": control the LED remotely. Arduino documents an LED remote control system between two MKR WAN boards for this card. Here, the board receives a LoRaWAN downlink ("ON"/"OFF") from the network and turns the LED on or off — the network plays the role of the browser.
Hardware
- 1 Arduino MKR WAN 1300 (successful OTAA join)
- 1 external LED + 220 Ω resistor (pin 6) or the onboard LED
- Access to the network server to send a downlink
Complete code
#include <MKRWAN.h>
LoRaModem modem;
const uint8_t LED = 6; // >
void setup {
Serial.begin(115200);
pinMode(LED, OUTPUT);
if (!modem.begin(EU868)) while (1) {}
if (!modem.joinOTAA("0080000000000001",
"00112233445566778899AABBCCDDEEFF")) while (1) {}
modem.minPollInterval(60);
}
void loop {
if (modem.available) {
String cmd = "";
while (modem.available) cmd += (char)modem.read;
cmd.trim; cmd.toUpperCase;
if (cmd == "ON") { digitalWrite(LED, HIGH); Serial.println("LED ON"); }
if (cmd == "OFF") { digitalWrite(LED, LOW); Serial.println("LED OFF"); }
}
// Une uplink periodique reveille les fenetres de reception (classe A)
static uint32_t t0 = 0;
if (millis - t0 > 120000) {
t0 = millis;
modem.beginPacket; modem.print("hb"); modem.endPacket(false);
}
delay(100);
}
Expected verification
From the server, send the downlink "ON": the LED turns on at the next exchange (Class A: the downlink only arrives in the windows following an uplink). "OFF" turns it off.
Common errors
- Downlink sent outside the window: in Class A, you must wait for the next uplink; increase the frequency during tests.
- Case- and space-sensitive comparison: use trim + toUpperCase before comparing.
- Confusing the onboard LED (D6) with an external LED on a pin.
Other tutorials for this board
Code examples for this board
emission-p2p.inoArduinoreception-p2p.inoArduinoreglage-sp.inoArduinometeo-lora.inoArduinotrame-crc.inoArduinorelais-radio.inoArduino