Search code examples
pythonraspberry-pimqttsensorsmissing-data

MQTT two way sensor data collection


I'm working in Python. I'm trying to connect a Windows PC with a Raspberry Pi. I have a sensor connected to the Pi, and a small Python script to publish data to the MQTT broker, also located on the Raspberry Pi.

The general logic is for the Window's PC to publish a "send" message to "topic 1". The RasPi listens to "topic 1", and when it see's "send", it gets the latest sensor data, and publishes it to "topic 2". The Window's PC then listens to "topic 2", and grabs the data.

My issue: I'm always one sensor reading behind. I never get the sensor reading attached to when my "send" message.

I am expected to publish a "send", and read the latest data once it has been sent.

Any thoughts?

-Parsko

PS - New poster here on SO, still learning how to ask questions of the community.


Solution

  • There is no need for the Windows machine to send any request messages.

    MQTT is a pub/sub protocol, which is a very different mindset from a request/response protocol (e.g. HTTP)

    The sensors should just publish their values to sensor specific topics and the Windows machine should just subscribe to those topics. This way it will always have the most up to date data.