mdk
mdk

Reputation: 133

Javascript Maths Node Red Thermistor Function

have node-red running with arduino analogue output through serial to pi, where I'm collecting thermistor data as a msg payload on change.

Trying to turn the Arduino signal into a Temperature - it's the most common 10K enamelised thermistor that adafruit sell. So quite useful code.

The issue is that I am a total noob at JS.

Here's my code so far - using a function node - and trying to replicate the steinhart equation (https://learn.adafruit.com/thermistor/using-a-thermistor)

 var input = { payload: msg.payload };
var R0 = 10000;
var R = R0 / ((1023 / input)-1);
var T0 = 273 + 25;
var B = 3950;
var T =  1 / ( (1/T0) + (1/B) * Math.log(R/R0) );
return T;

Upvotes: 0

Views: 2237

Answers (1)

user151496
user151496

Reputation: 1985

i am not sure whether will msg.payload will return number in an actual datatype "Number" meaning or a string that will be a numeric, but something like this should take care of any anomalies when trying to divide strings

var numInput = Number(msg.payload);
var R0 = 10000;
var R = R0 / ((1023 / input)-1);
var T0 = 273 + 25;
var B = 3950;
var T =  1 / ( (1/T0) + (1/B) * Math.log(R/R0) );
return T;

EDIT: this should fix the errors:

var numInput = Number(msg.payload);
var R0 = 10000;
var R = R0 / ((1023 / numInput)-1);
var T0 = 273 + 25;
var B = 3950;
var T =  1 / ( (1/T0) + (1/B) * Math.log(R/R0) );
msg.payload = T;
return msg;

Upvotes: 1

Related Questions