"""Small CPython MQTT example. Publish a QoS 2 message and then subscribe to the same topic. Set MQTT_BROKER_HOST to the broker IP address, not localhost. """ import os import time from pathlib import Path from shared.mqtt import BrokerClient BROKER_HOST = os.getenv("MQTT_BROKER_HOST", "localhost") TOPIC = "smartwave/demo" USE_TLS = os.getenv("MQTT_CLIENT_TLS", "true").lower() in {"1", "true", "yes", "on"} CA_FILE = os.getenv( "MQTT_CA_FILE", str(Path(__file__).resolve().parents[2] / "orchestrateur" / "mqtt" / "certs" / "ca.crt"), ) def main(): client = BrokerClient( host=BROKER_HOST, client_id="smartwave-cpython-demo", use_tls=USE_TLS, cafile=CA_FILE, keepalive=30, ) client.connect() client.subscribe(TOPIC, qos=2) client.publish(TOPIC, "hello from CPython", qos=2, retain=False) deadline = time.time() + 3 while time.time() < deadline: client.poll(0.1) message = client.get_message() if message is not None: print("received:", message) break client.close() if __name__ == "__main__": main()