FEAT : Tout le caclage OK
This commit is contained in:
parent
4a9d161286
commit
f4cf814c7b
104
main.py
104
main.py
@ -1,4 +1,6 @@
|
||||
from machine import Pin, I2C, RTC
|
||||
from neopixel import NeoPixel
|
||||
|
||||
import network
|
||||
import time
|
||||
import ntptime
|
||||
@ -7,15 +9,37 @@ import localtime_fr # TODO : Lire info HP.. HC.. sur HA > sensor.compteur_linky_
|
||||
import webrepl
|
||||
|
||||
# Capter exception pour éviter les sorties intempestives (comme timeout sur ntp)
|
||||
# Regarder https://docs.micropython.org/en/latest/esp32/quickref.html#timers
|
||||
|
||||
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)
|
||||
led_jaune = Pin(0, Pin.OUT) # D0
|
||||
button_jaune = Pin(1, Pin.IN, Pin.PULL_UP) # D1
|
||||
|
||||
i2c = I2C(scl=Pin(23), sda=Pin(22), freq=20000)
|
||||
led_bleue = Pin(2, Pin.OUT) # D2
|
||||
button_bleu = Pin(21, Pin.IN, Pin.PULL_UP) # D3
|
||||
|
||||
led_rouge = Pin(20, Pin.OUT) # D9
|
||||
button_rouge = Pin(18, Pin.IN, Pin.PULL_UP) # D10
|
||||
|
||||
button_moins = Pin(16, Pin.IN, Pin.PULL_UP) # D6 (et masse)
|
||||
button_plus = Pin(17, Pin.IN, Pin.PULL_UP) # D7 (et masse)
|
||||
|
||||
brightness = 0.01 # 1 %
|
||||
|
||||
def set_pixel(i, r, g, b):
|
||||
np[i] = (
|
||||
int(r * brightness),
|
||||
int(g * brightness),
|
||||
int(b * brightness)
|
||||
)
|
||||
|
||||
pin_leds = Pin(19, Pin.OUT) # D8
|
||||
np = NeoPixel(pin_leds, 10)
|
||||
set_pixel(0, 0, 255, 0)
|
||||
set_pixel(7, 0, 255, 0)
|
||||
np.write()
|
||||
# np.brightness(50)
|
||||
|
||||
i2c = I2C(scl=Pin(23), sda=Pin(22), freq=20000) # D5, D4
|
||||
I2C_ADDR = 0x11
|
||||
|
||||
ntp_ok = False
|
||||
@ -23,37 +47,68 @@ ntp_ttl = 3600
|
||||
ntp_attente = ntp_ttl
|
||||
rtc = RTC()
|
||||
|
||||
pas_temps = 30
|
||||
pas_temps = 10
|
||||
DEBOUNCE_MS = 500
|
||||
dernier_appui = 0
|
||||
|
||||
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)
|
||||
global dernier_appui
|
||||
maintenant = time.ticks_ms()
|
||||
if time.ticks_diff(maintenant, dernier_appui) > DEBOUNCE_MS:
|
||||
dernier_appui = maintenant
|
||||
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)
|
||||
global dernier_appui
|
||||
maintenant = time.ticks_ms()
|
||||
if time.ticks_diff(maintenant, dernier_appui) > DEBOUNCE_MS:
|
||||
dernier_appui = maintenant
|
||||
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)
|
||||
global dernier_appui
|
||||
maintenant = time.ticks_ms()
|
||||
if time.ticks_diff(maintenant, dernier_appui) > DEBOUNCE_MS:
|
||||
dernier_appui = maintenant
|
||||
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 button_moins_presse(pin):
|
||||
global dernier_appui
|
||||
maintenant = time.ticks_ms()
|
||||
if time.ticks_diff(maintenant, dernier_appui) > DEBOUNCE_MS:
|
||||
dernier_appui = maintenant
|
||||
print("Bouton moins pressé !")
|
||||
|
||||
button_moins.irq(trigger=Pin.IRQ_FALLING, handler=button_moins_presse)
|
||||
|
||||
def button_plus_presse(pin):
|
||||
global dernier_appui
|
||||
maintenant = time.ticks_ms()
|
||||
if time.ticks_diff(maintenant, dernier_appui) > DEBOUNCE_MS:
|
||||
dernier_appui = maintenant
|
||||
print("Bouton plus pressé !")
|
||||
|
||||
button_plus.irq(trigger=Pin.IRQ_FALLING, handler=button_plus_presse)
|
||||
|
||||
def set_relays(mask):
|
||||
"""mask = bits 0..3 (1=ON, 0=OFF)"""
|
||||
@ -107,4 +162,5 @@ while True:
|
||||
ntp()
|
||||
print(f"Charge : {charge()}")
|
||||
time.sleep(pas_temps)
|
||||
|
||||
# set_pixel(0, 0, 0, 0) # OFF
|
||||
# np.write()
|
||||
Loading…
Reference in New Issue
Block a user