forked from sebromero/arduino-lorawan-intro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoRaWAN-Downlink.ino
78 lines (60 loc) · 1.92 KB
/
LoRaWAN-Downlink.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <MKRWAN.h>
#include <Arduino_MKRENV.h>
#include "arduino_secrets.h"
LoRaModem modem;
void setup() {
Serial.begin(115200);
while (!Serial);
ENV.begin();
pinMode(LED_BUILTIN, OUTPUT);
// change this to your regional band (eg. US915, AS923, ...)
if (!modem.begin(EU868)) {
Serial.println("Failed to start module");
while (1) {}
};
Serial.print("Your module version is: ");
Serial.println(modem.version());
if (modem.version() != ARDUINO_FW_VERSION) {
Serial.println("Please make sure that the latest modem firmware is installed.");
Serial.println("To update the firmware upload the 'MKRWANFWUpdate_standalone.ino' sketch.");
}
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
Serial.println("Connecting...");
int connected = modem.joinOTAA(APP_EUI, APP_KEY);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
modem.minPollInterval(60); // Default is 300s
Serial.println("Waiting for messages...");
}
void loop() {
delay(60 * 1000); // Wait 60 secs before polling again
modem.poll();
// On The Things Stack the RX1 window is 5s which is the earliest moment
// to receive any downlink data. We add 1500ms to allow for the transmission
// of the data to complete.
delay(5000 + 1500);
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
char dataBuffer[64];
int i = 0;
while (modem.available()) {
dataBuffer[i++] = (char)modem.read();
}
Serial.print("Received: ");
for (unsigned int j = 0; j < i; j++) {
Serial.print(dataBuffer[j] >> 4, HEX);
Serial.print(dataBuffer[j] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
if(dataBuffer[0] == 1){
digitalWrite(LED_BUILTIN, HIGH);
} else if(dataBuffer[0] == 0) {
digitalWrite(LED_BUILTIN, LOW);
}
}