0

I’m using ESP8266 with Arduino code. Each device posts a ‘state’ and an ‘avail’ messages for a specific topic.

I wish to be able to read those retained messages and not only right after connecting and subscribe to MQTT broker.

EDIT 1 - Clarifications

  1. I'm using Raspberry Pi Zero W as a MQTT Broker ( Local ).
  2. When device boots, it changes its state from offline to online, but a different process checks periodically if state is offline or online for current device and others on the network.
  3. it is not same as when subscribing to a retained topic, that you get the state at that specific time.
  4. Storing chage of state in a variable, is one way to do it, but is there a different way ?
  • Sorry, question edited. – Guy . D Oct 15 at 6:04
  • @jsotola - I'm subscribed for specific topic, but how I actively ( on demand - inside an if statement ), verify that my retained state is online or offline ? – Guy . D Oct 15 at 6:35
  • @Guy.D, when you say: "it is not same as when subscribing to a retained topic, that you get the state at that specific time." what do you mean? If the publisher and the broker were on line all the time and the subscriber just came on line, the retained Node-RED message (topic and payload) would be the last message sent from the publisher. – st2000 Oct 15 at 10:05
1

Once a message has passed through an MQTT broker there is not a way to retrieve it from the broker.

Consider instead deciding upon the action at the time the message is received by the subscriber. If this decision needs to be made at a later time consider storing the state in the of the payload of the message on the subscriber.

Also consider running Node-RED. If the platform running the MQTT broker can support Node-RED (such as a Raspberry Pi), it turns out to be convenient to run both on the same computer. Node-RED contains nodes (graphical programming blocks) which can subscribe to and publish to MQTT topics. By connecting other nodes to these, the topic and payload can be manipulated. Of interest is an option within the Output MQTT node. Here you can select Retain True. This page explains how this option works:

Once you have sent a retained message to a topic, all subscribers will receive that message when they subscribe.

To clear a previously retained topic from the broker, send a blank message to that topic with the retain flag set.

  • This is the only way I had in mind ( storing state change in a specific variable ), but I was hoping there is a better way. – Guy . D Oct 15 at 9:14
  • Please edit your question: 1) Add your comment in which you make it clear why you want to retrieve old MQTT topics. 2) Tell us what you are running your MQTT broker on. 3) What you will do with the on-line / off-line information. As there are additional tools that work well with MQTT that may apply to your situation. – st2000 Oct 15 at 9:17
  • thank you. edited – Guy . D Oct 15 at 9:27
  • @Guy.D, I need to go off line for a while. If you do edit your question to make it better - I am going to assume you are using a normal personal computer for the MQTT broker. That platform likely will support Node-RED a graphical programming language designed to process MQTT topics which uses a web page interface. It will take some time to get used to - so we need to make a decision, based on your edited question, if it is worth the effort to install and learn. – st2000 Oct 15 at 9:32
  • thank you, did you see Q was edited with information asked? – Guy . D Oct 15 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.