How to retrieve individual parts of data from Serial port (arduino) and insert into InfluxDB

hi, i am meeting issue when trying to insert data retrieved from the Serial port into InfluxDB, can anyone help me on splitting up the retrieved data into individual parts before inserting them into InfluxDB?

as of now, i am receiving 2 components of information from the arduino through serial
29.79 (actual reading of the temperature sensor) 31 (preset threshold)
1 (the lightbulb is on) 250 (the reading from the LDR)
image

Are you in control of the data from the arduino, if so then it would be easier if you formatted it as a json string, something like
{"temperature": 29.79, "threshold": 31}
Then in node red you could feed it through a JSON node and it the values would be accessible as msg.payload.temperature and msg.payload.threshold.

hi, i have managed to change the obtained fields
how do i configure the potential JSON node to feed values into the DB?
image

You haven't got the quotes right, and you haven't got the {}. Once that is fixed feed it through a JSON node and check it looks right. Also use simple names for things not long strings with special chars and spaces. Otherwise it is a lot of tedious typing to get the data. For example you would have to use
msg.payload["Light is (1,on;2,off)"]
instead of something like
msg.payload.light_state

hi, have i got it right?
it seems that i am having difficulties quoting individual names such as "Temperature"
image

i dont think i am able to unquote ("") the entire string because that is the automatic output from the Arduino

The quotes round the outside are not part of the string they are just the debug node showing that it is a string. Probably in the arduino code you need to escape the double quotes, so something like \" in the text generated where you want them, so in the arduino code (which I don't know) you might have something like
"\"temperature\": ..."
Also you don't want the new line on the end (but that might be being added in node red if you have told it to add new line each message in the input node).

This is what i have obtained after adding "
I managed to put it through a json node
image

1 Like

Excellent. Now the next question is what is the structure of the influx database that you want? So what measurements, fields and tags do you want?

it can just be inserted according to the fields (temperature, threshold) (light, LDR_value) into two seperate Influx DB with name temp and light

both with location set as "office" so i will be able to filter through the database easier when using grafana

I advise not using separate influx databases, just use separate measurements within one database. Otherwise you are just adding complexity. If you look at the influxdb out node it tells you how to format the data for the fields and tags in the help text.
" If msg.payload is an array containing two objects, the first object will be written as the set of named fields, the second is the set of named tags."
So you will want an array with the first element (the fields) containing the temperature and threshold object that is currently in the payload and the second element (the tags) will be something like
{location: "office"}
You can do that in a change node but I would probably do it in a change node, something like

msg.payload = [msg.payload, {location: "office"}]

and a similar thing for the other one, though that needs to go to a different measurement, which you can either do by using two influxdb nodes or by setting msg.measurement.

thank you so much i have accomplished my objective

Excellent, could you mark the thread as Solved so it is clear it is sorted. Click the three dots at the bottom of the post that best represents a fix.

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