I have spent many more hours om my own trying to find what I did wrong.But when this is sent out via MQTT, I receive in Node-RED one single object, so I cannot separate the value ?
The value I receive is {"22 - Current temperature =":21.3}
what do you mean? Since it is an object, you can simply access the value using msg.payload.name_of_property
However, since you have chosen a god awful property name and awful topic structure, you are just making things difficult for yourself!
Recommendation:
structure your topics like module/99/info/blahblah
name properties in a way that doesnt require square bracket notation to access them. e.g. current_temperature instead of 22 - Current temperature =
You will eventually realise the benefit of a good topic structure but until that moment of realisation, please take on board what we have suggested here and previously:
I must admit I have been looking at this until late last night and I think I have confused myself !
Sometimes it takes someone to say that my JSON object is at least correct ( yes my format Topic is terrible ) but this is from when it was just a string sent out by my Arduino, so it didn't matter too much.
So I am now trying to sort out my Arduino code which was written about 4 years ago, before I had even heard of JSON.
Colin I didn't know it could do that, although I am using a very old version and as I have about 15 ESP modules all around my house, I thought I would change as little as possible for now, then when I test in one unit and get it to work, then look to change all of them to the new code when I understand things better.
Steve, At the moment I am trying to avoid the ArduinoJSON because my modules are very time critical as they are constantly looping with as little delay as possible. But I may go to ArduinoJSON when i need to test it.
Example ... {"temperature":21.3,"humidity":46.2}
But for the moment if I can, I would like to this like the above as I know that will have no impact on timing.
I want to do this in Node-RED debug, ( image example got from internet ).
I have learnt more doing it this way as I have never used strcpy or strcat before.
One question i know that a \ is an escape but how is it being used here, as there are so many inverted commas and colon's, i get lost exactly what it is all doing ?
And for anyone that may read this in the future, I will add the finished code below.
As I hate it when you follow a subject and they find a solution but they forget to post the final code. This includes the missing comma.
I feel a little stupid but I cannot get this to work
It should be shown in Node-RED as a key value pair object.
What I am getting is "{"PIR":movement}" in my debug node.
I have tried everything I can think of and looked on the internet for an hour.
I think it is something very simple but I cannot find it !
My code
char MQTT_Text_to_send[] = "movement";
strcpy (json_data, "{\"PIR\":"); // Copy this as it is the start of a key-value pair.
strcat (json_data, MQTT_Text_to_send); // Append to json_data, this Joins 2 Char arrays ?
strcat (json_data, "}"); // Required json_data output.
Serial.println ("");
Serial.print ("PIR = ");
Serial.println (json_data);
MQTT_client.publish (Topic_01, json_data);
That worked, 1 hour wasted and I knew if I came on here someone would see my mistake !
But I do try and find the problem without just coming here and asking.
Ha ha I knew you would say that, i have tried to use it, but it would not work on my code.
I seem to remember it was throwing errors and I cannot update my Arduino board manager past 2.7.4 at the moment, as again I get errors with my code & Arduino libraries.
My code was added to over a 4 year period with various libraries added, of which most are out of date now. All rolled into one master universal code that can do everything i need for my home automation.
There are about 12 modules in my home all doing different things and reporting back to Node-RED. So it is complicated.
I will have another go soon at ArduinoJSON
Mistakingly I thought the simple way would be quicker