-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_ABS.cpp
353 lines (203 loc) · 7.95 KB
/
monitor_ABS.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
/*
Copyright (C) <2016> <Luciano Bigiotti && Riccardo Nocella>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.*/
#include <stdlib.h>
#include <unistd.h>/* UNIX standard function definitions */
#include <math.h>
#include "birra.h"
#include <fstream>
#include <iostream>
#include <ctime>
#include <cstdio> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <algorithm>
#include "lib/serial.h"
#include "lib/udooSensor.h"
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
using namespace std;
int main () {
float temp,press;
float alcohol_val;
int FD_u;
int control,FD_a;
char buffer_alcohol[7];
float gradazione;
float BAC;
float percentage;
/*########## unmount kernel module of sensor mpl3115 to take control ######### */
system ("rmmod mpl3115");
Birra *peroni;
peroni = new Birra();
udooSensor *sensore_udoo;
sensore_udoo = new udooSensor;
SerialUdoo *comunication ;
comunication = new SerialUdoo;
/*###########-sensorUDOO init-#############*/
FD_u=sensore_udoo-> udooSensor_INIT(0x60);
/*################ init serial comunication ############ */
FD_a=comunication->open_port();
control=comunication->set_interface_attribs(FD_a,B115200,0);
if (control!=0 ){
perror("impossibile settari gli attributi \n ");
}
comunication->set_blocking (FD_a, 0);
/*############### init gpio led ##############à*/
system("echo 18 > /sys/class/gpio/export");
system("echo out > /sys/class/gpio/gpio18/direction");
/*############- build the file path -####################*/
srand (time(NULL));
time_t dataCorrente=time(NULL);
struct tm *strutturaData=localtime(&dataCorrente);
char fileName[50];
strftime(fileName , 50 , "%d-%m-%Y.csv" , localtime(&dataCorrente));
string path= "/var/www/udooBEER/csv/";
string temp_file;
temp_file.assign(fileName);
string PATH_FILE= path + temp_file ;
const char *FILE=PATH_FILE.c_str();
/*######## open stream + control ######### */
ifstream check(FILE);
//cout<<strerror(errno);
if (check){
cout <<"THE FILE EXIST\n\n"<<endl;
ofstream outline;
//cout<<strerror(errno);
outline.open(FILE,ios::app);
//se il file non esiste lo crea, altrimenti lo sovrascrive!
if(!outline.is_open()) {
cout<<"Errore nella creazione del file! 1"<<endl;
return -1;
}
/* ###### initialize the recevitor ########*/
while(1){
time_t t = time(NULL); // return hour
tm *tempo;
tempo = localtime(&t);
/* ######### read data in the sensor object ################*/
sensore_udoo->GetRawData(FD_u,0x60);
sensore_udoo->SetTemperature(FD_u,0x60);
sensore_udoo->SetPressure_ALT(FD_u,0x60);
temp=sensore_udoo->getTemperature();
press=sensore_udoo->getPressure();
do{
FD_a=comunication->read_port(buffer_alcohol,FD_a);
alcohol_val=atof(buffer_alcohol);
} while(alcohol_val > 1023.0 );
cout<< "sensore alcohol:"<<alcohol_val<<endl;
percentage= alcohol_val/1023.0;
BAC=percentage * 0.21;
if ( BAC < 0.10 ){
gradazione=0;
}else if ( BAC >= 0.10 && BAC <= 0.12){
gradazione=3.5;
}else if ( BAC > 0.12 && BAC <= 0.14 ){
gradazione=5.5;
}else if ( BAC > 0.14 && BAC<= 0.16 ){
gradazione=8;
}else if ( BAC > 0.16 && BAC <= 0.17 ){
gradazione=10;
}else if ( BAC > 0.17 && BAC <= 0.18 ) {
gradazione=20;
}else if( BAC > 0.20 && BAC <= 0.21 ) {
gradazione=30.5;
}else gradazione=40.5;
/*################ DATA SET in beer object #####################*/
peroni->Set_Alcohol(gradazione);
peroni->Set_Temperature(temp);
peroni->Set_Pressure(press);
cout<<"Temperatura:" << peroni->Get_Temperature()<<"\n";
cout<<"Pressione:" << peroni->Get_Pressure()<<"\n";
cout<<"Alcohol:" <<peroni->Get_Alcohol()<<"\n";
/*######### write on csv file ################### */
outline<< peroni->Get_Temperature()<<","<<peroni->Get_Alcohol()<<","<<peroni->Get_Pressure()<< ","<<tempo->tm_hour<<","<<tempo->tm_min<<","<<tempo->tm_sec<<","<<tempo->tm_mday<<"/"<<tempo->tm_mon + 1<<"/"<<tempo->tm_year + 1900 <<endl; //si può inserire qualsiasi cosa nello stream, stringhe
cout<<"Scrittura avvenuta con successo! \n\n";
if (peroni->Get_Temperature() < 22 ){
system ("echo 1 >/sys/class/gpio/gpio18/value");
}else {
system ("echo 0 >/sys/class/gpio/gpio18/value");
}
sleep(1);
}
/*############## CLOSE THE FILE ################*/
outline.close();
}else{
/*########## open the file where i write data to database ##############*/
ofstream outline;
//cout<<strerror(errno);
outline.open(FILE,ios::out);
if(!outline.is_open()) {
cout<<"error to create file"<<endl;
return -1;
}
outline<<"Temperature: "<<","<<"Alcohol:"<<","<<"Pressure:"<<","<<"Hour: "<<","<<"Minute:"<<","<<"Second:"<<","<<"Date:(Y.M.D) "<<endl;
while(1){
time_t t = time(NULL);
tm *tempo;
tempo = localtime(&t);
/* ######### read data in the sensor object ################*/
sensore_udoo->GetRawData(FD_u,0x60);
sensore_udoo->SetTemperature(FD_u,0x60);
sensore_udoo->SetPressure_ALT(FD_u,0x60);
temp=sensore_udoo->getTemperature();
press=sensore_udoo->getPressure();
do {
FD_a=comunication->read_port(buffer_alcohol,FD_a);
alcohol_val=atof(buffer_alcohol);
}while(alcohol_val < 100.0 && alcohol_val > 1023 );
cout<< "sensore alcohol:"<<alcohol_val<<endl;
percentage= alcohol_val/1023.0;
BAC=percentage * 0.21;
if ( BAC < 0.10 ){
gradazione=0;
}else if ( BAC >= 0.10 && BAC <= 0.12){
gradazione=3.5;
}else if ( BAC > 0.12 && BAC <= 0.14 ){
gradazione=5.5;
}else if ( BAC > 0.14 && BAC<= 0.16 ){
gradazione=8;
}else if ( BAC > 0.16 && BAC <= 0.17 ){
gradazione=10;
}else if ( BAC > 0.17 && BAC <= 0.18 ) {
gradazione=20;
}else if( BAC > 0.20 && BAC <= 0.21 ) {
gradazione=30.5;
}else gradazione=40.5;
/*################ DATA SET in beer object #####################*/
peroni->Set_Alcohol(gradazione);
peroni->Set_Temperature(temp);
peroni->Set_Pressure(press);
cout<<"Temperatura:" << peroni->Get_Temperature()<<"\n";
cout<<"Pressione:" << peroni->Get_Pressure()<<"\n";
cout<<"Alcohol:" <<peroni->Get_Alcohol()<<"\n";
/*######### write on csv file ################### */
outline << peroni->Get_Temperature()<<","<<peroni->Get_Alcohol()<<","<<peroni->Get_Pressure()<< ","<<tempo->tm_hour<<","<<tempo->tm_min<<","<<tempo->tm_sec <<","<<tempo->tm_mday<<"/"<<tempo->tm_mon + 1<<"/"<<tempo->tm_year + 1900 <<endl; //si può inserire qualsiasi cosa nello stream, stringhe
cout<<"Scrittura avvenuta con successo! \n\n";
if (peroni->Get_Temperature() < 22 ){
system ("echo 1 >/sys/class/gpio/gpio18/value");
}else {
system ("echo 0 >/sys/class/gpio/gpio18/value");
}
sleep(1);
}
/*############## CLOSE THE FILE ################*/
outline.close();
}
sensore_udoo->udooSensor_EXIT(FD_u);
check.close();
delete &sensore_udoo;
delete peroni;
//delete sensore;
//delete alcohol;
return 0;
}