Installer les dépendances :
Pour le DHT22, cloner la bibliothèque fourni par Adafruit et installer là :
$ git clone https://github.com/adafruit/Adafruit_Python_DHT.git
$ cd Adafuit_Python_DHT && sudo python setup.py install
Pour tester l’installation et votre câblage :
sudo examples/Adafruit_DHT 22 4
Ici 22 correspond au DHT22, pour le DHT11, utiliser 11 . Le 4 correspond à la broche GPIO #4 du Raspberry PI.
Recommencer pour le BMP185, ce dernier est câblé sur les broche I2C.
$ git clone https://github.com/adafruit/Adafruit_Python_BMP.git $ cd Adafruit_Python_BMP && sudo python setup.py install
Enfin un petit test :
sudo examples/simpletest.py
Exemple de code
# Adafruit library import Adafruit_DHT import Adafruit_BMP.BMP085 as BMP085 # sensor config DHT_SENSOR = 22 PIN = 4 # Try to grab a sensor reading. Use the read_retry method which will retry up # to 15 times to get a sensor reading (waiting 2 seconds between each retry). # humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, PIN) # # # Default constructor will pick a default I2C bus. # # For the Raspberry Pi this means you should hook up to the only exposed I2C bus # from the main GPIO header and the library will figure out the bus number based # on the Pi's revision. # # For the Beaglebone Black the library will assume bus 1 by default, which is # exposed with SCL = P9_19 and SDA = P9_20. # # Can enable debug output by uncommenting: #import logging #logging.basicConfig(level=logging.DEBUG) bmp_sensor = BMP085.BMP085() # Optionally you can override the bus number: #sensor = BMP085.BMP085(busnum=2) # You can also optionally change the BMP085 mode to one of BMP085_ULTRALOWPOWER, # BMP085_STANDARD, BMP085_HIGHRES, or BMP085_ULTRAHIGHRES. See the BMP085 # datasheet for more details on the meanings of each mode (accuracy and power # consumption are primarily the differences). The default mode is STANDARD. sensor = BMP085.BMP085(mode=BMP085.BMP085_ULTRAHIGHRES) bmp_temp = bmp_sensor.read_temperature() bmp_pressure = bmp_sensor.read_pressure() bmp_altitude = bmp_sensor.read_altitude() bmp_sealevel_pressure = bmp_sensor.read_sealevel_pressure() print 'BMP measures' print 'Temp = {0:0.2f} *C'.format(bmp_temp) print 'Pressure = {0:0.2f} Pa'.format(bmp_pressure) print 'Altitude = {0:0.2f} m'.format(bmp_altitude) print 'Sealevel Pressure = {0:0.2f} Pa'.format(bmp_sealevel_pressure) print 'DHT ' + str(DHT_SENSOR) + ' measures' if humidity is not None and temperature is not None: print 'Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity) else: print 'Failed to get reading. Try again!' sys.exit(1)
Sources :
A venir