简体   繁体   中英

Writing subscribed messages from MQTT to InfluxDB at the same timing

I am a beginner at influxdb, python, and using mqtt. I've succeeded to subscribe on multiple topics and send it on influxdb locally. But it appears to have approximately 0.1 second delay between those messages on the influxDB timestamps. I've tried to add "time" variable at the JSON body, it succeeded to eliminate the delay, but somehow I cannot graph it on Chronograf nor Grafana, showing "Your query or command is syntactically correct but returned no results". I also tried to use thread function to call the on_message_voltage and on_message_current at the same timing, but it didn't work. So is there any chance I'm able to send subscribed messages from MQTT to influxDB at the same timing?

from paho.mqtt import client as mqtt
from influxdb import InfluxDBClient
import datetime
import threading
import time

def on_connect(client, userdata, flags, rc):
    if rc==0:
        print("Connected to MQTT my-org.org.id")
        print("Starting to receive message")
    client.subscribe("IoT_Sensor/1",qos=1)
    client.subscribe("IoT_Sensor/2",qos=2)

def on_message_voltage(client, userdata, message):  
    current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    json_body= [
    {
        "measurement": "Solar",
            "tags": {
                "host": "Sensor_1",
                            },
                    # "time": int(time.time()),
                            "fields": {
                                "Voltage": float(message.payload),
        }
    }
    ]
    influx_client.write_points(json_body)
    print("Voltage: " + message.topic + " " + str(message.qos) + " " + str(message.payload.decode('utf-8')))
    
def on_message_current(client, userdata, message):
    current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    json_body = [
    {
        "measurement": "Solar",
            "tags": {
                "host": "Sensor_1",
                    },
                  # "time": int(time.time()),
                        "fields": {
                            "Current": float(message.payload),
     }
        
    }
    ]
    
    influx_client.write_points(json_body)
    print("Current: " + message.topic + " " + str(message.qos) + " " + str(message.payload.decode('utf-8')))

def on_message(client, userdata, message):
    print(message.topic + " " + str(message.qos) + " " + str(message.payload))

mqttc = mqtt.Client()
influx_client = InfluxDBClient('localhost', 8086, database='mqtt_test2')

def on_topic_1():
    mqttc.message_callback_add("IoT_Sensor/1", on_message_voltage)
def on_topic_2():
    mqttc.message_callback_add("IoT_Sensor/2", on_message_current)
    
try :
    threading.Thread(target=on_topic_1).start()
    threading.Thread(target=on_topic_2).start()
    mqttc.on_connect = on_connect
    mqttc.on_message = on_message
    mqttc.connect("my-org.or.id",1883,60)
    mqttc.loop_forever()
    
except KeyboardInterrupt:
    print("Exiting loop")

Your threading code is meaningless, just starting a new thread to run the message_callback_add() won't do anything useful. It just starts a thread which will add the callback then exit immediately.

You need to remove the following lines:

def on_topic_1():
    mqttc.message_callback_add("IoT_Sensor/1", on_message_voltage)
def on_topic_2():
    mqttc.message_callback_add("IoT_Sensor/2", on_message_current)

...

threading.Thread(target=on_topic_1).start()
threading.Thread(target=on_topic_2).start()

If you want to run the separate callbacks for different topics you should just add the mqttc.message_callback_add() calls to before the mqttc.connect() call.

As to why it's adding latency without a lot more information about what the whole system looks like also exactly how are you measuring the latency, have you measured how long each of the message callbacks takes to run?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM