Skip to content

Instantly share code, notes, and snippets.

@DanBrown47
Created October 21, 2021 15:38
Show Gist options
  • Save DanBrown47/3bf68c0f74df7f9bcb9d8d5920011d18 to your computer and use it in GitHub Desktop.
Save DanBrown47/3bf68c0f74df7f9bcb9d8d5920011d18 to your computer and use it in GitHub Desktop.
C code for getting temperature in Raspberry Pi
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
int temp = 0;
fp = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
fscanf(fp, "%d", &temp);
printf("CPU Temp: %.2f°C\n", temp / 1000.0);
fclose(fp);
return 0;
}
@DanBrown47
Copy link
Author

Compile this code

$ gcc temperature.c -o temp

Then move this to a directory in path to run from anywhere

$ sudo mv ./temp /usr/local/bin/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment