C - Tips'n Tricks - Retrieve temperature of hardware devices

The file from where we can retrieve temperature depends on the operating system.
For this tutorial I used Ubuntu 12.
The file on which I took temperature is /sys/class/thermal/thermal_zone8/temp.

So you can test temperature of each file present in /sys/class/thermal/thermal_zone[0-9]/temp.

On some GNU/Linux systems, files needed are sometimes in the /proc/acpi/thermal_zone/ directory.

In the example below the heart of the code is in the main() function.
All other functions are for testing that malloc(), open(), read(), close() and free() are called without any error.

I used the sleep() function to stop the program each second in the loop.
So it iterates during 10 seconds.

Code

h.h

/* BadproG.com */

#ifndef H_H_
#define H_H_

/**
 * Define
 */
#define BUF_SIZE 5
#define ERROR_VALUE -1
#define OK 0
#define BAD 1

/*
 * Structure
 */
typedef struct s_error
{
    int fd;
    char *av;
    char *buf;
} t_error;

#endif /* H_H_ */

main.c

/* BadproG.com */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include "h.h"

/**
 * Check if malloc() succeeded
 */
int xMalloc(t_error *e)
{
    if ((e->buf = malloc(sizeof(*e->buf) * BUF_SIZE + 1)) == NULL)
    {
        fprintf(stderr, "Error: malloc().\n");
        exit(BAD);
    }
    return OK;
}

/**
 * Check if close() succeeded
 */
int xClose(t_error *e)
{
    if ((close(e->fd)) == ERROR_VALUE)
    {
        fprintf(stderr, "Error: close() -> %s.\n", strerror(errno));
        return BAD;
    }
    return OK;
}

/**
 * Check if free() succeeded
 */
int xFree(t_error *e)
{
    if (e->buf != NULL)
    {
        free(e->buf);
    }
    else
    {
        fprintf(stderr, "Error: free().\n");
        return BAD;
    }
    return OK;
}

/**
 * Check if open() succeeded
 */
int xOpen(t_error *e)
{
    if ((e->fd = open(e->av, O_RDONLY)) == ERROR_VALUE)
    {
        fprintf(stderr, "Error: %s -> %s.\n", e->av, strerror(errno));
        xFree(e);
        return BAD;
    }
    return OK;
}

/**
 * Check if read() succeeded
 */
int xRead(t_error *e)
{
    if ((read(e->fd, e->buf, BUF_SIZE)) == ERROR_VALUE)
    {
        fprintf(stderr, "Error: read() -> %s.\n", strerror(errno));
        xFree(e);
        xClose(e);
        return BAD;
    }
    return OK;
}

/**
 * main()
 */
int main(int ac, char *av[])
{
    int i;
    t_error e;

    e.av = av[1];
    if (ac != 2)
    {
        fprintf(stderr, "Usage: ./exe [path/until/your/temp]\n");
        return BAD;
    }
    xMalloc(&e);
    i = 0;
    while (i < 10)
    {
        sleep(1);
        if (xOpen(&e))
            exit(BAD);
        if (xRead(&e))
            exit(BAD);
        if (xClose(&e))
            exit(BAD);
        printf("Temperature is: %s Celsius degree.\n", e.buf);
        ++i;
    }
    if (xFree(&e))
        exit(BAD);
    return EXIT_SUCCESS;
}

Compiling

gcc main.c -o checkTemp ; ./checkTemp /sys/class/thermal/thermal_zone8/temp

Result

Temperature is: 53000 Celsius degree.
Temperature is: 55000 Celsius degree.
Temperature is: 54000 Celsius degree.
Temperature is: 53000 Celsius degree.
Temperature is: 54000 Celsius degree.
Temperature is: 54000 Celsius degree.
Temperature is: 55000 Celsius degree.
Temperature is: 53000 Celsius degree.
Temperature is: 54000 Celsius degree.
Temperature is: 54000 Celsius degree.

We can see the temperature followed by some zeros but the right one is without any 0.
So for the first line above, the temperature is 53, for the second 55, and so on.
These temperatures are in Celsius degree of course.

Great job you made it. cool

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.