🛠 Commander une LED par downlink LoRaWAN

Arduino MKR WAN 1300 · intermediaire · Schéma : IMG-mkr-wan-1300-intermediaire-3-mkrwan-downlink-led.svg — LED pilotée par downlink LoRaWAN depuis le serveur réseau

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials