OpenCV(C++年)到MQTT代理

OpenCV (in C++) to MQTT broker

本文关键字:MQTT 代理 C++ OpenCV      更新时间:2023-10-16

我的目标是从OpenCV(C++)发布(x,y)坐标的JSON流到MQTT代理。我对从OpenCV将JSON数据传输到MQTT的好方法感到困惑。

我目前有一个 MQTT 代理,它管理计算机 1 上的 Python 发布者脚本和计算机 2 上的 Javascript 客户端脚本之间的 JSON 消息队列。我有运行OpenCV的计算机3,需要近乎实时地向MQTT代理发送许多(x,y)坐标的小JSON斑点。

我最初考虑将坐标从OpenCV连续写入本地文本文件,然后由并行运行的Python MQTT发布者脚本连续读取。但是,文件锁定存在问题,似乎不需要写入磁盘。

理想情况下,我希望坐标流保留在内存中并经常发布到 MQTT 代理,例如每 10 毫秒发布一次。我正在努力寻找一种方法来使用stdin/stdout在OpenCV代码和Python发布者脚本之间工作。

感谢您的指导。谢谢。

不确定你缺少哪些部分,但你基本上想要:

#include "mqtt.h"
...
// Start up
mqtt_broker_handle_t *broker = mqtt_connect(client_name, ip_addr, port);
...
...
// Update broker
mqtt_publish(broker, topic, msg, QoS1);

你提到了一个发布者的python脚本。

这是发布者:

#!/usr/bin/env python3
import paho.mqtt.client as mqtt
# This is the Publisher
client = mqtt.Client()
client.connect("localhost",1883,60)
client.publish("topic/test", "Hello world!", qos=0, retain=False)
client.disconnect()

这是一个订阅者: #!/usr/bin/env python3 import paho.mqtt.client as mqtt

# This is the Subscriber
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("topic/test")
def on_message(client, userdata, msg):
if msg.payload.decode() == "Hello world!":
print("Yes!")
client.disconnect()
client = mqtt.Client()
client.connect("localhost",1883,60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()