Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <dirent.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7.  
  8. int main(void) {
  9. DIR *dir;
  10. struct dirent *dirent;
  11. char dev[16]; // Dev ID
  12. char devPath[128]; // Path to device
  13. char buf[256]; // Data from device
  14. char tmpData[6]; // Temp C * 1000 reported by device
  15. char path[] = "/sys/bus/w1/devices";
  16. ssize_t numRead;
  17.  
  18. //tentative de lecture du dossier contenant le fichier dev, celui cmmence par 28-
  19. dir = opendir(path);
  20. if (dir != NULL)
  21. {
  22. while ((dirent = readdir(dir)))
  23. // 1-wire devices are links beginning with 28-
  24. if (dirent->d_type == DT_LNK &&
  25. strstr(dirent->d_name, "28-") != NULL) {
  26. strcpy(dev, dirent->d_name);
  27. printf("\nDevice: %s\n", dev);
  28.  
  29. }
  30. (void) closedir(dir);
  31.  
  32. }
  33. else
  34. {
  35. perror("Couldn't open the w1 devices directory");
  36. return 1;
  37. }
  38.  
  39.  
  40. //On va écrire dans devpath /sys/bus/w1/devices/28-xxxxxxxxxx/w1_slave
  41. sprintf(devPath, "%s/%s/w1_slave", path, dev);
  42. // Read temp continuously
  43. // On va lire le contenu du fichier ( la température) par groupe de 4 bytes
  44. while (1) {
  45. int fd = open(devPath, O_RDONLY);
  46. if (fd == -1)
  47. {
  48. perror("Couldn't open the w1 device.");
  49. return 1;
  50. }
  51. while ((numRead = read(fd, buf, 256)) > 0)
  52. {
  53. strncpy(tmpData, strstr(buf, "t=") + 2, 5);
  54. //convertit en float
  55. float tempC = strtof(tmpData, NULL);
  56. //nom du device
  57. printf("Device: %s - ", dev);
  58. //température en celius
  59. printf("Temp: %.3f C ", tempC / 1000);
  60. //température en fareneit
  61. printf("%.3f F\n\n", (tempC / 1000) * 9 / 5 + 32);
  62. }
  63. close(fd);
  64. }
  65. // return 0; --never called due to loop
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement