forked from elpeo/rbtemper
-
Notifications
You must be signed in to change notification settings - Fork 29
/
temper.c
58 lines (47 loc) · 1.15 KB
/
temper.c
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
/*
* Standalone temperature logger
*
*/
#include <stdio.h>
#include <time.h>
#include "pcsensor.h"
/* Calibration adjustments */
/* See http://www.pitt-pladdy.com/blog/_20110824-191017_0100_TEMPer_under_Linux_perl_with_Cacti/ */
static float scale = 1.0287;
static float offset = -0.85;
int main(){
int passes = 0;
float tempc = 0.0000;
do {
usb_dev_handle* lvr_winusb = pcsensor_open();
if (!lvr_winusb) {
/* Open fails sometime, sleep and try again */
sleep(3);
}
else {
tempc = pcsensor_get_temperature(lvr_winusb);
pcsensor_close(lvr_winusb);
}
++passes;
}
/* Read fails silently with a 0.0 return, so repeat until not zero
or until we have read the same zero value 3 times (just in case
temp is really dead on zero */
while ((tempc > -0.0001 && tempc < 0.0001) || passes >= 4);
if (!((tempc > -0.0001 && tempc < 0.0001) || passes >= 4)) {
/* Apply calibrations */
tempc = (tempc * scale) + offset;
struct tm *utc;
time_t t;
t = time(NULL);
utc = gmtime(&t);
char dt[80];
strftime(dt, 80, "%d-%b-%Y %H:%M", utc);
printf("%s,%f\n", dt, tempc);
fflush(stdout);
return 0;
}
else {
return 1;
}
}