As the topic states, I have a simple (to soon be more complex) need to consume MQTT data from sensors into node-red. I have successfully used mosquito and paho in a python script to create 2 separate MQTT messages from the sensor. I have successfully consumed these MQTT messages into node-red and built corresponding gauges/graphs. However my end goal is to write the data to a mysql DB, which I have also successfully set up getting insert to the DB for a single message. What I need is a single line insert into the table which will contain all the measurements/messages rather than just one measurement at a time.
Now for the ask...the BMP108 outputs temp and humidity, I have concatenated these two values into a string and can output that as a message containing both measurements. But when I get to node-red, I am unable to take the single topic with 2 messages and split out the data. I have tried the json convert and switch, but cannot seem to get the incoming two data point message to be split. I suspect it is in the structure of the data being sent via MQTT.
Info:
From python MQTT publish of temperature (working):
client = mqtt.Client()**
client.connect("localhost",1883,60)
client.publish("bmp180_temp", temperature);
Same for python MQTT publish of pressure (working):
client.publish("bmp180_temp", pressure);
However with the string created in python script to send both temperature and pressure by using in python:
msgs = temperature, pressure and client.publish ("bmp180_all", msgs), my raspberry pi terminal print(msgs) now shows (the correct temp, the current pressure) I cannot seem to parse this out via node red.
The msg.payload that is generated from the working (single data point) MQTT is:
For the payload that I am configuring for the multiple data point I have tried multiple different strings of which I cannot seem to parse any successfully.
I have the temperature value and the pressure value, each currently published separately, which works. To simplify I have been experimenting with a simple mosquitto_pub to try different outputs...for instance:
mosquitto_pub -t "bmp180_temp" -m 74.12,88.99
How would I make the key value pairs out of the 74.12 and 88.99?
I am gathering the data on my pi and crafting the mosquito topic and data there using paho in a Python script to generate the MQTT message. I am using the python script to do calibrations and set the frequency of the sent data, node-red is subscribed to this topic.
I can craft the MQTT message in different means prior to node-red reception, so that is what I am trying to manipulate. My hope is that with a properly crafted MQTT message I can then use the split or other node to break the needed data out of the concatenated message which has two data points.
The screen shot that I posted above is obviously the single data point received message
I would say when you establish what your MQTT data looks like maybe we can help. As it is we are chasing a moving target. You have a single data point and yet are trying to parse data. Nail down the data format for us and get a minimal flow together so we have something to work with.
mosquitto_pub -t "bmp_temp" -m '{"temp": 74.12, "hum": 88.99}'
and in the MQTT In node in node red select the output as Parsed JSON which convert that JSON string into a javascript object
That worked wonderfully... as long as I am using the pi terminal to generate the message using mosquito:
However I seem to be unable to translate that mosquitto message format into something that works via paho in my python script. Using paho: client.publish With "bmp_temp" as the topic, how can I construct the message format so I get the 2 data points into node red as the objects generated? I have tried a number of different formats, none of which send out the same message as using the mosquitto_pub command.
the pound symbol # means subscribe to all topics., so if it's behind a name it would subscribe to all topics below that name, such as testing/# would subscribe to testing/one testing/maybe testing/hello and so on.
How are you publishing it at the moment. It is only a string with the values embedded.
If you use some/topic/# then that will give you all topics starting with some/topic/, such as some/topic/x/q. Have a good read through this series for the lowdown on MQTT. It starts with the basic concepts and goes on the more meaty stuff. https://www.hivemq.com/blog/mqtt-essentials-part-1-introducing-mqtt/
msgs = {"temp: temperature, "hum": humidity} Where temperature is the value from the sensor previously defined and working, and humidity is the value from the sensor
Your last problem is nothing to do with mqtt it is a python question on how to buld a string containing {"temp": temperature, "hum": humidity}. I don't know much python so don't know the answer to that, but I am sure it is a trivial problem.