Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // include the library
- #include <RadioLib.h>
- typedef struct
- {
- float freq;
- float bw;
- uint8_t sf;
- uint8_t cr;
- uint8_t syncWord;
- int8_t power;
- uint16_t preambleLength;
- uint8_t gain;
- } radio_conf_t;
- radio_conf_t radio_conf = {468.0, // frequency
- 125.0, // bandwitch
- 9, // spreading factor
- 7, // coding rate denominator
- RADIOLIB_SX127X_SYNC_WORD,
- 17, // current limit 0 - disable
- 8, // preamble length
- 0}; //gain, 0 - AGC
- // SX1278 connections:
- // SDI pin: PA7
- // SDO pin: PA6
- // SCLK pin: PA5
- // NSS pin: PA4
- // DIO0 pin: PA3
- // RESET pin: PA2
- // DIO1 pin: PA1
- SX1278 radio = new Module(PA4, PA3, PA2, PA1);
- // flag to indicate that a packet was received
- volatile bool receivedFlag = false;
- // disable interrupt when it's not needed
- volatile bool enableInterrupt = true;
- // 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!
- void setFlag(void)
- {
- // check if the interrupt is enabledz
- if (!enableInterrupt)
- {
- return;
- }
- // we got a packet, set the flag
- receivedFlag = true;
- }
- void setup() {
- SerialUSB.begin();
- delay(10000);
- // initialize SX1278 with default settings
- SerialUSB.print(F("[SX1278] Initializing ... "));
- // int state = radio.begin(radio_conf.freq, radio_conf.bw, radio_conf.sf,
- // radio_conf.cr, radio_conf.syncWord, radio_conf.power,
- // radio_conf.preambleLength, radio_conf.gain);
- int state =radio.begin();
- if (state == RADIOLIB_ERR_NONE)
- {
- SerialUSB.println(F("success!"));
- }
- else
- {
- SerialUSB.print(F("failed, code "));
- SerialUSB.println(state);
- while (true)
- ;
- }
- // set the function that will be called
- // when new packet is received
- radio.setDio0Action(setFlag);
- // start listening for LoRa packets
- SerialUSB.print(F("[SX1272] Starting to listen ... "));
- state = radio.startReceive();
- if (state == RADIOLIB_ERR_NONE)
- {
- SerialUSB.println(F("success!"));
- }
- else
- {
- SerialUSB.print(F("failed, code "));
- SerialUSB.println(state);
- while (true)
- ;
- }
- SerialUSB.print("Frequency = ");
- SerialUSB.println(radio_conf.freq);
- SerialUSB.print("Bandwitch = ");
- SerialUSB.println(radio_conf.bw);
- SerialUSB.print("Spreading factor = ");
- SerialUSB.println(radio_conf.sf);
- SerialUSB.print("Coding rate = ");
- SerialUSB.println(radio_conf.cr);
- SerialUSB.print("Synch word = ");
- SerialUSB.println(radio_conf.syncWord);
- SerialUSB.print("Current limit = ");
- SerialUSB.println(radio_conf.power);
- SerialUSB.print("Preamble length = ");
- SerialUSB.println(radio_conf.preambleLength);
- SerialUSB.print("Gain = ");
- SerialUSB.println(radio_conf.gain);
- }
- void loop() {
- // check if the flag is set
- if (receivedFlag)
- {
- // disable the interrupt service routine while
- // processing the data
- enableInterrupt = false;
- // reset flag
- receivedFlag = false;
- // you can read received data as an Arduino String
- String str;
- int state = radio.readData(str);
- // you can also read received data as byte array
- /*
- byte byteArr[8];
- int state = radio.readData(byteArr, 8);
- */
- if (state == RADIOLIB_ERR_NONE)
- {
- // packet was successfully received
- SerialUSB.println(F("[SX1278] Received packet!"));
- // print data of the packet
- SerialUSB.print(F("[SX1278] Data:\t\t"));
- SerialUSB.println(str);
- // print RSSI (Received Signal Strength Indicator)
- SerialUSB.print(F("[SX1278] RSSI:\t\t"));
- SerialUSB.print(radio.getRSSI());
- SerialUSB.println(F(" dBm"));
- // print SNR (Signal-to-Noise Ratio)
- SerialUSB.print(F("[SX1278] SNR:\t\t"));
- SerialUSB.print(radio.getSNR());
- SerialUSB.println(F(" dB"));
- // print frequency error
- SerialUSB.print(F("[SX1278] Frequency error:\t"));
- SerialUSB.print(radio.getFrequencyError());
- SerialUSB.println(F(" Hz"));
- }
- else if (state == RADIOLIB_ERR_CRC_MISMATCH)
- {
- // packet was received, but is malformed
- SerialUSB.println(F("[SX1278] CRC error!"));
- }
- else
- {
- // some other error occurred
- SerialUSB.print(F("[SX1278] Failed, code "));
- SerialUSB.println(state);
- }
- // put module back to listen mode
- radio.startReceive();
- // we're ready to receive more packets,
- // enable interrupt service routine
- enableInterrupt = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement