From d77823375a3c4329a1a1182fcf4787f008878810 Mon Sep 17 00:00:00 2001 From: jgromes Date: Tue, 9 Apr 2024 22:18:40 +0200 Subject: [PATCH] [LoRaWAN] Generate random numbers instead of digital/analog read (#1056) --- examples/LoRaWAN/LoRaWAN_ABP/LoRaWAN_ABP.ino | 22 ++++++------- .../LoRaWAN_Reference/LoRaWAN_Reference.ino | 32 +++++++++---------- .../LoRaWAN_Starter/LoRaWAN_Starter.ino | 20 ++++++------ 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/examples/LoRaWAN/LoRaWAN_ABP/LoRaWAN_ABP.ino b/examples/LoRaWAN/LoRaWAN_ABP/LoRaWAN_ABP.ino index 99c65225..4d12601e 100644 --- a/examples/LoRaWAN/LoRaWAN_ABP/LoRaWAN_ABP.ino +++ b/examples/LoRaWAN/LoRaWAN_ABP/LoRaWAN_ABP.ino @@ -33,34 +33,34 @@ void setup() { Serial.begin(115200); - while (!Serial); + while(!Serial); delay(5000); // Give time to switch to the serial monitor Serial.println(F("\nSetup ... ")); - Serial.println(F("Initalise the radio")); + Serial.println(F("Initialise the radio")); int state = radio.begin(); - debug(state != RADIOLIB_ERR_NONE, F("Initalise radio failed"), state, true); + debug(state != RADIOLIB_ERR_NONE, F("Initialise radio failed"), state, true); - Serial.println(F("Initalise LoRaWAN Network credentials")); + Serial.println(F("Initialise LoRaWAN Network credentials")); state = node.beginABP(devAddr, NwkSEncKey, AppSKey, NwkSKey, SNwkSIntKey, true); debug(state < RADIOLIB_ERR_NONE, F("Session setup failed"), state, true); Serial.println(F("Ready!\n")); } - void loop() { Serial.println(F("Sending uplink")); - // Read some inputs - uint8_t Digital1 = digitalRead(2); - uint16_t Analog1 = analogRead(3); + // This is the place to gather the sensor inputs + // Instead of reading any real sensor, we just generate some random numbers as example + uint8_t value1 = radio.random(100); + uint16_t value2 = radio.random(2000); // Build payload byte array uint8_t uplinkPayload[3]; - uplinkPayload[0] = Digital1; - uplinkPayload[1] = highByte(Analog1); // See notes for high/lowByte functions - uplinkPayload[2] = lowByte(Analog1); + uplinkPayload[0] = value1; + uplinkPayload[1] = highByte(value2); // See notes for high/lowByte functions + uplinkPayload[2] = lowByte(value2); // Perform an uplink int state = node.sendReceive(uplinkPayload, sizeof(uplinkPayload)); diff --git a/examples/LoRaWAN/LoRaWAN_Reference/LoRaWAN_Reference.ino b/examples/LoRaWAN/LoRaWAN_Reference/LoRaWAN_Reference.ino index 608fb82f..e7253f2c 100644 --- a/examples/LoRaWAN/LoRaWAN_Reference/LoRaWAN_Reference.ino +++ b/examples/LoRaWAN/LoRaWAN_Reference/LoRaWAN_Reference.ino @@ -33,18 +33,17 @@ // include the library #include - void setup() { Serial.begin(115200); - while (!Serial); // Wait for serial to be initalised + while(!Serial); // Wait for serial to be initialised delay(5000); // Give time to switch to the serial monitor Serial.println(F("\nSetup")); int16_t state = 0; // return value for calls to RadioLib - Serial.println(F("Initalise the radio")); + Serial.println(F("Initialise the radio")); state = radio.begin(); - debug(state != RADIOLIB_ERR_NONE, F("Initalise radio failed"), state, true); + debug(state != RADIOLIB_ERR_NONE, F("Initialise radio failed"), state, true); // Override the default join rate // uint8_t joinDR = 3; @@ -70,8 +69,7 @@ void setup() { node.setDwellTime(true, 400); Serial.println(F("Ready!\n")); -} // setup - +} void loop() { int state = RADIOLIB_ERR_NONE; @@ -85,19 +83,19 @@ void loop() { uint8_t battLevel = 146; node.setDeviceStatus(battLevel); - - // Read some inputs - uint8_t Digital1 = digitalRead(2); - uint16_t Analog1 = analogRead(3); + // This is the place to gather the sensor inputs + // Instead of reading any real sensor, we just generate some random numbers as example + uint8_t value1 = radio.random(100); + uint16_t value2 = radio.random(2000); // Build payload byte array uint8_t uplinkPayload[3]; - uplinkPayload[0] = Digital1; - uplinkPayload[1] = highByte(Analog1); // See notes for high/lowByte functions - uplinkPayload[2] = lowByte(Analog1); + uplinkPayload[0] = value1; + uplinkPayload[1] = highByte(value2); // See notes for high/lowByte functions + uplinkPayload[2] = lowByte(value2); uint8_t downlinkPayload[10]; // Make sure this fits your plans! - size_t downlinkSize; // To hold the actual payload size rec'd + size_t downlinkSize; // To hold the actual payload size received // you can also retrieve additional information about an uplink or // downlink by passing a reference to LoRaWANEvent_t structure @@ -108,6 +106,7 @@ void loop() { // Retrieve the last uplink frame counter uint32_t fcntUp = node.getFcntUp(); + // Send a confirmed uplink every 64th frame // and also request the LinkCheck and DeviceTime MAC commands if(fcntUp % 64 == 0) { @@ -123,7 +122,7 @@ void loop() { // Check if downlink was received if(state != RADIOLIB_LORAWAN_NO_DOWNLINK) { // Did we get a downlink with data for us - if (downlinkSize > 0) { + if(downlinkSize > 0) { Serial.println(F("Downlink data: ")); arrayDump(downlinkPayload, downlinkSize); } else { @@ -194,5 +193,4 @@ void loop() { Serial.println(F("s")); delay(delayMs); - -} // loop +} diff --git a/examples/LoRaWAN/LoRaWAN_Starter/LoRaWAN_Starter.ino b/examples/LoRaWAN/LoRaWAN_Starter/LoRaWAN_Starter.ino index 0914c1c0..c9b32eb3 100644 --- a/examples/LoRaWAN/LoRaWAN_Starter/LoRaWAN_Starter.ino +++ b/examples/LoRaWAN/LoRaWAN_Starter/LoRaWAN_Starter.ino @@ -26,13 +26,13 @@ void setup() { Serial.begin(115200); - while (!Serial); + while(!Serial); delay(5000); // Give time to switch to the serial monitor Serial.println(F("\nSetup ... ")); - Serial.println(F("Initalise the radio")); + Serial.println(F("Initialise the radio")); int state = radio.begin(); - debug(state != RADIOLIB_ERR_NONE, F("Initalise radio failed"), state, true); + debug(state != RADIOLIB_ERR_NONE, F("Initialise radio failed"), state, true); Serial.println(F("Join ('login') to the LoRaWAN Network")); state = node.beginOTAA(joinEUI, devEUI, nwkKey, appKey, true); @@ -41,19 +41,19 @@ void setup() { Serial.println(F("Ready!\n")); } - void loop() { Serial.println(F("Sending uplink")); - // Read some inputs - uint8_t Digital1 = digitalRead(2); - uint16_t Analog1 = analogRead(3); + // This is the place to gather the sensor inputs + // Instead of reading any real sensor, we just generate some random numbers as example + uint8_t value1 = radio.random(100); + uint16_t value2 = radio.random(2000); // Build payload byte array uint8_t uplinkPayload[3]; - uplinkPayload[0] = Digital1; - uplinkPayload[1] = highByte(Analog1); // See notes for high/lowByte functions - uplinkPayload[2] = lowByte(Analog1); + uplinkPayload[0] = value1; + uplinkPayload[1] = highByte(value2); // See notes for high/lowByte functions + uplinkPayload[2] = lowByte(value2); // Perform an uplink int state = node.sendReceive(uplinkPayload, sizeof(uplinkPayload));