mqttiotinfluxdbtelegraf

Config Telegraf to influxdb to bind mqtt data


I have an esp32 and I have install mosquitto broker on a local pc that I expose public. the broker sends numeric data in string format like this

C:\Program Files\mosquitto>mosquitto_sub -h localhost -t Omiros/Home/temperature
28.10
28.00

In this pc also running onfluxdb and Telegraf. The Telegraf config is:

  # Configuration for telegraf agent
  [agent]
  ## Default data collection interval for all inputs
  interval = "10s" 
  round_interval = true
  metric_batch_size = 1000  
  metric_buffer_limit = 10000
   collection_jitter = "0s"
  flush_interval = "10s"  
  flush_jitter = "0s"
  precision = ""  
  hostname = ""
  omit_hostname = false
[[outputs.influxdb_v2]]  
  urls = ["http://localhost:8086"]
  token = "$INFLUX_TOKEN"
  organization = "PESREAS IoT"
  bucket = "telegraf"
 
[[inputs.system]]
 
[[inputs.mqtt_consumer]]
 
  servers = ["tcp://0.tcp.eu.ngrok.io:18142"]

  ## Topics that will be subscribed to.
  topics = [
 "Omiros/Home/temperature",
 "Omiros/Home/humidity",
 "Omiros/Home/air",
 "Omiros/Home/airToluene"  

  ]
 data_format = "influx"
  data_type = "float"
  
  username = "perseas"
  password = "RESIDENT"

When I run the telegraf agent I get :

PS C:\Program Files\InfluxData\telegraf> ./telegraf --config http://localhost:8086/api/v2/telegrafs/0ce15c9240aec000
2024-04-14T19:26:00Z I! Loading config: http://localhost:8086/api/v2/telegrafs/0ce15c9240aec000
2024-04-14T19:26:00Z I! Starting Telegraf 1.30.1 brought to you by InfluxData the makers of InfluxDB
2024-04-14T19:26:00Z I! Available plugins: 233 inputs, 9 aggregators, 31 processors, 24 parsers, 60 outputs, 5 secret-stores
2024-04-14T19:26:00Z I! Loaded inputs: mqtt_consumer system
2024-04-14T19:26:00Z I! Loaded aggregators:
2024-04-14T19:26:00Z I! Loaded processors:
2024-04-14T19:26:00Z I! Loaded secretstores:
2024-04-14T19:26:00Z I! Loaded outputs: influxdb_v2
2024-04-14T19:26:00Z I! Tags enabled: host=DESKTOP-V9OL7EV
2024-04-14T19:26:00Z I! [agent] Config: Interval:10s, Quiet:false, Hostname:"DESKTOP-V9OL7EV", Flush Interval:10s
2024-04-14T19:26:01Z I! [inputs.mqtt_consumer] Connected [tcp://0.tcp.eu.ngrok.io:18142]
2024-04-14T19:26:11Z E! [inputs.mqtt_consumer] Error in plugin: metric parse error: expected tag at 1:6: "29.60"
2024-04-14T19:26:11Z E! [inputs.mqtt_consumer] Error in plugin: metric parse error: expected tag at 1:6: "31.00"
2024-04-14T19:26:11Z E! [inputs.mqtt_consumer] Error in plugin: metric parse error: expected tag at 1:5: "1.79"
2024-04-14T19:26:11Z E! [inputs.mqtt_consumer] Error in plugin: metric parse error: expected tag at 1:5: "0.31"
2024-04-14T19:26:31Z E! [inputs.mqtt_consumer] Error in plugin: metric parse error: expected tag at 1:6: "29.50"

And In the data explorer I get the error : unsupported input type for mean aggregate: string enter image description here

How to config Telegraf to send data correctly to influxdb? Maybe I have to convert string to float? How to do this in the config Telegraf file?


Solution

  • After many days of search, finally this code works. enter image description here