python读取ds18b20温度

ds18b20连接树莓派时使用w1总线,具体针脚的定义在config.txt文件里

数据线还需要上拉电阻 10k的就行

连接好后 数据会写到/sys/bus/w1/devices/设备id/w1_slave  文件里 所以直接读取文件即可

代码如下:

凑和看吧。。

# -*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import time
import json
GPIO.setmode(GPIO.BOARD)
while True:
	file=open("/sys/bus/w1/devices/28-031504657dff/w1_slave")
	#read temprature
	text=file.read()
	#close file
	file.close()
	secondline = text.split("\n")[1]
	temperaturedata = secondline.split(" ")[9]
	temperature = float(temperaturedata[2:])
	temperature = temperature / 1000
	print temperature
	time.sleep(1)
点赞

发表评论

邮箱地址不会被公开。 必填项已用*标注