Files
Smartwave/micro_ondes/esp_lora/main.py
T
2026-07-30 17:39:29 +02:00

74 lines
2.5 KiB
Python

import _thread
from machine import Pin
from sensors import RGBLED
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.set_color(RGBLED.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, ())
# --- 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()
print(f"[Main] Received command from WiFi Board: {command}")
# uart_device.send(f"Hello from esp-32 lora ID {DEVICE_ID}")
# 2. Send local metrics over the wire to the WiFi board every few seconds
# uart_device.send("Data Pack: LoRa Link RSSI -72dBm")
time.sleep_ms(200)