-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
486 lines (430 loc) · 11.2 KB
/
Main.cpp
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include <Arduino.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <string.h>
// #define DEBUG 1
#include "clocks/MeteoClockRTC.h"
#include "clocks/MeteoClockSoft.h"
#include "sensors/ReadTemperature.h"
#include "sensors/ReadHumidity.h"
#include "sensors/ReadBarometer.h"
#include "sensors/ReadLight.h"
#include "HC05Module.h"
#include "MeteoEEPROM.h"
#include "MeteoMessages.h"
// Firmware version string
#define FWVERSION "03.01.00"
// External interrupt pin used by the RTC
#define SQ_PIN 3
// Size of the input buffer
#define INBUF_SIZE 40
// Size of the output buffer
#define OUTBUF_SIZE 128
// Timeout for command reads (in milis)
#define READ_TIMEOUT 1000
// MCU timer prescaler value
#define PRESCALER 1024
// MCU timer tick frequency (in Hz)
#define INT_FREQ_HZ 1
MeteoClockRTC rtcClock;
MeteoClockSoft softClock;
HC05Module bt(2);
MeteoEEPROM ee;
MeteoMessages msgs;
//struct {
// struct EepromData data;
//} eeprom;
bool firstConnection = true; // First connection flag
bool swClockSet = false; // Is the software clock set?
bool rtcPresent = false; // RTC detected flag
volatile bool doRead = true; // Read sensors timing flag, read at startup
volatile bool wdTriggered = false; // Flag: Watchdog triggered
enum deviceEnum { DE_CLOCK=0, DE_THERMOMETER=1, DE_HIGROMETER=2,
DE_BAROMETER=3, DE_LIGHT=4 };
char devList[] = " "; // List of present devices
static char line[OUTBUF_SIZE]; // Buffer to build output messages
static char line_with_check[OUTBUF_SIZE+8]; // Buffer for checksummed message
static char inBuff[INBUF_SIZE]; // Buffer for incoming commands
//+
// Fletcher 16 Checksum algorithm
// From https://www.luisllamas.es/arduino-checksum/
// (Thanks!)
//-
uint16_t ChecksumFletcher16(byte *data, size_t count )
{
uint8_t sum1 = 0;
uint8_t sum2 = 0;
for(size_t index = 0; index < count; ++index )
{
sum1 = sum1 + (uint8_t)data[index];
sum2 = sum2 + sum1;
}
return (sum2 << 8) | sum1;
}
//+
// Put the MCU to sleep
// Use IDLE mode to get the USART interrupts
// Remember the goal is not to save power, but to have correct
// timing of our readings
//-
void sleepNow() {
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_mode();
}
//+
// RTC SQW interrupt service routine
// It keeps a counter of invocations. Each pollDelay seconds
// it will raise the global doRead flag so the main loop will
// make a sensor reading.
//-
void sqInterrupt() {
static int sqCounter = -1; // The flag is up at startup, so we have
// to count from minus one.
wdt_reset(); // Reset the watchdog timer
if (++sqCounter >= ee.data.pollDelay) {
doRead = true;
sqCounter = 0;
}
}
//+
// Watchdog interrupt routine.
// Resets itself and asserts the 'watchdog triggered' and
// 'time to read' flags.
//-
ISR(WDT_vect) {
wdTriggered = true;
doRead = true;
}
//+
// MCU timer1 compare interruption
//-
ISR(TIMER1_COMPA_vect) {
static int timCounter = -1;
wdt_reset();
if (++timCounter >= ee.data.pollDelay) {
doRead = true;
timCounter = 0;
}
}
//+
// Enable the MCU timer
//-
void enableMCUTimer() {
uint32_t matchReg = F_CPU / (PRESCALER * INT_FREQ_HZ) - 1;
cli(); // Inhibit interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = matchReg;
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12) | (1 << CS10); // Set prescaler at 1024
TIMSK1 |= (1 << OCIE1A); // Enable timer comparer interrupt
sei(); // Enable interrupts
}
//+
// Disable timer1
//-
void disableMCUTimer() {
cli();
TCCR1B &= ~((1<<CS10) | (1<<CS12));
sei();
}
//+
// Enable watchdog in interrupt + reset mode
//-
void enableWatchdog() {
MCUSR &= ~(1<<WDRF);
cli(); // Disable interrupts during timed change
WDTCSR |= (1<<WDCE) | (1<<WDE); // Enable changes, 4 cycles to go
WDTCSR = (1<<WDP0) | (1<<WDP3); // Clear reset mode, set 8s timeout
sei(); // Enable interrupts
WDTCSR |= (1<<WDIE) | (1<<WDE); // Enable interrupt and reset modes
}
void checkClock() {
bool clockNow = rtcPresent;
if (!rtcClock.checkClock()) {
rtcPresent = false;
devList[DE_CLOCK] = '-';
detachInterrupt(INT1);
enableMCUTimer();
} else {
rtcPresent = true;
devList[DE_CLOCK] = 'C';
disableMCUTimer();
rtcClock.enableInterrupt();
if (!swClockSet) {
DateTime now = rtcClock.getDateTime();
softClock.setClock(now.year(), now.month(), now.day(),
now.hour(), now.minute(), now.second());
swClockSet = true;
}
attachInterrupt(INT1, sqInterrupt, FALLING);
}
if (clockNow != rtcPresent) {
if (rtcPresent) {
msgs.getMessage(CLOCKDET, line);
} else {
msgs.getMessage(CLOCKLST, line);
}
Serial.println(line);
}
}
void setup() {
int rc=0;
char msg[80];
// Disable watchdog, just in case
MCUSR=0;
wdt_disable();
Serial.begin(9600);
while (!bt.begin()) {
Serial.println("ERROR: No BT connection");
}
Serial.println("");
ee.readEEPROM();
// eeprom.data.pollDelay = 5;
#ifdef DEBUG
sprintf(msg, "DEBUG: CPU clock: %ld", F_CPU);
Serial.println(msg);
#endif
rc = thPrepare();
if (rc != 0) {
msgs.getMessage(THERMNOK, line);
Serial.println(line);
devList[DE_THERMOMETER] = '-';
} else {
devList[DE_THERMOMETER] = 'T';
}
rc = higInitialize();
if (rc != 0) {
msgs.getMessage(HYGRONOK, line);
Serial.println(line);
devList[DE_HIGROMETER] = '-';
} else {
devList[DE_HIGROMETER] = 'H';
}
rc = barInitialize();
if (rc != 0) {
msgs.getMessage(BAROMNOK, line);
Serial.println(line);
devList[DE_BAROMETER] = '-';
} else {
devList[DE_BAROMETER] = 'P';
}
rc = lightInitialize();
devList[DE_LIGHT] = 'L';
checkClock();
//+
// Set up the interrupt pin
// That pin will get the SQW signals from the RTC and will act
// on the falling flank
//-
pinMode(SQ_PIN, INPUT_PULLUP);
enableWatchdog();
sprintf(msg, "INFO : F:%s W:%s N:%s DLY:%d", FWVERSION, ee.data.hwVersion,
ee.data.devName, ee.data.pollDelay);
Serial.println(msg);
}
//+
// Read an incoming command from the serial line (hopefully connected to the HC-05)
// The accepted command terminators are NL or CR+NL
// If a command terminator has not been received in READ_TIMEOUT milliseconds
// the read will be rejected and a timeout will be signaled (returning FALSE).
//-
bool readCommand() {
bool eos = false;
bool timeout = false;
int numChars = 0;
char *buffPtr = inBuff;
unsigned long timecontrol = millis();
while (!eos && numChars < INBUF_SIZE && !timeout) {
if (millis() - timecontrol < READ_TIMEOUT) {
if (Serial.available()) {
char c = Serial.read();
if (c == 0x0a) { // Got command terminator
eos = true;
*(buffPtr++) = 0x0; // Terminate the command line
} else if (c == 0x0d) {
continue; // Ignore CR by itself. The terminator must be NL or CR+NL
} else {
*(buffPtr++) = c; // Got char, append to buffer
numChars++;
}
}
} else {
timeout = true; // Didn't get a complete command
*(buffPtr++) = 0x0; // Terminate partial command
}
}
return !timeout;
}
//+
// Process a command string
//-
void processCommand(String cmd) {
bool cmdOk = false;
String theCmd = cmd.substring(0,5);
if (theCmd.equals("TIME ")) {
String t = cmd.substring(5,19);
checkClock();
if (rtcPresent) {
rtcClock.setClock(t);
}
softClock.setClock(t);
swClockSet = true;
cmdOk = true;
} else if (theCmd.equals("BYE ")) {
Serial.println("OK-BYE");
cmdOk = true;
for (int i=0; i<5 && bt.checkConnection(); i++) delay(1000);
if (bt.checkConnection()) {
Serial.println("KO-ONL");
} else {
firstConnection = true;
}
return;
} else if (theCmd.equals("INFO ")) {
sprintf(line, "INFO : F:%s W:%s N:%s DLY:%d", FWVERSION, ee.data.hwVersion,
ee.data.devName, ee.data.pollDelay);
Serial.println(line);
cmdOk = true;
} else if (theCmd.equals("DLAY ")) {
int newDelay = atoi(cmd.substring(5,7).c_str());
if (newDelay < 1 || newDelay > 60) {
Serial.println("KO-INV");
} else {
ee.data.pollDelay = newDelay;
ee.writeEEPROM();
}
cmdOk = true;
} else if (theCmd.equals("NAME ")) {
strncpy(ee.data.devName, cmd.substring(5,13).c_str(), _SIZ_DEVNAME+1);
ee.writeEEPROM();
cmdOk = true;
} else if (theCmd.equals("VERS ")) {
strncpy(ee.data.hwVersion, cmd.substring(5,13).c_str(), _SIZ_HWVERSION+1);
Serial.println("WARNING: Hardware version changed by command.");
ee.writeEEPROM();
cmdOk = true;
}
if (cmdOk) {
Serial.println("OK-000");
} else {
sprintf(line, "KO-UNK [%s]", theCmd.c_str());
Serial.println(line);
}
}
// TIME 20180220221700
// BYE
//+
// Read the sensors and send a DATA line thru the USART
//-
void readSensors() {
String theTime;
float temp, press, humdt, light;
uint16_t check = 0;
// If we have an RTC, check it's still working. If not, switch to MPU soft clock
if (rtcPresent) {
checkClock();
if (rtcPresent) {
theTime = rtcClock.getClock();
}
}
// If we've got no RTC, use software clock and check if it has came back
if (!rtcPresent) {
theTime = softClock.getClock();
checkClock();
}
char stemp[10], shumt[10], spres[10], slght[10];
temp = thRead();
press = barRead();
humdt = higRead();
light = lightRead();
if (temp==-999.00) { // No thermometre?
temp = barTemp(); // Try BMP180
if (temp==-999.00) { // No BMP180?
temp = higTemp(); // Try DHT22
}
if (devList[DE_THERMOMETER] == 'T') {
msgs.getMessage(THERMLST, line);
Serial.println(line);
devList[DE_THERMOMETER] = '-';
}
} else {
if (devList[DE_THERMOMETER] == '-') {
msgs.getMessage(THERMDET, line);
Serial.println(line);
devList[DE_THERMOMETER] = 'T';
}
}
if (press == -999.00) {
if (devList[DE_BAROMETER] == 'P') {
msgs.getMessage(BAROMLST, line);
Serial.println(line);
devList[DE_BAROMETER] = '-';
}
} else {
if (devList[DE_BAROMETER] == '-') {
msgs.getMessage(BAROMDET, line);
Serial.println(line);
devList[DE_BAROMETER] = 'P';
}
}
if (humdt == -999.00) {
if (devList[DE_HIGROMETER] == 'H') {
msgs.getMessage(HYGROLST, line);
Serial.println(line);
devList[DE_HIGROMETER] = '-';
}
} else {
if (devList[DE_HIGROMETER] == '-') {
msgs.getMessage(HYGRODET, line);
Serial.println(line);
devList[DE_HIGROMETER] = 'H';
}
}
dtostrf(temp, 5, 2, stemp);
dtostrf(press, 6, 2, spres);
dtostrf(humdt, 5, 2, shumt);
dtostrf(light, 6, 2, slght);
sprintf(line, "DATA :C%s:F%s:T%s:H%s:P%s:L%s:D%s:W%s:N%s", theTime.c_str(), FWVERSION, stemp,
shumt, spres, slght, devList,ee.data.hwVersion,ee.data.devName);
check = ChecksumFletcher16((unsigned char *) line, strlen(line));
sprintf(line_with_check,"%s:X%05u",line,check);
if (bt.checkConnection()) {
if (firstConnection) {
msgs.getMessage(BEGINPGM, line);
firstConnection = false;
}
Serial.println(line_with_check);
}
}
//+
// Main loop
//-
void loop() {
if (wdTriggered) {
msgs.getMessage(WATCHDOG, line);
Serial.println(line);
Serial.flush();
wdTriggered = false;
}
if (doRead) {
doRead = false;
readSensors();
if (!swClockSet) {
msgs.getMessage(TIMEREQS, line);
Serial.println(line);
}
Serial.flush();
}
if (Serial.available()) {
if (readCommand()) {
Serial.flush();
} else {
msgs.getMessage(COMNDTMO, line);
Serial.println(line);
}
processCommand(String(inBuff));
}
sleepNow(); // Wait for next interrupt
}