Reputation: 29
Here, if telegraf to influxdb connection is successful because when i restart the telegraf it creates the db which i mentioned in the config file. But the messages which are publihsed by mqtt are not recieved by the telegraf I even tried to put it into file but it’s empty. so something is wrong.
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()`
Here's the telegraf config and my mqtt publisher code which is given. I want my published messages to be stored in influxdb.
telegraf says connected as given below but its not sending any messages from publisher. freezes here
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]
and mqtt config has
[[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"
tried with json also. no luck.
any help appreciated.
Upvotes: 0
Views: 3873
Reputation: 22592
There were 2 things wrong:
I’ve provided a working example of using the MQTT plugin for you here:
https://github.com/rawkode/influxdb-examples/tree/master/telegraf/mqtt
PS: Cross-post by Shekhar. Copying my answer here, with the demo; in-case someone else runs into a similiar problem.
Upvotes: 2