From 053964e56c6717693877d9be7d6a60c0123be197 Mon Sep 17 00:00:00 2001 From: Matthias Guillitte Date: Fri, 7 Aug 2026 16:14:43 +0200 Subject: [PATCH] UART commands for getting dish temperature --- micro_ondes/esp_lora/lib/microwaveScreen.py | 2 +- micro_ondes/esp_lora/main.py | 46 +++++++++++++++++++-- micro_ondes/esp_wifi/main.py | 12 +++--- shared/payloads.py | 6 +++ shared/uart_comm.py | 4 +- 5 files changed, 60 insertions(+), 10 deletions(-) diff --git a/micro_ondes/esp_lora/lib/microwaveScreen.py b/micro_ondes/esp_lora/lib/microwaveScreen.py index 358c470..2e897c1 100644 --- a/micro_ondes/esp_lora/lib/microwaveScreen.py +++ b/micro_ondes/esp_lora/lib/microwaveScreen.py @@ -100,7 +100,7 @@ class MicrowaveScreen: self.h_centered_text(f"{minutes:02}:{seconds:02}", 40, 1) # Current dish temperature - if cooking_state.current_dish_temp is not None: + if cooking_state.current_dish_temp is not None and cooking_state.current_dish_temp is not 0.0: self.display.blit(fb_image_weather_temperature_bits, 27, 2) self.display.text(str(int(cooking_state.current_dish_temp)), 37, 6, 1) self.display.ellipse(54, 6, 1, 1, 1, False) diff --git a/micro_ondes/esp_lora/main.py b/micro_ondes/esp_lora/main.py index ea5e9f3..ff265cc 100644 --- a/micro_ondes/esp_lora/main.py +++ b/micro_ondes/esp_lora/main.py @@ -6,6 +6,7 @@ from lib.microwaveScreen import MicrowaveScreen import uasyncio as asyncio from machine import Pin, SoftI2C import ssd1306 +import ujson # Clean memory immediately gc.collect() @@ -32,6 +33,9 @@ uart_device = None magnetron_led = None microwave_screen = None defrost_mode = False +current_temp = [None, None] +last_temp = [None, None] +temperature_asked = False PING_PAYLOAD = { @@ -100,7 +104,36 @@ def lora_hardware_thread(): # --- COOKING STATE CALLBACKS --- def cooking_state_temperature_provider(): - return 22.0, 29.0 # TODO: Replace with real temperature reading + global current_temp, last_temp, temperature_asked, uart_device + + def temp_is_none(temp): + return temp is None or temp[0] is None or temp[1] is None + + # Return the current temperature if available, + # otherwise return the last recorded temperature + # and request a new reading from the WiFi board. + if temp_is_none(current_temp): + if temp_is_none(last_temp): + temps = (0.0, 0.0) + print("[CookingState] No temperature data available. Returning default (0.0, 0.0).") + else: + temps = [last_temp[0], last_temp[1]] + print(f"[CookingState] Returning last known temperature: {temps}") + + # Asks the esp-wifi the temperature over UART + if not temperature_asked: + temperature_asked = True + uart_device.send_as_command(UARTCommand(UARTCommandType.TEMPERATURE_REQUEST, {})) + print("[CookingState] Requested new temperature reading from WiFi board.") + else: + print(f"[CookingState] Returning current temperature: {current_temp}") + temps = [current_temp[0], current_temp[1]] + last_temp[0] = current_temp[0] + last_temp[1] = current_temp[1] + current_temp[0] = None + current_temp[1] = None + + return temps def cooking_state_on_state_change(state): print(f"[CookingState] State changed to: {state.state}") @@ -134,7 +167,7 @@ def cooking_state_on_pause(state): async def uart_polling_task(): """Polls UART for incoming messages from the WiFi board.""" - global cooking_state + global cooking_state, temperature_asked while True: if uart_device and uart_device.any(): @@ -145,6 +178,8 @@ async def uart_polling_task(): params = command.payload print(f"[UART Task] Cooking parameters received: {params}") + uart_device.send_as_command(UARTCommand(UARTCommandType.TEMPERATURE_REQUEST, {})) + cooking_state = cookingState.CookingState( cook_time=params["cook_time"], power_level=params["power_level"], @@ -155,8 +190,13 @@ async def uart_polling_task(): cooking_state.set_refresh_callback(cooking_state_on_refresh) cooking_state.set_pause_callback(cooking_state_on_pause) - await asyncio.sleep_ms(20) + await asyncio.sleep_ms(200) cooking_state_on_state_change(cooking_state) + elif command.command_type == UARTCommandType.TEMPERATURE_RESPONSE: + payload = ujson.loads(command.payload) + current_temp[0] = payload["dish_temp"] + current_temp[1] = payload["ambient_temp"] + temperature_asked = False else: print(f"[UART Task] Unknown command type received: {command.command_type}") diff --git a/micro_ondes/esp_wifi/main.py b/micro_ondes/esp_wifi/main.py index c202d42..50abf83 100644 --- a/micro_ondes/esp_wifi/main.py +++ b/micro_ondes/esp_wifi/main.py @@ -195,14 +195,16 @@ async def uart_task(): try: cmd = uart_device.read_as_command() - if cmd: + if cmd and hasattr(cmd, "command_type"): print("[UART] Command received from LoRa board:", cmd.command_type, cmd.payload) - if ( - hasattr(cmd, "command_type") - and cmd.command_type == UARTCommandType.COOKING_STATE_UPDATE - ): + if cmd.command_type == UARTCommandType.COOKING_STATE_UPDATE: if cooking_state: cooking_state.set_state(cmd.payload.get("state")) + elif cmd.command_type == UARTCommandType.TEMPERATURE_REQUEST: + uart_device.send_as_command(UARTCommand(UARTCommandType.TEMPERATURE_RESPONSE, payloads.lora_sensor_data( + mlx_temperature_sensor.read_object_temp() if mlx_temperature_sensor else 0, + mlx_temperature_sensor.read_ambient_temp() if mlx_temperature_sensor else 0 + ))) except Exception as e: print("[UART Task] Error reading command:", e) diff --git a/shared/payloads.py b/shared/payloads.py index 6460673..923a893 100644 --- a/shared/payloads.py +++ b/shared/payloads.py @@ -39,6 +39,12 @@ def mqtt_sensor_data(id_microwave, dish_temp, ambient_temp): "ambient_temp": ambient_temp }) +def lora_sensor_data(dish_temp, ambient_temp): + return as_json({ + "dish_temp": dish_temp, + "ambient_temp": ambient_temp + }) + def mqtt_cooking_config(id_microwave, cook_time, power_level, target_temp): return as_json({ "id_microwave": id_microwave, diff --git a/shared/uart_comm.py b/shared/uart_comm.py index 17a609c..f0f3271 100644 --- a/shared/uart_comm.py +++ b/shared/uart_comm.py @@ -97,4 +97,6 @@ class UARTCommand: class UARTCommandType: """Enumeration of known UART command types.""" COOKING_PARAMS = "COOKING_PARAMS" - COOKING_STATE_UPDATE = "COOKING_STATE_UPDATE" \ No newline at end of file + COOKING_STATE_UPDATE = "COOKING_STATE_UPDATE" + TEMPERATURE_REQUEST = "TEMPERATURE_REQUEST" + TEMPERATURE_RESPONSE = "TEMPERATURE_RESPONSE" \ No newline at end of file