Init
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
# MQTT Examples
|
||||
|
||||
Small examples for the shared MQTT wrapper.
|
||||
|
||||
The examples use an explicit broker IP address. Replace `192.168.1.50` with the
|
||||
real address of the machine running the broker.
|
||||
|
||||
TLS is enabled by default in the broker stack. For Wireshark testing, start the
|
||||
broker with `MQTT_TLS_ENABLED=false` and use the plain example path.
|
||||
|
||||
|
||||
# On asus
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,48 @@
|
||||
"""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()
|
||||
@@ -0,0 +1,39 @@
|
||||
"""Small MicroPython MQTT example for ESP32.
|
||||
|
||||
Edit BROKER_HOST so it points to the broker machine IP address.
|
||||
Do not use localhost from the ESP32.
|
||||
"""
|
||||
|
||||
from shared.mqtt import BrokerClient
|
||||
|
||||
|
||||
BROKER_HOST = "192.168.50.1"
|
||||
TOPIC = b"smartwave/demo"
|
||||
CA_FILE = "/certs/ca.crt"
|
||||
|
||||
|
||||
def on_message(message):
|
||||
print("received:", message)
|
||||
|
||||
|
||||
def main():
|
||||
client = BrokerClient(
|
||||
host=BROKER_HOST,
|
||||
client_id="smartwave-esp32-demo",
|
||||
use_tls=True,
|
||||
cafile=CA_FILE,
|
||||
keepalive=30,
|
||||
)
|
||||
|
||||
client.set_callback(on_message)
|
||||
client.connect()
|
||||
client.subscribe(TOPIC, qos=2)
|
||||
client.publish(TOPIC, b"hello from MicroPython", qos=2, retain=False)
|
||||
|
||||
for _ in range(30):
|
||||
client.poll()
|
||||
|
||||
client.close()
|
||||
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user