【问题标题】:Mqtt publishing the messages to localhost. And telegraf is monitoring localhost but cannot send it to the influxdb. what might be going wrong?Mqtt 将消息发布到 localhost。而 telegraf 正在监视 localhost 但无法将其发送到 influxdb。可能出了什么问题?
【发布时间】: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


【解决方案1】:

有两件事错了:

  1. 您没有在 telegraf.conf 中配置 InfluxDB 输出
  2. 您的温度读数是字符串,而不是数字类型

我在这里为您提供了一个使用 MQTT 插件的工作示例:

https://github.com/rawkode/influxdb-examples/tree/master/telegraf/mqtt

PS:Shekhar 的交叉帖子。用演示复制我的答案;以防其他人遇到类似问题。

https://community.influxdata.com/t/mqtt-publishing-the-messages-but-telegraf-cannot-send-it-to-the-influxdb/8462/7

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 2021-08-08
    • 2015-07-28
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多