UART & Sensors
Build, push image, and notify Watchtower / build-image (push) Successful in 1m39s
Build, push image, and notify Watchtower / notify (push) Successful in 1m43s

This commit is contained in:
2026-07-23 15:50:43 +02:00
parent 1ec3ee7abf
commit 2dd664c4b4
25 changed files with 2333 additions and 71 deletions
+1
View File
@@ -3,6 +3,7 @@
import shared.deviceTypes as deviceTypes
import shared.config as config
import shared.payloads as payloads
def get_lora(*args, **kwargs):
from .lora_device import get_lora_device
+6 -3
View File
@@ -1,12 +1,15 @@
DEBUG=True
# LoRa
HEARTBEAT_INTERVAL = 10
HEARTBEAT_INTERVAL = 30
# MQTT
MQTT_BROKER_HOST = "192.168.50.1"
MQTT_TOPIC_HELLO = b"smartwave/hello"
MQTT_TOPIC_SENSOR = b"smartwave/sensor"
MQTT_TOPIC_COOKING = b"smartwave/cooking"
MQTT_KEEPALIVE = 30
USE_TLS = True
MQTT_QOS = 1
MQTT_QOS = 1
# Long because messages are stored into the broker and will be sent when the orchestrator is back online.
MQTT_HELLO_INTERVAL = 30
+6
View File
@@ -0,0 +1,6 @@
from shared.config import DEBUG
def log(message):
"""Log a message to the console if DEBUG is enabled."""
if DEBUG:
print(f"\n{message}")
+39 -7
View File
@@ -13,13 +13,13 @@ except ImportError:
from umqtt.simple import MQTTClient as _MQTTClient
BACKEND_NAME = "umqtt.simple"
IS_MICROPYTHON = True
except ImportError:
try:
from umqtt.robust import MQTTClient as _MQTTClient
BACKEND_NAME = "umqtt.robust"
IS_MICROPYTHON = True
except ImportError as exc:
raise ImportError("No MQTT client found. Expected paho.mqtt or umqtt.") from exc
# except ImportError:
# try:
# from umqtt.robust import MQTTClient as _MQTTClient
# BACKEND_NAME = "umqtt.robust"
# IS_MICROPYTHON = True
except ImportError as exc:
raise ImportError("No MQTT client found. Expected paho.mqtt or umqtt.") from exc
DEFAULT_PORT = 8884
@@ -179,6 +179,38 @@ class BrokerClient:
topic = topic.decode('utf-8')
return client.subscribe(topic, qos=qos)
def unsubscribe(self, topic):
client = self.open()
if IS_MICROPYTHON:
import struct
# Ensure the topic is bytes for writing to the socket
topic_bytes = topic if isinstance(topic, bytes) else topic.encode('utf-8')
# 1. Build the MQTT unsubscribe packet header
pkt = bytearray(b"\xa2\0\0\0")
client.pid += 1
# Packet length is: 2 bytes (PID) + 2 bytes (topic length indicator) + topic string length
struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic_bytes), client.pid)
# 2. Write the packet to the socket
client.sock.write(pkt)
client._send_str(topic_bytes)
# 3. Wait for the UNSUBACK confirmation frame (0xB0) from the broker
while True:
op = client.wait_msg()
if op == 0xB0:
resp = client.sock.read(3)
assert resp[1] == pkt[2] and resp[2] == pkt[3]
return client
return client
if isinstance(topic, bytes):
topic = topic.decode('utf-8')
return client.unsubscribe(topic)
def _on_micropython_message(self, topic, payload):
self._store_message(topic, payload, None, False)
+23
View File
@@ -0,0 +1,23 @@
try:
import ujson as json
except ImportError:
import json
def as_json(data):
"""Convert a dictionary to a JSON string."""
try:
return json.dumps(data)
except Exception as e:
print("[Payloads] Error converting to JSON:", e)
return "{}" # Return an empty JSON object on error
def mqtt_hello(id_microwave):
return as_json({
"id_microwave": id_microwave
})
def mqtt_hello_ack(id_orchestrator, id_microwave):
return as_json({
"id_microwave": id_microwave,
"id_orchestrator": id_orchestrator
})