jgromes / RadioLib

Universal wireless communication library for embedded devices
https://jgromes.github.io/RadioLib/
MIT License
1.48k stars 373 forks source link

Address filtering is not enable on lora sx1268 module. data recieved at reciever node irrespective of address. #855

Closed codev123 closed 10 months ago

codev123 commented 10 months ago

First of all, Thank you very much for writing such extensive library consisting of all radio modules. it helps a lot to people like me with little programming knowledge.

Issue : I have enable address filtering at transmitter end and sending data to specific rx_address node, but i can recieve data at my reciever node with has no address filtering enable. this should'nt be possible.

my Transmitter code :

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

#define INITIATING_NODE
uint8_t  Tx_nodeAddr = 34;
uint8_t  Rx_nodeAddr = 35;

// SX1262 has the following connections:
// NSS pin:   17
// DIO1 pin:  9
// NRST pin:  8
// BUSY pin:  15
SX1268 radio = new Module(17, 9, 8, 15);

// save transmission states between loops
int transmissionState = RADIOLIB_ERR_NONE;

// flag to indicate transmission or reception state
bool transmitFlag = false;

// flag to indicate that a packet was sent or received
volatile bool operationDone = false;

// this function is called when a complete packet is transmitted or received by the module
// IMPORTANT: this function MUST be 'void' type and MUST NOT have any arguments!
void setFlag(void) {
  // we sent or received a packet, set the flag
  operationDone = true;
}

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

  // initialize SX1268 with default settings
  Serial.print(F("[SX1268] Initializing ... "));
  //radio.begin (float freq=434.0, float bw=125.0, uint8_t sf=9, uint8_t cr=7, uint8_t syncWord=RADIOLIB_SX126X_SYNC_WORD_PRIVATE, int8_t power=10, uint16_t preambleLength=8, 
  //float tcxoVoltage=1.6, bool useRegulatorLDO=false)
  int state = radio.begin (434.0, 125.0, 10, 5, 0x34, 22, 8, 1.6, false);
  if (state == RADIOLIB_ERR_NONE) {
    Serial.println(F("success!"));
  } else {
    Serial.print(F("failed, code "));
    Serial.println(state);
    while (true);
  }

  //set Broadcast Address
  radio.setBroadcastAddress(36);

  // set Node Address to current radio node
  if (radio.setNodeAddress(Tx_nodeAddr) == RADIOLIB_ERR_NONE) {
    Serial.println(F("Selected frequency is invalid for this module!"));
    while (true);
  } 

  // set carrier frequency to 433.5 MHz
  if (radio.setFrequency(434.0) == RADIOLIB_ERR_INVALID_FREQUENCY) {
    Serial.println(F("Selected frequency is invalid for this module!"));
    while (true);
  }

  // set bandwidth to 250 kHz (  7.8, 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125.0, 250.0 and 500.0 kHz)
  if (radio.setBandwidth(125.0) == RADIOLIB_ERR_INVALID_BANDWIDTH) {
    Serial.println(F("Selected bandwidth is invalid for this module!"));
    while (true);
  }

  // set spreading factor to 10 (5 - 12)
  if (radio.setSpreadingFactor(10) == RADIOLIB_ERR_INVALID_SPREADING_FACTOR) {
    Serial.println(F("Selected spreading factor is invalid for this module!"));
    while (true);
  }

  // set coding rate to 6 (5-8)
  if (radio.setCodingRate(5) == RADIOLIB_ERR_INVALID_CODING_RATE) {
    Serial.println(F("Selected coding rate is invalid for this module!"));
    while (true);
  }
  /*
  // set LoRa sync word to 0xAB
  if (radio.setSyncWord(0x34) != RADIOLIB_ERR_NONE) {
    Serial.println(F("Unable to set sync word!"));
    while (true);
  }
  */
  // set output power to 10 dBm (accepted range is -17 - 22 dBm)
  if (radio.setOutputPower(22) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) {
    Serial.println(F("Selected output power is invalid for this module!"));
    while (true);
  }

  // set LoRa preamble length to 15 symbols (accepted range is 0 - 65535)
  if (radio.setPreambleLength(8) == RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH) {
    Serial.println(F("Selected preamble length is invalid for this module!"));
    while (true);
  }

  // Some SX126x modules have TCXO (temperature compensated crystal
  // oscillator). To configure TCXO reference voltage,
  if (radio.setTCXO(1.6) == RADIOLIB_ERR_INVALID_TCXO_VOLTAGE) {
    Serial.println(F("Selected TCXO voltage is invalid for this module!"));
    while (true);
  }

  // set over current protection limit to 80 mA (accepted range is 45 - 240 mA)
  // NOTE: set value to 0 to disable overcurrent protection
  if (radio.setCurrentLimit(140) == RADIOLIB_ERR_INVALID_CURRENT_LIMIT) {
    Serial.println(F("Selected current limit is invalid for this module!"));
    while (true);
  }

  // disable CRC
  if (radio.setCRC(true) == RADIOLIB_ERR_INVALID_CRC_CONFIGURATION) {
    Serial.println(F("Selected CRC is invalid for this module!"));
    while (true);
  }
  /*
  // set data rate range(0.6 - 60 kbps)
  if (radio.setDataRate(1.2) == RADIOLIB_ERR_INVALID_DATA_RATE ) {
    Serial.println(F("Selected data rate is invalid for this module!"));
    while (true);
  }

  // Some SX126x modules use DIO2 as RF switch. To enable
  // this feature, the following method can be used.
  // NOTE: As long as DIO2 is configured to control RF switch,
  //       it can't be used as interrupt pin!
  if (radio.setDio2AsRfSwitch() != RADIOLIB_ERR_NONE) {
    Serial.println(F("Failed to set DIO2 as RF switch!"));
    while (true);
  }
  */
  //Serial.print(F("[SX1268] Datarate:\t"));
  //Serial.print(radio.getDataRate());

  // set the function that will be called when new packet is received
  radio.setDio1Action(setFlag);

  #if defined(INITIATING_NODE)
    // send the first packet on this node
    Serial.print(F("[SX1268] Sending first packet ... "));
    //transmissionState = radio.startTransmit("Hello World!");
    uint8_t myData[] = "Hello World!";
    size_t dataLength = sizeof(myData);
    transmissionState = radio.startTransmit( myData, dataLength, Rx_nodeAddr );
    transmitFlag = true;
  #else
    // start listening for LoRa packets on this node
    Serial.print(F("[SX1268] Starting to listen ... "));
    state = radio.startReceive();
    if (state == RADIOLIB_ERR_NONE) {
      Serial.println(F("success!"));
    } else {
      Serial.print(F("failed, code "));
      Serial.println(state);
      while (true);
    }
  #endif
}

// counter to keep track of transmitted packets
int count = 0;

void loop() {
  // check if the previous operation finished
  if(operationDone) {
    // reset flag
    operationDone = false;

    if(transmitFlag) {
      // the previous operation was transmission, listen for response
      // print the result
      if (transmissionState == RADIOLIB_ERR_NONE) {
        // packet was successfully sent
        Serial.println(F("transmission finished!"));

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

      }

      // listen for response
      radio.startReceive();
      transmitFlag = false;

    } else {
      // the previous operation was reception
      // print data and send another packet
      String str;
      int state = radio.readData(str);

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

        // print data of the packet
        Serial.print(F("[SX1268] 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"));

      }

      // wait a second before transmitting again
      //delay(1000);

      // send another one
      Serial.print(F("[SX1268] Sending another packet ... "));
      //String str1 = "Hello World! #" + String(count++);
      uint8_t myData1[] = "Hello World!";
      size_t dataLength1 = sizeof(myData1);
      //transmissionState = radio.startTransmit(str1);
      transmissionState = radio.startTransmit( myData1, dataLength1, Rx_nodeAddr );
      transmitFlag = true;
    }

  }
}

Reciever code :

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

//#define INITIATING_NODE

// SX1262 has the following connections:
// NSS pin:   17
// DIO1 pin:  9
// NRST pin:  8
// BUSY pin:  15
SX1268 radio = new Module(17, 9, 8, 15);

// save transmission states between loops
int transmissionState = RADIOLIB_ERR_NONE;

// flag to indicate transmission or reception state
bool transmitFlag = false;

// flag to indicate that a packet was sent or received
volatile bool operationDone = false;

// this function is called when a complete packet is transmitted or received by the module
// IMPORTANT: this function MUST be 'void' type and MUST NOT have any arguments!
void setFlag(void) {
  // we sent or received a packet, set the flag
  operationDone = true;
}

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

  // initialize SX1268 with default settings
  Serial.print(F("[SX1268] Initializing ... "));
  //radio.begin (float freq=434.0, float bw=125.0, uint8_t sf=9, uint8_t cr=7, uint8_t syncWord=RADIOLIB_SX126X_SYNC_WORD_PRIVATE, int8_t power=10, uint16_t preambleLength=8, 
  //float tcxoVoltage=1.6, bool useRegulatorLDO=false)
  int state = radio.begin (434.0, 125.0, 10, 5, 0x34, 22, 8, 1.6, false);
  if (state == RADIOLIB_ERR_NONE) {
    Serial.println(F("success!"));
  } else {
    Serial.print(F("failed, code "));
    Serial.println(state);
    while (true);
  }

  // set carrier frequency to 433.5 MHz
  if (radio.setFrequency(434.0) == RADIOLIB_ERR_INVALID_FREQUENCY) {
    Serial.println(F("Selected frequency is invalid for this module!"));
    while (true);
  }

  // set bandwidth to 250 kHz
  if (radio.setBandwidth(125.0) == RADIOLIB_ERR_INVALID_BANDWIDTH) {
    Serial.println(F("Selected bandwidth is invalid for this module!"));
    while (true);
  }

  // set spreading factor to 10
  if (radio.setSpreadingFactor(10) == RADIOLIB_ERR_INVALID_SPREADING_FACTOR) {
    Serial.println(F("Selected spreading factor is invalid for this module!"));
    while (true);
  }

  // set coding rate to 6
  if (radio.setCodingRate(5) == RADIOLIB_ERR_INVALID_CODING_RATE) {
    Serial.println(F("Selected coding rate is invalid for this module!"));
    while (true);
  }

  // set LoRa sync word to 0xAB
  if (radio.setSyncWord(0x34) != RADIOLIB_ERR_NONE) {
    Serial.println(F("Unable to set sync word!"));
    while (true);
  }

  // set output power to 10 dBm (accepted range is -17 - 22 dBm)
  if (radio.setOutputPower(22) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) {
    Serial.println(F("Selected output power is invalid for this module!"));
    while (true);
  }

  // set LoRa preamble length to 15 symbols (accepted range is 0 - 65535)
  if (radio.setPreambleLength(8) == RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH) {
    Serial.println(F("Selected preamble length is invalid for this module!"));
    while (true);
  }

  // Some SX126x modules have TCXO (temperature compensated crystal
  // oscillator). To configure TCXO reference voltage,
  if (radio.setTCXO(1.6) == RADIOLIB_ERR_INVALID_TCXO_VOLTAGE) {
    Serial.println(F("Selected TCXO voltage is invalid for this module!"));
    while (true);
  }

  // set over current protection limit to 80 mA (accepted range is 45 - 240 mA)
  // NOTE: set value to 0 to disable overcurrent protection
  if (radio.setCurrentLimit(140) == RADIOLIB_ERR_INVALID_CURRENT_LIMIT) {
    Serial.println(F("Selected current limit is invalid for this module!"));
    while (true);
  }

  // disable CRC
  if (radio.setCRC(true) == RADIOLIB_ERR_INVALID_CRC_CONFIGURATION) {
    Serial.println(F("Selected CRC is invalid for this module!"));
    while (true);
  }
  /*
  // Some SX126x modules use DIO2 as RF switch. To enable
  // this feature, the following method can be used.
  // NOTE: As long as DIO2 is configured to control RF switch,
  //       it can't be used as interrupt pin!
  if (radio.setDio2AsRfSwitch() != RADIOLIB_ERR_NONE) {
    Serial.println(F("Failed to set DIO2 as RF switch!"));
    while (true);
  }
  */
  //Serial.print(F("[SX1268] Datarate:\t"));
  //Serial.print(radio.getDataRate());

  // set the function that will be called when new packet is received
  radio.setDio1Action(setFlag);

  #if defined(INITIATING_NODE)
    // send the first packet on this node
    Serial.print(F("[SX1268] Sending first packet ... "));
    transmissionState = radio.startTransmit("Hello World!");
    transmitFlag = true;
  #else
    // start listening for LoRa packets on this node
    Serial.print(F("[SX1268] Starting to listen ... "));
    state = radio.startReceive();
    if (state == RADIOLIB_ERR_NONE) {
      Serial.println(F("success!"));
    } else {
      Serial.print(F("failed, code "));
      Serial.println(state);
      while (true);
    }
  #endif
}

// counter to keep track of transmitted packets
int count = 0;

void loop() {
  // check if the previous operation finished
  if(operationDone) {
    // reset flag
    operationDone = false;

    if(transmitFlag) {
      // the previous operation was transmission, listen for response
      // print the result
      if (transmissionState == RADIOLIB_ERR_NONE) {
        // packet was successfully sent
        Serial.println(F("transmission finished!"));

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

      }

      // listen for response
      radio.startReceive();
      transmitFlag = false;

    } else {
      // the previous operation was reception
      // print data and send another packet
      String str;
      int state = radio.readData(str);

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

        // print data of the packet
        Serial.print(F("[SX1268] 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"));

      }

      // wait a second before transmitting again
      //delay(1000);

      // send another one
      Serial.print(F("[SX1268] Sending another packet ... "));
      String str1 = "Hello World! #" + String(count++);
      transmissionState = radio.startTransmit(str1);
      transmitFlag = true;
    }

  }
}

please sir guide me where i am making mistake in my code. thank you.

jgromes commented 10 months ago

The fundamental problem is that you are using the SX1268 in LoRa mode, which has no concept of address, that is only applicable in FSK mode.

RadioLib checks whether the requested action can be performed, so trying to call setNodeAddress in LoRa mode will return RADIOLIB_ERR_WRONG_MODEM. But your return status check is wrong:

// set Node Address to current radio node
  if (radio.setNodeAddress(Tx_nodeAddr) == RADIOLIB_ERR_NONE) {
    Serial.println(F("Selected frequency is invalid for this module!"));
    while (true);
  } 

And you are also not setting the address at the receiver as far as I can tell.

Since this is not really an issue but rather incorrect usage, I'm converting this to a discussion thread.