Split string to object and send to webserver (Traccar)

I'm not very good at coding and have now tried with split, function and JSON to Object convert nodes for days without success. Please help me: O

I'm getting this string below from an ESP8266 with a GPS and need it to publish a webserver like below:

{"idx": 8882, "RSSI": 8, "nvalue": 0, "svalue": "17.941607; 59.372120; 13.00; 1.86"}

http: //my.ip.adress: 5055 /? id = 123456 & lat = 59.372120 & lon = 17.941607 & altitude = 13.00 & speed = 1.8 & hdop = 1.53

id = and hdop = is not in the string and can be static.

Thanks in advance for any pointers to solve this.

Probably easiest to use a template or function node.

Here is a function node method...

var lat, lon;
var latlon = msg.payload.svalue.split(";");
var lat = latlon[0].trim();
var lon = latlon[1].trim();
var alt = latlon[2].trim();
var speed = latlon[3].trim();
msg.url = `http://my.ip.adress:5055/?id=${msg.payload.idx}&lat=${lat}&lon=${lon}&altitude=${alt}&speed=${speed}&hdop=1.53`
return msg;

demo flow...

[{"id":"73f8edcb.67ca04","type":"inject","z":"b872cb4b.5a6448","name":"your esp data (faking it)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"idx\":8882,\"RSSI\":8,\"nvalue\":0,\"svalue\":\"17.941607; 59.372120; 13.00; 1.86\"}","payloadType":"json","x":1440,"y":120,"wires":[["146bf932.54eda7"]]},{"id":"146bf932.54eda7","type":"function","z":"b872cb4b.5a6448","name":"build URL","func":"var lat, lon;\nvar latlon = msg.payload.svalue.split(\";\");\nvar lat = latlon[0].trim();\nvar lon = latlon[1].trim();\nvar alt = latlon[2].trim();\nvar speed = latlon[3].trim();\nmsg.url = `http://my.ip.adress:5055/?id=${msg.payload.idx}&lat=${lat}&lon=${lon}&altitude=${alt}&speed=${speed}&hdop=1.53`\n\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1680,"y":120,"wires":[["d4a0c67f.431328","e9163c66.aedae"]]},{"id":"d4a0c67f.431328","type":"debug","z":"b872cb4b.5a6448","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"url","targetType":"msg","statusVal":"","statusType":"auto","x":1680,"y":160,"wires":[]},{"id":"e9163c66.aedae","type":"http request","z":"b872cb4b.5a6448","name":"","method":"GET","ret":"txt","paytoqs":false,"url":"","persist":false,"authType":"","x":1910,"y":120,"wires":[["3eeb9475.f7d0cc"]]},{"id":"3eeb9475.f7d0cc","type":"debug","z":"b872cb4b.5a6448","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1910,"y":160,"wires":[]}]

important note:

Wherever this {"idx": 8882, "RSSI": 8, "nvalue": 0, "svalue": "17.941607; 59.372120; 13.00; 1.86"} is coming from (HTTP node or MQTT) either set it to parse JSON to object or pass the payload through a JSON node to convert the string to a JS object.

You can tell if it is a string ot JS object by the debug output.

1 Like

Absolutly amazing, very nice off you and it worked directly like a charm :slight_smile:

@Steve-Mcl Yor flow works great and now I also need it to go into a node as Object (node-red-contrib-web-worldmap).

How do I also send it further as an object (now it's a string), so it will be like:
{ "name":"Jason", "lat":59.3523, "lon":17.9691}

What is "it" - the flow i did for you only builds a URL - please remember we cannot see what you see. Use screenshots and export flows to further explain your requirements

Are you sure?

a JSON string is simply converted to a JS object by passing it through a JSON node (and visa versa).

But be sure you have things the right way around.

This is a JS object
image

This is the same JS object as a JSON string
image

Which do you have?

Hi,

From your superb function node i get this:

br,
Björn

I'm trying to build a flow like this (to a world map node):

"Fake data1" Object look like this (and works great):
{ "name":"112244", "lat":59.3523, "lon":17.9691}

Ok, I see (i think), you want to convert the same MQTT data to a worldmap location marker, well you can simply adjust the function I wrote for you to build the object ...

var latlon = msg.payload.svalue.split(";");
var lat = latlon[0].trim();
var lon = latlon[1].trim();
var alt = latlon[2].trim();
var speed = latlon[3].trim();
msg.url = `http://my.ip.adress:5055/?id=${msg.payload.idx}&lat=${lat}&lon=${lon}&altitude=${alt}&speed=${speed}&hdop=1.53`
msg.payload = { "name":"112244", "lat":lat , "lon":lon }; // << ADD THIS
return msg;

Pretty basic JS stuff.

oh, and rename that function to something like "Parse MQTT Data"


PS: I really dont mean to offend - but I suspect you would benefit from some JS training. There are loads of online resources. Go spend a few days learning about objects and arrays. It will save you a lot of time in the long run.

1 Like

Hi and thank you again, I'm so grateful for the help. I'm planning and want to learn some JS and actually started to watch some lessons on Youtube (the problem is to find the time to learn).

I'm using the home automation for our boat, already have sensors working for temperatures/humidity (engine room, saloon, cabins, galley and outside), water leakage alarm, voltage/consumption for different battery banks, motion sensors also use switches for charging, heating, cameras and more fun planned. AND NOW ALSO GPS TRACKING - thanks again for that!

br
Björn

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