Esp32 Subscribes to topic on boot and recieves an old unwanted message

Having an issue with node-red (im pretty sure), if you could help me that would be most aprechiated because its doing my head in.

So, esp32 has values stored in it, default value (value of 10), it subcribes to a topic, i can change the value from the dashboard (value of 5), the value is sent to the device and saved on a local SD card, ok all good so far, i send another value to the esp32 (value of 2), it stores it and saves it to the card, as expected. A serial monitor shows what the esp32 has recieved and done with the information, all done as expected.

I then reboot the esp32, outputs the stored data to the serial monitor on boot(for debugging), last sent value is present and correct (value of 2). The esp32 then subcribes again (due to sudden reboot). I then recieve a message from node-red, the first value i ever sent to it (value of 5)......the esp32 then stores that message as it was programed to do.

The issue is i cannot get node-red to stop sending the first (5 in this example) retained value (or any for that matter) when the device subscribes.

Using node-red 2.1.1 and its built in mqtt on a raspberry pi4. I must also note that the esp32 sends/recieves on different topics, if they were the same it just gets stuck in a odd loop, continously sending information back and forth as fast as it can, when it shouldn't (well not that i could see why).

All i really want it to do is send a new number to the esp32 to store on the card, then the esp32 sends back the stored value as a confirmation and displays it on the dashboard. On first boot i will eventually want to send all the values stored on the SD card to node-red.

below is the esp32 serial monitor output after a reboot.

Config.txt:              
baltarget = 2                              //this is what was sent previously & stored
Wifi.txt:
ssid = *************
password = ***************

Connecting to *************
.........
WiFi connected - ESP IP address: 192.168.100.11            //connected ok
subscribed to all                                                              
recieved message on topic BMSM/Rbaltarget  //imediatley recieves message
baltarget changed to = 5                             //old unwanted value
Done writing to Config.txt                      //arduino as expected stores value

And my node-red configuration:

[{"id":"bb719087.aa90d8","type":"mqtt in","z":"51c8672.3c7f018","name":"","topic":"BMSM/RbaltargetIN","qos":"0","datatype":"auto","broker":"c531823d.2c0e98","nl":false,"rap":false,"inputs":0,"x":170,"y":2860,"wires":[["2fb69685.2c777a"]]},{"id":"2fb69685.2c777a","type":"ui_text","z":"51c8672.3c7f018","group":"ad172d99.661848","order":3,"width":2,"height":1,"name":"output bal target","label":"","format":"{{msg.payload}}","layout":"row-spread","x":360,"y":2860,"wires":[]},{"id":"7b454bb3.fb6d14","type":"ui_text_input","z":"51c8672.3c7f018","name":"Bal Target input number","label":"","tooltip":"Balance Target (mV) (0-50mV)","group":"ad172d99.661848","order":2,"width":4,"height":1,"passthru":true,"mode":"number","delay":"0","topic":"","x":390,"y":2900,"wires":[["712af693.011ae8"]]},{"id":"bc513f9c.f57af8","type":"ui_text","z":"51c8672.3c7f018","group":"ad172d99.661848","order":1,"width":0,"height":0,"name":"Bal Target (mV) Text","label":"Bal Target (mV)","format":"","layout":"col-center","x":570,"y":2860,"wires":[]},{"id":"712af693.011ae8","type":"range","z":"51c8672.3c7f018","minin":"1","maxin":"50","minout":"1","maxout":"50","action":"clamp","round":false,"property":"payload","name":"","x":570,"y":2900,"wires":[["98b188c.60d5e78"]]},{"id":"98b188c.60d5e78","type":"mqtt out","z":"51c8672.3c7f018","name":"","topic":"BMSM/Rbaltarget","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"c531823d.2c0e98","x":790,"y":2880,"wires":[]},{"id":"c531823d.2c0e98","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"120","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"sessionExpiry":"0"},{"id":"ad172d99.661848","type":"ui_group","name":"Configuration","tab":"d91a31ea.18f87","order":4,"disp":true,"width":"6","collapse":true},{"id":"d91a31ea.18f87","type":"ui_tab","name":"BMS","icon":"battery_charging_50","order":6,"disabled":false,"hidden":false}]

I really cant see why it would be sending an old value.
I hope someone can make sense of this as changing the nodes configurations (like retain flag) dont seem to do anything.

It has to due with the retension policy of that first message. Check your mqtt-out node and see what you have for the retain option`.

I suggest you get and install MQTT Explorer so you can see (and delete if you want) the msgs that are stored.

If you haven't done so yet, and depending on your MQTT knowledge, I suggest you take the time to read thru HiveMQ's 10 part 'MQTT Essentials` series. Well worth the time.

2 Likes

Thanks for the reply.
Ive managed to sort it out using the mqtt explorer you mentioned. Not the easiest to figure out how to install and use as its not available on the raspberry pi, and very little install instuctions. (im'm not a massive fan of Github)
However, once logged in (using my windows pc on the same network) i found a retained message, stuck on the system, deleted the message, rebooted the esp32, serial monitor shows saved settings, subscribed and no aditional message!
Not really sure why it would be stuck on the system like that, and its not an easy task to get rid of it as simply recieving the message doesnt seem to get rid of it, it just stays there stuck.

Anyway,
Thanks again for your help, i will also take a look at the site you suggested, and at the very least it might help me understand the system a bit more.
Im not the best with learning languages as i'm comfortable using with C++ after years of use (compitent is the word), that and integrating with a raspberry pi with python and linking it all together into node-red without learning the language is hard if not impossible for me to understand sometimes.

depending on which broker you use there are often ways to remove a retained message. For example if using Mosquitto you should be able to send an empty packet (or string "" ) with retained flag set to the required topic.

1 Like

I did read about that somewhere, tried it (empty string ""), got sent back another message.

You have to send it with retain flag set true

I think publishing an empty string with Retained will keep it retained as an empty string. To remove you can do
mosquitto_pub -t the/topic -r -n
which effectively publishes a null message rather than an empty string.

hmm - last time I looked mqtt didn't know about types so "" becomes a zero byte length message - and doesn't get passed on. So mosquitto_pub -t fthe_topic -r -m "" works just as well.

When will I learn, before posting here, to test any suggestions I have that The All Knowing One (or The Other All Knowing One) may be incorrect? It turns out I have been labouring under a misapprehension all these years.

The method I suggested does save wear on the keyboard and finger though :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.