UART commands for getting dish temperature
This commit is contained in:
@@ -100,7 +100,7 @@ class MicrowaveScreen:
|
|||||||
self.h_centered_text(f"{minutes:02}:{seconds:02}", 40, 1)
|
self.h_centered_text(f"{minutes:02}:{seconds:02}", 40, 1)
|
||||||
|
|
||||||
# Current dish temperature
|
# 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.blit(fb_image_weather_temperature_bits, 27, 2)
|
||||||
self.display.text(str(int(cooking_state.current_dish_temp)), 37, 6, 1)
|
self.display.text(str(int(cooking_state.current_dish_temp)), 37, 6, 1)
|
||||||
self.display.ellipse(54, 6, 1, 1, 1, False)
|
self.display.ellipse(54, 6, 1, 1, 1, False)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from lib.microwaveScreen import MicrowaveScreen
|
|||||||
import uasyncio as asyncio
|
import uasyncio as asyncio
|
||||||
from machine import Pin, SoftI2C
|
from machine import Pin, SoftI2C
|
||||||
import ssd1306
|
import ssd1306
|
||||||
|
import ujson
|
||||||
|
|
||||||
# Clean memory immediately
|
# Clean memory immediately
|
||||||
gc.collect()
|
gc.collect()
|
||||||
@@ -32,6 +33,9 @@ uart_device = None
|
|||||||
magnetron_led = None
|
magnetron_led = None
|
||||||
microwave_screen = None
|
microwave_screen = None
|
||||||
defrost_mode = False
|
defrost_mode = False
|
||||||
|
current_temp = [None, None]
|
||||||
|
last_temp = [None, None]
|
||||||
|
temperature_asked = False
|
||||||
|
|
||||||
|
|
||||||
PING_PAYLOAD = {
|
PING_PAYLOAD = {
|
||||||
@@ -100,7 +104,36 @@ def lora_hardware_thread():
|
|||||||
|
|
||||||
# --- COOKING STATE CALLBACKS ---
|
# --- COOKING STATE CALLBACKS ---
|
||||||
def cooking_state_temperature_provider():
|
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):
|
def cooking_state_on_state_change(state):
|
||||||
print(f"[CookingState] State changed to: {state.state}")
|
print(f"[CookingState] State changed to: {state.state}")
|
||||||
@@ -134,7 +167,7 @@ def cooking_state_on_pause(state):
|
|||||||
|
|
||||||
async def uart_polling_task():
|
async def uart_polling_task():
|
||||||
"""Polls UART for incoming messages from the WiFi board."""
|
"""Polls UART for incoming messages from the WiFi board."""
|
||||||
global cooking_state
|
global cooking_state, temperature_asked
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if uart_device and uart_device.any():
|
if uart_device and uart_device.any():
|
||||||
@@ -145,6 +178,8 @@ async def uart_polling_task():
|
|||||||
params = command.payload
|
params = command.payload
|
||||||
print(f"[UART Task] Cooking parameters received: {params}")
|
print(f"[UART Task] Cooking parameters received: {params}")
|
||||||
|
|
||||||
|
uart_device.send_as_command(UARTCommand(UARTCommandType.TEMPERATURE_REQUEST, {}))
|
||||||
|
|
||||||
cooking_state = cookingState.CookingState(
|
cooking_state = cookingState.CookingState(
|
||||||
cook_time=params["cook_time"],
|
cook_time=params["cook_time"],
|
||||||
power_level=params["power_level"],
|
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_refresh_callback(cooking_state_on_refresh)
|
||||||
cooking_state.set_pause_callback(cooking_state_on_pause)
|
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)
|
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:
|
else:
|
||||||
print(f"[UART Task] Unknown command type received: {command.command_type}")
|
print(f"[UART Task] Unknown command type received: {command.command_type}")
|
||||||
|
|
||||||
|
|||||||
@@ -195,14 +195,16 @@ async def uart_task():
|
|||||||
try:
|
try:
|
||||||
cmd = uart_device.read_as_command()
|
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)
|
print("[UART] Command received from LoRa board:", cmd.command_type, cmd.payload)
|
||||||
if (
|
if cmd.command_type == UARTCommandType.COOKING_STATE_UPDATE:
|
||||||
hasattr(cmd, "command_type")
|
|
||||||
and cmd.command_type == UARTCommandType.COOKING_STATE_UPDATE
|
|
||||||
):
|
|
||||||
if cooking_state:
|
if cooking_state:
|
||||||
cooking_state.set_state(cmd.payload.get("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:
|
except Exception as e:
|
||||||
print("[UART Task] Error reading command:", e)
|
print("[UART Task] Error reading command:", e)
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,12 @@ def mqtt_sensor_data(id_microwave, dish_temp, ambient_temp):
|
|||||||
"ambient_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):
|
def mqtt_cooking_config(id_microwave, cook_time, power_level, target_temp):
|
||||||
return as_json({
|
return as_json({
|
||||||
"id_microwave": id_microwave,
|
"id_microwave": id_microwave,
|
||||||
|
|||||||
+3
-1
@@ -97,4 +97,6 @@ class UARTCommand:
|
|||||||
class UARTCommandType:
|
class UARTCommandType:
|
||||||
"""Enumeration of known UART command types."""
|
"""Enumeration of known UART command types."""
|
||||||
COOKING_PARAMS = "COOKING_PARAMS"
|
COOKING_PARAMS = "COOKING_PARAMS"
|
||||||
COOKING_STATE_UPDATE = "COOKING_STATE_UPDATE"
|
COOKING_STATE_UPDATE = "COOKING_STATE_UPDATE"
|
||||||
|
TEMPERATURE_REQUEST = "TEMPERATURE_REQUEST"
|
||||||
|
TEMPERATURE_RESPONSE = "TEMPERATURE_RESPONSE"
|
||||||
Reference in New Issue
Block a user