Skip to content

Commit

Permalink
Merge pull request #1 from nicklasb/nicklasb-patch-narrowing
Browse files Browse the repository at this point in the history
Fix narrowing conversion error on ESP-IDF
  • Loading branch information
nicklasb authored Aug 23, 2023
2 parents 8672419 + 2555857 commit f8ae458
Show file tree
Hide file tree
Showing 17 changed files with 1,270 additions and 193 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
# platform-dependent settings - extra board options, board index URLs, skip patterns etc.
include:
- id: arduino:avr:uno
run: echo "skip-pattern=(STM32WL|SSTV)" >> $GITHUB_OUTPUT
run: echo "skip-pattern=(STM32WL|SSTV|LoRaWAN)" >> $GITHUB_OUTPUT
- id: arduino:avr:mega
run: echo "options=':cpu=atmega2560'" >> $GITHUB_OUTPUT
- id: arduino:avr:leonardo
Expand Down
57 changes: 51 additions & 6 deletions examples/LoRaWAN/LoRaWAN_End_Device/LoRaWAN_End_Device.ino
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ void setup() {
// the end device in TTN and perform the join procedure again!
//node.wipe();

// application identifier - in LoRaWAN 1.1, it is also called joinEUI
// application identifier - pre-LoRaWAN 1.1.0, this was called appEUI
// when adding new end device in TTN, you will have to enter this number
// you can pick any number you want, but it has to be unique
uint64_t appEUI = 0x12AD1011B0C0FFEE;
uint64_t joinEUI = 0x12AD1011B0C0FFEE;

// device identifier - this number can be anything
// when adding new end device in TTN, you can generate this number,
Expand All @@ -76,10 +76,14 @@ void setup() {
const char nwkKey[] = "topSecretKey1234";
const char appKey[] = "aDifferentKeyABC";

// 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

// now we can start the activation
// this can take up to 20 seconds, and requires a LoRaWAN gateway in range
Serial.print(F("[LoRaWAN] Attempting over-the-air activation ... "));
state = node.beginOTAA(appEUI, devEUI, (uint8_t*)nwkKey, (uint8_t*)appKey);
state = node.beginOTAA(joinEUI, devEUI, (uint8_t*)nwkKey, (uint8_t*)appKey);
if(state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Expand Down Expand Up @@ -110,15 +114,56 @@ int count = 0;
void loop() {
// send uplink to port 10
Serial.print(F("[LoRaWAN] Sending uplink packet ... "));
String str = "Hello World! #" + String(count++);
int state = node.uplink(str, 10);
String strUp = "Hello World! #" + String(count++);
int state = node.uplink(strUp, 10);
if(state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
}

// after uplink, you can call downlink(),
// to receive any possible reply from the server
// this function must be called within a few seconds
// after uplink to receive the downlink!
Serial.print(F("[LoRaWAN] Waiting for downlink ... "));
String strDown;
state = node.downlink(strDown);
if(state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));

// print data of the packet (if there are any)
Serial.print(F("[LoRaWAN] Data:\t\t"));
if(strDown.length() > 0) {
Serial.println(strDown);
} else {
Serial.println(F("<MAC commands only>"));
}

// print RSSI (Received Signal Strength Indicator)
Serial.print(F("[LoRaWAN] RSSI:\t\t"));
Serial.print(radio.getRSSI());
Serial.println(F(" dBm"));

// print SNR (Signal-to-Noise Ratio)
Serial.print(F("[LoRaWAN] SNR:\t\t"));
Serial.print(radio.getSNR());
Serial.println(F(" dB"));

// print frequency error
Serial.print(F("[LoRaWAN] Frequency error:\t"));
Serial.print(radio.getFrequencyError());
Serial.println(F(" Hz"));

} else if(state == RADIOLIB_ERR_RX_TIMEOUT) {
Serial.println(F("timeout!"));

} else {
Serial.print(F("failed, code "));
Serial.println(state);
}

// wait before sending another one
// wait before sending another packet
delay(10000);
}
51 changes: 48 additions & 3 deletions examples/LoRaWAN/LoRaWAN_End_Device_APB/LoRaWAN_End_Device_APB.ino
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ void setup() {
const char nwkSKey[] = "topSecretKey1234";
const char appSKey[] = "aDifferentKeyABC";

// 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

// start the device by directly providing the encryption keys and device address
Serial.print(F("[LoRaWAN] Attempting over-the-air activation ... "));
state = node.beginAPB(devAddr, (uint8_t*)nwkSKey, (uint8_t*)appSKey);
Expand Down Expand Up @@ -105,15 +109,56 @@ int count = 0;
void loop() {
// send uplink to port 10
Serial.print(F("[LoRaWAN] Sending uplink packet ... "));
String str = "Hello World! #" + String(count++);
int state = node.uplink(str, 10);
String strUp = "Hello World! #" + String(count++);
int state = node.uplink(strUp, 10);
if(state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
}

// after uplink, you can call downlink(),
// to receive any possible reply from the server
// this function must be called within a few seconds
// after uplink to receive the downlink!
Serial.print(F("[LoRaWAN] Waiting for downlink ... "));
String strDown;
state = node.downlink(strDown);
if(state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));

// print data of the packet (if there are any)
Serial.print(F("[LoRaWAN] Data:\t\t"));
if(strDown.length() > 0) {
Serial.println(strDown);
} else {
Serial.println(F("<MAC commands only>"));
}

// print RSSI (Received Signal Strength Indicator)
Serial.print(F("[LoRaWAN] RSSI:\t\t"));
Serial.print(radio.getRSSI());
Serial.println(F(" dBm"));

// print SNR (Signal-to-Noise Ratio)
Serial.print(F("[LoRaWAN] SNR:\t\t"));
Serial.print(radio.getSNR());
Serial.println(F(" dB"));

// print frequency error
Serial.print(F("[LoRaWAN] Frequency error:\t"));
Serial.print(radio.getFrequencyError());
Serial.println(F(" Hz"));

} else if(state == RADIOLIB_ERR_RX_TIMEOUT) {
Serial.println(F("timeout!"));

} else {
Serial.print(F("failed, code "));
Serial.println(state);
}

// wait before sending another one
// wait before sending another packet
delay(10000);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
RadioLib SX126x Receive after Channel Activity Detection Example
This example uses SX1262 to scan the current LoRa
channel and detect ongoing LoRa transmissions.
Unlike SX127x CAD, SX126x can detect any part
of LoRa transmission, not just the preamble.
If a packet is detected, the module will switch
to receive mode and receive the packet.
Other modules from SX126x family can also be used.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx126x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
*/

// include the library
#include <RadioLib.h>

// SX1262 has the following connections:
// NSS pin: 10
// DIO1 pin: 2
// NRST pin: 3
// BUSY pin: 9
SX1262 radio = new Module(10, 2, 3, 9);

// or using RadioShield
// https://github.com/jgromes/RadioShield
//SX1262 radio = RadioShield.ModuleA;

// or using CubeCell
//SX1262 radio = new Module(RADIOLIB_BUILTIN_MODULE);

void setup() {
Serial.begin(9600);

// initialize SX1262 with default settings
Serial.print(F("[SX1262] Initializing ... "));
int state = radio.begin();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
}

// set the function that will be called
// when LoRa packet or timeout is detected
radio.setDio1Action(setFlag);

// start scanning the channel
Serial.print(F("[SX1262] Starting scan for LoRa preamble ... "));
state = radio.startChannelScan();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
}
}

// flag to indicate that a packet was detected or CAD timed out
volatile bool scanFlag = false;

bool receiving = false;

// this function is called when a complete packet
// is received by the module
// IMPORTANT: this function MUST be 'void' type
// and MUST NOT have any arguments!
#if defined(ESP8266) || defined(ESP32)
ICACHE_RAM_ATTR
#endif
void setFlag(void) {
// something happened, set the flag
scanFlag = true;
}

void loop() {
// check if the flag is set
if(scanFlag) {
int state = RADIOLIB_ERR_NONE;

// reset flag
scanFlag = false;

// check ongoing reception
if(receiving) {
// DIO triggered while reception is ongoing
// that means we got a packet

// you can read received data as an Arduino String
String str;
state = radio.readData(str);

// you can also read received data as byte array
/*
byte byteArr[8];
state = radio.readData(byteArr, 8);
*/

if (state == RADIOLIB_ERR_NONE) {
// packet was successfully received
Serial.println(F("[SX1262] Received packet!"));

// print data of the packet
Serial.print(F("[SX1262] Data:\t\t"));
Serial.println(str);

// print RSSI (Received Signal Strength Indicator)
Serial.print(F("[SX1262] RSSI:\t\t"));
Serial.print(radio.getRSSI());
Serial.println(F(" dBm"));

// print SNR (Signal-to-Noise Ratio)
Serial.print(F("[SX1262] SNR:\t\t"));
Serial.print(radio.getSNR());
Serial.println(F(" dB"));

// print frequency error
Serial.print(F("[SX1262] Frequency error:\t"));
Serial.print(radio.getFrequencyError());
Serial.println(F(" Hz"));

} else {
// some other error occurred
Serial.print(F("[SX1262] Failed, code "));
Serial.println(state);

}

// reception is done now
receiving = false;

} else {
// check CAD result
state = radio.getChannelScanResult();

if (state == RADIOLIB_LORA_DETECTED) {
// LoRa packet was detected
Serial.print(F("[SX1262] Packet detected, starting reception ... "));
state = radio.startReceive();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
}

// set the flag for ongoing reception
receiving = true;

} else if (state == RADIOLIB_CHANNEL_FREE) {
// channel is free
Serial.println(F("[SX1262] Channel is free!"));

} else {
// some other error occurred
Serial.print(F("[SX1262] Failed, code "));
Serial.println(state);

}

}

// if we're not receiving, start scanning again
if(!receiving) {
Serial.print(F("[SX1262] Starting scan for LoRa preamble ... "));
state = radio.startChannelScan();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
}

}

}

}
11 changes: 11 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ wipe KEYWORD2
beginOTAA KEYWORD2
beginAPB KEYWORD2
uplink KEYWORD2
downlink KEYWORD2
configureChannel KEYWORD2

#######################################
Expand Down Expand Up @@ -390,3 +391,13 @@ RADIOLIB_ERR_RANGING_TIMEOUT LITERAL1
RADIOLIB_ERR_INVALID_PAYLOAD LITERAL1
RADIOLIB_ERR_ADDRESS_NOT_FOUND LITERAL1
RADIOLIB_ERR_INVALID_FUNCTION LITERAL1

RADIOLIB_ERR_NETWORK_NOT_JOINED LITERAL1
RADIOLIB_ERR_DOWNLINK_MALFORMED LITERAL1
RADIOLIB_ERR_INVALID_REVISION LITERAL1
RADIOLIB_ERR_INVALID_PORT LITERAL1
RADIOLIB_ERR_NO_RX_WINDOW LITERAL1
RADIOLIB_ERR_INVALID_CHANNEL LITERAL1
RADIOLIB_ERR_INVALID_CID LITERAL1
RADIOLIB_ERR_COMMAND_QUEUE_FULL LITERAL1
RADIOLIB_ERR_COMMAND_QUEUE_EMPTY LITERAL1
Loading

0 comments on commit f8ae458

Please sign in to comment.