Skip to content

Instantly share code, notes, and snippets.

@jancumps
Created January 13, 2024 16:10
Show Gist options
  • Save jancumps/354389fc667cf03da5ea85a3d7c55ea7 to your computer and use it in GitHub Desktop.
Save jancumps/354389fc667cf03da5ea85a3d7c55ea7 to your computer and use it in GitHub Desktop.
The Things Network (TTN) replacement example. Alternative for the no longer available LoRa LED/ON off basic sketch for Arduino MKR WAN 1310.
/*
Lora_LED_ON_OFF: The Things Network testsketch that integrates with their payload formatters and tutorials
Altrnative for the no longer available LoRa LED/ON off basic sketch:
Please refer to https://create.arduino.cc/editor/FT-CONTENT/043f42fb-2b04-4cfb-a277-b1a3dd5366c2/preview
Jan Cumps 15-03-2024
With code from:
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:
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 0);
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.
}
// #define WAITBEFOREAUTOSEND (6 * 60 * 2) // every 2 hours
#define WAITBEFOREAUTOSEND (6 * 10 * 2) // every 20 minutes
void loop() {
static int previousLedState = 0;
// enfore a communication every so many minutes, to check for downlinks
static int loopcounter = 0;
if (loopcounter % (WAITBEFOREAUTOSEND) == 0) {
loopcounter = 0;
}
int err;
char state;
if ((digitalRead(LED_BUILTIN) != previousLedState) || (loopcounter == 0) ) {
previousLedState = digitalRead(LED_BUILTIN);
state = previousLedState ? 0x01 : 0x00;
modem.beginPacket();
modem.print(state);
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)");
}
}
loopcounter ++;
delay(10000);
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: ");
switch (rcv[0]) {
case 0x00:
Serial.print("led off");
digitalWrite(LED_BUILTIN, 0);
break;
case 0x01:
Serial.print("led on");
digitalWrite(LED_BUILTIN, 1);
break;
default:
Serial.print("unexpected payload");
Serial.print(rcv[0] + 32);
break;
}
Serial.println();
}
@jancumps
Copy link
Author

The TTN LoRa LED/ON off basic sketch for Arduino MKR WAN 1310 is no longer available on create.arduino.cc .
You may find a pointer to that sketch in the payload formatter, when you create an Arduino SO thing on The Things Network (TTN).

This example can be used as a replacement. Read more on the element14 blog post Arduino MKR WAN 1310 on The Things Network (TTN).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment