How to read a 1-wire sensor with C on a Raspberry Pi
Jamon Camisso
jamon.camisso-H217xnMUJC0sA/PxXw9srA at public.gmane.org
Fri Jan 17 00:55:57 UTC 2014
So I've managed to get a temperature sensor wired up and outputting data
from my Raspberry Pi. I can poll the /sys/bus/w1/devices file for the
sensor and see output like the following:
1a 01 4b 46 7f ff 06 10 ea : crc=ea YES
1a 01 4b 46 7f ff 06 10 ea t=17625
I've given myself the exercise of learning some C and so far I've done a
reasonable job with a C function to read it and get the t=17625 value as
a temperature, the function's output: temp is : 17.7C
const char file[] = "/sys/bus/w1/devices/device-id-here/w1_slave";
float get_temp() {
FILE *fp;
char line[40], temp_raw[5];
fp = fopen(file, "r");
if (fp == NULL) {
fprintf(stderr, "File %s not found\n", file);
exit(1);
}
else {
while (fgets(line, 40, fp) != NULL) {
// pass, just want the last line of the file
}
fclose(fp);
}
int len = strlen(line);
strncpy(temp_raw, line+len-6, 6);
float temp_f = floorf((atof(temp_raw)/1000 + 0.05)*100)/100;
printf("temp is : %.1f\n", temp_f );
return temp_f;
}
So ok, great, job done right? Well I don't think so. I think I'm going
about this in a very non-reusable manner. I can't tell if the device is
returning lines one at a time to stdout first of all, hence the while loop.
Question 1) Would fread( )or fscanf() be better here? Can I skip the
while loop? What if this was 500,000 lines long? Any better way? I was
thinking of looking at how head and tail in coreutils handle this but
figured to ask here first.
Second, I'm not sure how to extract the t=17625 value - it is always at
position len-6 (\0 line termination needs to be accounted for). It is
always the same length which is great. But:
Question 2) Is there a better way than strncpy?
Thanks for any tips,
Jamon
--
The Toronto Linux Users Group. Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
More information about the Legacy
mailing list