-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
342 lines (289 loc) · 9.48 KB
/
main.cpp
File metadata and controls
342 lines (289 loc) · 9.48 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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <signal.h>
#include <pthread.h>
#include "Externs.h"
#include "HTU21D.h"
#include "SHT31D.h"
#include "CO2.h"
#include "ILI9341.h"
#include "WebServer.h"
#include "Buzzer.h"
#include "Logger.h"
#define TH_MSR_DUR 50000000 // Tempreture + Humidity mesuring duration
#define CO2_MSR_DUR 30000000 // CO2 mesuring duration
#define NSEC_PASSED(ts0, ts1, tns0, tns1) ((ts1 - ts0) * 1000000000 + (tns1 - tns0))
// Extern variables
volatile bool allow_poweroff;
volatile bool update_allowed = true;
volatile bool lcd_is_on;
volatile int lcd_on_off_time;
pthread_mutex_t lcd_on_off_time_lock;
volatile int co2_warning = 1000; // ASHRAE 2006 / EN 13779:2008 / WHO 1990
volatile int humd_warning_low = 30;
volatile int humd_warning_high = 50;
volatile int temp_warning_low = 20;
volatile int temp_warning_high = 27;
pthread_mutex_t warning_levels_lock;
volatile int co2_warning_song = SNG_BEEP;
pthread_mutex_t co2_warning_song_lock;
const int update_period_ms = 1000;
pthread_attr_t master_thread_attr; // Will be used to create all threads
HTU21D thsens1;
SHT31D thsens2;
MHZ19B co2sens;
void sigCatcher(int signum);
void initLocks();
void destroyLocks();
void loadConfig();
void saveConfig();
int main()
{
// Set up signal handlers
signal(SIGINT, sigCatcher);
signal(SIGTERM, sigCatcher);
signal(SIGQUIT, sigCatcher);
// Prepare master thread attributes and mutex locks
pthread_attr_init(&master_thread_attr);
pthread_attr_setdetachstate(&master_thread_attr, PTHREAD_CREATE_DETACHED);
initLocks();
loadConfig();
pthread_t servthd;
pthread_create(&servthd, &master_thread_attr, serverMain, NULL);
initLCD();
int msec = 0;
int ppm = co2sens.GetPPM();
float humd = 0.0f, temp = 0.0f;
unsigned int upd_period_ns = update_period_ms * 1000000;
struct timespec beg, end; // No Hobbits live here!
struct timespec th, co, th_aw, end_wait = {0}, th_wait = {0}, co_wait = {0};
struct tm t;
time_t sec;
while(1)
{
clock_gettime(CLOCK_MONOTONIC, &beg);
int th1_res = thsens1.Measure();
int th2_res = thsens2.Measure();
float h1, h2, t1, t2;
h1 = thsens1.GetHumd();
h2 = thsens2.GetHumd();
t1 = thsens1.GetTemp();
t2 = thsens2.GetTemp();
if(th1_res == 0 && th2_res == 0)
{
humd = roundf((h1 + h2)/2.0f * 10.0f)/10.0f;
temp = roundf((t1 + t2)/2.0f * 10.0f)/10.0f;
}
else if(th1_res < 0 && th2_res == 0)
{
humd = h2;
temp = t2;
}
else if(th1_res == 0 && th2_res < 0)
{
humd = h1;
temp = t1;
}
clock_gettime(CLOCK_MONOTONIC, &th);
th_wait.tv_nsec = TH_MSR_DUR - NSEC_PASSED(beg.tv_sec, th.tv_sec, beg.tv_nsec, th.tv_nsec);
nanosleep(&th_wait, NULL);
clock_gettime(CLOCK_MONOTONIC, &th_aw);
// CO2 sensor measures every 5 seconds no matter how often it's probed
if(msec >= 5000)
{
ppm = co2sens.GetPPM();
msec = 0;
}
clock_gettime(CLOCK_MONOTONIC, &co);
co_wait.tv_nsec = CO2_MSR_DUR - NSEC_PASSED(th_aw.tv_sec, co.tv_sec, th_aw.tv_nsec, co.tv_nsec);
nanosleep(&co_wait, NULL);
WebUpdate upd;
memset(&upd, 0, sizeof(WebUpdate));
upd.op = WRT_RDINGS;
upd.ppm = ppm;
upd.humd = humd;
upd.temp = temp;
putWebQueue(&upd);
if(lcd_is_on && update_allowed)
{
updateReadings(ppm, humd, temp);
}
// Critical Section Beg
pthread_mutex_lock(&lcd_on_off_time_lock);
int onh = lcd_on_off_time >> 24;
int onm = (lcd_on_off_time & 0xFF0000) >> 16;
int offh = (lcd_on_off_time & 0xFF00) >> 8;
int offm = lcd_on_off_time & 0xFF;
pthread_mutex_unlock(&lcd_on_off_time_lock);
// Critical Section End
if(onh == offh && onm == offm)
{
goto loopend;
}
sec = time(NULL);
t = *localtime(&sec);
if(!lcd_is_on && t.tm_hour == onh && t.tm_min == onm)
{
onLCD();
}
else if(lcd_is_on && t.tm_hour == offh && t.tm_min == offm)
{
offLCD();
}
loopend:
msec += update_period_ms;
clock_gettime(CLOCK_MONOTONIC, &end);
// This operation takes from 4000 to 1000 nano seconds, which is fine and shouldn't cause much desync
end_wait.tv_nsec = upd_period_ns - NSEC_PASSED(beg.tv_sec, end.tv_sec, beg.tv_nsec, end.tv_nsec);
nanosleep(&end_wait, NULL);
}
sigCatcher(0);
return 0;
}
void sigCatcher(int signum)
{
logError("SIGINT/SIGTERM/SIGQUIT catched, Deiniting", 0);
saveConfig();
pthread_attr_destroy(&master_thread_attr);
destroyLocks();
deinitWebServer();
deinitLCD();
thsens1.DeInit();
thsens2.DeInit();
co2sens.DeInit();
if(allow_poweroff)
{
logError("Powering off Raspberry Pi", 0);
deinitLogger();
system("poweroff");
exit(0);
}
else
{
logError("Exiting normally", 0);
deinitLogger();
exit(0);
}
sleep(10);
}
void initLocks()
{
pthread_mutex_init(&lcd_on_off_time_lock, NULL);
pthread_mutex_init(&warning_levels_lock, NULL);
pthread_mutex_init(&co2_warning_song_lock, NULL);
}
void destroyLocks()
{
pthread_mutex_destroy(&lcd_on_off_time_lock);
pthread_mutex_destroy(&warning_levels_lock);
pthread_mutex_destroy(&co2_warning_song_lock);
}
// Only Main Thread will be present on config load, so no need to lock
void loadConfig()
{
FILE* f = fopen("./log/config.cfg", "r");
if(f == NULL)
{
return;
}
char c[140];
fread(c, 1, 140, f);
fclose(f);
for(int i = 0; i < 140; ++i)
{
if(c[i] == '\n')
{
c[i] = 0;
}
}
lcd_on_off_time = atoi(c+12) << 24 | atoi(c+27) << 16 | atoi(c+43) << 8 | atoi(c+59);
co2_warning = atoi(c+68);
humd_warning_low = atoi(c+82);
humd_warning_high = atoi(c+95);
temp_warning_low = atoi(c+108);
temp_warning_high = atoi(c+121);
co2_warning_song = atoi(c+136);
}
void saveConfig()
{
FILE* f = fopen("./log/config.cfg", "w");
// Critical Section Beg
pthread_mutex_lock(&lcd_on_off_time_lock);
int onh = lcd_on_off_time >> 24;
int onm = (lcd_on_off_time & 0xFF0000) >> 16;
int offh = (lcd_on_off_time & 0xFF00) >> 8;
int offm = lcd_on_off_time & 0xFF;
pthread_mutex_unlock(&lcd_on_off_time_lock);
// Critical Section End
fprintf(f, "lcd_on_hrs= %02d\nlcd_on_min= %02d\nlcd_off_hrs= %02d\nlcd_off_min= %02d\n",
onh, onm, offh, offm);
// Critical Section Beg
pthread_mutex_lock(&warning_levels_lock);
fprintf(f, "wco2= %04d\nwhumd_l= %03d\nwhumd_h= %03d\nwtemp_l= %03d\nwtemp_h= %03d\n",
co2_warning, humd_warning_low, humd_warning_high, temp_warning_low, temp_warning_high);
pthread_mutex_unlock(&warning_levels_lock);
pthread_mutex_lock(&co2_warning_song_lock);
fprintf(f, "wco2_song= %02d\n", co2_warning_song);
pthread_mutex_unlock(&co2_warning_song_lock);
// Critical Section End
fclose(f);
}
/*printf("NSEC PASSED: %d\n", (end.tv_sec - beg.tv_sec) * 1000000000 + (end.tv_nsec - beg.tv_nsec));
fprintf(f, "%lu,", (th_aw.tv_sec - beg.tv_sec) * 1000000000 + (th_aw.tv_nsec - beg.tv_nsec));
fprintf(f, "%lu,", (co_aw.tv_sec - th_aw.tv_sec) * 1000000000 + (co_aw.tv_nsec - th_aw.tv_nsec));
fprintf(f, "%lu,", (t3.tv_sec - co_aw.tv_sec) * 1000000000 + (t3.tv_nsec - co_aw.tv_nsec));
fprintf(f, "%lu,", (t4.tv_sec - t3.tv_sec) * 1000000000 + (t4.tv_nsec - t3.tv_nsec));
fprintf(f, "%lu,", (t5.tv_sec - t4.tv_sec) * 1000000000 + (t5.tv_nsec - t4.tv_nsec));
fprintf(f, "%lu,", (end.tv_sec - t5.tv_sec) * 1000000000 + (end.tv_nsec - t5.tv_nsec));
fprintf(f, "%lu\n", (end.tv_sec - beg.tv_sec) * 1000000000 + (end.tv_nsec - beg.tv_nsec));*/
//printf("%-d ppm HTU: %-3.1f *C %-3.1f%% SHT: %-3.1f *C %-3.1f%%\n", ppm, t1, h1, t2, h2);
//printf("h1 %.3f t1 %.3f h2 %.3f t2 %.3f avgh %.3f avgt %.3f\n", h1, t1, h2, t2, humd, temp);
/* Measurements comparisons
1116 ppm HTU: 26.4 *C 37.8% SHT: 26.4 *C 37.5%
1116 ppm HTU: 26.4 *C 37.9% SHT: 26.5 *C 37.5%
1116 ppm HTU: 26.4 *C 37.9% SHT: 26.4 *C 37.6%
1116 ppm HTU: 26.4 *C 37.9% SHT: 26.4 *C 37.6%
1116 ppm HTU: 26.4 *C 38.0% SHT: 26.5 *C 37.6%
1114 ppm HTU: 26.4 *C 38.0% SHT: 26.5 *C 37.7%
1114 ppm HTU: 26.5 *C 38.1% SHT: 26.4 *C 37.8%
1114 ppm HTU: 26.5 *C 38.2% SHT: 26.4 *C 37.9%
1114 ppm HTU: 26.5 *C 38.3% SHT: 26.5 *C 37.7%
1114 ppm HTU: 26.5 *C 38.4% SHT: 26.4 *C 37.7%
1112 ppm HTU: 26.5 *C 38.5% SHT: 26.4 *C 37.7%
1112 ppm HTU: 26.5 *C 38.6% SHT: 26.5 *C 37.8%
1112 ppm HTU: 26.5 *C 38.7% SHT: 26.4 *C 37.9%
1112 ppm HTU: 26.5 *C 38.8% SHT: 26.5 *C 38.0%
1112 ppm HTU: 26.5 *C 38.9% SHT: 26.5 *C 38.0%
1111 ppm HTU: 26.6 *C 38.9% SHT: 26.5 *C 38.0%
1111 ppm HTU: 26.6 *C 38.9% SHT: 26.5 *C 38.0%
1111 ppm HTU: 26.6 *C 38.8% SHT: 26.5 *C 37.9%
1111 ppm HTU: 26.6 *C 38.7% SHT: 26.5 *C 37.9%
1111 ppm HTU: 26.6 *C 38.7% SHT: 26.5 *C 37.9%
1117 ppm HTU: 26.6 *C 38.6% SHT: 26.5 *C 37.9%
1117 ppm HTU: 26.6 *C 38.6% SHT: 26.5 *C 37.9%
1117 ppm HTU: 26.6 *C 38.6% SHT: 26.5 *C 37.8%
1117 ppm HTU: 26.6 *C 38.6% SHT: 26.4 *C 37.8%
1117 ppm HTU: 26.6 *C 38.5% SHT: 26.5 *C 37.8%
1119 ppm HTU: 26.6 *C 38.5% SHT: 26.5 *C 37.8%
1119 ppm HTU: 26.6 *C 38.5% SHT: 26.5 *C 37.8%
1119 ppm HTU: 26.6 *C 38.4% SHT: 26.5 *C 38.1%
1119 ppm HTU: 26.6 *C 38.4% SHT: 26.5 *C 38.2%
1119 ppm HTU: 26.6 *C 38.3% SHT: 26.5 *C 38.1%
1122 ppm HTU: 26.6 *C 38.2% SHT: 26.5 *C 38.0%
1122 ppm HTU: 26.6 *C 38.2% SHT: 26.5 *C 37.9%
1122 ppm HTU: 26.6 *C 38.1% SHT: 26.5 *C 37.8%
1122 ppm HTU: 26.6 *C 38.1% SHT: 26.5 *C 37.8%
1122 ppm HTU: 26.6 *C 38.2% SHT: 26.5 *C 37.8%
1124 ppm HTU: 26.6 *C 38.2% SHT: 26.5 *C 38.4%
1124 ppm HTU: 26.6 *C 38.2% SHT: 26.5 *C 39.2%
1124 ppm HTU: 26.6 *C 38.3% SHT: 26.5 *C 39.1%
1124 ppm HTU: 26.6 *C 38.4% SHT: 26.6 *C 38.8%
1124 ppm HTU: 26.6 *C 38.5% SHT: 26.5 *C 38.6%
1122 ppm HTU: 26.6 *C 38.5% SHT: 26.6 *C 38.5%
1122 ppm HTU: 26.6 *C 38.5% SHT: 26.6 *C 38.2%
1122 ppm HTU: 26.6 *C 38.5% SHT: 26.5 *C 38.0%
1122 ppm HTU: 26.6 *C 38.5% SHT: 26.5 *C 37.9%
1122 ppm HTU: 26.6 *C 38.4% SHT: 26.6 *C 37.9% */