-
Notifications
You must be signed in to change notification settings - Fork 0
/
dustnet-mapper.ino
262 lines (213 loc) · 5.93 KB
/
dustnet-mapper.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
/* Copyright 2018 Dustmap.org. All rights reserved.
*
* This work is licensed under the terms of the MIT license.
* For a copy, see <https://opensource.org/licenses/MIT>.
*/
#include <lmic.h>
#include <hal/hal.h>
#include <SSD1306.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include "config_000002.h"
#define STATUS_DISCONNECTED 0
#define STATUS_JOINING 1
#define STATUS_JOIN_TIMEOUT 2
#define STATUS_JOIN_FAILED 3
#define STATUS_CONNECTED 4
#define STATUS_PACKAGE_SENT 5
SSD1306 display(0x3c, 4, 15);
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
uint8_t status = STATUS_DISCONNECTED;
int sentCount = 0;
int lastContact = 0;
bool bleConnected = false;
uint16_t joining_duration = 0;
BLECharacteristic *pCharacteristic;
BLECharacteristic *gpsCharacteristic;
static osjob_t sendjob;
static u1_t devEUI[8] = DEVEUI;
void os_getDevEui (u1_t* buf) {
memcpy(buf, devEUI, 8);
}
static const u1_t PROGMEM appEUI[8] = APPEUI;
void os_getArtEui (u1_t* buf) {
memcpy_P(buf, appEUI, 8);
}
static const u1_t PROGMEM appKey[16] = APPKEY;
void os_getDevKey (u1_t* buf) {
memcpy_P(buf, appKey, 16);
}
const lmic_pinmap lmic_pins = {
.nss = LMIC_PIN_NSS,
.rxtx = LMIC_UNUSED_PIN,
.rst = LMIC_UNUSED_PIN,
.dio = LMIC_PIN_DIO,
};
void onEvent (ev_t ev) {
switch(ev) {
case EV_JOINING:
status = STATUS_JOINING;
joining_duration = 0;
break;
case EV_JOINED:
status = STATUS_CONNECTED;
LMIC_setDrTxpow(DR_SF7, 14);
LMIC_setLinkCheckMode(0);
break;
case EV_JOIN_FAILED:
status = STATUS_JOIN_FAILED;
break;
case EV_REJOIN_FAILED:
status = STATUS_JOIN_FAILED;
break;
case EV_TXCOMPLETE:
status = STATUS_PACKAGE_SENT;
sentCount++;
digitalWrite(25, LOW);
break;
case EV_LINK_DEAD:
status = STATUS_DISCONNECTED;
digitalWrite(25, LOW);
break;
}
}
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
bleConnected = true;
sentCount = 0;
};
void onDisconnect(BLEServer* pServer) {
status = STATUS_DISCONNECTED;
bleConnected = false;
digitalWrite(25, LOW);
LMIC_reset();
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
}
};
class gpsCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
byte payload[rxValue.length()];
Serial.print("gps_Callbacks Value: ");
for (int i = 0; i < rxValue.length(); i++) {
Serial.print(rxValue[i]);
payload[i] = rxValue[i];
}
Serial.println();
lastContact = 0;
if (LMIC.opmode & OP_TXRXPEND) {
} else {
digitalWrite(25, HIGH);
LMIC_reset();
LMIC_setTxData2(1, payload, sizeof(payload), 0);
}
}
}
};
void IRAM_ATTR onTimer() {
portENTER_CRITICAL_ISR(&timerMux);
lastContact++;
if (status == STATUS_JOINING) {
joining_duration++;
}
portEXIT_CRITICAL_ISR(&timerMux);
}
void setup() {
Serial.begin(115200);
pinMode(16,OUTPUT);
pinMode(25,OUTPUT);
digitalWrite(16, LOW);
delay(50);
digitalWrite(16, HIGH);
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
delay(1000);
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 1000000, true);
timerAlarmEnable(timer);
BLEDevice::init(DEVICE_NAME);
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->setValue("Disconnected");
pCharacteristic->setCallbacks(new MyCallbacks());
gpsCharacteristic = pService->createCharacteristic(
GPS_CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
gpsCharacteristic->setCallbacks(new gpsCallbacks());
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
os_init();
LMIC_reset();
pCharacteristic->notify();
LMIC.dn2Dr = DR_SF9;
}
void loop() {
os_runloop_once();
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0, 0, (String)DEVICE_NAME);
switch (status) {
case STATUS_DISCONNECTED:
display.drawString(0, 10, "Status: Disconnected");
pCharacteristic->setValue("Disconnected");
break;
case STATUS_JOINING:
display.drawString(0, 10, "Status: Joining");
pCharacteristic->setValue("Joining");
break;
case STATUS_JOIN_TIMEOUT:
display.drawString(0, 10, "Status: Join Timeout");
pCharacteristic->setValue("Join Timeout");
break;
case STATUS_JOIN_FAILED:
display.drawString(0, 10, "Status: Join Failed");
pCharacteristic->setValue("Join Failed");
break;
case STATUS_CONNECTED:
display.drawString(0, 10, "Status: Connected");
pCharacteristic->setValue("Connected");
break;
case STATUS_PACKAGE_SENT:
display.drawString(0, 10, "Status: Package sent");
pCharacteristic->setValue("Package sent");
break;
}
if (sentCount > 0) {
uint8_t seconds = lastContact % 60;
uint8_t minutes = (lastContact / 60) % 60;
uint8_t hours = lastContact / 3600;
char str[12];
sprintf( str, "%02u:%02u:%02u", hours, minutes, seconds);
display.drawString(0, 30, "Last contact: " + (String)str);
display.drawString(0, 40, "Packages sent: " + (String)sentCount);
}
if (bleConnected) {
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(128, 0, "B");
}
display.display();
if (joining_duration > MAX_JOINING_DURATION) {
status = STATUS_JOIN_TIMEOUT;
joining_duration = 0;
}
if (status == STATUS_PACKAGE_SENT) {
status = STATUS_DISCONNECTED;
}
}