Change coding style to match library style guide.
This commit is contained in:
parent
20e1ab23df
commit
c4fec32965
2 changed files with 73 additions and 96 deletions
|
@ -18,68 +18,61 @@
|
||||||
The SX1276 / 7 / 8 / 9 supports FHSS or Frequency Hopping Spread Spectrum.
|
The SX1276 / 7 / 8 / 9 supports FHSS or Frequency Hopping Spread Spectrum.
|
||||||
Once a hopping period is set and a transmission is started the radio
|
Once a hopping period is set and a transmission is started the radio
|
||||||
will begin triggering interrupts every hop period where the radio frequency
|
will begin triggering interrupts every hop period where the radio frequency
|
||||||
is changed to the next channel. This allows a simple mechanism to abide by
|
is changed to the next channel.
|
||||||
the FCC 400ms max dwell time rule.
|
|
||||||
https://www.govinfo.gov/content/pkg/CFR-2019-title47-vol1/pdf/CFR-2019-title47-vol1-sec15-247.pdf
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <RadioLib.h> //Click here to get the library: http://librarymanager/All#RadioLib
|
#include <RadioLib.h> //Click here to get the library: http://librarymanager/All#RadioLib
|
||||||
|
|
||||||
//Pins for RFM97 100mW Shield to SparkFun ESP32 Thing Plus C
|
// SX1276 has the following connections:
|
||||||
int pin_cs = 15;
|
const int pin_cs = 10;
|
||||||
int pin_dio0 = 26;
|
const int pin_dio0 = 2;
|
||||||
int pin_dio1 = 25;
|
const int pin_dio1 = 9;
|
||||||
int pin_rst = 32;
|
const int pin_rst = 3;
|
||||||
SX1276 radio = new Module(pin_cs, pin_dio0, pin_rst, pin_dio1);
|
SX1276 radio = new Module(pin_cs, pin_dio0, pin_rst, pin_dio1);
|
||||||
|
|
||||||
int counter = 0;
|
|
||||||
|
|
||||||
volatile bool rxComplete = false;
|
volatile bool rxComplete = false;
|
||||||
volatile bool fhssChange = false;
|
volatile bool fhssChange = false;
|
||||||
|
|
||||||
//The channel frequencies can be generated randomly or hard coded
|
// the channel frequencies can be generated randomly or hard coded
|
||||||
float channels[] = {908.0, 906.0, 907.0, 905.0, 903.0, 910.0, 909.0};
|
float channels[] = {908.0, 906.0, 907.0, 905.0, 903.0, 910.0, 909.0};
|
||||||
int numberOfChannels = sizeof(channels) / sizeof(float);
|
int numberOfChannels = sizeof(channels) / sizeof(float);
|
||||||
|
|
||||||
int hopsCompleted = 0;
|
int hopsCompleted = 0;
|
||||||
|
|
||||||
void setup()
|
void setup() {
|
||||||
{
|
Serial.begin(9600);
|
||||||
Serial.begin(115200);
|
|
||||||
|
|
||||||
//Begin radio on home channel
|
// begin radio on home channel
|
||||||
Serial.print(F("[SX127x] Initializing ... "));
|
Serial.print(F("[SX127x] Initializing ... "));
|
||||||
int state = radio.begin(channels[0]);
|
int state = radio.begin(channels[0]);
|
||||||
if (state != RADIOLIB_ERR_NONE)
|
if (state != RADIOLIB_ERR_NONE) {
|
||||||
{
|
|
||||||
Serial.print(F("Failed with code: "));
|
Serial.print(F("Failed with code: "));
|
||||||
Serial.println(state);
|
Serial.println(state);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
Serial.println(F("Success!"));
|
Serial.println(F("Success!"));
|
||||||
|
|
||||||
// Set hop period to enable FHSS
|
// set hop period to enable FHSS
|
||||||
// We set an artifically short period to show lots of hops
|
state = radio.setFHSSHoppingPeriod(9);
|
||||||
// HoppingPeriod = Tsym * FreqHoppingPeriod
|
if (state != RADIOLIB_ERR_NONE) {
|
||||||
// Given defaults of spreadfactor = 9, bandwidth = 125, it follows Tsym = 4.10ms
|
Serial.print(F("Error setting hopping period: "));
|
||||||
// HoppingPeriod = 4.10 * 9 = 36.9ms. Can be as high as 400ms to be within regulatory limits
|
Serial.println(state);
|
||||||
radio.setFHSSHoppingPeriod(9);
|
}
|
||||||
|
radio.setDio0Action(dio0ISR); // called when transmission is finished
|
||||||
|
radio.setDio1Action(dio1ISR); // called after a transmission has started, so we can move to next freq
|
||||||
|
|
||||||
Serial.print(F("Hopping period: "));
|
// start listening for LoRa packets
|
||||||
Serial.println(radio.getFHSSHoppingPeriod());
|
Serial.print(F("[SX1278] Starting to listen ... "));
|
||||||
|
state = radio.startReceive();
|
||||||
radio.setDio0Action(dio0ISR); //Called when transmission is finished
|
if (state != RADIOLIB_ERR_NONE) {
|
||||||
radio.setDio1Action(dio1ISR); //Called after a transmission has started, so we can move to next freq
|
Serial.print(F("failed, code "));
|
||||||
|
Serial.println(state);
|
||||||
radio.startReceive();
|
while (true);
|
||||||
|
}
|
||||||
Serial.println(F("Waiting for new packet"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop() {
|
||||||
{
|
if (rxComplete == true) {
|
||||||
if (rxComplete == true)
|
|
||||||
{
|
|
||||||
uint8_t incomingBuffer[255];
|
uint8_t incomingBuffer[255];
|
||||||
radio.readData(incomingBuffer, 255);
|
radio.readData(incomingBuffer, 255);
|
||||||
uint8_t receivedBytes = radio.getPacketLength();
|
uint8_t receivedBytes = radio.getPacketLength();
|
||||||
|
@ -95,11 +88,8 @@ void loop()
|
||||||
rxComplete = false;
|
rxComplete = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fhssChange == true)
|
if (fhssChange == true) {
|
||||||
{
|
|
||||||
radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]);
|
radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]);
|
||||||
//Serial.print(F("Radio on channel: "));
|
|
||||||
//Serial.println(radio.getFHSSChannel());
|
|
||||||
|
|
||||||
hopsCompleted++;
|
hopsCompleted++;
|
||||||
radio.clearFHSSInt();
|
radio.clearFHSSInt();
|
||||||
|
@ -107,16 +97,14 @@ void loop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//ISR when DIO0 goes low
|
// ISR when DIO0 goes low
|
||||||
//Called when transmission is complete or when RX is received
|
// called when transmission is complete or when RX is received
|
||||||
void dio0ISR(void)
|
void dio0ISR(void) {
|
||||||
{
|
|
||||||
rxComplete = true;
|
rxComplete = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//ISR when DIO1 goes low
|
// ISR when DIO1 goes low
|
||||||
//Called when FhssChangeChannel interrupt occurs (at the beginning of each transmission)
|
// called when FhssChangeChannel interrupt occurs (at the beginning of each transmission)
|
||||||
void dio1ISR(void)
|
void dio1ISR(void) {
|
||||||
{
|
|
||||||
fhssChange = true;
|
fhssChange = true;
|
||||||
}
|
}
|
|
@ -18,77 +18,71 @@
|
||||||
The SX1276 / 7 / 8 / 9 supports FHSS or Frequency Hopping Spread Spectrum.
|
The SX1276 / 7 / 8 / 9 supports FHSS or Frequency Hopping Spread Spectrum.
|
||||||
Once a hopping period is set and a transmission is started the radio
|
Once a hopping period is set and a transmission is started the radio
|
||||||
will begin triggering interrupts every hop period where the radio frequency
|
will begin triggering interrupts every hop period where the radio frequency
|
||||||
is changed to the next channel. This allows a simple mechanism to abide by
|
is changed to the next channel.
|
||||||
the FCC 400ms max dwell time rule.
|
|
||||||
https://www.govinfo.gov/content/pkg/CFR-2019-title47-vol1/pdf/CFR-2019-title47-vol1-sec15-247.pdf
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <RadioLib.h> //Click here to get the library: http://librarymanager/All#RadioLib
|
#include <RadioLib.h> //Click here to get the library: http://librarymanager/All#RadioLib
|
||||||
|
|
||||||
//Pins for SparkFun 1W EBYTE Breakout to Uno
|
// SX1276 has the following connections:
|
||||||
int pin_cs = 7;
|
const int pin_cs = 10;
|
||||||
int pin_dio0 = 3;
|
const int pin_dio0 = 2;
|
||||||
int pin_dio1 = 2;
|
const int pin_dio1 = 9;
|
||||||
int pin_rst = A2;
|
const int pin_rst = 3;
|
||||||
SX1276 radio = new Module(pin_cs, pin_dio0, pin_rst, pin_dio1);
|
SX1276 radio = new Module(pin_cs, pin_dio0, pin_rst, pin_dio1);
|
||||||
|
|
||||||
volatile bool xmitComplete = false;
|
volatile bool xmitComplete = false;
|
||||||
volatile bool fhssChange = false;
|
volatile bool fhssChange = false;
|
||||||
|
|
||||||
//The channel frequencies can be generated randomly or hard coded
|
// the channel frequencies can be generated randomly or hard coded
|
||||||
float channels[] = {908.0, 906.0, 907.0, 905.0, 903.0, 910.0, 909.0};
|
float channels[] = {908.0, 906.0, 907.0, 905.0, 903.0, 910.0, 909.0};
|
||||||
int numberOfChannels = sizeof(channels) / sizeof(float);
|
int numberOfChannels = sizeof(channels) / sizeof(float);
|
||||||
|
|
||||||
int hopsCompleted = 0;
|
int hopsCompleted = 0;
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
|
|
||||||
void setup()
|
void setup() {
|
||||||
{
|
Serial.begin(9600);
|
||||||
Serial.begin(115200);
|
|
||||||
|
|
||||||
//Begin radio on home channel
|
// begin radio on home channel
|
||||||
Serial.print(F("[SX127x] Initializing ... "));
|
Serial.print(F("[SX127x] Initializing ... "));
|
||||||
int state = radio.begin(channels[0]);
|
int state = radio.begin(channels[0]);
|
||||||
if (state != RADIOLIB_ERR_NONE)
|
if (state != RADIOLIB_ERR_NONE) {
|
||||||
{
|
|
||||||
Serial.print(F("Failed with code: "));
|
Serial.print(F("Failed with code: "));
|
||||||
Serial.println(state);
|
Serial.println(state);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
Serial.println(F("Success!"));
|
Serial.println(F("Success!"));
|
||||||
|
|
||||||
// Set hop period to enable FHSS
|
// set hop period to enable FHSS
|
||||||
// We set an artifically short period to show lots of hops
|
state = radio.setFHSSHoppingPeriod(9);
|
||||||
// HoppingPeriod = Tsym * FreqHoppingPeriod
|
if(state != RADIOLIB_ERR_NONE) {
|
||||||
// Given defaults of spreadfactor = 9, bandwidth = 125, it follows Tsym = 4.10ms
|
Serial.print(F("Error setting hopping period: "));
|
||||||
// HoppingPeriod = 4.10 * 9 = 36.9ms. Can be as high as 400ms to be within regulatory limits
|
Serial.println(state);
|
||||||
radio.setFHSSHoppingPeriod(9);
|
}
|
||||||
|
|
||||||
Serial.print(F("Hopping period: "));
|
radio.setDio0Action(dio0ISR); // called when transmission is finished
|
||||||
Serial.println(radio.getFHSSHoppingPeriod());
|
radio.setDio1Action(dio1ISR); // called after a transmission has started, so we can move to next freq
|
||||||
|
|
||||||
radio.setDio0Action(dio0ISR); //Called when transmission is finished
|
|
||||||
radio.setDio1Action(dio1ISR); //Called after a transmission has started, so we can move to next freq
|
|
||||||
|
|
||||||
Serial.print(F("Transmitting packet..."));
|
Serial.print(F("Transmitting packet..."));
|
||||||
|
|
||||||
char output[256];
|
String longOutput = "Let's create a really long packet to trigger lots of hop interrupts. A packet can be up to 256 bytes long. This packet is 222 bytes so using sf = 9, bw = 125, timeOnAir is 1488ms. 1488ms / (9*4.10ms) = 40 hops. Counter: ";
|
||||||
sprintf(output, "Let's create a really long packet to trigger lots of hop interrupts. A packet can be up to 256 bytes long. This packet is 222 bytes so using sf = 9, bw = 125, timeOnAir is 1488ms. 1488ms / (9*4.10ms) = 40 hops. Counter: %d", counter++);
|
|
||||||
|
|
||||||
radio.startTransmit(output, strlen(output) - 1);
|
state = radio.startTransmit(longOutput + counter);
|
||||||
|
if (state != RADIOLIB_ERR_NONE) {
|
||||||
|
Serial.print(F("Error transmitting with code: "));
|
||||||
|
Serial.println(state);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop() {
|
||||||
{
|
if (xmitComplete == true) {
|
||||||
if (xmitComplete == true)
|
|
||||||
{
|
|
||||||
xmitComplete = false;
|
xmitComplete = false;
|
||||||
Serial.println(F("Transmit complete"));
|
Serial.println(F("Transmit complete"));
|
||||||
Serial.print(F("Radio after xmit is on channel: "));
|
Serial.print(F("Radio after xmit is on channel: "));
|
||||||
Serial.println(radio.getFHSSChannel());
|
Serial.println(radio.getFHSSChannel());
|
||||||
//The FHSS channel is automatically reset to 0 upon end of transmission
|
// the FHSS channel is automatically reset to 0 upon end of transmission
|
||||||
|
|
||||||
radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]); //Return to home channel before next transaction
|
radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]); // Return to home channel before next transaction
|
||||||
|
|
||||||
Serial.print(F("Hops completed: "));
|
Serial.print(F("Hops completed: "));
|
||||||
Serial.println(hopsCompleted);
|
Serial.println(hopsCompleted);
|
||||||
|
@ -97,11 +91,8 @@ void loop()
|
||||||
radio.startReceive();
|
radio.startReceive();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fhssChange == true)
|
if (fhssChange == true) {
|
||||||
{
|
|
||||||
radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]);
|
radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]);
|
||||||
//Serial.print(F("Radio on channel: "));
|
|
||||||
//Serial.println(radio.getFHSSChannel());
|
|
||||||
|
|
||||||
hopsCompleted++;
|
hopsCompleted++;
|
||||||
fhssChange = false;
|
fhssChange = false;
|
||||||
|
@ -109,16 +100,14 @@ void loop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//ISR when DIO0 goes low
|
// ISR when DIO0 goes low
|
||||||
//Called when transmission is complete or when RX is received
|
// called when transmission is complete or when RX is received
|
||||||
void dio0ISR(void)
|
void dio0ISR(void) {
|
||||||
{
|
|
||||||
xmitComplete = true;
|
xmitComplete = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//ISR when DIO1 goes low
|
// ISR when DIO1 goes low
|
||||||
//Called when FhssChangeChannel interrupt occurs (at regular HoppingPeriods)
|
// called when FhssChangeChannel interrupt occurs (at regular HoppingPeriods)
|
||||||
void dio1ISR(void)
|
void dio1ISR(void) {
|
||||||
{
|
|
||||||
fhssChange = true;
|
fhssChange = true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue