Weatherg : Lecture des capteurs DHT22 et BMP185 en Python

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

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Matomo encountered an error: Uncaught Error: Class "Piwik\Plugins\CustomVariables\CustomVariables" not found in /var/www/piwik/core/Tracker/TrackerCodeGenerator.php:101 Stack trace: #0 /var/www/piwik/plugins/SitesManager/API.php(160): Piwik\Tracker\TrackerCodeGenerator->generate() #1 [internal function]: Piwik\Plugins\SitesManager\API->getJavascriptTag() #2 /var/www/piwik/core/API/Proxy.php(255): call_user_func_array() #3 /var/www/piwik/core/Context.php(29): Piwik\API\Proxy->Piwik\API\{closure}() #4 /var/www/piwik/core/API/Proxy.php(158): Piwik\Context::executeWithQueryParameters() #5 /var/www/piwik/core/API/Request.php(274): Piwik\API\Proxy->call() #6 /var/www/thegtricks/wp-content/plugins/wp-piwik/classes/WP_Piwik/Request/Php.php(46): Piwik\API\Request->process() #7 /var/www/thegtricks/wp-content/plugins/wp-piwik/classes/WP_Piwik/Request/Php.php(18): WP_Piwik\Request\Php->call() #8 /var/www/thegtricks/wp-content/plugins/wp-piwik/classes/WP_Piwik/Request.php(63): WP_Piwik\Request\Php->request() #9 /var/www/thegtricks/wp-content/plugins/wp-piwik/classes/WP_Piwik.php(1038): WP_Piwik\Request->perform() #10 /var/www/thegtricks/wp-content/plugins/wp-piwik/classes/WP_Piwik.php(1205): WP_Piwik->request() #11 /var/www/thegtricks/wp-content/plugins/wp-piwik/classes/WP_Piwik/TrackingCode.php(16): WP_Piwik->updateTrackingCode() #12 /var/www/thegtricks/wp-content/plugins/wp-piwik/classes/WP_Piwik.php(296): WP_Piwik\TrackingCode->__construct() #13 /var/www/thegtricks/wp-includes/class-wp-hook.php(324): WP_Piwik->addJavascriptCode() #14 /var/www/thegtricks/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters() #15 /var/www/thegtricks/wp-includes/plugin.php(517): WP_Hook->do_action() #16 /var/www/thegtricks/wp-includes/general-template.php(3081): do_action() #17 /var/www/thegtricks/wp-content/themes/twentyfifteen-child/footer.php(45): wp_footer() #18 /var/www/thegtricks/wp-includes/template.php(810): require_once('...') #19 /var/www/thegtricks/wp-includes/template.php(745): load_template() #20 /var/www/thegtricks/wp-includes/general-template.php(92): locate_template() #21 /var/www/thegtricks/wp-content/themes/twentyfifteen/single.php(53): get_footer() #22 /var/www/thegtricks/wp-includes/template-loader.php(106): include('...') #23 /var/www/thegtricks/wp-blog-header.php(19): require_once('...') #24 /var/www/thegtricks/index.php(17): require('...') #25 {main} thrown (which lead to: Session must be started before any output has been sent to the browser; output started in /var/www/thegtricks/wp-includes/script-loader.php/2936)