RaspberrPi project source code
guowenxue
2024-03-12 cbbab621e5e5d3b1407bd898544ab8b7487563a3
commit | author | age
13d8a8 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     int             found = 0;
53
54     if( !temp )
55     {
56         return -1;
57     }
58
59     /*+-------------------------------------------------------------------+
60      *|  open dierectory /sys/bus/w1/devices to get chipset Serial Number |
61      *+-------------------------------------------------------------------+*/
62     if((dirp = opendir(w1_path)) == NULL)
63     {
64         log_error("opendir error: %s\n", strerror(errno));
65         return -2;
66     }
67
68     while((direntp = readdir(dirp)) != NULL)
69     {
70         if(strstr(direntp->d_name,"28-"))
71         {
72             /* find and get the chipset SN filename */
73             strcpy(chip,direntp->d_name);
74             found = 1;
75             break;
76         }
77     }
78     closedir(dirp);
79
80     if( !found )
81     {
82         log_error("Can not find ds18b20 in %s\n", w1_path);
83         return -3;
84     }
85
86     /* get DS18B20 sample file full path: /sys/bus/w1/devices/28-xxxx/w1_slave */
87     strncat(w1_path, chip, sizeof(w1_path)-strlen(w1_path));
88     strncat(w1_path, "/w1_slave", sizeof(w1_path)-strlen(w1_path));
89
90     /* open file /sys/bus/w1/devices/28-xxxx/w1_slave to get temperature */
91     if( (fd=open(w1_path, O_RDONLY)) < 0 )
92     {
93         log_error("open %s error: %s\n", w1_path, strerror(errno));
94         return -4;
95     }
96
97     if(read(fd, buf, sizeof(buf)) < 0)
98     {
99         log_error("read %s error: %s\n", w1_path, strerror(errno));
100         return -5;
101     }
102
103     ptr = strstr(buf, "t=");
104     if( !ptr )
105     {
106         log_error("ERROR: Can not get temperature\n");
107         return -6;
108     }
109
110     ptr+=2;
111
112     /* convert string value to float value */
113     *temp = atof(ptr)/1000;
114
115     close(fd);
116
117     return 0;
118 }