Skip to content Skip to sidebar Skip to footer

How To Accumulate Messages As Mqtt Client For 1 Second, Then Save It To A File

my problem is as follows: I wrote a program that subscribes to a topic, where 2 dictionaries with one key respectively arrive more times a second. On every message they change thei

Solution 1:

Instead of manually stopping the networking thread I would prefer using a timer which fires every second. In addition it might be a good idea to lock the data when storing it to a file - otherwise there might occur an update in between:

# ...import threading

def test1_callback(client, userdata, msg):
   msg_dict = json.loads((msg.payload))
   lock.acquire()
   Status.update(msg_dict)
   lock.release()

def timer_event():
   lock.acquire()
   # save to file here
   lock.release()
   # restart timer
   threading.Timer(1, timer_event).start()

Status = {}
lock = threading.Lock()

# client initialization# ...

client.loop_start()
threading.Timer(1, timer_event).start()

while True:
   pass

But this won't prevent your stored value to drift away because the topic is apparently published too frequently so your subscriber (or even the broker) is not able to handle a message fast enough.

So you might want to reduce the interval in which this topic is published. Also notice that you subscribed to a multi-level topic - even if the topics besides "topic/test1" are not handled in your code they still cause load for the broker and the subscribing client

Post a Comment for "How To Accumulate Messages As Mqtt Client For 1 Second, Then Save It To A File"