raspberry pi (11) DS18B20温度传感器,DHT11数字温湿度传感器,BMP180气压传感器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guzhou_diaoke/article/details/89600475

1.DS18B20温度传感器

#!/usr/bin/env python3

import os

ds18b20 = ''

def setup():
	global ds18b20
	for i in os.listdir('/sys/bus/w1/devices'):
		if i != 'w1_bus_master1':
			ds18b20 = i

def read():
#	global ds18b20
	location = '/sys/bus/w1/devices/' + ds18b20 + '/w1_slave'
	tfile = open(location)
	text = tfile.read()
	tfile.close()
	secondline = text.split("\n")[1]
	temperaturedata = secondline.split(" ")[9]
	temperature = float(temperaturedata[2:])
	temperature = temperature / 1000
	return temperature
	
def loop():
	while True:
		if read() != None:
			print("Current temperature : %0.3f C" % read())

def destroy():
	pass

if __name__ == '__main__':
	try:
		setup()
		loop()
	except KeyboardInterrupt:
		destroy()

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.DHT11数字温湿度传感器

#!/usr/bin/env python
import Adafruit_DHT as DHT
import RPi.GPIO as GPIO

Sensor = 11
humiture = 17
def setup():
	print 'Setting up, please wait...'

def loop():
	while True:
		humidity, temperature = DHT.read_retry(Sensor, humiture)

		if humidity is not None and temperature is not None:
			print 'Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity)
		else:
			print 'Failed to get reading. Try again!'

def destroy():
	GPIO.cleanup()

if __name__ == "__main__":
	setup()
	try:
		loop()
	except KeyboardInterrupt:
		destroy()

在这里插入图片描述
3.BMP180气压传感器

#!/usr/bin/env python
#---------------------------------------------------------
#
#		This is a program for Barometer Pressure Sensor
#	Module. It could measure the pressure and temperature.
#
#		This program depend on BMP085.py writted by 
#	Adafruit. 
#
#	   Barometer Module			   Pi
#			VCC ----------------- 3.3V
#			GND ------------------ GND
#			SCL ----------------- SCL1
#			SDA ----------------- SDA1
#
#---------------------------------------------------------
import Adafruit_BMP.BMP085 as BMP085
import time

def setup():
	print '\n Barometer begins...'

def loop():
	while True:
		sensor = BMP085.BMP085()
		temp = sensor.read_temperature()	# Read temperature to veriable temp
		pressure = sensor.read_pressure()	# Read pressure to veriable pressure

		print ''
		print '      Temperature = {0:0.2f} C'.format(temp)		# Print temperature
		print '      Pressure = {0:0.2f} Pa'.format(pressure)	# Print pressure
		time.sleep(1)			
		print ''

def destory():
	GPIO.cleanup()				# Release resource

if __name__ == '__main__':		# Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  	# When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destory()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/guzhou_diaoke/article/details/89600475