jardin_bmp280_script
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
jardin_bmp280_script [2025/06/26 14:54] – créée admin | jardin_bmp280_script [2025/06/27 12:27] (Version actuelle) – admin | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== Présentation de la sonde BMP280 ====== | + | Dans cette partie, nous allons modifier nos scripts existants pour tester |
- | Le **BMP280** est un petit capteur environnemental, | + | ====== Modification du script meteo_dht22.py ====== |
- | * La **pression atmosphérique** (en hPa) | + | < |
- | * La **température de l' | + | # |
+ | import adafruit_dht | ||
+ | import adafruit_bmp280 | ||
+ | import board | ||
+ | import busio | ||
+ | import time | ||
+ | import math | ||
+ | from datetime import datetime | ||
- | C’est un capteur | + | # |
+ | dhtDevice = adafruit_dht.DHT22(board.D4) | ||
- | ===== Câblage du BMP280 sur le Raspberry Pi ===== | + | i2c = busio.I2C(board.SCL, |
- | Le BMP280 communique avec le Raspberry Pi via le protocole **I2C**, qui utilise **2 fils de données**, plus l' | + | bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x76) |
- | | Broche BMP280 | + | #Couleurs |
- | | | + | RED = " |
- | | VCC | Broche 17 (3.3V) | + | GREEN = " |
- | | GND | Broche 9 (GND) | Masse | | + | YELLOW = " |
- | | SCL | Broche 5 (GPIO3) | + | BLUE = " |
- | | SDA | Broche 3 (GPIO2) | + | MAGENTA = " |
+ | CYAN = " | ||
+ | RESET = " | ||
- | ===== Activation | + | # |
+ | def calculer_point_de_rosee(temperature, | ||
+ | # Formule pour calculer le point de rosée | ||
+ | alpha = 17.27 | ||
+ | beta = 237.7 | ||
+ | gamma = (alpha * temperature) / (beta + temperature) + math.log(humidity / 100.0) | ||
+ | point_de_rosee | ||
+ | return point_de_rosee | ||
- | Le protocole I2C est désactivé par défaut sur Raspberry Pi OS. Il faut l’activer manuellement. | + | #Calcul de l' |
+ | def calculer_humidex(temperature, | ||
+ | # Formule pour calculer l' | ||
+ | humidex = temperature + (5/9) * (6.11 * math.exp(5417.7530 * ((1/273.16) - (1/273.15 + point_de_rosee))) - 10) | ||
+ | return humidex | ||
- | **Ouvrir | + | #Boucle et affichage |
+ | while True: | ||
+ | humidity = dhtDevice.humidity | ||
+ | temperature = dhtDevice.temperature | ||
+ | pression = bmp280.pressure | ||
+ | if humidity is not None and temperature is not None: | ||
+ | now = datetime.now() | ||
+ | date_heure = now.strftime(" | ||
+ | point_de_rosee = calculer_point_de_rosee(temperature, | ||
+ | humidex = calculer_humidex(temperature, | ||
+ | print(f" | ||
+ | print(f" | ||
+ | print(f" | ||
+ | print(f" | ||
+ | print(f" | ||
+ | print(f" | ||
+ | print(" | ||
+ | else: | ||
+ | print(" | ||
- | < | + | #Pause de 20 secondes |
- | sudo raspi-config | + | |
</ | </ | ||
- | {{ : | + | ====== Modification du script capteur.py ====== |
- | + | ||
- | **Aller dans le menu :** | + | |
< | < | ||
- | 3 Interface Options | + | # |
- | I2C → Enable | + | import adafruit_dht |
- | </ | + | import adafruit_bmp280 |
+ | import board | ||
+ | import busio # | ||
+ | import math | ||
+ | from datetime import datetime | ||
- | {{ : | + | # |
+ | i2c = busio.I2C(board.SCL, | ||
- | **Redémarrer le Raspberry Pi :** | + | # |
- | < | + | bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, |
- | sudo reboot | + | |
- | </ | + | |
- | ===== Vérification de la détection du BMP280 ===== | + | def lire_donnees_capteur(): |
+ | try: | ||
+ | dhtDevice | ||
+ | humidity | ||
+ | temperature | ||
+ | dhtDevice.exit() | ||
+ | if humidity is not None and temperature is not None: | ||
+ | return round(humidity, | ||
+ | else: | ||
+ | return None, None | ||
- | Installer les **outils I2C** : | + | except RuntimeError as error: |
+ | print(" | ||
+ | return None, None | ||
- | < | + | except Exception as error: |
- | sudo apt install i2c-tools | + | |
- | </ | + | raise error #Le programme crashe, car c'est une erreur critique |
- | **Scanner | + | def calculer_point_de_rosee(temperature, |
+ | #Formule pour calculer | ||
+ | alpha = 17.27 | ||
+ | beta = 237.7 | ||
+ | gamma = (alpha | ||
+ | point_de_rosee = (beta * gamma) / (alpha - gamma) | ||
+ | return round(point_de_rosee, | ||
- | < | + | def calculer_humidex(temperature, |
- | i2cdetect | + | # |
- | </code> | + | humidex = temperature + (5/9) * (6.11 * math.exp(5417.7530 * ((1/ |
+ | return round(humidex, | ||
- | Exemple | + | def lire_pression(): |
+ | try: | ||
+ | pression = bmp280.pressure | ||
+ | return round(pression, | ||
+ | except Exception as error: | ||
+ | print(" | ||
+ | return None | ||
- | {{ :raspberry: | + | def recuperer_date_heure(): |
+ | return datetime.now().strftime(" | ||
+ | </ | ||
- | 👉 Ici, on voit bien 76, ce qui indique que le BMP280 est bien détecté. | + | ====== Modification du script app.py ====== |
- | ===== Test de la sonde en mode interactif ===== | + | < |
+ | # | ||
+ | from flask import Flask, render_template_string | ||
+ | from capteur import ( | ||
+ | lire_donnees_capteur, | ||
+ | calculer_point_de_rosee, | ||
+ | calculer_humidex, | ||
+ | recuperer_date_heure, | ||
+ | lire_pression | ||
+ | ) | ||
- | Dans votre **environnement virtuel Python**, commencer par installer la **bibliothèque pour la sonde BMP280** : | + | # Définition de l' |
+ | app = Flask(__name__) | ||
- | < | + | @app.route('/' |
- | pip3 install adafruit-circuitpython-bmp280 | + | def index(): |
- | </ | + | |
+ | if humidity is not None and temperature is not None: | ||
+ | point_de_rosee = calculer_point_de_rosee(temperature, | ||
+ | humidex = calculer_humidex(temperature, | ||
+ | date_heure = recuperer_date_heure() | ||
+ | pression = lire_pression() | ||
- | Test en mode interactif | + | html = f""" |
- | <code> | + | < |
- | import board | + | < |
- | import busio | + | < |
- | import adafruit_bmp280 | + | <li> |
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | """ | ||
+ | else: | ||
+ | html = "< | ||
- | i2c = busio.I2C(board.SCL, board.SDA) | + | return render_template_string(html) |
- | bm280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, | + | |
- | print(bme280.pressure) | + | if __name__ == ' |
- | print(bme280.temperature) | + | |
</ | </ | ||
- | |||
- | Si le scan de votre bus i2c à donné 77 comme résultat, remplacer // | ||
- | |||
- | ---- | ||
- | |||
- | Suite vers [[jardin_bmp280_script|Ajout de la sonde BMP280 à nos scripts]] |
jardin_bmp280_script.1750949694.txt.gz · Dernière modification : de admin