-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh_.cpp
More file actions
449 lines (349 loc) · 11.9 KB
/
mesh_.cpp
File metadata and controls
449 lines (349 loc) · 11.9 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Mesh imports
#include "mesh_.h"
#include "mesh_sensor_.h"
// General imports
#include <Wire.h>
// Bluetooth imports
#include <SoftwareSerial.h>
// Clock imports
#include <DS3232RTC.h>
// Storage imports
#include <SD.h>
#include <SPI.h>
uint8_t MeshOS::_CONTROL_VERSION = 1;
String MeshOS::_SAMPLE_SPLITTER = "\n";
String MeshOS::_DATA_CHUNK_SPLITTER = "^";
String MeshOS::_INTRADATA_SPLITTER = "<>";
#define STATE_INITIALIZING -1
#define STATE_SLEEPING 0
#define STATE_AWAKE 1
#define MILLISECONDS_TO_SECONDS 1000
#define SECONDS_TO_MINUTES 60
#define MINUTES_TO_HOURS 60
#define HOURS_DAYS 24
#define _BAUD_RATE 9600
#define _BAUD_RATE_COMMS 9600
#define MINUTES_BETWEEN_TRANSMISSIONS 60
#define MAXIMUM_TRANSMISSIONS 1000
/* * * * * * * * * * * * * * * * * * * * * * * *
Configuration: Node Set up
* * * * * * * * * * * * * * * * * * * * * * * */
MeshOS::MeshOS(
String NODE_NAME,
uint8_t NODE_VERSION,
uint8_t BLT_RXD_PIN,
uint8_t BLT_TXD_PIN,
uint8_t BLT_LED_PING_PIN,
uint8_t SD_CS_PIN,
float SECONDS_BETWEEN_SAMPLES
) :
_nodeCommsPort(BLT_RXD_PIN, BLT_TXD_PIN)
{
_NODE_NAME = NODE_NAME;
_NODE_VERSION = NODE_VERSION;
_BLT_LED_PING_PIN = BLT_LED_PING_PIN;
_SD_CS_PIN = SD_CS_PIN;
_SAMPLE_DELAY = SECONDS_BETWEEN_SAMPLES * MILLISECONDS_TO_SECONDS;
_TRANSMISSION_DELAY = MINUTES_BETWEEN_TRANSMISSIONS * SECONDS_TO_MINUTES * MILLISECONDS_TO_SECONDS;
}
/* * * * * * * * * * * * * * * * * * * * * * * *
SETUP: INITIALIZE
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::initialize(MeshSensorCtrl** sensorRelay) {
Serial.begin(_BAUD_RATE);
Wire.begin(_BAUD_RATE);
Serial.println('*');
_sensorRelay = sensorRelay;
_controlState = STATE_INITIALIZING;
// Initialize connected hardware
if (_IS_ENABLED_CLOCK) { _initializeClock(); };
if (_IS_ENABLED_COMMS) { _initializeWirelessCommunication(); };
if (_IS_ENABLED_STORAGE) { _initializeStorage(); };
// Initialize all the sensors
String currentSensorName;
String currentFilename;
for (int sensorRelayIndex = 0; sensorRelayIndex < sizeof(_sensorRelay); sensorRelayIndex++) {
_sensorRelay[sensorRelayIndex]->initialize();
_createSensorRelayDataFiles(sensorRelayIndex);
}
// Start sample times
_startTimeStamp = now();
_currentTimeStamp = _startTimeStamp;
_lastSensorRelaySample = 0;
_lastTransmission = 0;
_hasStartedSampling = false;
_sleep();
}
/* * * * * * * * * * * * * * * * * * * * * * * *
SETUP: Set up the clock
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::_initializeClock() {
// Strictly for setting time
// setTime(8, 51, 25, 18, 2, 2019);
// RTC.set(now());
setSyncProvider(RTC.get);
}
/* * * * * * * * * * * * * * * * * * * * * * * *
SETUP: Set up the SD storage system
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::_initializeStorage() {
pinMode(_SD_CS_PIN, OUTPUT);
bool sdCardStatus = SD.begin();
if (!sdCardStatus) {
Serial.println('x');
}
}
/* * * * * * * * * * * * * * * * * * * * * * * *
SETUP: Set up the bluetooth communication
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::_initializeWirelessCommunication() {
_nodeCommsPort.begin(_BAUD_RATE_COMMS);
}
/* * * * * * * * * * * * * * * * * * * * * * * *
COMMAND: Looping node function
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::loop() {
_currentTimeStamp = now();
unsigned long _millisecondsDelta = millis() - _timeStartSync;
if (!_hasStartedSampling) {
// Check frequently to sync with the turn over of a second to start sampling
_hasStartedSampling = _startTimeStamp != _currentTimeStamp;
// Set delta
if (_hasStartedSampling) {
Serial.println('o');
_startTimeStamp = _currentTimeStamp;
_timeStartSync = millis();
}
} else {
// Sample the sensors at a set frequency
if ((_millisecondsDelta - _lastSensorRelaySample) >= _SAMPLE_DELAY) {
Serial.println('.');
_sampleSensorRelay();
if(_IS_ENABLED_SAMPLE_TIME_TRANSMISSION) {
}
_lastSensorRelaySample = _millisecondsDelta;
}
// Send data at a set freqency
// if ((_millisecondsDelta - _lastTransmission) >= _TRANSMISSION_DELAY) {
// // Stop sampling when in transmission mode
// _hasStartedSampling = false;
// // Begin transmission
// _transmitSensorRelayFiles();
// _lastTransmission = _millisecondsDelta;
// }
}
}
// /* * * * * * * * * * * * * * * * * * * * * * * *
// MESH CONTROL: Data chunk splitter
// * * * * * * * * * * * * * * * * * * * * * * * */
static String MeshOS::getDataChunkSplitter() {
return MeshOS::_DATA_CHUNK_SPLITTER;
}
// /* * * * * * * * * * * * * * * * * * * * * * * *
// MESH CONTROL: Intradata Splitter
// * * * * * * * * * * * * * * * * * * * * * * * */
static String MeshOS::getIntraDataSplitter() {
return MeshOS::_INTRADATA_SPLITTER;
}
/* * * * * * * * * * * * * * * * * * * * * * * *
NODE SPECIFIC: Toggle comms
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::toggleSettingsComms(bool componentSettings) {
_IS_ENABLED_COMMS = componentSettings;
}
/* * * * * * * * * * * * * * * * * * * * * * * *
NODE SPECIFIC: Toggle clock
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::toggleSettingsClock(bool componentSettings) {
_IS_ENABLED_CLOCK = componentSettings;
}
/* * * * * * * * * * * * * * * * * * * * * * * *
NODE SPECIFIC: Toggle storage
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::toggleSettingsStorage(bool componentSettings) {
_IS_ENABLED_STORAGE = componentSettings;
}
/* * * * * * * * * * * * * * * * * * * * * * * *
NODE SPECIFIC: Toggle sample transmission
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::toggleSettingsSampleTimeTransmission(bool componentSettings) {
_IS_ENABLED_SAMPLE_TIME_TRANSMISSION = componentSettings;
}
/* * * * * * * * * * * * * * * * * * * * * * * *
NODE SPECIFIC: Read state
* * * * * * * * * * * * * * * * * * * * * * * */
int MeshOS::getControlState() {
return _controlState;
}
/* * * * * * * * * * * * * * * * * * * * * * * *
INTERNAL: Awake mode
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::_awake() {
Serial.println('<');
_controlState = STATE_AWAKE;
// TODO: Wake up all sensors and periphery connections
// Check for any imcoming messages
if (_nodeCommsPort.available() > 0) {
_receivedIncomingCommunication();
}
}
/* * * * * * * * * * * * * * * * * * * * * * * *
INTERNAL: Sleep mode - power conservation
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::_sleep() {
_controlState = STATE_SLEEPING;
// TODO: Put to sleep all periphery connections
Serial.println('>');
}
/* * * * * * * * * * * * * * * * * * * * * * * *
INTERNAL: Sample all sensors
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::_sampleSensorRelay() {
_awake();
for (int sensorRelayIndex = 0; sensorRelayIndex < sizeof(_sensorRelay); sensorRelayIndex++) {
// Saving separately because memory issues timestamps getting written
if (_IS_ENABLED_STORAGE) {
_storeCurrentSensorReading(sensorRelayIndex);
}
if (_IS_ENABLED_SAMPLE_TIME_TRANSMISSION) {
String currentSensorReading = _sensorRelay[sensorRelayIndex]->sample();
_transmitMessage(currentSensorReading);
}
}
_sleep();
}
/* * * * * * * * * * * * * * * * * * * * * * * *
STORAGE: Get current file name
* * * * * * * * * * * * * * * * * * * * * * * */
String MeshOS::_getCurrentFilename(int sensorRelayIndex) {
// Create file and save the format for the data storing
String sensorName = _sensorRelay[sensorRelayIndex]->getSensorName();
String filename = sensorName + ".txt";
return filename;
}
/* * * * * * * * * * * * * * * * * * * * * * * *
STORAGE: Creating the files
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::_createSensorRelayDataFiles(int sensorRelayIndex) {
// Create new files for all the sensors
String filename = _getCurrentFilename(sensorRelayIndex);
_currentStorageFile = SD.open(filename, FILE_WRITE);
if (_currentStorageFile) {
Serial.println('|');
_closeStorageFile();
} else {
Serial.println('_');
}
}
/* * * * * * * * * * * * * * * * * * * * * * * *
STORAGE: Add readings
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::_storeCurrentSensorReading(int sensorRelayIndex) {
// Calculate the current timestamps with milliseconds
unsigned long currentReadingTimestamp = _currentTimeStamp;
String currentSensorReading = String(currentReadingTimestamp);
uint8_t millisecondsAdded = (millis() - _timeStartSync) % 1000;
if (millisecondsAdded < 10) currentSensorReading += "00";
else if (millisecondsAdded < 100) currentSensorReading += "0";
currentSensorReading += String(millisecondsAdded);
currentSensorReading += _DATA_CHUNK_SPLITTER;
currentSensorReading += _sensorRelay[sensorRelayIndex]->sample();
// Serial.println(currentSensorReading);
bool openResult = _openStorageFile(sensorRelayIndex, true);
if (openResult) {
_writeToStorageFile(currentSensorReading);
_closeStorageFile();
}
}
/* * * * * * * * * * * * * * * * * * * * * * * *
STORAGE: Open a file
* * * * * * * * * * * * * * * * * * * * * * * */
bool MeshOS::_openStorageFile(int sensorRelayIndex, bool toWrite) {
String currentlyOpenFileName = _getCurrentFilename(sensorRelayIndex);
if (toWrite) {
_currentStorageFile = SD.open(currentlyOpenFileName, FILE_WRITE);
} else {
_currentStorageFile = SD.open(currentlyOpenFileName);
}
if (_currentStorageFile) {
Serial.println('{');
return true;
} else {
Serial.println("x");
return false;
}
}
/* * * * * * * * * * * * * * * * * * * * * * * *
STORAGE: Write data to an opened file
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::_writeToStorageFile(String textToWrite) {
if (_currentStorageFile) {
_currentStorageFile.println(textToWrite);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * *
STORAGE: Close an opened file
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::_closeStorageFile() {
if (_currentStorageFile) {
_currentStorageFile.close();
Serial.println('}');
}
}
/* * * * * * * * * * * * * * * * * * * * * * * *
STORAGE: Close an opened file
* * * * * * * * * * * * * * * * * * * * * * * */
String MeshOS::_readLineFromOpenStorageFile() {
String received = "";
char ch;
while (_currentStorageFile.available()){
ch = _currentStorageFile.read();
if (ch == '\n') {
return String(received);
} else {
received += ch;
}
}
return received;
}
/* * * * * * * * * * * * * * * * * * * * * * * *
TRANSMISSION: Received incoming
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::_receivedIncomingCommunication() {
Serial.println('~');
if (_nodeCommsPort.available()) {
if (_nodeCommsPort.read() == "download") {
_transmitSensorRelayFiles();
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * *
TRANSMISSION: Send the data that is saved
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::_transmitSensorRelayFiles() {
// Start light
digitalWrite(_BLT_LED_PING_PIN, HIGH);
for (int sensorRelayIndex = 0; sensorRelayIndex < sizeof(_sensorRelay); sensorRelayIndex++) {
_openStorageFile(sensorRelayIndex);
// Loop through each line and transmit to gateway
String currentLine = "$";
uint8_t lineIndex = 0;
// Check the length of the line to see if we are at the end of the file or max transmissions
while ((currentLine.length() > 0) || (lineIndex >= MAXIMUM_TRANSMISSIONS)) {
// Transmit line
currentLine = _readLineFromOpenStorageFile();
_nodeCommsPort.println(currentLine);
lineIndex++;
}
// Close file
_closeStorageFile();
}
// Turn off light
digitalWrite(_BLT_LED_PING_PIN, LOW);
}
/* * * * * * * * * * * * * * * * * * * * * * * *
TRANSMISSION: Send a message
* * * * * * * * * * * * * * * * * * * * * * * */
void MeshOS::_transmitMessage(String currentMessage) {
_nodeCommsPort.println(currentMessage);
}