Range of tweaks & dutycycle / FUP

This commit is contained in:
Nick McCloud 2024-02-28 20:46:47 +00:00
parent 5766d386af
commit 22045d7189

View file

@ -21,6 +21,9 @@
For full API reference, see the GitHub Pages For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/ https://jgromes.github.io/RadioLib/
For LoRaWAN details, see the wiki page
https://github.com/jgromes/RadioLib/wiki/LoRaWAN
*/ */
// include the library // include the library
@ -28,11 +31,11 @@
// SX1262 has the following pin order: // SX1262 has the following pin order:
// Module(NSS/CS, DIO1, RESET, BUSY) // Module(NSS/CS, DIO1, RESET, BUSY)
// SX1262 radio = new Module(8, 14, 12, 13); SX1262 radio = new Module(8, 14, 12, 13);
// SX1278 has the following pin order: // SX1278 has the following pin order:
// Module(NSS/CS, DIO0, RESET, DIO1) // Module(NSS/CS, DIO0, RESET, DIO1)
SX1278 radio = new Module(10, 2, 9, 3); // SX1278 radio = new Module(10, 2, 9, 3);
// create the node instance on the EU-868 band // create the node instance on the EU-868 band
// using the radio module and the encryption key // using the radio module and the encryption key
@ -44,9 +47,8 @@ LoRaWANNode node(&radio, &EU868);
// such as US915 and AU915, you must specify // such as US915 and AU915, you must specify
// the subband that matches the Frequency Plan // the subband that matches the Frequency Plan
// that you selected on your LoRaWAN console // that you selected on your LoRaWAN console
/* // LoRaWANNode node(&radio, &US915, 2);
LoRaWANNode node(&radio, &US915, 2);
*/
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
@ -62,48 +64,29 @@ void setup() {
while(true); while(true);
} }
// application identifier - pre-LoRaWAN 1.1.0, this was called appEUI // JoinEUI - previous versions of LoRaWAN called this AppEUI
// when adding new end device in TTN, you will have to enter this number // for development purposes you can use all zeros - see wiki for details
// you can pick any number you want, but it has to be unique uint64_t joinEUI = 0x0000000000000000;
uint64_t joinEUI = 0x12AD1011B0C0FFEE;
// device identifier - this number can be anything // DevEUI - The device's Extended Unique Identifier
// when adding new end device in TTN, you can generate this number, // TTN will generate one for you
// or you can set any value you want, provided it is also unique uint64_t devEUI = 0x----------------;
uint64_t devEUI = 0x70B3D57ED005E120;
// select some encryption keys which will be used to secure the communication // encryption keys used to secure the communication
// there are two of them - network key and application key // TTN will generate them for you
// because LoRaWAN uses AES-128, the key MUST be 16 bytes (or characters) long // see wiki for details on copying & pasting them
uint8_t nwkKey[] = { 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--,
// network key is the ASCII string "topSecretKey1234" 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x-- };
uint8_t nwkKey[] = { 0x74, 0x6F, 0x70, 0x53, 0x65, 0x63, 0x72, 0x65, uint8_t appKey[] = { 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--,
0x74, 0x4B, 0x65, 0x79, 0x31, 0x32, 0x33, 0x34 }; 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x--, 0x-- };
// application key is the ASCII string "aDifferentKeyABC"
uint8_t appKey[] = { 0x61, 0x44, 0x69, 0x66, 0x66, 0x65, 0x72, 0x65,
0x6E, 0x74, 0x4B, 0x65, 0x79, 0x41, 0x42, 0x43 };
// prior to LoRaWAN 1.1.0, only a single "nwkKey" is used
// when connecting to LoRaWAN 1.0 network, "appKey" will be disregarded
// and can be set to NULL
// on EEPROM-enabled boards, after the device has been activated, // Manages uplink intervals to the TTN Fair Use Policy
// the session can be restored without rejoining after device power cycle node.setDutyCycle(true, 1250);
// this is intrinsically done when calling `beginOTAA()` with the same keys
// in that case, the function will not need to transmit a JoinRequest
// now we can start the activation // Begin the join to the network
// this can take up to 10 seconds, and requires a LoRaWAN gateway in range
// a specific starting-datarate can be selected in dynamic bands (e.g. EU868):
/*
uint8_t joinDr = 4;
state = node.beginOTAA(joinEUI, devEUI, nwkKey, appKey, joinDr);
*/
Serial.print(F("[LoRaWAN] Attempting over-the-air activation ... ")); Serial.print(F("[LoRaWAN] Attempting over-the-air activation ... "));
state = node.beginOTAA(joinEUI, devEUI, nwkKey, appKey); state = node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
if(state >= RADIOLIB_ERR_NONE) { if(state >= RADIOLIB_ERR_NONE) {
Serial.println(F("success!")); Serial.println(F("success!"));
delay(2000); // small delay between joining and uplink delay(2000); // small delay between joining and uplink
@ -112,8 +95,7 @@ void setup() {
Serial.println(state); Serial.println(state);
while(true); while(true);
} }
} // setup
}
// counter to keep track of transmitted packets // counter to keep track of transmitted packets
int count = 0; int count = 0;
@ -121,7 +103,7 @@ int count = 0;
void loop() { void loop() {
// send uplink to port 10 // send uplink to port 10
Serial.print(F("[LoRaWAN] Sending uplink packet ... ")); Serial.print(F("[LoRaWAN] Sending uplink packet ... "));
String strUp = "Hello!" + String(count++); String strUp = "Hello! " + String(count++);
String strDown; String strDown;
int state = node.sendReceive(strUp, 10, strDown); int state = node.sendReceive(strUp, 10, strDown);
if(state == RADIOLIB_ERR_NONE) { if(state == RADIOLIB_ERR_NONE) {
@ -151,21 +133,25 @@ void loop() {
Serial.println(F(" Hz")); Serial.println(F(" Hz"));
} else if(state == RADIOLIB_ERR_RX_TIMEOUT) { } else if(state == RADIOLIB_ERR_RX_TIMEOUT) {
Serial.println(F("no downlink!")); Serial.println(F(""));
} else { } else {
Serial.print(F("failed, code ")); Serial.print(F("failed, code "));
Serial.println(state); Serial.println(state);
} }
// on EEPROM enabled boards, you should save the current session // on boards that can save to Flash or EEPROMthis saves the session
// by calling "saveSession" which allows retrieving the session after reboot or deepsleep // which allows recall of the session after reboot or deepsleep
node.saveSession(); node.saveSession();
// wait before sending another packet // wait before sending another packet
uint32_t minimumDelay = 60000; // try to send once every minute uint32_t minimumDelay = 300000; // try to send once every 3 minutes
uint32_t interval = node.timeUntilUplink(); // calculate minimum duty cycle delay (per law!) uint32_t interval = node.timeUntilUplink(); // calculate minimum duty cycle delay (per FUP & law!)
uint32_t delayMs = max(interval, minimumDelay); // cannot send faster than duty cycle allows uint32_t delayMs = max(interval, minimumDelay); // cannot send faster than duty cycle allows
Serial.print(F("[LoRaWAN] Next uplink in "));
Serial.print(delayMs/60);
Serial.println(F("s"));
delay(delayMs); delay(delayMs);
} } // loop