# 树莓派4B+ubuntu20.04读取ds18b20温度传感器数据

# 测试环境

树莓派4B 8G + Ubuntu20.04 64位

# 1. 断电取下内存卡插入到win10电脑上,修改内存卡下的usercfg.txt文件

# 2. 在usercfg.txt下添加如下内容:

#ds18b20
dtoverlay=w1-gpio-pullup,gpiopin=4
1
2

修改如下图所示:

# 3. 接线

将传感器模块:DQ引脚接GPIO.7引脚上、VCC接3.3V、GND接GND

up-1220fc7294b1ddaa4c0921340d95f1f94bc.png

# 4. 连接树莓派

①挂载设备驱动

sudo modprobe w1-gpio

sudo modprobe w1-therm
1
2
3

② 确认设备是否生效

cd /sys/bus/w1/devices/

1
2
ls
1

up-ef32b68032006901203b5d09c75f2349152.png

28-011939632f5b就是外接的温度传感器设备,但并不是每个客户端都显示一样的,这个是传感器的序列号。

③ 查看当前温度

cat 28-011939632f5b/w1_slave 
1

up-6b90703c659bbde547a890bb0f0dcbc02c3.png

第二行的t=25062就是当前的温度值,要换算成摄氏度,除以1000,即当前温度为25062 / 1000=25.062摄氏度。

# 5. 编写测试代码

  • # 用C语言实现

①编写代码 ds18b20.c


//ds18b20.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int Open_send(char *base){//打开发送数据
    int fd, size;
    char buffer[1024];
    fd = open(base,O_RDONLY);
    lseek(fd,69,SEEK_SET);
    size = read(fd,buffer,sizeof(buffer));
    close(fd);
    printf("temp ℃ = %f\n",(float)atoi(buffer)/1000.0);
    return 0;
}

int readFileList(char *basePath){//文件查找
    DIR *dir;
    struct dirent *ptr;
    char base[1024];

    if ((dir=opendir(basePath)) == NULL){
        perror("Open dir error...");
        exit(1);
    }
    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0){//current dir OR parrent dir
            continue;
        } else if(ptr->d_type == 10){
            memset(base,'\0',sizeof(base));
            sprintf(base,"%s",ptr->d_name);
            if((strcmp("27",base)<0)&&(strcmp("29",base)>0)){
                sprintf(base,"%s/%s/w1_slave",basePath,ptr->d_name);
                //printf("%s\n",base);
                while(1)
                Open_send(base);
            }
        }
    }
    closedir(dir);
    return 1;
}

int main(void){
    DIR *dir;
    char basePath[1024];
    memset(basePath,'\0',sizeof(basePath));
    strcpy(basePath,"/sys/bus/w1/devices");
    readFileList(basePath);
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

②编译运行

gcc -o ds18b20 ds18b20.c

./ds18b20 

1
2
3
4

  • # 用Erlang语言实现

①编写代码:ds18b20.erl

%%%-------------------------------------------------------------------
%%% @author SummerGao
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 15. 7月 2020 13:59
%%%-------------------------------------------------------------------
-module(ds18b20).
-author("SummerGao").

%% API
-export([loop/0]).

loop() ->
  {ok, Original} = file:read_file("/sys/bus/w1/devices/28-011939632f5b/w1_slave"),
  [_, A] = binary:split(Original, [<<"t=">>]),
  [B, _] = binary:split(A, [<<"\n">>]),
  T = binary_to_integer(B) / 1000,
  io:format("Temp ℃ : ~p~n", [T]),
  loop()
.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

②编译运行

Erlang/OTP 22 [erts-10.4] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]

Eshell V10.4  (abort with ^G)
1> c(ds18b20).    
{ok,ds18b20}
2> ds18b20:loop().

1
2
3
4
5
6
7

# 未完待续..........

# 想了解传感器相关知识,关注微信公众号:物联网智能传感器

物联网智能传感器

Last Updated: 11/1/2020, 2:18:11 PM