I know you can get the temperature using the tool vcgencmd but I was wondering if there is any API I can use in C to get the temperature programatically without reading the stdout of vcgencmd ?
You can read the file
/sys/class/thermal/thermal_zone0/temp, as specified in this answer. There it's about measuring from the command line, and the file is read with
cat. But you should be able to just open the file in C. The temperature is returned in milli-degrees Centigrade and as ASCII numbers.
Perhaps like this, not tested :)
FILE *temperatureFile; double T; temperatureFile = fopen ("/sys/class/thermal/thermal_zone0/temp", "r"); if (temperatureFile == NULL) ; //print some message fscanf (temperatureFile, "%lf", &T); T /= 1000; printf ("The temperature is %6.3f C.\n", T); fclose (temperatureFile);
As @Frepa said, there is kernel thermal driver for RaspberryPi now so the easiest way is to use it. As far as I know, that's not the way
vgencmd works however. It was able to read the temperature even before thermal kernel driver existed. It is using special communication mechanism available on RaspberryPi called
mailbox in order to communicate with VideoCore OS running on GPU. It is partly documented here.