Files
Smartwave/micro_ondes/esp_lora/main.py
T
Ninluc 2dd664c4b4
Build, push image, and notify Watchtower / build-image (push) Successful in 1m39s
Build, push image, and notify Watchtower / notify (push) Successful in 1m43s
UART & Sensors
2026-07-23 15:50:43 +02:00

69 lines
2.3 KiB
Python

import _thread
from machine import Pin
from shared import get_lora, get_uart, deviceTypes, config
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)
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)