RaspberrPi project source code
guowenxue
2024-03-14 7fa125e56f1de17c2f6aeb9a410ff02ac4e78e85
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 int ds18b20_get_temperature(float *temp);
36
37 int main(int argc, char *argv[])
38 {
39     float       temp;
40
41     if( ds18b20_get_temperature(&temp) < 0 )
42     {
43         printf("ERROR: ds18b20 get temprature failure\n");
44         return 1;
45     }
46
47     printf("DS18B20 get temperature: %f ℃\n", temp);
48     return 0;
49 }
50
51
52 /* File Content:
53    pi@raspberrypi:~/guowenxue $ cat /sys/bus/w1/devices/28-041731f7c0ff/w1_slave
54    3a 01 4b 46 7f ff 0c 10 a5 : crc=a5 YES
55    3a 01 4b 46 7f ff 0c 10 a5 t=19625
56    */
57
58 int ds18b20_get_temperature(float *temp)
59 {
60     char            w1_path[50] = "/sys/bus/w1/devices/";
61     char            chip[20];
62     char            buf[128];
63     DIR            *dirp;
64     struct dirent  *direntp;
65     int             fd =-1;
66     char           *ptr;
67     float           value;
68     int             found = 0;
69
70     if( !temp )
71     {
72         return -1;
73     }
74
75     /*+-------------------------------------------------------------------+
76      *|  open dierectory /sys/bus/w1/devices to get chipset Serial Number |
77      *+-------------------------------------------------------------------+*/
78     if((dirp = opendir(w1_path)) == NULL)
79     {
80         printf("opendir error: %s\n", strerror(errno));
81         return -2;
82     }
83
84     while((direntp = readdir(dirp)) != NULL)
85     {
86         if(strstr(direntp->d_name,"28-"))
87         {
88             /* find and get the chipset SN filename */
89             strcpy(chip,direntp->d_name);
90             found = 1;
91             break;
92         }
93     }
94     closedir(dirp);
95
96     if( !found )
97     {
98         printf("Can not find ds18b20 in %s\n", w1_path);
99         return -3;
100     }
101
102     /* get DS18B20 sample file full path: /sys/bus/w1/devices/28-xxxx/w1_slave */
103     strncat(w1_path, chip, sizeof(w1_path)-strlen(w1_path));
104     strncat(w1_path, "/w1_slave", sizeof(w1_path)-strlen(w1_path));
105
106     /* open file /sys/bus/w1/devices/28-xxxx/w1_slave to get temperature */
107     if( (fd=open(w1_path, O_RDONLY)) < 0 )
108     {
109         printf("open %s error: %s\n", w1_path, strerror(errno));
110         return -4;
111     }
112
113     if(read(fd, buf, sizeof(buf)) < 0)
114     {
115         printf("read %s error: %s\n", w1_path, strerror(errno));
116         return -5;
117     }
118
119     ptr = strstr(buf, "t=");
120     if( !ptr )
121     {
122         printf("ERROR: Can not get temperature\n");
123         return -6;
124     }
125
126     ptr+=2;
127
128     /* convert string value to float value */
129     *temp = atof(ptr)/1000;
130
131     close(fd);
132
133     return 0;
134 }