forked from DEVPAR/LoRa_TRX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoRa_TRX.ino
248 lines (198 loc) · 6.02 KB
/
LoRa_TRX.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Adapted for TTGO T3 1.6.1 LoRa32 board
#include "BluetoothSerial.h"
#include <stdio.h>
// used to get BT MAC
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h> // by Sandeep Mistry
//Libraries for OLED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
/*Frequency setup*/
#define BAND 869525000 //868 MHz band
//#define BAND 433875000 //433 MHz band
//Board: TTGO T3 1.6.1 LoRa32 board
//LoRa and OLED Pins
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 23
#define DIO0 26
#define DIO1 0 // GPIO 33 NON EXISTENT ON TTGO T3 1.6.1
#define DIO2 0 // GPIO 32 NON EXISTENT ON TTGO T3 1.6.1
//OLED pins
#define OLED_SDA 21
#define OLED_SCL 22
#define OLED_RST 0 // NON EXISTENT ON TTGO T3 1.6.1
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define LED_BUILTIN 25
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
BluetoothSerial SerialBT;
String LoRaData;
String message;
char str[7];
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // green led
Serial.begin(115200);
initBluetooth();
const uint8_t* point = esp_bt_dev_get_address(); //get BT MAC address last 3 hex digits
sprintf(str, "%02X%02X%02X", (int)point[3], (int)point[4], (int)point[5]);
SerialBT.begin(str); //Bluetooth device name
Serial.println("Started, pair it with bluetooth!");
//initialize OLED
Wire.begin(OLED_SDA, OLED_SCL);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("LoRa_TRX by DM5LG");
display.display();
Serial.println("LoRa Receiver Test");
//SPI LoRa pins
SPI.begin(SCK, MISO, MOSI, SS);
//setup LoRa transceiver module
LoRa.setPins(SS, RST, DIO0);
if (!LoRa.begin(BAND)) {
Serial.println("Starting LoRa failed!");
while (1)
;
}
Serial.println("LoRa Initializing OK!");
display.setCursor(0, 10);
display.setTextSize(1);
display.println("Version 0.2 Beta");
display.display();
//LoRa settings, same as meshtastic defaults long-fast, SF11 BW250 CR8
LoRa.setSpreadingFactor(11);
LoRa.setSignalBandwidth(250000);
LoRa.setCodingRate4(8);
// Change sync word to match the sender
// The sync word assures you don't get LoRa messages from other LoRa transceivers
// ranges from 0x00 - 0xFF, 0x12 is private network (default)
LoRa.setSyncWord(0x12);
LoRa.enableCrc();
LoRa.setTxPower(12); // dBm
delay(5000);
display.clearDisplay();
display.setCursor(0, 10);
display.setTextSize(1);
display.print("LORA RX ");
display.println(str);
display.display();
// LoRa send start BT digit, kind of a start ping
LoRa.beginPacket();
LoRa.println(str);
LoRa.endPacket();
}
void loop() {
// usbserial input also
while (Serial.available()) {
// read the incoming byte:
char incomingThing = Serial.read();
message += String(incomingThing);
// sending a packet
if (incomingThing == '\n') {
display.clearDisplay();
display.setCursor(0, 0);
display.println("LORA TX");
display.display();
LoRa.beginPacket();
LoRa.print(message);
LoRa.endPacket();
display.clearDisplay();
display.setCursor(0, 10);
display.print("LORA RX ");
display.println(str);
display.display();
Serial.println(message);
}
}
// BT input too
while (SerialBT.available()) {
//Serial.write(SerialBT.read());
char incomingChar = SerialBT.read();
message += String(incomingChar);
// sending a packet
if (incomingChar == '\n') {
display.clearDisplay();
display.setCursor(0, 0);
display.println("LORA TX");
display.display();
LoRa.beginPacket();
LoRa.print(message);
LoRa.endPacket();
display.clearDisplay();
display.setCursor(0, 10);
display.print("LORA RX ");
display.println(str);
display.display();
}
}
//try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
//received a packet
Serial.print("Received packet: ");
//read packet
while (LoRa.available()) {
LoRaData = LoRa.readString();
Serial.print(LoRaData);
}
//print RSSI and SNR of packet
int rssi = LoRa.packetRssi();
float snr = LoRa.packetSnr();
Serial.print(" with RSSI ");
Serial.println(rssi);
Serial.print(" with SNR ");
Serial.println(snr);
//send LoRaData and RSSI and SNR via SerialBT
SerialBT.println(LoRaData + "RSSI:" + rssi + " dBm " + "SNR:" + snr + " dB");
// Display information
display.clearDisplay();
display.setCursor(0, 0);
display.print("Received packet:");
display.setCursor(0, 10);
display.print("RSSI:");
display.setCursor(30, 10);
display.print(rssi);
display.setCursor(0, 20);
display.print("SNR:");
display.setCursor(30, 20);
display.print(snr);
display.setCursor(0, 30);
display.print(LoRaData);
display.display();
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on to signal a received sentence
delay(100); // wait for 100 milliseconds
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
}
message = "";
}
// this seems necessary to retrieve the BT MAC address, might be done better in some other way
bool initBluetooth() {
if (!btStart()) {
Serial.println("Failed to initialize controller");
return false;
}
if (esp_bluedroid_init() != ESP_OK) {
Serial.println("Failed to initialize bluedroid");
return false;
}
if (esp_bluedroid_enable() != ESP_OK) {
Serial.println("Failed to enable bluedroid");
return false;
}
}