From b07a2e366930d224f576b9882466528f30e3f8d9 Mon Sep 17 00:00:00 2001 From: Raymond Bourges Date: Sat, 29 Nov 2025 22:08:18 +0100 Subject: [PATCH] FEAT : WIFI et NTP --- main.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 8cb7336..c23dc17 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,7 @@ -from machine import Pin, I2C +from machine import Pin, I2C, RTC +import network import time +import ntptime led_jaune = Pin(0, Pin.OUT) led_bleue = Pin(2, Pin.OUT) @@ -9,9 +11,13 @@ button_bleu = Pin(21, Pin.IN, Pin.PULL_UP) button_rouge = Pin(20, Pin.IN, Pin.PULL_UP) i2c = I2C(scl=Pin(23), sda=Pin(22), freq=20000) - I2C_ADDR = 0x11 +ntp_ok = False +ntp_ttl = 3600 +ntp_attente = ntp_ttl +rtc = RTC() + def button_jaune_presse(pin): print("Bouton jaune pressé !") led_jaune.on() @@ -44,12 +50,30 @@ def set_relays(mask): """mask = bits 0..3 (1=ON, 0=OFF)""" i2c.writeto_mem(I2C_ADDR, 0x10, bytes([mask])) +def ntp(): + global ntp_ok, ntp_ttl, ntp_attente + if (wifi.isconnected() and (not ntp_ok or ntp_attente < 0)): + print("WIFI OK, call NTP") + ntptime.settime() + ntp_ok = True + ntp_attente = ntp_ttl + if (ntp_ok): + date_heure = rtc.datetime() + print(f"Date et heure : {date_heure} ({ntp_attente})") + ntp_attente = ntp_attente - 1 + # Init set_relays(0b0000) led_jaune.off() led_bleue.off() led_rouge.off() +wifi = network.WLAN(network.STA_IF) +wifi.active(True) +wifi.connect("Livebox-BDC6", "KFQSn7PDCMSgPpM2Ws") while True: - time.sleep(1) + # now = datetime.now() + # print("Date et heure :", now) + ntp() + time.sleep(1) \ No newline at end of file