[LoRaWAN] Generate random numbers instead of digital/analog read (#1056)

This commit is contained in:
jgromes 2024-04-09 22:18:40 +02:00
parent c9d8c601df
commit d77823375a
3 changed files with 36 additions and 38 deletions

View file

@ -33,34 +33,34 @@
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
while (!Serial); while(!Serial);
delay(5000); // Give time to switch to the serial monitor delay(5000); // Give time to switch to the serial monitor
Serial.println(F("\nSetup ... ")); Serial.println(F("\nSetup ... "));
Serial.println(F("Initalise the radio")); Serial.println(F("Initialise the radio"));
int state = radio.begin(); 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); state = node.beginABP(devAddr, NwkSEncKey, AppSKey, NwkSKey, SNwkSIntKey, true);
debug(state < RADIOLIB_ERR_NONE, F("Session setup failed"), state, true); debug(state < RADIOLIB_ERR_NONE, F("Session setup failed"), state, true);
Serial.println(F("Ready!\n")); Serial.println(F("Ready!\n"));
} }
void loop() { void loop() {
Serial.println(F("Sending uplink")); Serial.println(F("Sending uplink"));
// Read some inputs // This is the place to gather the sensor inputs
uint8_t Digital1 = digitalRead(2); // Instead of reading any real sensor, we just generate some random numbers as example
uint16_t Analog1 = analogRead(3); uint8_t value1 = radio.random(100);
uint16_t value2 = radio.random(2000);
// Build payload byte array // Build payload byte array
uint8_t uplinkPayload[3]; uint8_t uplinkPayload[3];
uplinkPayload[0] = Digital1; uplinkPayload[0] = value1;
uplinkPayload[1] = highByte(Analog1); // See notes for high/lowByte functions uplinkPayload[1] = highByte(value2); // See notes for high/lowByte functions
uplinkPayload[2] = lowByte(Analog1); uplinkPayload[2] = lowByte(value2);
// Perform an uplink // Perform an uplink
int state = node.sendReceive(uplinkPayload, sizeof(uplinkPayload)); int state = node.sendReceive(uplinkPayload, sizeof(uplinkPayload));

View file

@ -33,18 +33,17 @@
// include the library // include the library
#include <RadioLib.h> #include <RadioLib.h>
void setup() { void setup() {
Serial.begin(115200); 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 delay(5000); // Give time to switch to the serial monitor
Serial.println(F("\nSetup")); Serial.println(F("\nSetup"));
int16_t state = 0; // return value for calls to RadioLib 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(); 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 // Override the default join rate
// uint8_t joinDR = 3; // uint8_t joinDR = 3;
@ -70,8 +69,7 @@ void setup() {
node.setDwellTime(true, 400); node.setDwellTime(true, 400);
Serial.println(F("Ready!\n")); Serial.println(F("Ready!\n"));
} // setup }
void loop() { void loop() {
int state = RADIOLIB_ERR_NONE; int state = RADIOLIB_ERR_NONE;
@ -85,19 +83,19 @@ void loop() {
uint8_t battLevel = 146; uint8_t battLevel = 146;
node.setDeviceStatus(battLevel); node.setDeviceStatus(battLevel);
// This is the place to gather the sensor inputs
// Read some inputs // Instead of reading any real sensor, we just generate some random numbers as example
uint8_t Digital1 = digitalRead(2); uint8_t value1 = radio.random(100);
uint16_t Analog1 = analogRead(3); uint16_t value2 = radio.random(2000);
// Build payload byte array // Build payload byte array
uint8_t uplinkPayload[3]; uint8_t uplinkPayload[3];
uplinkPayload[0] = Digital1; uplinkPayload[0] = value1;
uplinkPayload[1] = highByte(Analog1); // See notes for high/lowByte functions uplinkPayload[1] = highByte(value2); // See notes for high/lowByte functions
uplinkPayload[2] = lowByte(Analog1); uplinkPayload[2] = lowByte(value2);
uint8_t downlinkPayload[10]; // Make sure this fits your plans! 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 // you can also retrieve additional information about an uplink or
// downlink by passing a reference to LoRaWANEvent_t structure // downlink by passing a reference to LoRaWANEvent_t structure
@ -108,6 +106,7 @@ void loop() {
// Retrieve the last uplink frame counter // Retrieve the last uplink frame counter
uint32_t fcntUp = node.getFcntUp(); uint32_t fcntUp = node.getFcntUp();
// Send a confirmed uplink every 64th frame // Send a confirmed uplink every 64th frame
// and also request the LinkCheck and DeviceTime MAC commands // and also request the LinkCheck and DeviceTime MAC commands
if(fcntUp % 64 == 0) { if(fcntUp % 64 == 0) {
@ -123,7 +122,7 @@ void loop() {
// Check if downlink was received // Check if downlink was received
if(state != RADIOLIB_LORAWAN_NO_DOWNLINK) { if(state != RADIOLIB_LORAWAN_NO_DOWNLINK) {
// Did we get a downlink with data for us // Did we get a downlink with data for us
if (downlinkSize > 0) { if(downlinkSize > 0) {
Serial.println(F("Downlink data: ")); Serial.println(F("Downlink data: "));
arrayDump(downlinkPayload, downlinkSize); arrayDump(downlinkPayload, downlinkSize);
} else { } else {
@ -194,5 +193,4 @@ void loop() {
Serial.println(F("s")); Serial.println(F("s"));
delay(delayMs); delay(delayMs);
}
} // loop

View file

@ -26,13 +26,13 @@
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
while (!Serial); while(!Serial);
delay(5000); // Give time to switch to the serial monitor delay(5000); // Give time to switch to the serial monitor
Serial.println(F("\nSetup ... ")); Serial.println(F("\nSetup ... "));
Serial.println(F("Initalise the radio")); Serial.println(F("Initialise the radio"));
int state = radio.begin(); 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")); Serial.println(F("Join ('login') to the LoRaWAN Network"));
state = node.beginOTAA(joinEUI, devEUI, nwkKey, appKey, true); state = node.beginOTAA(joinEUI, devEUI, nwkKey, appKey, true);
@ -41,19 +41,19 @@ void setup() {
Serial.println(F("Ready!\n")); Serial.println(F("Ready!\n"));
} }
void loop() { void loop() {
Serial.println(F("Sending uplink")); Serial.println(F("Sending uplink"));
// Read some inputs // This is the place to gather the sensor inputs
uint8_t Digital1 = digitalRead(2); // Instead of reading any real sensor, we just generate some random numbers as example
uint16_t Analog1 = analogRead(3); uint8_t value1 = radio.random(100);
uint16_t value2 = radio.random(2000);
// Build payload byte array // Build payload byte array
uint8_t uplinkPayload[3]; uint8_t uplinkPayload[3];
uplinkPayload[0] = Digital1; uplinkPayload[0] = value1;
uplinkPayload[1] = highByte(Analog1); // See notes for high/lowByte functions uplinkPayload[1] = highByte(value2); // See notes for high/lowByte functions
uplinkPayload[2] = lowByte(Analog1); uplinkPayload[2] = lowByte(value2);
// Perform an uplink // Perform an uplink
int state = node.sendReceive(uplinkPayload, sizeof(uplinkPayload)); int state = node.sendReceive(uplinkPayload, sizeof(uplinkPayload));