RaspberrPi project source code
guowenxue
2024-05-27 2c971f2fcf6c6322a0ea584b2af4c3cef20d3d63
commit | author | age
d6b4a7 1 /*********************************************************************************
G 2  *      Copyright:  (C) 2023 LingYun IoT System Studio
3  *                  All rights reserved.
4  *
5  *       Filename:  ds18b20.c
6  *    Description:  This file is temperature sensor DS18B20 code
7  *
8  *        Version:  1.0.0(2023/8/10)
9  *         Author:  Guo Wenxue <guowenxue@gmail.com>
10  *      ChangeLog:  1, Release initial version on "2023/8/10 12:13:26"
11  *
12  * Pin connection:
13  *
14  *               DS18B20 Module          Raspberry Pi Board
15  *                   VCC      <----->      #Pin1(3.3V)
16  *                   DQ       <----->      #Pin7(BCM GPIO4)
17  *                   GND      <----->      GND
18  *
19  * /boot/config.txt:
20  *
21  *          dtoverlay=w1-gpio-pullup,gpiopin=4
22  *
23  ********************************************************************************/
24
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <dirent.h>
31 #include <string.h>
32 #include <time.h>
33 #include <errno.h>
34
35 #include "logger.h"
36
37 /* File Content:
38    pi@raspberrypi:~/guowenxue $ cat /sys/bus/w1/devices/28-041731f7c0ff/w1_slave
39    3a 01 4b 46 7f ff 0c 10 a5 : crc=a5 YES
40    3a 01 4b 46 7f ff 0c 10 a5 t=19625
41    */
42
43 int ds18b20_get_temperature(float *temp)
44 {
45     char            w1_path[50] = "/sys/bus/w1/devices/";
46     char            chip[20];
47     char            buf[128];
48     DIR            *dirp;
49     struct dirent  *direntp;
50     int             fd =-1;
51     char           *ptr;
52     float           value;
53     int             found = 0;
54
55     if( !temp )
56     {
57         return -1;
58     }
59
60     /*+-------------------------------------------------------------------+
61      *|  open dierectory /sys/bus/w1/devices to get chipset Serial Number |
62      *+-------------------------------------------------------------------+*/
63     if((dirp = opendir(w1_path)) == NULL)
64     {
65         log_error("opendir error: %s\n", strerror(errno));
66         return -2;
67     }
68
69     while((direntp = readdir(dirp)) != NULL)
70     {
71         if(strstr(direntp->d_name,"28-"))
72         {
73             /* find and get the chipset SN filename */
74             strcpy(chip,direntp->d_name);
75             found = 1;
76             break;
77         }
78     }
79     closedir(dirp);
80
81     if( !found )
82     {
83         log_error("Can not find ds18b20 in %s\n", w1_path);
84         return -3;
85     }
86
87     /* get DS18B20 sample file full path: /sys/bus/w1/devices/28-xxxx/w1_slave */
88     strncat(w1_path, chip, sizeof(w1_path)-strlen(w1_path));
89     strncat(w1_path, "/w1_slave", sizeof(w1_path)-strlen(w1_path));
90
91     /* open file /sys/bus/w1/devices/28-xxxx/w1_slave to get temperature */
92     if( (fd=open(w1_path, O_RDONLY)) < 0 )
93     {
94         log_error("open %s error: %s\n", w1_path, strerror(errno));
95         return -4;
96     }
97
98     if(read(fd, buf, sizeof(buf)) < 0)
99     {
100         log_error("read %s error: %s\n", w1_path, strerror(errno));
101         return -5;
102     }
103
104     ptr = strstr(buf, "t=");
105     if( !ptr )
106     {
107         log_error("ERROR: Can not get temperature\n");
108         return -6;
109     }
110
111     ptr+=2;
112
113     /* convert string value to float value */
114     *temp = atof(ptr)/1000;
115
116     close(fd);
117
118     return 0;
119 }