Skip to content

Commit

Permalink
Retry loop added; Formatted datetime added to output; Calibration met…
Browse files Browse the repository at this point in the history
…hod added;
  • Loading branch information
SSL authored and bitplane committed Feb 10, 2012
1 parent 583d9a4 commit 3c4f51c
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions temper.c
Expand Up @@ -3,23 +3,56 @@
*
*/

#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);

usb_dev_handle* lvr_winusb = pcsensor_open();
if (!((tempc > -0.0001 && tempc < 0.0001) || passes >= 4)) {
/* Apply calibrations */
tempc = (tempc * scale) + offset;

if (!lvr_winusb) {
printf("NaN");
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 -1;
return 0;
}
else {
return 1;
}

float tempc = pcsensor_get_temperature(lvr_winusb);
pcsensor_close(lvr_winusb);
printf("%f", tempc);
fflush(stdout);

return 0;
}

0 comments on commit 3c4f51c

Please sign in to comment.