-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver_RX_RAW.ino
172 lines (149 loc) · 4.04 KB
/
receiver_RX_RAW.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
/*
Project Name: receiver
Developer: Eric Klein Jr. (temp2@ericklein.com)
Description: Simple communication between 900mhz radios, adapted from RadioHead69_RawDemo_RX
See README.md for target information, revision history, feature requests, etc.
*/
#define DEBUG // Output to the serial port
#define EXT_DEBUG // Output to external LCD display
#include <SPI.h>
#include <RH_RF69.h>
#ifdef EXT_DEBUG
#include <Adafruit_LiquidCrystal.h>
#include <Wire.h>
// LCD connection via i2c, default address #0 (A0-A2 not jumpered)
Adafruit_LiquidCrystal lcd(0);
#endif
// Radio setup
// 400-[433]-600 for LoRa radio, 850-[868,915]-950 for 900mhz, must match transmitter
#define RF69_FREQ 915.0
// Conditional code compiles
#if defined(ARDUINO_SAMD_FEATHER_M0)
#define RFM69_CS 8
#define RFM69_INT 3
#define RFM69_RST 4
#endif
#if defined (__AVR_ATmega328P__)
#define RFM69_INT 3 //
#define RFM69_CS 4 //
#define RFM69_RST 2 // "A"
#endif
// Instantiate radio driver
RH_RF69 rf69(RFM69_CS, RFM69_INT);
// packet counter, incremented per transmission
int16_t packetnum = 0;
void setup()
{
#ifdef DEBUG
Serial.begin(115200);
while (!Serial) ;
#endif
#ifdef EXT_DEBUG
//LCD setup
lcd.begin(16, 2);
lcd.setBacklight(HIGH);
#endif
// radio setup
pinMode(RFM69_RST, OUTPUT);
digitalWrite(RFM69_RST, LOW);
// radio reset
digitalWrite(RFM69_RST, HIGH);
delay(10);
digitalWrite(RFM69_RST, LOW);
delay(10);
if (!rf69.init())
{
#ifdef DEBUG
Serial.println("RFM69 radio init failed");
#endif
#ifdef EXT_DEBUG
displayMessage(0, "Radio init failed");
#endif
while (1);
}
// Default after radio init is 434.0MHz
if (!rf69.setFrequency(RF69_FREQ))
{
#ifdef DEBUG
Serial.println("RFM69 set frequency failed");
#endif
#ifdef EXT_DEBUG
displayMessage(0, "Can't set frequency");
#endif
while (1);
}
// Defaults after radio init are modulation GFSK_Rb250Fd250, +13dbM (for low power module), no encryption
// For RFM69HW, 14-20dbm for power, 2nd arg must be true for 69HCW
rf69.setTxPower(20, true);
// The encryption key has to match the transmitter
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
rf69.setEncryptionKey(key);
// radio successfully set up
#ifdef DEBUG
Serial.print("RFM69 radio OK @ ");
Serial.print((int)RF69_FREQ);
Serial.println("MHz. This node is receiver.");
#endif
#ifdef EXT_DEBUG
displayMessage(0, "Radio OK, =>receiver");
delay(2000);
#endif
}
void loop()
{
if (rf69.available())
{
// Should be a message for us now
uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf69.recv(buf, &len))
{
if (!len) return;
buf[len] = 0;
// display message
#ifdef DEBUG
Serial.print("Received: ");
Serial.println((char*)buf);
Serial.print("RSSI:");
Serial.println(rf69.lastRssi(), DEC);
#endif
#ifdef EXT_DEBUG
displayMessage(0, (char*)buf);
//displayMessage(1,(int) rf69.lastRssi());
String rssi_Message = "RSSI: ";
rssi_Message += rf69.lastRssi();
displayMessage(1, rssi_Message);
//displayMessage(1, (char*)rf69.lastRssi());
#endif
// respond to a specific message
if (strstr((char *)buf, "Hello World"))
{
// Send a reply!
uint8_t data[] = "And hello back to you";
rf69.send(data, sizeof(data));
rf69.waitPacketSent();
#ifdef DEBUG
Serial.println("Reply message sent");
#endif
}
else
{
#ifdef DEBUG
Serial.println("Receive failed");
#endif
#ifdef EXT_DEBUG
displayMessage(0,"No message received");
#endif
}
}
}
}
#ifdef EXT_DEBUG
void displayMessage(int row, String message)
// first row is zero
{
lcd.setCursor(0, row);
lcd.print(message);
}
#endif