BMP180 sensor to MQTT get multiple values

Still trying to get this working. Can you please advise...from my reading, node red is expecting Java Scrip in the received MQTT message, correct? If this is true, I am struggling to find a method within Python to translate the data from the sensors into a properly formatted Java Script package/message. Thx

As I said I don't know python. It is not javascript you are trying to generate but JSON, which is nothing directly to do with javascript. If the two values in your variables are, for example, 21.2 and 63.5 then the message you need to send to mqtt is a string containing
{"temp": 21.2, "hum": 63.5}
Googling for "insert variable into string in python" seems to yield an number of suggestions. One complication may be getting the double quotes in which might add a complication. Again googling for "python double quotes in string" looks likely to be useful.

Colin, Thank you...JSON it is. I have done the exact search you describe but every time I get the information over to the MQTT listener in node red, it is still a string. I am able to get the numbers from the sensors (variables) into the sent message via methods found...but only as a string and not objects. Thus the issue is not capturing and sending the variables from Python, but rather how node red MQTT sees that data.

What string are you getting out of the MQTT In node?

The third line in the shot below is the msgs variable from Python generated by: client.publish("bmp_temp",msgs);

image

When this hits the node red MQTT, below is the error received:
image

Even getting the required double quotes into the string and the colon via:
image

Which gives the output of:
image

Still results in the same "Failed to parse JSON string error

That says it is not getting a valid json string, obviously, so change the Output dropdown in the MQTT In node to Auto Detect so you can see the raw data it is receiving. What does it show then?

Dooh, never mind...forgot the comma, this in Python works (for others that may have a similar issue)
msgs = "{{"temp:": {} ,"pres:"{}}}"..format(temperature, pressure)

Succes:
image

A sincere thank you Colin!! :100: Hopefully I will be able to offer someone assistance in the future. I truly am loving node red and will sing its praises!

1 Like

You have too many colons, take out the ones extra ones inside the double quotes, so
msgs = "{{"temp": {} ,"pres"{}}}"

As you have it at the moment you have attributes called temp: and pres: whereas you want them called temp and pres.

Minor change to eliminate the double colon

msgs = "{{"temp": {} ,"pres" :{}}}".format(temperature, pressure)

May I extend the question a bit? Now that I have the 2 objects coming in via MQTT, I need to execute a function on the temp object to do a conversion. Multiple experiments have failed. Ideas on how I can consume the temp object coming from MQTT in the function node to execute a math operation against it? Currently have:
image

Which results in the following error:
image

Itā€™s arrives as msg. So you need msg.payload.temp

Thank you for the quick reply, however when attempting the following:
image
I get the error:
image

actually I believe this is the correct error, attempting to resolve
image

OK, so there may be a more elegant way, but what is working for me is to use the change node to set the payload to a number prior to doing the conversion by using the following:
image

1 Like

It comes in as msg AND you need to return msg, (not return msg.payload)
But your jsonata version is just fine.

I use MQTT to broker a similar message from a basic DHT sensor. It arrives into Node-Red as a msg.payload (which is just a string based message). I simply put that through a CSV node to make it into a buffer as such, by adding the "temp" and "humidity" to the msg. The gauges can then extract what they need from the msg.payload directly, as msg.payload.temp and msg.payload.humidity. It keeps the MQTT message size to a minimum.

msg = (b'{0:3.1f},{1:3.1f}'.format(t,h))
client.publish(TOPIC, msg)

1 Like

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