Community
cancel
Showing results for 
Search instead for 
Did you mean: 
JKess
Beginner
1,468 Views

Getting Intel Edison CPU core temperatures with code

Hi, I'm working on a program and I want to retrieve the cpu temperatures. Is it possible to get the temperatures of the separate cores with code? I tried using an ifstream to open the file, but I end up getting a segmentation fault. I'm using the Intel System Studio, and writing code in C++. I know where the files are that contain them, but I don't think it's allowing me to access the files and that's why it's giving me the segmentation fault. An example of the code I use to try to retrieve the temperatures is below. There are other blocks written the same way but accessing the different thermal_zones. Would be great if I could get this working. Thanks!

std::ifstream readFile; (This is in the header file)

readFile.open("/sys/devices/virtual/thermal/thermal_zone3/temp.txt");

if(readFile.is_open())

getline(readFile,core1Temp);

else{

core1Temp = "0";

}

temp = (atoi(core1Temp.c_str()))/1000;

convert << temp;

core1Temp = convert.str();

readFile.close();

Tags (1)
0 Kudos
4 Replies
idata
Community Manager
89 Views

Hi thatboijo,

It should be possible to read the temperature for the Edison cores, and you're on the right track actually. However, I would suggest to use system calls. So for example from C, you could use the following:

system(cat /sys/class/thermal/thermal_zone1/temp)

If I'm not mistaken you'll get temperature values from thermal_zone1, thermal_zone3 and thermal_zone4. For zone0 and zone2 the cat command will throw a read error.

Regards,

 

-Pablo
JKess
Beginner
89 Views

Thanks! It works, but it's only printing out what the temp is. I've tried setting an int variable to it, and it just gets set to 0. I tried setting a string to it, and it was just a blank space. Can you not set a variable with that system call?

idata
Community Manager
89 Views

Hi thatboijo,

 

 

It is possible to do it. However, as you already noticed, it is not possible to do it just by setting a variable and getting the value after reading it. This is because you're reading from outside your code (even if you're making a system call from your C program). One way to do it would be to make a system call to read the value and then to write the read value to a new/existing file (all of this using system calls), after that, using C file operations you'll just need to read the value from the file where you wrote it previously. I hope this all makes sense, please let us know if there's something you don't understand.

 

 

Regards,

 

-Pablo
JKess
Beginner
89 Views

Thanks Pablo, I finally got it. I used the open call to open the file I needed to read, the read call to read the contents of the file, and then the write call to write it to a new file in a location that I could retrieve it from.

int read1 = open("file_location", O_RDONLY); //FILE THAT I WANTED TO READ FROM.

int write1 = open("new_file", O_TRUNC | O_WRONLY | O_APPEND | O_CREAT, S_IXUSR); //NEW FILE THAT I WANTED TO CREATE

while((n = read(read1,buf,BUFSIZ) > 0))

write(write1,buf,read1);

close(read1);

close(read2);

Reply