Retain messages MQTT?



  • Hi all,

    I have been trying to setup a small MySensors network for a project of mine, where I want to have the MQTT messages kept retained at the MQTT broker(using mosquitto). I know that the broker is capable for that.

    ATM my setup is:
    some Arduino mini pro's (3.3V) with a nRF23L01+ for sending with a temperature sensor.
    those send their temperature data to a raspberry pi with the network gateway, which sends it to my MQTT broker.

    So my question is, is it possible within MySensors to set the retain flag at the sensor or gateway site so that the broker will retain the message? So that when I need the data I ask from the relevant topic I only get the last value.

    Thanks already in advanced πŸ™‚


  • Mod

    Hi @sin_treclay, welcome to the MySensors forum!

    It is currently not possible to configure retention, but if you modify https://github.com/mysensors/MySensors/blob/6ffe29cb5fcfa33fa83133044c2d16ee45c66697/core/MyGatewayTransportMQTTClient.cpp#L113 by changing

    #if defined(MY_MQTT_CLIENT_PUBLISH_RETAIN)
    	bool retain = mGetCommand(message) == C_SET ||
    	              (mGetCommand(message) == C_INTERNAL && message.type == I_BATTERY_LEVEL);
    #else
    	bool retain = false;
    #endif /* End of MY_MQTT_CLIENT_PUBLISH_RETAIN */
    

    to

    	bool retain = true;
    

    all messages should become retained.

    You could also use NodeRed or similar to set the retain flag.

    Could you share any details on when/why retaining messages is useful? Retaining messages has not been requested many times before, so maybe there is a simpler solution.



  • Thanks @mfalkvidd for the welcome ☺.

    It did indeed the job, I somehow created a problem by making it again. (forgot to setup mqtt or something)...
    But it's working now as I thought I wanted it.

    At the moment I am trying to build a system that gets the last message from the sensor into the dashboard. So when the dashboard wants to refresh itself, he asks the latest sensor value from the MQTT broker. He gets the data and displays it then.

    That was in my mind the most simplest solution for the first start.

    So that the sensors can save their batteries and only the broker and gateway are hanging on power. When I need the data I request it. No need atm to save the data and query it from that.

    Now my further thoughts were to use the retain function and a always active subscriber to log all the data getting into it to create statistics.

    But I felt a bit that this is probably not the usual way, and I think atm that maybe I have misunderstood something about the conceptπŸ™„. My first idea was also only to have the sensor values retained. But probably for that I need to change moreπŸ˜…(when that's the only way).
    I also get the fact that when creating a bigger sensor network that storing all the data at the MQTT broker could become a problem πŸ€” . So now with two sensors running there is no problem, there's only a temp or hum value in it. No more so that's also not really I problem I think. But maybe if the MQTT broker has small storage it could be not smart.


  • Mod

    Thanks for explaining @sin_treclay,a dn great that the fix worked.

    Does the dashboard speak mqtt directly? Is it web-based, a mobile app or something else?

    I guess the difference from your setup and most other setups is that most systems have a controller that acts as a backend for the dashboard, stores data permanently and talks to different gateways (mqtt, serial, tcp, etc).


  • Mod

    Usually it is the controller that holds data and statuses, so it should be the target of your queries. Of course you could use the mqtt broker as a cache for reading values



  • @mfalkvidd

    Well I the dashboard speaks to my server application. And the server application makes a request to the MQTT broker.
    Both server application and dashboard are on my pc.

    So it probably should be better to have the server application continually asking the data from the broker, and storing it there and then using that data?

    atm I had no plans for storing the data, so I was thinking to keep the latest values saved at the broker and ask them when needed.

    @gohan
    I see, I'm going to take a look into that, how a controller works.


  • Mod

    Actually if you are subscribed to a mqtt topic there is no need to continually ask for data, the broker will send automatically new data to all the subscribed clients as soon as it is received.


Log in to reply
 

Suggested Topics

  • 4
  • 8
  • 4
  • 9
  • 3
  • 17

20
Online

11.4k
Users

11.1k
Topics

112.7k
Posts