106 lines
2.7 KiB
Python
106 lines
2.7 KiB
Python
from machine import Pin, I2C, RTC
|
|
import network
|
|
import time
|
|
import ntptime
|
|
import urequests
|
|
import localtime_fr
|
|
|
|
led_jaune = Pin(0, Pin.OUT)
|
|
led_bleue = Pin(2, Pin.OUT)
|
|
led_rouge = Pin(19, Pin.OUT)
|
|
button_jaune = Pin(1, Pin.IN, Pin.PULL_UP)
|
|
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()
|
|
|
|
pas_temps = 30
|
|
|
|
token_ha = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJkNGY4MDdmYWYzNDQ0NTc0ODY4MmFmNzA4NDdmMTE0MyIsImlhdCI6MTc2NDQ1Mzk0NSwiZXhwIjoyMDc5ODEzOTQ1fQ.DJgSqeTKPHWbKEFH3HuFih4QKt3CSqLqot34_vhCOQU"
|
|
|
|
def button_jaune_presse(pin):
|
|
print("Bouton jaune pressé !")
|
|
led_jaune.on()
|
|
led_bleue.off()
|
|
led_rouge.off()
|
|
set_relays(0b0011)
|
|
|
|
button_jaune.irq(trigger=Pin.IRQ_FALLING, handler=button_jaune_presse)
|
|
|
|
def button_bleu_presse(pin):
|
|
print("Bouton bleu !")
|
|
led_jaune.off()
|
|
led_bleue.on()
|
|
led_rouge.off()
|
|
set_relays(0b1100)
|
|
|
|
button_bleu.irq(trigger=Pin.IRQ_FALLING, handler=button_bleu_presse)
|
|
|
|
def button_rouge_presse(pin):
|
|
print("Bouton rouge pressé !")
|
|
led_jaune.off()
|
|
led_bleue.off()
|
|
led_rouge.on()
|
|
set_relays(0b1100)
|
|
|
|
button_rouge.irq(trigger=Pin.IRQ_FALLING, handler=button_rouge_presse)
|
|
|
|
|
|
def set_relays(mask):
|
|
"""mask = bits 0..3 (1=ON, 0=OFF)"""
|
|
i2c.writeto_mem(I2C_ADDR, 0x10, bytes([mask]))
|
|
|
|
def HC(date_heure) -> bool:
|
|
heure, minute = date_heure[4], date_heure[5]
|
|
nb_minutes = heure * 60 + minute
|
|
_21h30 = 21 * 60 + 30 # TODO : Ne pas faire le calcul à chaque appel
|
|
_23h30 = 23 * 60 + 30
|
|
if (nb_minutes > _21h30 and nb_minutes < _23h30):
|
|
return True
|
|
return False
|
|
|
|
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 = localtime_fr.localtime()
|
|
print(f"Date et heure : {date_heure[3]}:{date_heure[4]}:{date_heure[5]} ({ntp_attente}, {HC(date_heure)})")
|
|
ntp_attente = ntp_attente - pas_temps
|
|
|
|
def charge() -> int:
|
|
try:
|
|
headers = {"Authorization": f"Bearer {token_ha}", "Content-Type": "application/json"}
|
|
response = urequests.get("https://ha-demo.arbi.fr/api/states/sensor.zoe_batterie", headers=headers)
|
|
data = response.json()
|
|
response.close()
|
|
return int(data["state"])
|
|
except Exception as e:
|
|
print("Erreur :", e)
|
|
return -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:
|
|
# now = datetime.now()
|
|
# print("Date et heure :", now)
|
|
ntp()
|
|
print(f"Charge : {charge()}")
|
|
time.sleep(pas_temps)
|
|
|