forked from codeforfrankfurt/firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firmware.ino
313 lines (266 loc) · 7.13 KB
/
firmware.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#define USE_GPS 0
#define USE_LORA 0
#define USE_SDS011 1
#include <TimerTCC0.h>
int sec = 0;
int secSinceStart = 0;
#if USE_SDS011
#include "SDS011.h"
SDS011 sdsSensor;
#define SDS_PIN_TX 5
#define SDS_PIN_RX 6
boolean readSDS;
float pm25;
float pm10;
int sdsErrorCode;
#endif
#if USE_GPS
#include "TinyGPS++.h"
TinyGPSPlus gps;
boolean readGPS = false;
#endif
#if USE_LORA
#include <LoRaWan.h>
#define DEV_ADDR "foo"
#define DEV_EUI "foo"
#define APP_EUI "foo"
#define NWK_S_KEY "foo"
#define APP_S_KEY "foo"
#define APP_KEY "foo"
const float EU_channels[8] = {868.1, 868.3, 868.5, 867.1, 867.3, 867.5, 867.7, 867.9}; //rx 869.525
#define UPLINK_DATA_RATE_MIN DR0 //The min uplink data rate for all countries / plans is DR0
#define UPLINK_DATA_RATE_MAX_EU DR5
#define MAX_EIRP_NDX_EU 5
#define DOWNLINK_DATA_RATE_EU DR3
#define FREQ_RX_WNDW_SCND_EU 869.525
#define DEFAULT_RESPONSE_TIMEOUT 5
unsigned int frame_counter = 1;
char buffer[256];
boolean sendPacket = false;
#endif
void setup() {
SerialUSB.begin(115200);
while(!SerialUSB);
#if USE_SDS011
sdsSensor.begin(SDS_PIN_RX, SDS_PIN_TX);
#endif
#if USE_GPS
char c;
bool locked;
Serial.begin(9600); // open the GPS
locked = false;
// For S&G, let's get the GPS fix now, before we start running arbitary
// delays for the LoRa section
while (!gps.location.isValid()) {
while (Serial.available() > 0) {
if (gps.encode(c=Serial.read())) {
displayGPSInfo();
if (gps.location.isValid()) {
// locked = true;
break;
}
}
// SerialUSB.print(c);
}
// if (locked)
// break;
if (millis() > 15000 && gps.charsProcessed() < 10)
{
SerialUSB.println(F("No GPS detected: check wiring."));
SerialUSB.println(gps.charsProcessed());
while(true);
}
else if (millis() > 20000) {
SerialUSB.println(F("Not able to get a fix in alloted time."));
break;
}
}
#endif
#if USE_LORA
lora.init();
lora.setDeviceDefault();
memset(buffer, 0, 256);
lora.getVersion(buffer, 256, 1);
SerialUSB.print(buffer);
memset(buffer, 0, 256);
lora.getId(buffer, 256, 1);
SerialUSB.print(buffer);
lora.setId(DEV_ADDR, DEV_EUI, APP_EUI);
lora.setKey(NWK_S_KEY, APP_S_KEY, APP_KEY);
lora.setDeciveMode(LWABP);
lora.setDataRate(DR0, EU868); // 0 = SF12 / 125 kHz / 440bps
// 1 = SF11 / 125 kHz / 440bps
// 2 = Max EIRP – 4dB
// 5 = Max EIRP - 10dB - MAX_EIRP_NDX_EU_433
// 7 = Max EIRP - 14dB - MAX_EIRP_NDX_EU_863
// power is signal strength as well, might override above data rate setting
lora.setPower(15);
setChannelsForTTN(EU_channels);
lora.setReceiceWindowFirst(7, 867.9);
lora.setReceiceWindowSecond(FREQ_RX_WNDW_SCND_EU, DOWNLINK_DATA_RATE_EU);
// lora.setDutyCycle(false);
// lora.setJoinDutyCycle(false);
#endif
//TimerTcc0.initialize(60000000); 1 Minute
TimerTcc0.initialize(1000000); //1 second
TimerTcc0.attachInterrupt(timerIsr);
}
//interrupt routine
void timerIsr(void) {
secSinceStart += 1;
#if USE_LORA
// every minute
if (secSinceStart % 60 == 0) {
SerialUSB.println("LORA sendPacket=true");
sendPacket = true;
} else {
sendPacket = false;
}
#endif
// sec is forced to a range of 0-5, so we can decide to do something in either of those seconds
sec = (sec + 1) % 6;
SerialUSB.println(sec);
#if USE_GPS
if (sec == 1) {
Serial.write("h"); //Turn on GPS
}
#endif
#if USE_SDS011
if (sec == 2) {
readSDS = true;
}
#endif
#if USE_GPS
if (sec == 3) {
readGPS = true;
}
#endif
if (sec == 5) {
#if USE_SDS011
readSDS = false;
#endif
#if USE_GPS
displayGPSInfo();
readGPS = false;
Serial.write("$PMTK161,0*28\r\n");
#endif
}
}
void loop(void) {
#if USE_SDS011
if(readSDS) {
// return code is 0, if new values were read, and 1 if there were no new values.
sdsErrorCode = sdsSensor.read(&pm25, &pm10);
if (!sdsErrorCode) {
SerialUSB.println("PM2.5: " + String(pm25));
SerialUSB.println("PM10: " + String(pm10));
} else {
SerialUSB.print(F("SDS Error Code: "));
SerialUSB.println(sdsErrorCode);
}
readSDS = false;
}
#endif
#if USE_GPS
if(readGPS) {
while (Serial.available() > 0){
char currChar = Serial.read();
gps.encode(currChar);
}
}
#endif
#if USE_LORA
if(sendPacket) {
SerialUSB.print("sendAndReceiveLoRaPacket(): ");
SerialUSB.println(frame_counter);
sendAndReceiveLoRaPacket();
frame_counter += 1;
}
#endif
}
#if USE_GPS
void displayGPSInfo()
{
SerialUSB.print(F("Location: "));
if (gps.location.isValid())
{
SerialUSB.print(gps.location.lat(), 6);
SerialUSB.print(F(","));
SerialUSB.print(gps.location.lng(), 6);
}
else
{
SerialUSB.print(F("INVALID"));
}
SerialUSB.print(F(" Date/Time: "));
if (gps.date.isValid())
{
SerialUSB.print(gps.date.month());
SerialUSB.print(F("/"));
SerialUSB.print(gps.date.day());
SerialUSB.print(F("/"));
SerialUSB.print(gps.date.year());
}
else
{
SerialUSB.print(F("INVALID"));
}
SerialUSB.print(F(" "));
if (gps.time.isValid())
{
if (gps.time.hour() < 10) SerialUSB.print(F("0"));
SerialUSB.print(gps.time.hour());
SerialUSB.print(F(":"));
if (gps.time.minute() < 10) SerialUSB.print(F("0"));
SerialUSB.print(gps.time.minute());
SerialUSB.print(F(":"));
if (gps.time.second() < 10) SerialUSB.print(F("0"));
SerialUSB.print(gps.time.second());
SerialUSB.print(F("."));
if (gps.time.centisecond() < 10) SerialUSB.print(F("0"));
SerialUSB.print(gps.time.centisecond());
}
else
{
SerialUSB.print(F("INVALID"));
}
SerialUSB.println();
}
#endif
#if USE_LORA
void setChannelsForTTN(const float* channels) {
for(int i = 0; i < 8; i++){
// DR0 is the min data rate
// UPLINK_DATA_RATE_MAX_EU = DR5 is the max data rate for the EU
if(channels[i] != 0){
lora.setChannel(i, channels[i], UPLINK_DATA_RATE_MIN, UPLINK_DATA_RATE_MAX_EU);
}
}
}
void sendAndReceiveLoRaPacket() {
bool result = false;
char payload[256];
memset(payload, 0, 256);
String(frame_counter).toCharArray(payload, 256);
result = lora.transferPacket(payload, 10);
if(result) {
short length;
short rssi;
memset(buffer, 0, 256);
length = lora.receivePacket(buffer, 256, &rssi);
if(length) {
SerialUSB.print("Length is: ");
SerialUSB.println(length);
SerialUSB.print("RSSI is: ");
SerialUSB.println(rssi);
SerialUSB.print("Data is: ");
for(unsigned char i = 0; i < length; i ++) {
SerialUSB.print("0x");
SerialUSB.print(buffer[i], HEX);
SerialUSB.print(" ");
}
SerialUSB.println();
}
}
}
#endif