Клиент определен на плате ESP следующим образом:< /p>
Код: Выделить всё
WiFiClient espClient;
PubSubClient client(espClient);
Код: Выделить всё
client.setServer(mqtt_server, mqtt_port);
Код: Выделить всё
client.connect("ESP32Client", mqtt_user, mqtt_pass)
Код: Выделить всё
client.loop();
Я публикую, используя эти строки (где jsonBuffer имеет тип char и состоит из сериализованного документа Json) :
Код: Выделить всё
client.publish("device/status", jsonBuffer, true)
Код: Выделить всё
client.publish("sensors/data", jsonBuffer, true)
Из mqtt_exchange с ключом маршрутизации device/status >; Из mqtt_exchange с датчиками/данными ключа маршрутизации
Когда я смотрю на онлайн-графический интерфейс RabbitMQ во время выполнения кода ESP, очередь показывает, что там не публикуются сообщения .
Очередь имеет следующие свойства:
Код: Выделить всё
features: durable:true
expires: 2419200000
max-length: 10000
queue-mode: lazy
queue-version: 2
Код: Выделить всё
import paho.mqtt.client as mqtt
# Define the callback for connection
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
client.subscribe("sensors/data")
client.subscribe("device/status")
else:
print("Connection failed with code ", rc)
# Define the callback for receiving messages
def on_message(client, userdata, message):
payload = message.payload.decode()
print(f"Message received from topic {message.topic}: {payload}")
if message.topic == "sensors/data":
with open("sensor_data.txt", "a") as file:
file.write(f"{payload}\n")
elif message.topic == "device/status":
with open("device_status.txt", "a") as file:
file.write(f"{payload}\n")
# Create an MQTT client instance
client = mqtt.Client()
# Set username and password
client.username_pw_set("example_username", password='example_password')
# Connect to the MQTT broker
client.connect("example_server.com", 1883)
# Attach callback functions
client.on_connect = on_connect
client.on_message = on_message
# Start the network loop
client.loop_forever()
Подробнее здесь: https://stackoverflow.com/questions/787 ... t-can-be-c