-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathSX127x_Receive_FHSS.ino
189 lines (153 loc) · 5.27 KB
/
SX127x_Receive_FHSS.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
RadioLib SX127x Transmit with Frequency Hopping Example
This example transmits packets using SX1278 LoRa radio module.
Each packet contains up to 256 bytes of data, in the form of:
- Arduino String
- null-terminated char array (C-string)
- arbitrary binary data (byte array)
Other modules from SX127x/RFM9x family can also be used.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
SX127x supports FHSS or Frequency Hopping Spread Spectrum.
Once a hopping period is set and a transmission is started, the radio
will begin triggering interrupts every hop period where the radio frequency
is changed to the next channel.
*/
#include <RadioLib.h>
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio = new RadioModule();
*/
// flag to indicate that a packet was received
volatile bool receivedFlag = false;
// flag to indicate frequency must be changed
volatile bool fhssChangeFlag = false;
// the channel frequencies can be generated randomly or hard coded
// NOTE: The frequency list MUST be the same on both sides!
float channels[] = { 433.0, 433.4, 433.2, 433.6, 434.0, 433.8 };
int numberOfChannels = sizeof(channels) / sizeof(float);
// counter to keep track of how many frequency hops were performed
int hopsCompleted = 0;
// 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 setRxFlag(void) {
receivedFlag = true;
}
// this function is called when FhssChangeChannel interrupt occurs
// (at the beginning of each transmission)
// IMPORTANT: this function MUST be 'void' type
// and MUST NOT have any arguments!
#if defined(ESP8266) || defined(ESP32)
ICACHE_RAM_ATTR
#endif
void setFHSSFlag(void) {
fhssChangeFlag = true;
}
void setup() {
Serial.begin(9600);
// begin radio on home channel
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.begin(channels[0]);
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// set hop period in symbols
// this will also enable FHSS
Serial.print(F("[SX1278] Setting hopping period ... "));
state = radio.setFHSSHoppingPeriod(9);
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// set the function to call when reception is finished
radio.setDio0Action(setRxFlag, RISING);
// set the function to call when we need to change frequency
radio.setDio1Action(setFHSSFlag, RISING);
// start listening for LoRa packets
Serial.print(F("[SX1278] 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) { delay(10); }
}
}
void loop() {
// check if the reception flag is set
if (receivedFlag == true) {
// 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 numBytes = radio.getPacketLength();
int state = radio.readData(byteArr, numBytes);
*/
if (state == RADIOLIB_ERR_NONE) {
// packet was successfully received
Serial.println(F("[SX1278] Received packet!"));
// print data of the packet
Serial.print(F("[SX1278] Data:\t\t"));
Serial.println(str);
} else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
// packet was received, but is malformed
Serial.println(F("[SX1278] CRC error!"));
} else {
// some other error occurred
Serial.print(F("[SX1278] Failed, code "));
Serial.println(state);
}
// print the number of hops it took
Serial.print(F("[SX1278] Hops completed: "));
Serial.println(hopsCompleted);
// reset the counter
hopsCompleted = 0;
// return to home channel before the next transaction
radio.setFrequency(channels[0]);
// put the module back to listen mode
radio.startReceive();
// we're ready to receive more packets, clear the flag
receivedFlag = false;
}
// check if we need to do another frequency hop
if (fhssChangeFlag == true) {
// we do, change it now
int state = radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]);
if (state != RADIOLIB_ERR_NONE) {
Serial.print(F("[SX1278] Failed to change frequency, code "));
Serial.println(state);
}
// increment the counter
hopsCompleted++;
// clear the FHSS interrupt
radio.clearFHSSInt();
// we're ready to do another hop, clear the flag
fhssChangeFlag = false;
}
}