129 lines
4.8 KiB
Python
129 lines
4.8 KiB
Python
import _thread
|
|
from machine import Pin
|
|
from shared import get_lora, get_uart, deviceTypes, config, cookingState
|
|
from shared.uart_comm import UARTCommand, UARTCommandType
|
|
from shared.sensors import RGBLED
|
|
from shared.logging import log
|
|
import time
|
|
|
|
# --- Configuration Matérielle ---
|
|
vext = Pin(19, Pin.OUT)
|
|
vext.value(0)
|
|
time.sleep_ms(100)
|
|
|
|
# --- Lecture de l'ID unique de l'ESP ---
|
|
try:
|
|
with open("device_id.txt", "r") as f:
|
|
DEVICE_ID = f.read().strip()
|
|
except Exception:
|
|
DEVICE_ID = "ESP32_Inconnu"
|
|
|
|
# --- Initialisation LoRa ---
|
|
lora = get_lora()
|
|
lora.configure(freq=868.1, sf=7)
|
|
|
|
# --- Création des lEDs RGB ---
|
|
magnetron_led = RGBLED(red_pin=48, green_pin=47, blue_pin=33)
|
|
magnetron_led.color = RGBLED.WHITE_YELLOW
|
|
magnetron_led.off()
|
|
|
|
print(f"ESP32 initialisé avec l'ID : '{DEVICE_ID}' (Type : {deviceTypes.DEVICE_TYPES['MICROWAVE']})")
|
|
|
|
def heartbeat_loop():
|
|
while True:
|
|
print(f"\nESP32 : Envoi du Heartbeat...")
|
|
# Envoi périodique
|
|
ping_payload = {
|
|
"id": DEVICE_ID,
|
|
"type": deviceTypes.DEVICE_TYPES["MICROWAVE"]
|
|
}
|
|
lora.send(ping_payload)
|
|
|
|
# Le receive_packet est maintenant protégé par le lock dans lora_device
|
|
# Si le main thread utilise la radio, ce thread attendra son tour
|
|
paquet = lora.receive_packet(timeout_ms=2000)
|
|
|
|
if paquet and not paquet["raw"]:
|
|
donnees = paquet["data"]
|
|
# Vérification si le paquet reçu est bien la réponse attendue de l'orchestrateur
|
|
if donnees.get("type") == deviceTypes.DEVICE_TYPES["ORCHESTRATOR"]:
|
|
print(f"ESP32 : Réponse reçue de l'orchestrateur '{donnees.get('id')}' ! [Statut: ALIVE]")
|
|
else:
|
|
print(f"ESP32 : Paquet reçu d'un type inattendu : {donnees.get('type')}")
|
|
else:
|
|
print("ESP32 : Pas de réponse de l'orchestrateur (Le RPI est-il éteint ?)")
|
|
|
|
time.sleep(config.HEARTBEAT_INTERVAL)
|
|
|
|
# UART
|
|
uart_device = get_uart(uart_id=1, tx_pin=46, rx_pin=45)
|
|
|
|
# Lancer la boucle de heartbeat dans un thread séparé
|
|
_thread.start_new_thread(heartbeat_loop, ())
|
|
|
|
# Cooking parameters
|
|
cooking_state = None
|
|
def cooking_state_temperature_provider():
|
|
return 22.0, 29.0 # TODO Remplacer par la lecture réelle de la température du plat et de l'air ambiant
|
|
|
|
def cooking_state_on_state_change(state):
|
|
print(f"[Main] Cooking state changed to: {state.state}")
|
|
|
|
# Send to the Wifi board the current state
|
|
uart_device.send_as_command(UARTCommand(UARTCommandType.COOKING_STATE_UPDATE, {"state": state.state}))
|
|
|
|
if state.paused or state.state == cookingState.CookingStates.DONE:
|
|
magnetron_led.off()
|
|
else:
|
|
magnetron_led.on()
|
|
|
|
|
|
if state.state == cookingState.CookingStates.COOKING:
|
|
pass
|
|
if state.state == cookingState.CookingStates.STIRRING_REQUIRED:
|
|
pass
|
|
if state.state == cookingState.CookingStates.DONE:
|
|
global cooking_state
|
|
cooking_state = None
|
|
if state.state == cookingState.CookingStates.ALERT:
|
|
pass
|
|
|
|
def cooking_state_on_refresh(state):
|
|
# TODO Show screen information
|
|
pass
|
|
|
|
|
|
# --- MAIN APPLICATION THREAD ---
|
|
print("[Main] Main execution path active.")
|
|
while True:
|
|
# 1. Listen for incoming UART serial packets from the WROOM board
|
|
while uart_device.any():
|
|
command = uart_device.read_as_command()
|
|
if command:
|
|
print(f"[Main] Received command from WiFi Board: {command.command_type}")
|
|
if command.command_type == UARTCommandType.COOKING_PARAMS:
|
|
# Handle cooking parameters command
|
|
params = command.payload
|
|
print(f"[Main] Cooking parameters received: {params}")
|
|
cooking_state = cookingState.CookingState(
|
|
cook_time=params["cook_time"],
|
|
power_level=params["power_level"],
|
|
target_temp=params["target_temp"]
|
|
)
|
|
cooking_state.set_temperature_provider(cooking_state_temperature_provider)
|
|
cooking_state.set_state_change_callback(cooking_state_on_state_change)
|
|
cooking_state.set_refresh_callback(cooking_state_on_refresh)
|
|
time.sleep_ms(20) # Before sending back right away
|
|
cooking_state_on_state_change(cooking_state)
|
|
|
|
else:
|
|
print(f"[Main] Unknown command type received: {command.command_type}")
|
|
|
|
# uart_device.send(f"Hello from esp-32 lora ID {DEVICE_ID}")
|
|
|
|
# Cooking State Update
|
|
if cooking_state:
|
|
cooking_state.update_tick()
|
|
print(f"[Main] Cooking state : State : {cooking_state.state}, Temperature: {cooking_state.current_dish_temp}, Paused: {cooking_state.paused}, Remaining Time: {cooking_state.get_remaining_time():.2f}s, Estimated Remaining Time: {cooking_state.get_remaining_time_estimation():.2f}s")
|
|
|
|
time.sleep_ms(200) |