-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
369 lines (257 loc) · 7.6 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
//
//
// Cablage DS18B20 douche JAUNE:DATA ORANGE:V+ MARRON/GRIS:GND
//
//
//
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "AXP209.h"
#include "log.h"
#define MAX_PATH 50
#define TIME_SLOT 5 // timeslot in minute for synchronizing temperature log writing
#define FALSE 0
#define TRUE 1
static time_t previous_timestamp = 0 ;
typedef struct
{
char device_id[16] ; // 28-000003430b68
float offset ; // offset de compensation suite à calibration
char name[35] ; //
} ST_W1, *PST_W1 ;
#define NB_W1_SENSOR 2
ST_W1 w1[] = {
{ "28-03146d2111ff", -2.5, "temperature_01"},
{ "28-03146d25c2ff", -1.5, "temperature_02"},
{ "28-000003430b68", 0, "temperature_douche" }
} ;
ST_W1 w1_douche = { "28-000003430b68", 0, "temperature_douche" } ;
static char w1_devices_path[] = "/sys/bus/w1/devices/" ;
//static char log_path[] = "/tmp/" ;
AXP209 chip("/dev/i2c-0", 0x34) ;
log trace("/home/chip/log/", "log") ;
// prototypes
void init(void) ;
int is_time(void) ;
int shower_time(void) ;
int log_temperature(void) ;
int log_douche(void) ;
int w1_read(char *sensor_id, float *temperature) ;
//void write_to_log(char *filename, char *log_text) ;
void log_battery(void) ;
bool presence_tension(void) ;
int main(int argc, char **argv)
{
static bool flag_tension = false ;
static bool flag_battery_low = false ;
static bool flag_activation_mode = true ;
trace.write("events", (char*) "Service is starting !", true) ;
while(1)
{ // forever
sleep(1) ;
if (chip.presence_tension())
{ // power supply ok
if (!flag_tension)
{
flag_tension = true ;
flag_battery_low = false ;
trace.write("events", (char*) "power supply ok", true) ;
}
} // power supply ok
else
{ // no power supply
if (flag_tension)
{
flag_tension = false ;
trace.write("events", (char*) "Warning : no power supply", true) ;
}
float tension ;
// TODO faire une comparaison time pour ne pas lire en permanence
tension = chip.battery_voltage() ;
if ((tension < 3.9) && (!flag_battery_low))
{
flag_battery_low = true ;
trace.write("events", (char*) "Battery voltage low !", true) ;
}
} // no power supply
if (chip.activation_mode())
{ // activation mode activated
if (!flag_activation_mode)
{ // we notice the state change off --> on
flag_activation_mode = true ;
trace.write("events", (char*) "activation_mode change to: ON", true) ;
} // we notice the state change off --> on
} // activation mode activated
else
{ // activation mode not activated
if (flag_activation_mode)
{ // we notice the state change on --> off
flag_activation_mode = false ;
trace.write("events", (char*) "activation_mode change to: OFF", true) ;
} // we notice the state change on --> off
} // activation mode not activated
if (is_time())
{ // attendre le bon moment : toutes les x minutes
log_battery() ;
log_temperature() ;
} // attendre le bon moment : toutes les x minutes
//log_douche() ;
} // forever
return 0;
}
int log_temperature()
{
float temperature ;
char st_temperature[20] ;
//char filename[MAX_PATH+1] ;
int i ;
for(i=0; i<NB_W1_SENSOR; i++)
{
if (w1_read(w1[i].device_id, &temperature))
{ // lecture ok du sensor
// ajout de l'offset de compensation
temperature += w1[i].offset ;
sprintf( st_temperature, "%.1f" , temperature ) ;
trace.write(w1[i].name, st_temperature, true) ;
} // lecture ok du sensor
}
return 0 ;
}
int log_douche()
{
float temperature ;
char st_temperature[20] ;
//bool flag_log = false ;
if (w1_read(w1_douche.device_id, &temperature))
{ // lecture ok du sensor
temperature += w1_douche.offset ;
if (temperature > 30)
{ // on tire de l'eau chaude
if (shower_time())
{
sprintf( st_temperature, "%.1f" , temperature ) ;
trace.write(w1_douche.name, st_temperature, false) ;
}
} // on tire de l'eau chaude
} // lecture ok du sensor
return 0 ;
}
void log_battery()
{
float battery_voltage ;
float charging_current ;
float discharging_current ;
float VBUS_voltage ;
float VBUS_current ;
float chip_current ;
float temperature ; // temperature du AXP209
char st_log[40] ;
//char filename[MAX_PATH+1] ;
VBUS_voltage = chip.VBUS_voltage() ;
VBUS_current = chip.VBUS_current() ;
battery_voltage = chip.battery_voltage() ;
charging_current = chip.battery_charging_current() ;
discharging_current = chip.battery_discharging_current() ;
temperature = chip.temperature() ;
if (VBUS_current == 0)
{ // all current comming from battery
chip_current = discharging_current ;
} // all current comming from battery
else
{ // charger give current to chip to all system
if (charging_current == 0)
{ // charger is only giving current to chip
chip_current = VBUS_current ;
} // charger is only giving current to chip
else
{ // charger is charging battery and gving current to chip
chip_current = VBUS_current - charging_current ;
} // charger is charging battery and gving current to chip
} // charger give current to chip to all system
sprintf( st_log, "%.2f %.0f %.2f" , VBUS_voltage, VBUS_current, temperature) ;
trace.write("vbus", st_log, true) ;
sprintf( st_log, "%.2f %.0f %.0f" , battery_voltage, charging_current, discharging_current ) ;
trace.write("batterie", st_log, true) ;
sprintf ( st_log, "%.0f", chip_current) ;
trace.write("chip", st_log, true) ;
}
//
// return :
// TRUE si lecture ok
// FALSE en cas d'erreur de lecture
// valeur de la temperature
int w1_read(char *sensor_id, float *temperature)
{
int retcode = TRUE ;
int fd ;
char devPath[40] ;
char buf[256]; // Data from device
char tmpData[6]; // Temp C * 1000 reported by device
ssize_t numRead;
sprintf(devPath, "%s%s/w1_slave", w1_devices_path, sensor_id);
fd = open(devPath, O_RDONLY);
if(fd != -1)
{ // file open : ok
while((numRead = read(fd, buf, 256)) > 0)
{
memset(tmpData, '\0', sizeof(tmpData)) ;
strncpy(tmpData, strstr(buf, "t=") + 2, 5);
//printf("tmpData:%s\n", tmpData) ;
*temperature = strtof(tmpData, NULL) / 1000 ;
}
close(fd);
} // file open : ok
else
{
retcode = FALSE ;
//perror ("Couldn't open the w1 device.");
}
return retcode ;
}
//
// return TRUE if elapse time since last trig is more than TIME_SLOT
// return FALSE if time slot isn't reached
//
int is_time()
{
// TIME_SLOT
// static time_t previous_timestamp ;
int retcode = FALSE ;
time_t current_timestamp;
struct tm *t;
current_timestamp = time (NULL);
t = localtime(¤t_timestamp);
if (t->tm_min % TIME_SLOT == 0)
{ // we are on a slot, regarding to TIME_SLOT
if ( !previous_timestamp || (current_timestamp - previous_timestamp) >= TIME_SLOT*60)
{
previous_timestamp = current_timestamp ;
retcode = TRUE ;
}
} // we are on a slot, regarding to TIME_SLOT
return retcode ;
}
int shower_time()
{
// TIME_SLOT
// static time_t previous_timestamp ;
static time_t previous_timestamp = 0 ;
int retcode = FALSE ;
time_t current_timestamp;
//struct tm *t;
current_timestamp = time (NULL);
//t = localtime(¤t_timestamp);
if ( !previous_timestamp || (current_timestamp - previous_timestamp) >= 30)
{
previous_timestamp = current_timestamp ;
retcode = TRUE ;
}
return retcode ;
}