jardin_bmp280_script
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| jardin_bmp280_script [2025/06/27 11:45] – admin | jardin_bmp280_script [2025/08/04 13:10] (Version actuelle) – [Modification du script app.py] admin | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | Dans cette partie, nous allons modifier nos scripts existants pour tester la sonde bmp280 aussi bien dans le **terminal** que sur le **serveur web (flask)**. | ||
| + | |||
| ====== Modification du script meteo_dht22.py ====== | ====== Modification du script meteo_dht22.py ====== | ||
| Ligne 24: | Ligne 26: | ||
| BLUE = " | BLUE = " | ||
| MAGENTA = " | MAGENTA = " | ||
| + | CYAN = " | ||
| RESET = " | RESET = " | ||
| Ligne 56: | Ligne 59: | ||
| print(f" | print(f" | ||
| print(f" | print(f" | ||
| - | print(f" | + | print(f" |
| print(" | print(" | ||
| else: | else: | ||
| Ligne 64: | Ligne 67: | ||
| time.sleep(20) | time.sleep(20) | ||
| </ | </ | ||
| + | |||
| + | ====== Modification du script capteur.py ====== | ||
| + | |||
| + | < | ||
| + | # | ||
| + | import adafruit_dht | ||
| + | import adafruit_bmp280 | ||
| + | import board | ||
| + | import busio # | ||
| + | import math | ||
| + | from datetime import datetime | ||
| + | |||
| + | # | ||
| + | i2c = busio.I2C(board.SCL, | ||
| + | |||
| + | # | ||
| + | bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, | ||
| + | |||
| + | def lire_donnees_capteur(): | ||
| + | try: | ||
| + | dhtDevice = adafruit_dht.DHT22(board.D4) | ||
| + | humidity = dhtDevice.humidity | ||
| + | temperature = dhtDevice.temperature | ||
| + | dhtDevice.exit() | ||
| + | if humidity is not None and temperature is not None: | ||
| + | return round(humidity, | ||
| + | else: | ||
| + | return None, None | ||
| + | |||
| + | except RuntimeError as error: | ||
| + | print(" | ||
| + | return None, None | ||
| + | |||
| + | except Exception as error: | ||
| + | dhtDevice.exit() #Libérer le GPIO même en cas de crash | ||
| + | raise error #Le programme crashe, car c'est une erreur critique | ||
| + | |||
| + | 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) | ||
| + | point_de_rosee = (beta * gamma) / (alpha - gamma) | ||
| + | return round(point_de_rosee, | ||
| + | |||
| + | 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 round(humidex, | ||
| + | |||
| + | def lire_pression(): | ||
| + | try: | ||
| + | pression = bmp280.pressure | ||
| + | return round(pression, | ||
| + | except Exception as error: | ||
| + | print(" | ||
| + | return None | ||
| + | |||
| + | def recuperer_date_heure(): | ||
| + | return datetime.now().strftime(" | ||
| + | </ | ||
| + | |||
| + | ====== Modification du script app.py ====== | ||
| + | |||
| + | < | ||
| + | # | ||
| + | from flask import Flask, render_template_string | ||
| + | from capteur import ( | ||
| + | lire_donnees_capteur, | ||
| + | calculer_point_de_rosee, | ||
| + | calculer_humidex, | ||
| + | recuperer_date_heure, | ||
| + | lire_pression | ||
| + | ) | ||
| + | |||
| + | # Définition de l' | ||
| + | app = Flask(__name__) | ||
| + | |||
| + | @app.route('/' | ||
| + | def index(): | ||
| + | humidity, temperature = lire_donnees_capteur() | ||
| + | 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() | ||
| + | |||
| + | html = f""" | ||
| + | < | ||
| + | <ul> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ul> | ||
| + | """ | ||
| + | else: | ||
| + | html = "< | ||
| + | |||
| + | return render_template_string(html) | ||
| + | |||
| + | if __name__ == ' | ||
| + | app.run(host=' | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | Suite vers [[jardin_couleur|Utilisation de couleurs dans le terminal avec rich]] | ||
jardin_bmp280_script.1751024707.txt.gz · Dernière modification : de admin
