FEAT : WIFI et NTP

This commit is contained in:
Raymond Bourges 2025-11-29 22:08:18 +01:00
parent 186a467d45
commit b07a2e3669

28
main.py
View File

@ -1,5 +1,7 @@
from machine import Pin, I2C from machine import Pin, I2C, RTC
import network
import time import time
import ntptime
led_jaune = Pin(0, Pin.OUT) led_jaune = Pin(0, Pin.OUT)
led_bleue = Pin(2, 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) button_rouge = Pin(20, Pin.IN, Pin.PULL_UP)
i2c = I2C(scl=Pin(23), sda=Pin(22), freq=20000) i2c = I2C(scl=Pin(23), sda=Pin(22), freq=20000)
I2C_ADDR = 0x11 I2C_ADDR = 0x11
ntp_ok = False
ntp_ttl = 3600
ntp_attente = ntp_ttl
rtc = RTC()
def button_jaune_presse(pin): def button_jaune_presse(pin):
print("Bouton jaune pressé !") print("Bouton jaune pressé !")
led_jaune.on() led_jaune.on()
@ -44,12 +50,30 @@ def set_relays(mask):
"""mask = bits 0..3 (1=ON, 0=OFF)""" """mask = bits 0..3 (1=ON, 0=OFF)"""
i2c.writeto_mem(I2C_ADDR, 0x10, bytes([mask])) 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 # Init
set_relays(0b0000) set_relays(0b0000)
led_jaune.off() led_jaune.off()
led_bleue.off() led_bleue.off()
led_rouge.off() led_rouge.off()
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect("Livebox-BDC6", "KFQSn7PDCMSgPpM2Ws")
while True: while True:
# now = datetime.now()
# print("Date et heure :", now)
ntp()
time.sleep(1) time.sleep(1)