参考:http://shumeipai.nxez.com/2013/10/03/raspberry-pi-temperature-sensor-monitors.html

第一步,允许单总线接口

sudo raspi-config进入interfacingoptions

enable 1-wire interface

第二步,接线

接BCM编码为4即图上物理引脚7

第三步,升级内核

sudo apt-get update
sudo apt-get upgrade
pi@raspberrypi:~$ cd /sys/bus/w1/devices/
pi@raspberrypi:/sys/bus/w1/devices$ ls
-00000xxxxxx w1_bus_master1

第四步,查看当前温度

cd -00000xxxxxx cat w1_slave
显示:
4b 7f ff 0c 2f : crc=2f YES
4b 7f ff 0c 2f t=
第二行的t=20375就是当前的温度值,要换算成摄氏度,除以1000,即当前温度为20

python

 #!/usr/bin/python3
import os,time device_file ='/sys/bus/w1/devices/28-031681e171ff/w1_slave' def read_temp_raw():
f = open(device_file,'r')
lines = f.readlines()
f.close()
return lines def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string)/1000.0
return temp_c while True:
print('temp C = %f'%read_temp())
time.sleep(1)

打印结果:

 pi@raspberrypi:~/myPython $ ./temp_ds18b20.py
temp C = 20.687000
temp C = 20.687000
temp C = 20.687000
temp C = 20.750000
temp C = 20.750000
temp C = 20.750000

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>
#include <hiredis/hiredis.h> int Open_send(char *base){ //打开发送数据
int fd, size;
char buffer[];
fd = open(base,O_RDONLY);
lseek(fd,,SEEK_SET);
size = read(fd,buffer,sizeof(buffer));
close(fd);
printf("temp C = %f\n",(float)atoi(buffer)/1000.0);
return ;
} int readFileList(char *basePath) //文件查找
{
DIR *dir;
struct dirent *ptr;
char base[]; if ((dir=opendir(basePath)) == NULL){
perror("Open dir error...");
exit();
}
while ((ptr=readdir(dir)) != NULL)
{
if(strcmp(ptr->d_name,".")== || strcmp(ptr->d_name,"..")==) ///current dir OR parrent dir
continue;
else if(ptr->d_type == ){
memset(base,'\0',sizeof(base));
sprintf(base,"%s",ptr->d_name);
if((strcmp("",base)<)&&(strcmp("",base)>)){
sprintf(base,"%s/%s/w1_slave",basePath,ptr->d_name);
//printf("%s\n",base);
while()
Open_send(base);
}
}
}
closedir(dir);
return ;
} int main(void)
{
DIR *dir;
char basePath[];
memset(basePath,'\0',sizeof(basePath));
strcpy(basePath,"/sys/bus/w1/devices");
readFileList(basePath);
return ;
}

=======================================华丽的分割线=======================================

由于根据官方的说法,在2015-02-16之后的Raspbain版本,为了防止GPIO的冲突,使用了新的dt策略,更改了原来单总线gpio的配置方法,需要在配置文件中添加gpiopin=8,配置单总线的gpio引脚。

修改配置:

sudo vim /boot/config.txt

在最后一行手动添加这个,保存并重启树莓派。

dtoverlay=w1-gpio-pullup,gpiopin=

在通过配置sudo raspi-config进入interfacingoptions配置单总线时,系统会在/boot/config.txt文件添加dtoverlay=w1-gpio-pullupz,只需要在后面通过gpiopin指定引脚。

树莓派进阶之路 (015) - 树莓派使用DS18B20模块测量温度的更多相关文章

  1. 树莓派进阶之路 (012) - 树莓派配置文档 config.txt 说明

    原文连接:http://elinux.org/RPi_config.txt 由于树莓派并没有传统意义上的BIOS, 所以现在各种系统配置参数通常被存在”config.txt”这个文本文件中. 树莓派的 ...

  2. 树莓派进阶之路 (028) - 树莓派SQLite3的安装

    MySQL占用内存太大,而SQLite是一款轻量级零配置数据库,非常适合在树莓派和其他嵌入式系统中使用.SQLite文档详细资料丰富,本文不会详细解释SQLite数据库操作的方方面面,只能结合具体场景 ...

  3. 树莓派进阶之路 (019) - 树莓派通过filezilla,samba与PC文件共享(转)

    虽然我们可以很方便的通过ssh譬如putty或者vnc连接操控树莓派,但是毕竟树莓派资源没那么高,在上面编程,调试要吃力的多.所以还是想在pc上编程上传到树莓派或者最好,文件共享,可以直接读写共同的文 ...

  4. 树莓派进阶之路 (013) - 树莓派2/3 C语言使用PWM

    我手里面的是树莓派3,系统是Raspbian官方操作系统,已经安装好了wiringPi.        PWM简介:脉宽调制(PWM)是指用微处理器的数字输出来对模拟电路进行控制,是一种对模拟信号电平 ...

  5. 树莓派进阶之路 (010) - 树莓派raspi-config配置(转)

    经过前面两步我们的树莓派已经正常的工作起来了,但是在真正用它开发之前还需要进行一些列的配置以及软件的安装,这样开发起来才会得心应手,下面我们介绍一下常用的软件和服务 1.配置选项: 树莓派第一次使用的 ...

  6. 树莓派进阶之路 (008) - 树莓派安装ftp服务器(转)

    vsftpd是开源的轻量级的常用ftp服务器.   1,安装vsftpd服务器 (约400KB) sudo apt-get install vsftpd     2,启动ftp服务 sudo serv ...

  7. 树莓派进阶之路 (006) - 树莓派安装wiringPi

    安装git-core sudo apt-get install git-core 下载winringPi库 git clone git://git.drogon.net/wiringPi 编译和安装库 ...

  8. 树莓派进阶之路 (009) - 树莓派ftp脚本(原创)

    FTP.sh #!/bin/sh cd echo "彻底卸载原有的ftp" sudo apt-get remove --purge vsftpd #(--purge 选项表示彻底删 ...

  9. 树莓派进阶之路 (005) - 树莓派Zsh安装脚本(原创)

    zsh.sh #!/bin/bash cd #安装zsh sudo apt-get install zsh #查看zsh cat /etc/shells #更改zsh chsh -s /bin/zsh ...

随机推荐

  1. mahout安装

    mahout是hadoop的一种高级应用.运行mahout需要提前安装好hadoop.hadoop的安装网上很多.而且也不复杂,这里不再讲述.这里默认hadoop已经安装完成. 1:下载二进制解压安装 ...

  2. (转)unity使用line renderer画线

    原文地址:http://www.xuanyusong.com/archives/561 任何一个无规则曲线它都是有若干个线段组成,及时是圆形它也是又若干个线段组成的,也就是说将若干个线段拼接起来就是我 ...

  3. js的技巧

    字典: if(tac["detail"][sid] || tac["detail"][sid]==0) //判断某项是否存在,0为真 tac["det ...

  4. Mongoose的分页功能

    来自: https://github.com/edwardhotchkiss/mongoose-paginate   拷贝如下: Note: This plugin will only work wi ...

  5. Emeditor V14注册码

    Emeditor V14注册码 姓 名:ttrar.com 序 列 号:DKAZQ-R9TYP-5SM2A-9Z8KD-3E2RK

  6. [Algorithm] Check if a binary tree is binary search tree or not

    What is Binary Search Tree (BST) A binary tree in which for each node, value of all the nodes in lef ...

  7. (纪录片)现代生活的秘密规则:算法 The Secret Rules of Modern Living: Algorithms

    简介: The Secret Rules of Modern Living: Algorithms (2015) 导演: David Briggs主演: Marcus du Sautoy类型: 纪录片 ...

  8. python之simplejson,Python版的简单、 快速、 可扩展 JSON 编码器/解码器

    python之simplejson,Python版的简单. 快速. 可扩展 JSON 编码器/解码器 simplejson Python版的简单. 快速. 可扩展 JSON 编码器/解码器 编码基本的 ...

  9. jQuery函数的等价原生函数代码示例

    选择器 jQuery的核心之一就是能非常方便的取到DOM元素.我们只需输入CSS选择字符串,便可以得到匹配的元素.但在大多数情况下,我们可以用简单的原生代码达到同样的效果. .代码如下: //---- ...

  10. C# FTP常规方法

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...