Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <dirent.h>
- #include <string.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <unistd.h>
- int main(void) {
- DIR *dir;
- struct dirent *dirent;
- char dev[16]; // Dev ID
- char devPath[128]; // Path to device
- char buf[256]; // Data from device
- char tmpData[6]; // Temp C * 1000 reported by device
- char path[] = "/sys/bus/w1/devices";
- ssize_t numRead;
- //tentative de lecture du dossier contenant le fichier dev, celui cmmence par 28-
- dir = opendir(path);
- if (dir != NULL)
- {
- while ((dirent = readdir(dir)))
- // 1-wire devices are links beginning with 28-
- if (dirent->d_type == DT_LNK &&
- strstr(dirent->d_name, "28-") != NULL) {
- strcpy(dev, dirent->d_name);
- printf("\nDevice: %s\n", dev);
- }
- (void) closedir(dir);
- }
- else
- {
- perror("Couldn't open the w1 devices directory");
- return 1;
- }
- //On va écrire dans devpath /sys/bus/w1/devices/28-xxxxxxxxxx/w1_slave
- sprintf(devPath, "%s/%s/w1_slave", path, dev);
- // Read temp continuously
- // On va lire le contenu du fichier ( la température) par groupe de 4 bytes
- while (1) {
- int fd = open(devPath, O_RDONLY);
- if (fd == -1)
- {
- perror("Couldn't open the w1 device.");
- return 1;
- }
- while ((numRead = read(fd, buf, 256)) > 0)
- {
- strncpy(tmpData, strstr(buf, "t=") + 2, 5);
- //convertit en float
- float tempC = strtof(tmpData, NULL);
- //nom du device
- printf("Device: %s - ", dev);
- //température en celius
- printf("Temp: %.3f C ", tempC / 1000);
- //température en fareneit
- printf("%.3f F\n\n", (tempC / 1000) * 9 / 5 + 32);
- }
- close(fd);
- }
- // return 0; --never called due to loop
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement