I have a question. Sensor data classification

As shown in the photo above, i2c succeeded in fetching data values. The problem is that it is difficult to distinguish the payload values. For example, in the first case it is ph, in the second case it is temperature and in the third case it is a co2 sensor.

The problem is trying to load the data in the same way as the sensor hat. And I am planning to make it in dashboard form, but JavaScript is very difficult. Please ask for help.

[{"id":"6458ef98.82df1","type":"tab","label":"플로우 1","disabled":false,"info":""},{"id":"541e2113.16012","type":"inject","z":"6458ef98.82df1","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":260,"wires":[["3b1a27be.913388"]]},{"id":"3b1a27be.913388","type":"exec","z":"6458ef98.82df1","command":"/usr/bin/python /home/pi/i2c.py","addpay":false,"append":"","useSpawn":"true","timer":"","oldrc":false,"name":"","x":630,"y":120,"wires":[["5d2f163e.903a68"],,]},{"id":"5d2f163e.903a68","type":"debug","z":"6458ef98.82df1","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":930,"y":60,"wires":}]

Use a function to split the string on :

Put part [0] into msg.topic and part [1] into msg.payload.


var parts = msg.payload.split(":")
msg.topic = parts[0].trim();
msg.payload = parts[1].trim();
return msg;

Ps, in order to make code more readable and importable it is important to post it between two sets of three backticks - ``` - see this post for more details - How to share code or flow json

Please edit your first post.

thank you for the reply. I used the split function, but there was a problem that the problem was divided into 6 pieces of 3 data. As I said before: I split with a string, but it is divided into 6 instead of 3. Is it possible with 3 data? I will go home and test it tomorrow.

Based on the screen shot of 3 messages, each containing 1 colon : that function will split the value and make 3 unique messages each with an identifiable topic and correct value.

You can then place a switch node after and direct the 3 messages down their own path/wire.

Isn't that 3 string arrays of colon separated string values?

Edit: nevermind it's the string length. I'm tired. :smile:

Let's try it first. For reference, there is no topic. In other words, the payload itself comes out like this at pH 99: 0000. Actually, I modified it in the Python code to get the data value.

There will be when you try the function i wrote

I can see that...

as I said 3 messages, each with a single colon...

there for...

"pH 99: 0.000".split(":") will return a 2 part array of [ "pH 99", " 0.000" ]

msg.topic = parts[0].trim(); 
msg.payload = parts[1].trim();
return msg;

will make a msg WITH a topic and the corresponding payload!

and this will happen for every subsequent message - making unique messages with thier own topic and own payload.

I will finish with saying, if you sent (from your python output) standard JSON instead of this made up format, it would have been a simple case of sending the payload through a JSON node.

So while the function above will work - its not ideal (because of the data format)

I you want to do this better, then send JSON out from the python.

e.g. send { "topic" : "pH 99", "payload": 0 } then send it through a JSON node - job done, no string splitting.

1 Like

Absolutely +lots on @Steve-Mcl's suggestion. If you have control of the python then change to code to pass a JSON string. Then you can feed that through a JSON node and you will immediately have it as a javascript object.

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