-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoRa_RX.ino
164 lines (141 loc) · 3.85 KB
/
LoRa_RX.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
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>
// OLED SSD1306
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// time sync
#include <WiFi.h>
#include "time.h"
const char* ssid = "";
const char* password = "";
//const char* ntpServer = "de.pool.ntp.org";
const char* ntpServer = "192.168.1.1";
// settings for .DE
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
// define the pins used by the LoRa transceiver module
// these can be different on different devices
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 23 // 14
#define DIO0 26
// 433E6 for Asia, 866E6 for Europe, 915E6 for North America
#define BAND 866E6
// OLED pins
// these can be different on different devices
#define OLED_SDA 21 //4
#define OLED_SCL 22 // 15
#define OLED_RST 23 // 16
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
String LoRaData, tmpData;
int ReceivedPackets = 0;
void setup() {
Serial.begin(115200);
Serial.println("========== LoRa RX ==========");
//reset OLED display via software
pinMode(OLED_RST, OUTPUT);
digitalWrite(OLED_RST, LOW);
delay(20);
digitalWrite(OLED_RST, HIGH);
//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("WiFi: ");
// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.print(ssid);
display.print(ssid);
display.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("\nWiFi connected, IP: ");
Serial.println(WiFi.localIP());
display.setCursor(0,10);
display.print("IP: ");
display.print(WiFi.localIP());
display.display();
// Init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
Serial.println(printLocalTime());
display.setCursor(0,20);
display.print("LoRa RX Init OK");
display.setCursor(0,30);
display.print("Boottime: ");
display.setCursor(78,30);
display.print(printLocalTime());
display.setCursor(0,40);
display.print("Receiving...");
display.display();
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("Init OK");
display.display();
}
void loop() {
//try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
ReceivedPackets++;
Serial.print("Received packet ");
LoRaData = "";
while (LoRa.available()) {
LoRaData += LoRa.readString();
}
Serial.print(LoRaData);
int rssi = LoRa.packetRssi();
Serial.print("\nwith RSSI ");
Serial.println(rssi);
display.clearDisplay();
display.setCursor(0,0);
display.print("#");
display.setCursor(6,0);
display.print(ReceivedPackets);
display.setCursor(36,0);
display.print(rssi);
display.setCursor(60,0);
display.print("dB");
display.setCursor(78,0);
display.print(printLocalTime());
display.setCursor(0,10);
display.print(LoRaData);
display.display();
}
}
String printLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
return("--:--:--");
} else {
char strTime[9];
strftime(strTime, sizeof strTime, "%H:%M:%S", &timeinfo);
return(strTime);
}
}
String old_printLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
// Serial.println("Failed to obtain time");
return(" no time");
}
}