MKRWAN Examples

Examples for the MKRWAN library, which is used to communicate with the LoRa module onboard the MKR WAN 1300/1310 boards.

These examples will show you how to set up your board to use the LoRa network and how to send and receive data from the LoRa network.

Hardware Required

Circuit

Connect the antenna to the board.
Connect the antenna to the board.

Examples

MKR WAN First Configuration

This example for a MKR WAN 1300 allows you to setup your board to use the LoRa network.

1/*
2
3 First Configuration
4
5 This sketch demonstrates the usage of MKR WAN 1300/1310 LoRa module.
6
7 This example code is in the public domain.
8
9*/
10
11#include <MKRWAN.h>
12
13LoRaModem modem;
14
15// Uncomment if using the Murata chip as a module
16// LoRaModem modem(Serial1);
17
18String appEui;
19
20String appKey;
21
22String devAddr;
23
24String nwkSKey;
25
26String appSKey;
27
28void setup() {
29
30 // put your setup code here, to run once:
31
32 Serial.begin(115200);
33
34 while (!Serial);
35
36 Serial.println("Welcome to MKRWAN1300/1310 first configuration sketch");
37
38 Serial.println("Register to your favourite LoRa network and we are ready to go!");
39
40 // change this to your regional band (eg. US915, AS923, ...)
41
42 if (!modem.begin(EU868)) {
43
44 Serial.println("Failed to start module");
45
46 while (1) {}
47
48 };
49
50 Serial.print("Your module version is: ");
51
52 Serial.println(modem.version());
53
54 Serial.print("Your device EUI is: ");
55
56 Serial.println(modem.deviceEUI());
57
58 int mode = 0;
59
60 while (mode != 1 && mode != 2) {
61
62 Serial.println("Are you connecting via OTAA (1) or ABP (2)?");
63
64 while (!Serial.available());
65
66 mode = Serial.readStringUntil('\n').toInt();
67
68 }
69
70 int connected;
71
72 if (mode == 1) {
73
74 Serial.println("Enter your APP EUI");
75
76 while (!Serial.available());
77
78 appEui = Serial.readStringUntil('\n');
79
80 Serial.println("Enter your APP KEY");
81
82 while (!Serial.available());
83
84 appKey = Serial.readStringUntil('\n');
85
86 appKey.trim();
87
88 appEui.trim();
89
90 connected = modem.joinOTAA(appEui, appKey);
91
92 } else if (mode == 2) {
93
94 Serial.println("Enter your Device Address");
95
96 while (!Serial.available());
97
98 devAddr = Serial.readStringUntil('\n');
99
100 Serial.println("Enter your NWS KEY");
101
102 while (!Serial.available());
103
104 nwkSKey = Serial.readStringUntil('\n');
105
106 Serial.println("Enter your APP SKEY");
107
108 while (!Serial.available());
109
110 appSKey = Serial.readStringUntil('\n');
111
112 devAddr.trim();
113
114 nwkSKey.trim();
115
116 appSKey.trim();
117
118 connected = modem.joinABP(devAddr, nwkSKey, appSKey);
119
120 }
121
122 if (!connected) {
123
124 Serial.println("Something went wrong; are you indoor? Move near a window and retry");
125
126 while (1) {}
127
128 }
129
130 delay(5000);
131
132 int err;
133
134 modem.setPort(3);
135
136 modem.beginPacket();
137
138 modem.print("HeLoRA world!");
139
140 err = modem.endPacket(true);
141
142 if (err > 0) {
143
144 Serial.println("Message sent correctly!");
145
146 } else {
147
148 Serial.println("Error sending message :(");
149
150 }
151}
152
153void loop() {
154
155 while (modem.available()) {
156
157 Serial.write(modem.read());
158
159 }
160
161 modem.poll();
162}

MKR WAN Lora Send and Receive

This sketch demonstrates how to send and receive data with the MKR WAN 1300 LoRa module.

1/*
2
3 Lora Send And Receive
4
5 This sketch demonstrates how to send and receive data with the MKR WAN 1300/1310 LoRa module.
6
7 This example code is in the public domain.
8
9*/
10
11#include <MKRWAN.h>
12
13LoRaModem modem;
14
15// Uncomment if using the Murata chip as a module
16// LoRaModem modem(Serial1);
17
18#include "arduino_secrets.h"
19// Please enter your sensitive data in the Secret tab or arduino_secrets.h
20
21String appEui = SECRET_APP_EUI;
22
23String appKey = SECRET_APP_KEY;
24
25void setup() {
26
27 // put your setup code here, to run once:
28
29 Serial.begin(115200);
30
31 while (!Serial);
32
33 // change this to your regional band (eg. US915, AS923, ...)
34
35 if (!modem.begin(EU868)) {
36
37 Serial.println("Failed to start module");
38
39 while (1) {}
40
41 };
42
43 Serial.print("Your module version is: ");
44
45 Serial.println(modem.version());
46
47 Serial.print("Your device EUI is: ");
48
49 Serial.println(modem.deviceEUI());
50
51 int connected = modem.joinOTAA(appEui, appKey);
52
53 if (!connected) {
54
55 Serial.println("Something went wrong; are you indoor? Move near a window and retry");
56
57 while (1) {}
58
59 }
60
61 // Set poll interval to 60 secs.
62
63 modem.minPollInterval(60);
64
65 // NOTE: independently by this setting the modem will
66
67 // not allow to send more than one message every 2 minutes,
68
69 // this is enforced by firmware and can not be changed.
70}
71
72void loop() {
73
74 Serial.println();
75
76 Serial.println("Enter a message to send to network");
77
78 Serial.println("(make sure that end-of-line 'NL' is enabled)");
79
80 while (!Serial.available());
81
82 String msg = Serial.readStringUntil('\n');
83
84 Serial.println();
85
86 Serial.print("Sending: " + msg + " - ");
87
88 for (unsigned int i = 0; i < msg.length(); i++) {
89
90 Serial.print(msg[i] >> 4, HEX);
91
92 Serial.print(msg[i] & 0xF, HEX);
93
94 Serial.print(" ");
95
96 }
97
98 Serial.println();
99
100 int err;
101
102 modem.beginPacket();
103
104 modem.print(msg);
105
106 err = modem.endPacket(true);
107
108 if (err > 0) {
109
110 Serial.println("Message sent correctly!");
111
112 } else {
113
114 Serial.println("Error sending message :(");
115
116 Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
117
118 Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
119
120 }
121
122 delay(1000);
123
124 if (!modem.available()) {
125
126 Serial.println("No downlink message received at this time.");
127
128 return;
129
130 }
131
132 char rcv[64];
133
134 int i = 0;
135
136 while (modem.available()) {
137
138 rcv[i++] = (char)modem.read();
139
140 }
141
142 Serial.print("Received: ");
143
144 for (unsigned int j = 0; j < i; j++) {
145
146 Serial.print(rcv[j] >> 4, HEX);
147
148 Serial.print(rcv[j] & 0xF, HEX);
149
150 Serial.print(" ");
151
152 }
153
154 Serial.println();
155}

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.