【发布时间】:2019-02-07 11:40:06
【问题描述】:
这里, 如果电报到 influxdb 连接成功,因为当我重新启动电报时,它会创建我在配置文件中提到的数据库。但是mqtt发布的消息没有被telegraf接收到我什至试图将它放入文件但它是空的。 所以出了点问题。
import paho.mqtt.client as mqtt
from influxdb import InfluxDBClient
import json
influxclient = InfluxDBClient(host='localhost', port=8086)
# This is the Publisher
dict_msg={"temperature":"20.5"}
msg = json.dumps(dict_msg)
MQTT_HOST = "127.0.0.1"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "sensors"
count = 0
# Define on_publish event function
def on_publish(client, userdata, mid):
print("Message Published..")
# Initiate MQTT Client
mqttc = mqtt.Client()
mqttc.on_publish = on_publish
# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
x = 0
while x <= 100000:
# Publish message to MQTT Broker
mqttc.publish(MQTT_TOPIC,msg)
# influx_line_protocol = ("published,counts" = count)
# print(count)
x += 1
mqttc.loop(30)
# Disconnect from MQTT_Broker
mqttc.disconnect()`
这是给出的电报配置和我的 mqtt 发布者代码。 我希望将我发布的消息存储在 influxdb 中。
telegraf 说如下所示已连接,但它没有从发布者发送任何消息。 冻结在这里
2019-02-07T11:02:18Z I! [agent] Config: Interval:10s, Quiet:false, Hostname:"shekhar-Inspiron-3441", Flush Interval:10s
2019-02-07T11:02:18Z I! [inputs.mqtt_consumer] Connected [tcp://127.0.0.1:1883]
mqtt 配置有
[[inputs.mqtt_consumer]]
## MQTT broker URLs to be used. The format should be
scheme://host:port,
## schema can be tcp, ssl, or ws.
servers = ["tcp://127.0.0.1:1883"]
## Topics to subscribe to
topics = [
"telegraf/sensors/#",
]
data_format = "influx"
也尝试使用 json。 不走运。
任何帮助表示赞赏。
【问题讨论】:
-
您正在发布主题
sensors并订阅telegraph/sensors/# -
必须对传感器进行订阅/#只有这样???
-
要匹配您发布的内容,只需订阅
sensors即可
标签: python-3.x mqtt influxdb telegraf