-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
552 lines (401 loc) · 11.7 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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
////////////////////////////////////////////////////////////////////////////////////////
//
// humifand - the humidity fan deamon
//
// Please check www.way2.net for more information
//
// (c) Copyright 2014 by way2.net Services.
//
////////////////////////////////////////////////////////////////////////////////////////
#include <bcm2835.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <map>
#include <string>
#include "conffile.h"
#include "RPi_SHT1x.h"
#include "fan.h"
#include "logger.h"
#include "ProcessValues.h"
////////////////////////////////////////////////////////////////////////////////////////
//bool opt_getADaemon = false;
//bool opt_verbose = false;
const char *opt_ConfigFile = "/etc/humifand.conf";
bool g_QuitMainLoop = false;
// --- here are the last values read!
float g_InsideTemp;
float g_InsideHumi;
float g_OutsideTemp;
float g_OutsideHumi;
// --- out sensor instances
SHT1x g_InsideSensor(RPI_BPLUS_GPIO_J8_38, RPI_BPLUS_GPIO_J8_40);
SHT1x g_OutsideSensor(RPI_BPLUS_GPIO_J8_35, RPI_BPLUS_GPIO_J8_37);
// --- signal handlign
struct sigaction sigact;
////////////////////////////////////////////////////////////////////////////////////////
void printTempAndHumidity(SHT1x &f_Sensor)
{
unsigned char noError = 1;
value humi_val,temp_val;
// Set up the SHT1x Data and Clock Pins
f_Sensor.SHT1x_InitPins();
// Reset the SHT1x
f_Sensor.SHT1x_Reset();
// Request Temperature measurement
noError = f_Sensor.SHT1x_Measure_Start(SHT1xMeaT );
if (!noError) {
printf("SHT1x_Measure_Start failed\n");
return;
}
// Read g_InsideSensor measurement
noError = f_Sensor.SHT1x_Get_Measure_Value((unsigned short int*) &temp_val.i );
if (!noError) {
printf("SHT1x_Get_Measure_Value failed\n");
return;
}
// Request Humidity Measurement
noError = f_Sensor.SHT1x_Measure_Start(SHT1xMeaRh );
if (!noError) {
printf("SHT1x_Measure_Start failed\n");
return;
}
// Read Humidity measurement
noError = f_Sensor.SHT1x_Get_Measure_Value((unsigned short int*) &humi_val.i );
if (!noError) {
printf("SHT1x_Get_Measure_Value failed\n");
return;
}
// Convert intergers to float and calculate true values
temp_val.f = (float)temp_val.i;
humi_val.f = (float)humi_val.i;
// Calculate Temperature and Humidity
f_Sensor.SHT1x_Calc(&humi_val.f, &temp_val.f);
// Calc Dew Point
float fDP = f_Sensor.SHT1x_CalcDewpoint(humi_val.f,temp_val.f);
//Print the Temperature to the console
printf("Temperature: %0.2f C\n",temp_val.f);
//Print the Humidity to the console
printf("Humidity: %0.2f%%\n",humi_val.f);
//Print the Temperature to the console
printf("Dew Point: %0.2f C\n",fDP);
}
////////////////////////////////////////////////////////////////////////////////////////
void CheckFan(void)
{
FAN->InitPins(RPI_BPLUS_GPIO_J8_36);
for (int i=0;i<10;++i)
{
printf("Starting fan....\n");
FAN->Start();
sleep(5);
printf("Stopping fan....\n");
FAN->Stop();
sleep(5);
}
}
////////////////////////////////////////////////////////////////////////////////////////
bool AskSensor(SHT1x &f_Sensor,float &f_Humi, float &f_Temp)
{
unsigned short int l_TempDigit;
unsigned short int l_HumiDigit;
delay(100);
int noError = f_Sensor.SHT1x_Measure_Start(SHT1xMeaT );
if (!noError)
{
LOGGER->Log("SHT1x_Measure_Start failed");
return false;
}
delay(100);
// Read Temperature measurement
noError = f_Sensor.SHT1x_Get_Measure_Value(&l_TempDigit);
if (!noError)
{
LOGGER->Log("SHT1x_Get_Measure_Value failed");
return false;
}
delay(100);
// Request Humidity Measurement
noError = f_Sensor.SHT1x_Measure_Start(SHT1xMeaRh );
if (!noError)
{
LOGGER->Log("SHT1x_Measure_Start failed");
return false;
}
delay(100);
// Read Humidity measurement
noError = f_Sensor.SHT1x_Get_Measure_Value(&l_HumiDigit);
if (!noError)
{
LOGGER->Log("SHT1x_Get_Measure_Value failed");
return false;
}
// Convert intergers to float and calculate true values
f_Temp = (float)l_TempDigit;
f_Humi = (float)l_HumiDigit;
// Calculate Temperature and Humidity
f_Sensor.SHT1x_Calc(&f_Humi, &f_Temp);
return true;
}
////////////////////////////////////////////////////////////////////////////////////////
void MainLoop(void)
{
int l_CycleTime = CONF->GetOptionIntDefault("cycle_time",30);
LOGGER->Log("Cycle time is %d seconds.",l_CycleTime);
// --- do some init (fan and sensors)
delay(100);
g_InsideSensor.SHT1x_InitPins();
g_InsideSensor.SHT1x_Reset();
delay(100);
g_OutsideSensor.SHT1x_InitPins();
g_OutsideSensor.SHT1x_Reset();
delay(100);
FAN->InitPins(RPI_BPLUS_GPIO_J8_36);
delay(100);
// --- the magic loop
int l_LoopCounter = 0;
while (!g_QuitMainLoop)
{
//LOGGER->Log("Loop %d",l_LoopCounter);
// --- bounce every 1 sec through the loop to react more fast e.g. on quit signal
l_LoopCounter++;
// --- if we hit the cycle time, process!
if (l_LoopCounter == l_CycleTime)
{
// --- get humi and temp for both sensors
bool l_insideok = AskSensor(g_InsideSensor, g_InsideHumi, g_InsideTemp);
bool l_outsideok = AskSensor(g_OutsideSensor, g_OutsideHumi, g_OutsideTemp);
if (!l_insideok)
LOGGER->Log("Failed to get inside sensor value");
if (!l_outsideok)
LOGGER->Log("Failed to get outside sensor value");
if (CONF->coGetVerbose())
{
if (l_insideok)
LOGGER->Log("Inside Temp %0.2f °C Humi %0.2f%%",g_InsideTemp,g_InsideHumi);
else
LOGGER->Log("Inside Temp invalid");
if (l_outsideok)
LOGGER->Log("Outside Temp %0.2f °C Humi %0.2f%%",g_OutsideTemp,g_OutsideHumi);
else
LOGGER->Log("Outside Temp invalid");
}
// --- this is where rrd gets updated and the fans get controlled
ProcessValues(l_insideok,g_InsideTemp,g_InsideHumi,l_outsideok,g_OutsideTemp,g_OutsideHumi);
l_LoopCounter = 0;
}
sleep(1);
}
}
////////////////////////////////////////////////////////////////////////////////////////
bool ReadConfigFile(void)
{
CONF->Clear();
// --- try to read the config file
if (CONF->coGetVerbose()) printf("humifand: use config file '%s'\n",opt_ConfigFile);
if (!CONF->processConfigFile(opt_ConfigFile))
{
printf("Error reading config file. Abort.\n");
return false;
}
/*
// --- print values in case we are in verbose mode
if (CONF->coGetVerbose())
{
printf("Successfully read the config file. Following values:\n");
CONF->PrintConfMap();
}
*/
return true;
}
////////////////////////////////////////////////////////////////////////////////////////
static void signal_handler(int sig)
{
if (sig == SIGINT)
{
LOGGER->Log("CTRL_C signal detected. Exit gracefully");
g_QuitMainLoop = true;
}
if (sig == SIGHUP)
{
LOGGER->Log("SIGHUP detected. Read config file again!");
ReadConfigFile();
}
if (sig == SIGTERM)
{
LOGGER->Log("SIGTERM detected. Exit gracefully!");
g_QuitMainLoop = true;
}
}
////////////////////////////////////////////////////////////////////////////////////////
void cleanup(void)
{
sigemptyset(&sigact.sa_mask);
}
////////////////////////////////////////////////////////////////////////////////////////
void init_signals(void)
{
sigact.sa_handler = signal_handler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, (struct sigaction *)NULL);
sigaction(SIGTERM, &sigact, (struct sigaction *)NULL);
sigaction(SIGHUP, &sigact, (struct sigaction *)NULL);
}
////////////////////////////////////////////////////////////////////////////////////////
void start_daemon(void)
{
pid_t pid, sid;
// --- Fork the Parent Process
pid = fork();
if (pid < 0)
{
fprintf (stderr, "Fork for daemon failed (%d)\n", errno);
exit(EXIT_FAILURE);
}
// --- We got a good pid, Close the Parent Process
if (pid > 0)
{
exit(EXIT_SUCCESS);
}
// --- Change File Mask
umask(0);
// --- reate a new Signature Id for our child
sid = setsid();
if (sid < 0)
{
fprintf (stderr, "setsid for daemon failed (%d)\n", errno);
exit(EXIT_FAILURE);
}
// --- Change Directory
// --- If we cant find the directory we exit with failure.
if ((chdir("/")) < 0)
{
fprintf (stderr, "chdir to root failed (%d)\n", errno);
exit(EXIT_FAILURE);
}
// --- Close Standard File Descriptors
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
////////////////////////////////////////////////////////////////////////////////////////
void CreatePIDFile()
{
if (!CONF->isValid("pid_file")) return;
std::string l_pf = CONF->GetOption("pid_file");
std::ofstream l_file;
l_file.open(l_pf.c_str(), ios::out);
l_file << getpid() << std::endl;
}
////////////////////////////////////////////////////////////////////////////////////////
void RemovePIDFile()
{
if (!CONF->isValid("pid_file")) return;
std::string l_pf = CONF->GetOption("pid_file");
remove(l_pf.c_str());
}
////////////////////////////////////////////////////////////////////////////////////////
int main (int argc, char **argv)
{
// --- process options
int c;
opterr = 0;
while ((c = getopt (argc, argv, "dc:tvf")) != -1)
switch (c)
{
case 'v':
CONF->coSetVerbose(true);
break;
case 'd':
CONF->coSetDaemonMode(true);
break;
case 't':
bcm2835_init();
delay(20);
printf("Inside:\n");
printTempAndHumidity(g_InsideSensor);
printf("Outside:\n");
printTempAndHumidity(g_OutsideSensor);
return 255;
break;
case 'f':
bcm2835_init();
delay(20);
CheckFan();
return 255;
break;
case 'c':
opt_ConfigFile = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument. Abort.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'. Abort.\n", optopt);
else
fprintf (stderr,"Unknown option character `\\x%x'. Abort.\n",optopt);
return 255;
default:
abort();
}
// --- if there are more arguments, this is an error
if (optind != argc)
{
printf ("Too many arguments supplied: '%s'. Abort.\n", argv[optind]);
return 255;
}
// --- read the config file
if (!ReadConfigFile())
{
return 255;
}
// --- open the log file
if (!CONF->isValid("log_file"))
{
printf("Missing log file parameter in config file. Abort.\n");
return 255;
}
std::string l_logfile = CONF->GetOption("log_file");
if (!LOGGER->OpenLog(l_logfile))
{
printf("Unable to open logfile '%s'. Abort.\n",l_logfile.c_str());
return 255;
}
// --- Initialise the Raspberry Pi GPIO
if(!bcm2835_init())
{
fprintf (stderr, "bcm2835_init failed. Are you root? Abort.\n");
return 255;
}
// --- init signals
atexit(cleanup);
init_signals();
// --- get a daemon?
if (CONF->coGetDaemonMode())
{
start_daemon();
CreatePIDFile();
}
// --- startup messages
LOGGER->Log("humifand deamon (v1.1) starting up...let's make it less wet!");
// --- Enter the main loop
MainLoop();
// --- remove PID file
if (CONF->coGetDaemonMode())
{
RemovePIDFile();
}
// --- shutdown messages
LOGGER->Log("humifand deamon shutting down...may the drought be with you!");
return 0;
}