Mqtt 将消息发布到本地主机。 telegraf 正在监视 localhost 但无法将其发送到 influxdb。可能出了什么问题?
Mqtt publishing the messages to localhost. And telegraf is monitoring localhost but cannot send it to the influxdb. what might be going wrong?
在这里,
如果 telegraf 到 influxdb 连接成功,因为当我重新启动 telegraf 时,它会创建我在配置文件中提到的数据库。但是 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()`
这是 telegraf 配置和我的 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。
运气不好。
感谢任何帮助。
有两处错误:
- 您没有在 telegraf.conf
中配置 InfluxDB 输出
- 您的体温读数是字符串,不是数字类型
我在这里为您提供了一个使用 MQTT 插件的工作示例:
https://github.com/rawkode/influxdb-examples/tree/master/telegraf/mqtt
PS:Shekhar 交叉post。在这里复制我的答案,以及演示;以防其他人遇到类似问题。
在这里, 如果 telegraf 到 influxdb 连接成功,因为当我重新启动 telegraf 时,它会创建我在配置文件中提到的数据库。但是 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()`
这是 telegraf 配置和我的 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。 运气不好。
感谢任何帮助。
有两处错误:
- 您没有在 telegraf.conf 中配置 InfluxDB 输出
- 您的体温读数是字符串,不是数字类型
我在这里为您提供了一个使用 MQTT 插件的工作示例:
https://github.com/rawkode/influxdb-examples/tree/master/telegraf/mqtt
PS:Shekhar 交叉post。在这里复制我的答案,以及演示;以防其他人遇到类似问题。