python - Mqtt : Publish the last line from a file that is periodically updated -
i have arduino board sends periodically temperature txt file. file updating every minute(the new data overwritten not appended).
i want make mqtt client reads file periodically , publishes last received data. client publishes no text.
is there way can ?
here have until now:
import mosquitto import time client=mosquitto.mosquitto("read-file") client.connect("broker.mqttdashboard.com", 1883) f=open("/home/ioana/date_sensor","rb") while client.loop()==0: file=f.read() client.publish("ioana/test",file) time.sleep(60)
i have little idea of mqtt needs, think should put code
f=open("/home/ioana/date_sensor","rb")
and
time.sleep(60)
inside while loop, , add file close (near end of while loop)...
import mosquitto import time client=mosquitto.mosquitto("read-file") client.connect("broker.mqttdashboard.com", 1883) while client.loop() == 0: f = open("/home/ioana/date_sensor", "rb") data = f.read() client.publish("ioana/test", data) f.close() time.sleep(60)
i assuming in while loop time, publishes new temperature values every minute or so.
Comments
Post a Comment