micro-machines:esp32:projet_meteo
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 | ||
| micro-machines:esp32:projet_meteo [2026/11/14 12:27] – [Dialogue avec Home Assistant] dan | micro-machines:esp32:projet_meteo [2026/11/14 12:33] (Version actuelle) – dan | ||
|---|---|---|---|
| Ligne 75: | Ligne 75: | ||
| - | ### Dialogue avec Home Assistant | ||
| - | |||
| - | |||
| - | #### Exemple : affichage des valeurs de trois sondes dans la Console de Thonny (sans CYD) | ||
| - | |||
| - | Cet exemple est composé de trois fichiers : | ||
| - | - boot.py : assure la connexion au Wifi, affiche l'IP, la Mac Adress, la date, l' | ||
| - | - main.py : le code qui assure la connexion et affiche les valeurs dans la console de Thonny | ||
| - | - hadial.conf : stocke les données de connexion à Home Assistant (IP de Home Assistant, le port, le jeton d' | ||
| - | |||
| - | **Fichier boot.py** | ||
| - | |||
| - | ```python | ||
| - | # connexion WIFI au boot | ||
| - | import network | ||
| - | import time | ||
| - | import ntptime | ||
| - | import utime | ||
| - | import ubinascii | ||
| - | from machine import RTC | ||
| - | |||
| - | ssid = ' | ||
| - | password = ' | ||
| - | |||
| - | if __name__==" | ||
| - | wlan = network.WLAN(network.STA_IF) | ||
| - | wlan.active(True) | ||
| - | if not wlan.isconnected(): | ||
| - | print(f" | ||
| - | wlan.connect(ssid, | ||
| - | |||
| - | while not wlan.isconnected(): | ||
| - | print(' | ||
| - | time.sleep_ms(500) | ||
| - | | ||
| - | # récupération de la Mac Address | ||
| - | wlan_mac = wlan.config(' | ||
| - | # Décodage de la Mac Address | ||
| - | wlan_mac = ':' | ||
| - | | ||
| - | # Affichage des données réseau | ||
| - | print(' | ||
| - | connect = wlan.ifconfig() | ||
| - | print(" | ||
| - | print(" | ||
| - | print(" | ||
| - | print(" | ||
| - | print(" | ||
| - | | ||
| - | try: | ||
| - | # mise à l' | ||
| - | ntptime.settime() | ||
| - | dt = RTC().datetime() | ||
| - | | ||
| - | # Affichage de la date et de l' | ||
| - | print(f" | ||
| - | print(f" | ||
| - | | ||
| - | print(" | ||
| - | print(" | ||
| - | except: | ||
| - | print(" | ||
| - | ``` | ||
| - | |||
| - | **Fichier main.py** | ||
| - | |||
| - | ```python | ||
| - | import time | ||
| - | import urequests | ||
| - | |||
| - | with open(" | ||
| - | lines = f.readlines() | ||
| - | IP_HA = lines[0].rstrip() | ||
| - | port_HA = lines[1].rstrip() | ||
| - | jeton = " | ||
| - | |||
| - | IP_port_HA = IP_HA + ":" | ||
| - | |||
| - | |||
| - | class DialHA: | ||
| - | |||
| - | def __init__(self, | ||
| - | self.HA_URL = " | ||
| - | |||
| - | # Déclaration du jeton fourni par Home Assistant | ||
| - | self.HEADERS = { | ||
| - | " | ||
| - | " | ||
| - | } | ||
| - | self.sensor = sensor | ||
| - | |||
| - | def get_sensor_value(self): | ||
| - | url = self.HA_URL + self.sensor | ||
| - | response = None | ||
| - | | ||
| - | try: | ||
| - | response = urequests.get(url, | ||
| - | if response.status_code == 200: # Si l' | ||
| - | data = response.json() | ||
| - | state = data.get(" | ||
| - | return float(state) | ||
| - | else: | ||
| - | print(f" | ||
| - | | ||
| - | except Exception as e: | ||
| - | print(f" | ||
| - | | ||
| - | finally: | ||
| - | if response: | ||
| - | response.close() | ||
| - | |||
| - | return None # La sonde n'a rien renvoyé | ||
| - | |||
| - | |||
| - | # Instanciation des sondes | ||
| - | tempe_cuisi = DialHA(" | ||
| - | humid_cuisi = DialHA(" | ||
| - | tempe_salon = DialHA(" | ||
| - | humid_salon = DialHA(" | ||
| - | tempe_cham1 = DialHA(" | ||
| - | humid_cham1 = DialHA(" | ||
| - | |||
| - | while True: | ||
| - | tempe_cu = tempe_cuisi.get_sensor_value() | ||
| - | humid_cu = humid_cuisi.get_sensor_value() | ||
| - | tempe_sa = tempe_salon.get_sensor_value() | ||
| - | humid_sa = humid_salon.get_sensor_value() | ||
| - | tempe_c1 = tempe_cham1.get_sensor_value() | ||
| - | humid_c1 = humid_cham1.get_sensor_value() | ||
| - | | ||
| - | print(f" | ||
| - | print(f" | ||
| - | print(f" | ||
| - | print(" | ||
| - | |||
| - | time.sleep(60) | ||
| - | |||
| - | ``` | ||
| - | |||
| - | **Fichier hadial.conf :** | ||
| - | |||
| - | ```python | ||
| - | [IP_Home_Assistant] | ||
| - | [PORT] | ||
| - | [JETON] | ||
| - | ``` | ||
| - | |||
| - | > **Note :** il faut alimenter hadial.conf avec les valeurs propres à Home Assistant. | ||
| - | > | ||
| - | > Pour créer le jeton, il faut aller dans Home Assistant , cliquer sur " | ||
| - | |||
| - | |||
| - | > **Attention :** | ||
| - | > Échec de la connexion TCP : [Errno 116] ETIMEDOUT | ||
| - | > Vérifier l' | ||
| - | > Vérifier le pare-feu de Home Assistant | ||
| ### Les icônes de Home Assistant | ### Les icônes de Home Assistant | ||
| - https:// | - https:// | ||
| - https:// | - https:// | ||
micro-machines/esp32/projet_meteo.1794655627.txt.gz · Dernière modification : de dan
