Initializes the MKRWAN library and module
modem.begin();
modem.begin(band);
Band: the chosen band. Can be one between:
- AS923
- AU915
- EU868 (default value)
- KR920
- IN865
- US915
- US915_HYBRID
true if correctly configured, false otherwise
/*
Lora Send And Receive
This sketch demonstrates how to send and receive data with the MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
*/
#include <MKRWAN.h>
LoRaModem modem;
// Uncomment if using the Murata chip as a module
// LoRaModem modem(Serial1);
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
// 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());
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
int connected = modem.joinOTAA(appEui, appKey);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
// Set poll interval to 60 secs.
modem.minPollInterval(60);
// NOTE: independent of this setting, the modem will
// not allow sending more than one message every 2 minutes,
// this is enforced by firmware and can not be changed.
}
void loop() {
Serial.println();
Serial.println("Enter a message to send to network");
Serial.println("(make sure that end-of-line 'NL' is enabled)");
while (!Serial.available());
String msg = Serial.readStringUntil('###');
Serial.println();
Serial.print("Sending: " + msg + " - ");
for (unsigned int i = 0; i < msg.length(); i++) {
Serial.print(msg[i] >> 4, HEX);
Serial.print(msg[i] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
int err;
modem.beginPacket();
modem.print(msg);
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
}
delay(1000);
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
char rcv[64];
int i = 0;
while (modem.available()) {
rcv[i++] = (char)modem.read();
}
Serial.print("Received: ");
for (unsigned int j = 0; j < i; j++) {
Serial.print(rcv[j] >> 4, HEX);
Serial.print(rcv[j] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
}
Restarts the LoRaWAN module
modem.restart();
None
/*
Lora Send And Receive
This sketch demonstrates how to send and receive data with the MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
*/
#include <MKRWAN.h>
LoRaModem modem;
// Uncomment if using the Murata chip as a module
// LoRaModem modem(Serial1);
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
// 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());
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
int connected = modem.joinOTAA(appEui, appKey);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
// Set poll interval to 60 secs.
modem.minPollInterval(60);
// NOTE: independent of this setting, the modem will
// not allow sending more than one message every 2 minutes,
// this is enforced by firmware and can not be changed.
}
void loop() {
Serial.println();
Serial.println("Enter a message to send to network");
Serial.println("(make sure that end-of-line 'NL' is enabled)");
while (!Serial.available());
String msg = Serial.readStringUntil('\n');
Serial.println();
Serial.print("Sending: " + msg + " - ");
for (unsigned int i = 0; i < msg.length(); i++) {
Serial.print(msg[i] >> 4, HEX);
Serial.print(msg[i] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
int err;
modem.beginPacket();
modem.print(msg);
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
}
delay(1000);
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
char rcv[64];
int i = 0;
while (modem.available()) {
rcv[i++] = (char)modem.read();
}
Serial.print("Received: ");
for (unsigned int j = 0; j < i; j++) {
Serial.print(rcv[j] >> 4, HEX);
Serial.print(rcv[j] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
}
Returns the MKRWAN firmware version
modem.version();
A string containing the firmware version
/*
First Configuration
This sketch demonstrates the usage of MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
*/
#include <MKRWAN.h>
LoRaModem modem;
// Uncomment if using the Murata chip as a module
// LoRaModem modem(Serial1);
String appEui;
String appKey;
String devAddr;
String nwkSKey;
String appSKey;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
Serial.println("Welcome to MKR WAN 1300/1310 first configuration sketch");
Serial.println("Register to your favourite LoRa network and we are ready to go!");
// 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());
int mode = 0;
while (mode != 1 && mode != 2) {
Serial.println("Are you connecting via OTAA (1) or ABP (2)?");
while (!Serial.available());
mode = Serial.readStringUntil('\n').toInt();
}
int connected;
if (mode == 1) {
Serial.println("Enter your APP EUI");
while (!Serial.available());
appEui = Serial.readStringUntil('\n');
Serial.println("Enter your APP KEY");
while (!Serial.available());
appKey = Serial.readStringUntil('\n');
appKey.trim();
appEui.trim();
connected = modem.joinOTAA(appEui, appKey);
} else if (mode == 2) {
Serial.println("Enter your Device Address");
while (!Serial.available());
devAddr = Serial.readStringUntil('\n');
Serial.println("Enter your NWS KEY");
while (!Serial.available());
nwkSKey = Serial.readStringUntil('\n');
Serial.println("Enter your APP SKEY");
while (!Serial.available());
appSKey = Serial.readStringUntil('\n');
devAddr.trim();
nwkSKey.trim();
appSKey.trim();
connected = modem.joinABP(devAddr, nwkSKey, appSKey);
}
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
delay(5000);
int err;
modem.setPort(3);
modem.beginPacket();
modem.print("HeLoRA world!");
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
}
}
void loop() {
while (modem.available()) {
Serial.write(modem.read());
}
modem.poll();
}
Returns the MKRWAN firmware version
modem.version();
A string containing the firmware version
/*
First Configuration
This sketch demonstrates the usage of MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
*/
#include <MKRWAN.h>
LoRaModem modem;
// Uncomment if using the Murata chip as a module
// LoRaModem modem(Serial1);
String appEui;
String appKey;
String devAddr;
String nwkSKey;
String appSKey;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
Serial.println("Welcome to MKR WAN 1300/1310 first configuration sketch");
Serial.println("Register to your favourite LoRa network and we are ready to go!");
// 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());
int mode = 0;
while (mode != 1 && mode != 2) {
Serial.println("Are you connecting via OTAA (1) or ABP (2)?");
while (!Serial.available());
mode = Serial.readStringUntil('\n').toInt();
}
int connected;
if (mode == 1) {
Serial.println("Enter your APP EUI");
while (!Serial.available());
appEui = Serial.readStringUntil('\n');
Serial.println("Enter your APP KEY");
while (!Serial.available());
appKey = Serial.readStringUntil('\n');
appKey.trim();
appEui.trim();
connected = modem.joinOTAA(appEui, appKey);
} else if (mode == 2) {
Serial.println("Enter your Device Address");
while (!Serial.available());
devAddr = Serial.readStringUntil('\n');
Serial.println("Enter your NWS KEY");
while (!Serial.available());
nwkSKey = Serial.readStringUntil('\n');
Serial.println("Enter your APP SKEY");
while (!Serial.available());
appSKey = Serial.readStringUntil('\n');
devAddr.trim();
nwkSKey.trim();
appSKey.trim();
connected = modem.joinABP(devAddr, nwkSKey, appSKey);
}
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
delay(5000);
int err;
modem.setPort(3);
modem.beginPacket();
modem.print("HeLoRA world!");
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
}
}
void loop() {
while (modem.available()) {
Serial.write(modem.read());
}
modem.poll();
}
Attempts to join to the LoRaWAN network using Over-The-Air-Activation (OTAA)
modem.joinOTAA(appEui, appKey)
modem.joinOTAA(appEui, appKey, devEui)
appEui: 64 bit application identifier, EUI-64 (unique) appKey: 128 bit application key devEui: 64 bit end-device identifier, EUI-64 (unique)
/*
First Configuration
This sketch demonstrates the usage of MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
*/
#include <MKRWAN.h>
LoRaModem modem;
// Uncomment if using the Murata chip as a module
// LoRaModem modem(Serial1);
String appEui;
String appKey;
String devAddr;
String nwkSKey;
String appSKey;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
Serial.println("Welcome to MKR WAN 1300/1310 first configuration sketch");
Serial.println("Register to your favourite LoRa network and we are ready to go!");
// 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());
int mode = 0;
while (mode != 1 && mode != 2) {
Serial.println("Are you connecting via OTAA (1) or ABP (2)?");
while (!Serial.available());
mode = Serial.readStringUntil('\n').toInt();
}
int connected;
if (mode == 1) {
Serial.println("Enter your APP EUI");
while (!Serial.available());
appEui = Serial.readStringUntil('\n');
Serial.println("Enter your APP KEY");
while (!Serial.available());
appKey = Serial.readStringUntil('\n');
appKey.trim();
appEui.trim();
connected = modem.joinOTAA(appEui, appKey);
} else if (mode == 2) {
Serial.println("Enter your Device Address");
while (!Serial.available());
devAddr = Serial.readStringUntil('\n');
Serial.println("Enter your NWS KEY");
while (!Serial.available());
nwkSKey = Serial.readStringUntil('\n');
Serial.println("Enter your APP SKEY");
while (!Serial.available());
appSKey = Serial.readStringUntil('\n');
devAddr.trim();
nwkSKey.trim();
appSKey.trim();
connected = modem.joinABP(devAddr, nwkSKey, appSKey);
}
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
delay(5000);
int err;
modem.setPort(3);
modem.beginPacket();
modem.print("HeLoRA world!");
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
}
}
void loop() {
while (modem.available()) {
Serial.write(modem.read());
}
modem.poll();
}
Attempts to join to the LoRaWAN network using Activating-By-Personalization (ABP)
modem.joinABP(devAddr, nwkSKey)
modem.joinABP(devAddr, nwkSKey, appSKey)
devAddr: 32 bit device address (non-unique) nwkSKey: 128 bit network session key appSKey: 128 bit application key
1 if successfully joined, 0 otherwise
/*
First Configuration
This sketch demonstrates the usage of MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
*/
#include <MKRWAN.h>
LoRaModem modem;
// Uncomment if using the Murata chip as a module
// LoRaModem modem(Serial1);
String appEui;
String appKey;
String devAddr;
String nwkSKey;
String appSKey;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
Serial.println("Welcome to MKR WAN 1300/1310 first configuration sketch");
Serial.println("Register to your favourite LoRa network and we are ready to go!");
// 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());
int mode = 0;
while (mode != 1 && mode != 2) {
Serial.println("Are you connecting via OTAA (1) or ABP (2)?");
while (!Serial.available());
mode = Serial.readStringUntil('\n').toInt();
}
int connected;
if (mode == 1) {
Serial.println("Enter your APP EUI");
while (!Serial.available());
appEui = Serial.readStringUntil('\n');
Serial.println("Enter your APP KEY");
while (!Serial.available());
appKey = Serial.readStringUntil('\n');
appKey.trim();
appEui.trim();
connected = modem.joinOTAA(appEui, appKey);
} else if (mode == 2) {
Serial.println("Enter your Device Address");
while (!Serial.available());
devAddr = Serial.readStringUntil('\n');
Serial.println("Enter your NWS KEY");
while (!Serial.available());
nwkSKey = Serial.readStringUntil('\n');
Serial.println("Enter your APP SKEY");
while (!Serial.available());
appSKey = Serial.readStringUntil('\n');
devAddr.trim();
nwkSKey.trim();
appSKey.trim();
connected = modem.joinABP(devAddr, nwkSKey, appSKey);
}
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
delay(5000);
int err;
modem.setPort(3);
modem.beginPacket();
modem.print("HeLoRA world!");
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
}
}
void loop() {
while (modem.available()) {
Serial.write(modem.read());
}
modem.poll();
}
Begins the process of sending a packet
modem.beginPacket();
None
/*
Lora Send And Receive
This sketch demonstrates how to send and receive data with the MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
*/
#include <MKRWAN.h>
LoRaModem modem;
// Uncomment if using the Murata chip as a module
// LoRaModem modem(Serial1);
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
// 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());
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
int connected = modem.joinOTAA(appEui, appKey);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
// Set poll interval to 60 secs.
modem.minPollInterval(60);
// NOTE: independent of this setting, the modem will
// not allow sending more than one message every 2 minutes,
// this is enforced by firmware and can not be changed.
}
void loop() {
Serial.println();
Serial.println("Enter a message to send to network");
Serial.println("(make sure that end-of-line 'NL' is enabled)");
while (!Serial.available());
String msg = Serial.readStringUntil('\n');
Serial.println();
Serial.print("Sending: " + msg + " - ");
for (unsigned int i = 0; i < msg.length(); i++) {
Serial.print(msg[i] >> 4, HEX);
Serial.print(msg[i] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
int err;
modem.beginPacket();
modem.print(msg);
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
}
delay(1000);
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
char rcv[64];
int i = 0;
while (modem.available()) {
rcv[i++] = (char)modem.read();
}
Serial.print("Received: ");
for (unsigned int j = 0; j < i; j++) {
Serial.print(rcv[j] >> 4, HEX);
Serial.print(rcv[j] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
}
Adds binary data to the packet to send over LoRaWAN. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.
modem.write(val)
modem.write(str)
modem.write(buf, len)
val: a value to send as a single byte
str: a string to send as a series of bytes
buf: an array to send as a series of bytes
len: the length of the buffer
Reads incoming LoRaWAN data. read() inherits from the Stream utility class.
modem.read()
None
the first byte of incoming LoRaWAN data available (or -1 if no data is available) - int
/*
Lora Send And Receive
This sketch demonstrates how to send and receive data with the MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
*/
#include <MKRWAN.h>
LoRaModem modem;
// Uncomment if using the Murata chip as a module
// LoRaModem modem(Serial1);
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
// 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());
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
int connected = modem.joinOTAA(appEui, appKey);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
// Set poll interval to 60 secs.
modem.minPollInterval(60);
// NOTE: independent of this setting, the modem will
// not allow sending more than one message every 2 minutes,
// this is enforced by firmware and can not be changed.
}
void loop() {
Serial.println();
Serial.println("Enter a message to send to network");
Serial.println("(make sure that end-of-line 'NL' is enabled)");
while (!Serial.available());
String msg = Serial.readStringUntil('\n');
Serial.println();
Serial.print("Sending: " + msg + " - ");
for (unsigned int i = 0; i < msg.length(); i++) {
Serial.print(msg[i] >> 4, HEX);
Serial.print(msg[i] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
int err;
modem.beginPacket();
modem.print(msg);
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
}
delay(1000);
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
char rcv[64];
int i = 0;
while (modem.available()) {
rcv[i++] = (char)modem.read();
}
Serial.print("Received: ");
for (unsigned int j = 0; j < i; j++) {
Serial.print(rcv[j] >> 4, HEX);
Serial.print(rcv[j] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
}
Add characters to the packet to be sent over LoRaWAN. This data is sent as a character or series of characters; to send the binary data use the write() function instead.
modem.print(val)
modem.print(str)
modem.print(buf, len)
val: a value to send characters representing the value
str: a string to send as a series of characters
buf: an array to send as a series of characters
len: the length of the buffer
/*
Lora Send And Receive
This sketch demonstrates how to send and receive data with the MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
*/
#include <MKRWAN.h>
LoRaModem modem;
// Uncomment if using the Murata chip as a module
// LoRaModem modem(Serial1);
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
// 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());
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
int connected = modem.joinOTAA(appEui, appKey);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
// Set poll interval to 60 secs.
modem.minPollInterval(60);
// NOTE: independent of this setting, the modem will
// not allow sending more than one message every 2 minutes,
// this is enforced by firmware and can not be changed.
}
void loop() {
Serial.println();
Serial.println("Enter a message to send to network");
Serial.println("(make sure that end-of-line 'NL' is enabled)");
while (!Serial.available());
String msg = Serial.readStringUntil('\n');
Serial.println();
Serial.print("Sending: " + msg + " - ");
for (unsigned int i = 0; i < msg.length(); i++) {
Serial.print(msg[i] >> 4, HEX);
Serial.print(msg[i] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
int err;
modem.beginPacket();
modem.print(msg);
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
}
delay(1000);
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
char rcv[64];
int i = 0;
while (modem.available()) {
rcv[i++] = (char)modem.read();
}
Serial.print("Received: ");
for (unsigned int j = 0; j < i; j++) {
Serial.print(rcv[j] >> 4, HEX);
Serial.print(rcv[j] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
}
Returns the next byte (character) of incoming serial data without removing it from the internal buffer. That is, successive calls to peek() will return the same character, as will the next call to read(). peek() inherits from the Stream utility class.
modem.peek()
None
the first byte of incoming serial data available (or -1 if no data is available) - int
Get the number of bytes (characters) available for reading. This is data that's already arrived and stored in a receive buffer. available() inherits from the Stream utility class.
modem.available()
none
the number of bytes available to read
/*
Lora Send And Receive
This sketch demonstrates how to send and receive data with the MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
*/
#include <MKRWAN.h>
LoRaModem modem;
// Uncomment if using the Murata chip as a module
// LoRaModem modem(Serial1);
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
// 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());
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
int connected = modem.joinOTAA(appEui, appKey);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
// Set poll interval to 60 secs.
modem.minPollInterval(60);
// NOTE: independent of this setting, the modem will
// not allow sending more than one message every 2 minutes,
// this is enforced by firmware and can not be changed.
}
void loop() {
Serial.println();
Serial.println("Enter a message to send to network");
Serial.println("(make sure that end-of-line 'NL' is enabled)");
while (!Serial.available());
String msg = Serial.readStringUntil('\n');
Serial.println();
Serial.print("Sending: " + msg + " - ");
for (unsigned int i = 0; i < msg.length(); i++) {
Serial.print(msg[i] >> 4, HEX);
Serial.print(msg[i] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
int err;
modem.beginPacket();
modem.print(msg);
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
}
delay(1000);
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
char rcv[64];
int i = 0;
while (modem.available()) {
rcv[i++] = (char)modem.read();
}
Serial.print("Received: ");
for (unsigned int j = 0; j < i; j++) {
Serial.print(rcv[j] >> 4, HEX);
Serial.print(rcv[j] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
}
Checks for the presence of a LoRaWAN packet, and reports the size. parsePacket() must be called before reading the buffer with modem.read().
modem.parsePacket()
None
int: the size of a received LoRaWAN packet
Completes the process of sending a LoRaWAN packet started by beginPacket.
modem.endPacket();
Returns an int: 1 if the packet was sent successfully, 0 if there was an error
/*
Lora Send And Receive
This sketch demonstrates how to send and receive data with the MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
*/
#include <MKRWAN.h>
LoRaModem modem;
// Uncomment if using the Murata chip as a module
// LoRaModem modem(Serial1);
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
// 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());
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
int connected = modem.joinOTAA(appEui, appKey);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
// Set poll interval to 60 secs.
modem.minPollInterval(60);
// NOTE: independent of this setting, the modem will
// not allow sending more than one message every 2 minutes,
// this is enforced by firmware and can not be changed.
}
void loop() {
Serial.println();
Serial.println("Enter a message to send to network");
Serial.println("(make sure that end-of-line 'NL' is enabled)");
while (!Serial.available());
String msg = Serial.readStringUntil('\n');
Serial.println();
Serial.print("Sending: " + msg + " - ");
for (unsigned int i = 0; i < msg.length(); i++) {
Serial.print(msg[i] >> 4, HEX);
Serial.print(msg[i] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
int err;
modem.beginPacket();
modem.print(msg);
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
}
delay(1000);
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
char rcv[64];
int i = 0;
while (modem.available()) {
rcv[i++] = (char)modem.read();
}
Serial.print("Received: ");
for (unsigned int j = 0; j < i; j++) {
Serial.print(rcv[j] >> 4, HEX);
Serial.print(rcv[j] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
}
Change the LoRaWAN module band
modem.configureBand(band); //e.g. EU868, US915, AS923
band: the chosen band. Can be one between:
- AS923
- AU915
- EU868 (default value)
- KR920
- IN865
- US915
- US915_HYBRID
true if correctly configured, false otherwise