|
| 1 | +#include <SX127x.h> |
| 2 | + |
| 3 | +SX127x LoRa; |
| 4 | + |
| 5 | +// receive data length and pointer |
| 6 | +const uint8_t packetLength = 15; |
| 7 | +uint8_t packetData[packetLength]; |
| 8 | +volatile bool flag = false; |
| 9 | + |
| 10 | +void setup() { |
| 11 | + |
| 12 | + // Begin serial communication |
| 13 | + Serial.begin(38400); |
| 14 | + |
| 15 | + // Begin LoRa radio and set NSS, reset, txen, and rxen pin with connected arduino pins |
| 16 | + Serial.println("Begin LoRa radio"); |
| 17 | + int8_t nssPin = 10, resetPin = 9, irqPin = 2, txenPin = 8, rxenPin = 7; |
| 18 | + if (!LoRa.begin(nssPin, resetPin, irqPin, txenPin, rxenPin)){ |
| 19 | + Serial.println("Something wrong, can't begin LoRa radio"); |
| 20 | + while(1); |
| 21 | + } |
| 22 | + |
| 23 | + // Set frequency to 915 Mhz |
| 24 | + Serial.println("Set frequency to 915 Mhz"); |
| 25 | + LoRa.setFrequency(915E6); |
| 26 | + |
| 27 | + // Set RX gain. RX gain option are power saving gain or boosted gain |
| 28 | + Serial.println("Set RX gain to power saving gain"); |
| 29 | + LoRa.setRxGain(SX127X_RX_GAIN_AUTO, false); |
| 30 | + |
| 31 | + // Configure modulation parameter including spreading factor (SF), bandwidth (BW), and coding rate (CR) |
| 32 | + // Transmitter must have same SF and BW setting so receiver can receive LoRa packet |
| 33 | + Serial.println("Set modulation parameters:\n\tSpreading factor = 7\n\tBandwidth = 125 kHz\n\tCoding rate = 4/5"); |
| 34 | + LoRa.setSpreadingFactor(7); |
| 35 | + LoRa.setBandwidth(125000); |
| 36 | + LoRa.setCodeRate(5); |
| 37 | + |
| 38 | + // Configure packet parameter including header type, preamble length, payload length, and CRC type |
| 39 | + // The explicit packet includes header contain CR, number of byte, and CRC type |
| 40 | + // Packet with explicit header can't be received by receiver with implicit header mode |
| 41 | + Serial.println("Set packet parameters:\n\tExplicit header type\n\tPreamble length = 12\n\tPayload Length = 15\n\tCRC on"); |
| 42 | + LoRa.setHeaderType(SX127X_HEADER_EXPLICIT); |
| 43 | + LoRa.setPreambleLength(12); |
| 44 | + LoRa.setPayloadLength(15); |
| 45 | + LoRa.setCrcType(true); |
| 46 | + |
| 47 | + // Set syncronize word for public network (0x3444) |
| 48 | + Serial.println("Set syncronize word to 0x3444"); |
| 49 | + LoRa.setSyncWord(0x34); |
| 50 | + |
| 51 | + Serial.println("\n-- LoRa RECEIVER CALLBACK --\n"); |
| 52 | + |
| 53 | + // Register callback function to be called every RX done |
| 54 | + LoRa.onReceive(getReceiveData); |
| 55 | + |
| 56 | + // Begin request LoRa packet in continuous mode |
| 57 | + LoRa.request(SX127X_RX_CONTINUOUS); |
| 58 | +} |
| 59 | + |
| 60 | +void loop() { |
| 61 | + |
| 62 | + if (flag) { |
| 63 | + // Print received package |
| 64 | + Serial.write(packetData, packetLength - 1); |
| 65 | + Serial.print(" "); |
| 66 | + Serial.println(packetData[packetLength - 1]); |
| 67 | + |
| 68 | + // Print packet/signal status including RSSI, SNR, and signalRSSI |
| 69 | + Serial.print("Packet status: RSSI = "); |
| 70 | + Serial.print(LoRa.packetRssi()); |
| 71 | + Serial.print(" dBm | SNR = "); |
| 72 | + Serial.print(LoRa.snr()); |
| 73 | + Serial.println(" dB"); |
| 74 | + Serial.println(); |
| 75 | + |
| 76 | + // Reset flag |
| 77 | + flag = false; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void getReceiveData() { |
| 82 | + |
| 83 | + // set flag |
| 84 | + flag = true; |
| 85 | + // Store received data |
| 86 | + for (uint8_t i=0; i<packetLength; i++) { |
| 87 | + packetData[i] = LoRa.read(); |
| 88 | + } |
| 89 | +} |
0 commit comments