commit | author | age
|
e30a4c
|
1 |
/********************************************************************************* |
GW |
2 |
* Copyright: (C) 2018 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(2018/10/14) |
|
9 |
* Author: Guo Wenxue <guowenxue@gmail.com> |
|
10 |
* ChangeLog: 1, Release initial version on "2018/10/14 12:13:26" |
|
11 |
* |
|
12 |
********************************************************************************/ |
|
13 |
|
|
14 |
#include <stdio.h> |
|
15 |
#include <stdlib.h> |
|
16 |
#include <unistd.h> |
|
17 |
#include <fcntl.h> |
|
18 |
#include <dirent.h> |
|
19 |
#include <string.h> |
|
20 |
#include <time.h> |
|
21 |
#include <errno.h> |
|
22 |
|
|
23 |
#include "logger.h" |
|
24 |
|
|
25 |
/* File Content: |
|
26 |
pi@raspberrypi:~/guowenxue $ cat /sys/bus/w1/devices/28-041731f7c0ff/w1_slave |
|
27 |
3a 01 4b 46 7f ff 0c 10 a5 : crc=a5 YES |
|
28 |
3a 01 4b 46 7f ff 0c 10 a5 t=19625 |
|
29 |
*/ |
|
30 |
|
|
31 |
int ds18b20_get_temperature(float *temp) |
|
32 |
{ |
|
33 |
char w1_path[50] = "/sys/bus/w1/devices/"; |
|
34 |
char chip[20]; |
|
35 |
char buf[128]; |
|
36 |
DIR *dirp; |
|
37 |
struct dirent *direntp; |
|
38 |
int fd =-1; |
|
39 |
char *ptr; |
|
40 |
int found = 0; |
|
41 |
|
|
42 |
if( !temp ) |
|
43 |
{ |
|
44 |
return -1; |
|
45 |
} |
|
46 |
|
|
47 |
/*+-------------------------------------------------------------------+ |
|
48 |
*| open dierectory /sys/bus/w1/devices to get chipset Serial Number | |
|
49 |
*+-------------------------------------------------------------------+*/ |
|
50 |
if((dirp = opendir(w1_path)) == NULL) |
|
51 |
{ |
|
52 |
log_error("opendir '%s' error: %s\n", w1_path, strerror(errno)); |
|
53 |
return -2; |
|
54 |
} |
|
55 |
|
|
56 |
while((direntp = readdir(dirp)) != NULL) |
|
57 |
{ |
|
58 |
if(strstr(direntp->d_name,"28-")) |
|
59 |
{ |
|
60 |
/* find and get the chipset SN filename */ |
|
61 |
strcpy(chip,direntp->d_name); |
|
62 |
found = 1; |
|
63 |
break; |
|
64 |
} |
|
65 |
} |
|
66 |
closedir(dirp); |
|
67 |
|
|
68 |
if( !found ) |
|
69 |
{ |
|
70 |
log_error("Can not find ds18b20 in %s\n", w1_path); |
|
71 |
return -3; |
|
72 |
} |
|
73 |
|
|
74 |
/* get DS18B20 sample file full path: /sys/bus/w1/devices/28-xxxx/w1_slave */ |
|
75 |
strncat(w1_path, chip, sizeof(w1_path)-strlen(w1_path)); |
|
76 |
strncat(w1_path, "/w1_slave", sizeof(w1_path)-strlen(w1_path)); |
|
77 |
|
|
78 |
/* open file /sys/bus/w1/devices/28-xxxx/w1_slave to get temperature */ |
|
79 |
if( (fd=open(w1_path, O_RDONLY)) < 0 ) |
|
80 |
{ |
|
81 |
log_error("open %s error: %s\n", w1_path, strerror(errno)); |
|
82 |
return -4; |
|
83 |
} |
|
84 |
|
|
85 |
if(read(fd, buf, sizeof(buf)) < 0) |
|
86 |
{ |
|
87 |
log_error("read %s error: %s\n", w1_path, strerror(errno)); |
|
88 |
return -5; |
|
89 |
} |
|
90 |
|
|
91 |
ptr = strstr(buf, "t="); |
|
92 |
if( !ptr ) |
|
93 |
{ |
|
94 |
log_error("ERROR: Can not get temperature\n"); |
|
95 |
return -6; |
|
96 |
} |
|
97 |
|
|
98 |
ptr+=2; |
|
99 |
|
|
100 |
/* convert string value to float value */ |
|
101 |
*temp = atof(ptr)/1000; |
|
102 |
|
|
103 |
close(fd); |
|
104 |
|
|
105 |
return 0; |
|
106 |
} |