Splitting arrays from an accelerometer into small records for Influxdb

Hi, I am trying to convert the MQTT msg below into separate records for the ingest InfluxDB node:

{"device_id":"VEHIGE9820","sr":32,"device_t":1680654643559,
"x":[324,294,358,230,279,332,385,490,381,362,290,336,328,275,370,411,543,385,306,234,475,426,396,298,264,211,309,298,400,362,306,283],
"y":[155,143,181,324,234,166,260,362,207,204,302,223,226,207,234,136,358,396,298,306,260,321,226,181,449,290,109,256,91,339,219,181],
"z":[2150,2071,2184,2041,2018,2007,2033,2139,2082,2029,2003,2093,2154,1901,2018,2010,2056,2188,2161,2041,2124,2044,2135,2105,2108,2097,1897,2297,2033,1942,2154,2071]}

This sensor data arrives every second from an accelerometer that buffers 32 samples per second for all 3 axis (x, y and z). It registers the timestamp device_t when it grabs the last of those samples, then sends the mqtt msg.

In order to plot these msgs out in grafana, I need to have individual records of each 1/32s containing 3 axis values.

Therefore I need 32 records written to the influxdb node each sec, eg (device_id, x1, y1, z1, time1), (device_id, x2, y2, z2, time2)...(device_id, x32, y32, z32, time32), etc, where time1 is the device time second (device_t) / 32.

I hope this makes sense so far!. I am having issues creating a node that can split the msg into these individual msgs with their corresponding times... Any help or pointers would really help me out! thanks.

I can get the individual msgs from this function node by iterating over the array:

var deviceid = msg.payload.device_id;
var timestamp = msg.payload.device_t;


for (var i = 0; i < msg.payload.x.length; i++) {
    var newMsg = {};
    var accelx = msg.payload.x[i];
    var accely = msg.payload.y[i];
    var accelz = msg.payload.z[i];
    newMsg.payload = { d: deviceid, t: timestamp, x: accelx, y: accely, z: accelz };
    node.send(newMsg);
}
return null;

but I still need to add the correct timestamp to each msg which takes away the last second and divides it by increments of 1/32...

ok this is my best effort, which spits out something that looks ok, but pls let me know if my maths works out..

var timestamp = msg.payload.device_t;


for (var i = 0; i < msg.payload.x.length; i++) {
    var newMsg = {};
    var accelx = msg.payload.x[i];
    var accely = msg.payload.y[i];
    var accelz = msg.payload.z[i];
    var time = timestamp - 1000 + (i/32 * 1000);
    newMsg.payload = { d: deviceid, t: time, x: accelx, y: accely, z: accelz };
    node.send(newMsg);
}
return null;

all done. i got there in the end.
Peek 2023-04-05 00-55

1 Like

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