resolver101
resolver101

Reputation: 2255

node red and Raspberry pi sense hat charts

I'm new to the node red, I've create a function that takes data from the Raspberry sense hat and I've written the following function to separate the data into 3 streams:

var msg1 = { payload: msg.payload.acceleration.z };
var msg2 = { payload: msg.payload.temperature };
var msg3 = { payload: msg.payload.pressure };

return [ [msg1], [msg2], [msg3] ];

From these data streams I've attached the charts and it all seems to be displaying the data correctly. However, these 3 messages keep on appearing in the debug window. Do you know why or how to stop them?

21 Mar 18:28:20 - [error] [ui_gauge:21ce1e34.466272] TypeError: Cannot read property 'toString' of undefined
21 Mar 18:28:20 - [error] [ui_gauge:f04d20fd.29fdd] TypeError: Cannot read property 'toString' of undefined
21 Mar 18:28:20 - [error] [function:89bbcb93.c61508] TypeError: Cannot read property 'z' of undefined

Upvotes: 0

Views: 801

Answers (1)

knolleary
knolleary

Reputation: 10117

The SenseHat node emits separate messages for the three types of event it generates. That means each message is either a motion event, environment event or joystick event.

Your code currently assumes that each message has all properties on it which won't be the case.

You should add a check to see whether each property exists before trying to access it. In fact, msg.topic identifies the type of event the message contains:

var msg1,msg2,msg3;
if (msg.topic === 'motion') {
    msg1 = { payload: msg.payload.acceleration.z };
} else if (msg.topic === 'environment') {
    msg2 = { payload: msg.payload.temperature };
    msg3 = { payload: msg.payload.pressure };
}
return [ msg1, msg2, msg3 ];

Upvotes: 3

Related Questions