-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadioLibPicoExample.cpp
More file actions
138 lines (111 loc) · 3.55 KB
/
RadioLibPicoExample.cpp
File metadata and controls
138 lines (111 loc) · 3.55 KB
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
/*
A copy of the "RadioLib SX127x Receive with Interrupts Example"
modified to build for the Raspberry Pico without Arduino
****
This example listens for LoRa transmissions and tries to
receive them. Once a packet is received, an interrupt is
triggered. To successfully receive data, the following
settings have to be the same on both transmitter
and receiver:
- carrier frequency
- bandwidth
- spreading factor
- coding rate
- sync word
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/
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "config.h"
#include <RadioLib.h>
volatile bool receivedFlag = false;
volatile bool enableInterrupt = true;
void setFlag()
{
if(!enableInterrupt)
return;
// we got a packet, set the flag
receivedFlag = true;
}
int main()
{
stdio_init_all();
// Pins set in config.h
SX1276 radio = new Module(PIN_CS, PIN_DI0, PIN_RST, PIN_DI1);
printf("[SX1276] Initializing ... ");
int state = radio.begin(868, 125.0, 9, 7, 0x12, 10, 8, 0);
if (state == RADIOLIB_ERR_NONE)
{
printf("success!\n");
}
else
{
printf("failed, code: %d\n", state);
while (true);
}
radio.setDio0Action(setFlag);
printf("[SX1276] Starting to listen ...");
state = radio.startReceive();
if (state == RADIOLIB_ERR_NONE)
{
printf("success!\n");
}
else
{
printf("failed, code %d\n", state);
while (true);
}
while (1)
{
// 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
std::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
printf("[SX1276] Received packet!\n");
// print data of the packet
printf("[SX1276] Data:\t\t %s\n", str.c_str());
// print RSSI (Received Signal Strength Indicator)
printf("[SX1276] RSSI:\t\t %f dBm\n", radio.getRSSI());
// print SNR (Signal-to-Noise Ratio)
printf("[SX1276] SNR:\t\t %f dB\n", radio.getSNR());
// print frequency error
printf("[SX1276] Frequency error:\t %f\n", radio.getFrequencyError());
}
else if (state == RADIOLIB_ERR_CRC_MISMATCH)
{
// packet was received, but is malformed
printf("[SX1276] CRC error!");
}
else
{
// some other error occurred
printf("[SX1276] Failed, code %d\n", state);
}
// put module back to listen mode
radio.startReceive();
// we're ready to receive more packets,
// enable interrupt service routine
enableInterrupt = true;
}
}
return 0;
}