Node Red Java Script

Hi,

I want to send the messages received from in node-red to m2x at&t IoT platform which can only be numeric.
To begin with, I am new to javascript. I have a javascript code which is

var data_type = {
none : 0x00,
led1 : 0x01,
led2 : 0x02,
lux_max : 0x03,
lux_min : 0x04,
lux_curr : 0x05,
baro_max : 0x06,
baro_min : 0x07,
baro_curr : 0x08,
temp_max : 0x09,
temp__min : 0x0A,
temp_curr : 0x0B,
accel_max : 0x0C,
accel_min : 0x0D,
accel_curr : 0x0E,
configuration : 0x0F,
gpio_in : 0x10,
gpio_out : 0x11,
current_max : 0x12,
current_min : 0x13,
current_curr : 0x14,
gps_latitude : 0x15,
gps_longitude : 0x16,
gps_time : 0x17,
gps_date : 0x18,
gps_lock : 0x19,
qos_up : 0x1A,
qos_dwn : 0x1B,
rf_out : 0x1C,
data_mark : 0x1D,
};

var data_struc = {
data_valid : 0,
block_start :0,
temperature : 0,
x_pos : 0,
y_pos : 0,
z_pos : 0,
baro_pressure : 0,
lux : 0,
pkt_timer :0,
rf_pwr : 0,
sf_val : 0,
rssi_up : 0,
snr_up : 0,
rssi_dwn : 0,
snr_dwn :0 ,
lat_deg : 0 ,
lat_min : 0,
long_deg : 0,
long_min : 0,
num_sats : 0 ,
gps_status : 0,
};

context.global.data_out = context.global.data_out || data_struc;
var pData = context.global.data_out;

var msg_pntr = 0;
var temp = 0;

while (msg_pntr < msg.payload.length){
switch (msg.payload[msg_pntr]){
case data_type.lux_max:
case data_type.lux_min:
case data_type.lux_curr:
pData.lux = msg.payload[++msg_pntr] << 8
pData.lux |= msg.payload[++msg_pntr];
pData.lux = pData.lux * 0.24;
msg_pntr++;
break;
case data_type.baro_max:
case data_type.baro_min:
case data_type.baro_curr:
pData.baro_pressure = msg.payload[++msg_pntr]<<16;
pData.baro_pressure |= msg.payload[++msg_pntr]<<8;
pData.baro_pressure |= msg.payload[++msg_pntr];
pData.baro_pressure = pData.baro_pressure * 0.25;
msg_pntr++;
break;
case data_type.accel_max:
case data_type.accel_min:
case data_type.accel_curr:
pData.x_pos = ((msg.payload[++msg_pntr] << 24) >> 24) * 0.0625;
pData.y_pos = ((msg.payload[++msg_pntr] << 24) >> 24) * 0.0625;
pData.z_pos = ((msg.payload[++msg_pntr] << 24) >> 24) * 0.0625;
msg_pntr++;
break;
case data_type.temp_min:
case data_type.temp_max:
case data_type.temp_curr:
pData.temperature = msg.payload[++msg_pntr] << 24;
pData.temperature |= msg.payload[++msg_pntr] << 16;
pData.temperature = (pData.temperature >> 16) * .0625;
msg_pntr++;
break;
case data_type.configuration:
pData.pkt_timer = msg.payload[++msg_pntr];
msg_pntr++
break;
case data_type.current_max:
case data_type.current_min:
case data_type.current_curr:
msg_pntr++;
msg_pntr++;
msg_pntr++;
break;
case data_type.gps_latitude:
pData.lat_deg = (msg.payload[++msg_pntr] << 24) >> 24;
pData.lat_min = msg.payload[++msg_pntr];
temp = msg.payload[++msg_pntr] << 8
temp |= msg.payload[++msg_pntr];
pData.lat_min = pData.lat_min + (temp * 0.0001);
msg_pntr++;
break;
case data_type.gps_longitude:
pData.long_deg = (msg.payload[++msg_pntr] << 24);
pData.long_deg |= (msg.payload[++msg_pntr] << 16);
pData.long_deg = pData.long_deg >> 16;
pData.long_min = msg.payload[++msg_pntr];
temp = msg.payload[++msg_pntr] << 8
temp |= msg.payload[++msg_pntr];
pData.long_min = pData.long_min + (temp * 0.0001);
msg_pntr++;
break;
case data_type.gps_time:
msg_pntr++;
msg_pntr++;
msg_pntr++;
msg_pntr++;
break;
case data_type.gps_date:
msg_pntr++;
msg_pntr++;
msg_pntr++;
msg_pntr++;
break;
case data_type.gps_lock:
msg_pntr++;
pData.gps_status = msg.payload[msg_pntr] & 0x0F;
pData.num_sats = msg.payload[msg_pntr++] >> 4;
break;
case data_type.qos_up:
pData.rssi_up = msg.payload[++msg_pntr] << 24;
pData.rssi_up |= msg.payload[++msg_pntr] << 16;
pData.rssi_up = pData.rssi_up >> 16;
pData.snr_up = (msg.payload[++msg_pntr] << 24) >> 24;
pData.rf_pwr = (msg.payload[++msg_pntr] << 24) >> 24;
msg_pntr++;
break;
case data_type.qos_dwn:
pData.rssi_dwn = msg.payload[++msg_pntr] << 24;
pData.rssi_dwn |= msg.payload[++msg_pntr] << 16;
pData.rssi_dwn = pData.rssi_dwn >> 16;
pData.snr_dwn = ((msg.payload[++msg_pntr] << 24) >> 24) / 4;
msg_pntr++;
break;
case data_type.rf_out:
pData.rf_pwr = (msg.payload[++msg_pntr] << 24) >> 24;
msg_pntr++;
break;
case data_type.data_mark:
if (msg_pntr == 0) {
pData = data_struc;
pData.block_start = 1;
msg_pntr++;
}
else if (msg_pntr == (msg.payload.length - 1) && (pData.block_start === 1)) {
pData.data_valid = 1;
msg_pntr++;
}
else {
pData = data_struc;
msg_pntr = msg.payload.length;
}
break;
default:
pData = data_struc;
msg_pntr = msg.payload.length;
}
}

pData.sf_val = parseInt(msg.datr.replace("SF"," "),10);

context.global.data_out = pData;

return pData;

And the result for this is in debug dash is

{ "temperature": 24.4375, "_msgid": "51855b8f.ae7aa4", "x_pos": -0.25, "y_pos": 0, "z_pos": 0.9375, "baro_pressure": 100101.5, "lux": 550.8, "sf_val": 12 }

Could you help me in editing this code and just print the temperature numeric and nothing else. Like,

24.4375

I have a javascript code which is

You have copied javascript code.

What input data do you get ?

Thanks for the reply.

The input is from mdot evaluation board which has sensors like temperature, humidity, pressure and accelerometer.

Can we just modify the code to just get the temperature numeric.

I mean input example data.

But sure, you can remove anything that has nothing to do with the temperature.

This is the input data.

{ "tmst": 3297610324, "chan": 7, "rfch": 1, "freq": 867.9, "stat": 1, "modu": "LORA", "datr": "SF12BW125", "codr": "4/5", "lsnr": 10.5, "rssi": -63, "opts": "", "size": 14, "fcnt": 319, "cls": 0, "port": 1, "mhdr": "407714dd00003f01", "appeui": "e4-4d-04-7a-aa-db-e0-78", "deveui": "00-80-00-00-00-00-d9-c1", "ack": false, "adr": false, "gweui": "00-80-00-00-a0-00-2f-bc", "seqn": 319, "time": "2019-08-07T12:53:06.904012Z", "payload": [ 14, 252, 0, 15, 8, 6, 28, 14, 5, 7, 35, 11, 1, 139 ], "eui": "00-80-00-00-00-00-d9-c1", "_msgid": "c53dc386.3ac24" }

You can see the input data as the payload

Please make sure that you wrap code with back-ticks or triple back-ticks so that it doesn't get mangled.

I am no hero in terms of buffer stuff, but what you can do is filter the output with a change node:

[{"id":"1243c00.48a824","type":"inject","z":"2cf143ce.b68414","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":144,"y":476,"wires":[["d180e5c2.0d6be"]]},{"id":"6589a58f.cf7774","type":"function","z":"2cf143ce.b68414","name":"","func":"var data_type = {\nnone : 0x00,\nled1 : 0x01,\nled2 : 0x02,\nlux_max : 0x03,\nlux_min : 0x04,\nlux_curr : 0x05,\nbaro_max : 0x06,\nbaro_min : 0x07,\nbaro_curr : 0x08,\ntemp_max : 0x09,\ntemp__min : 0x0A,\ntemp_curr : 0x0B,\naccel_max : 0x0C,\naccel_min : 0x0D,\naccel_curr : 0x0E,\nconfiguration : 0x0F,\ngpio_in : 0x10,\ngpio_out : 0x11,\ncurrent_max : 0x12,\ncurrent_min : 0x13,\ncurrent_curr : 0x14,\ngps_latitude : 0x15,\ngps_longitude : 0x16,\ngps_time : 0x17,\ngps_date : 0x18,\ngps_lock : 0x19,\nqos_up : 0x1A,\nqos_dwn : 0x1B,\nrf_out : 0x1C,\ndata_mark : 0x1D,\n};\n\nvar data_struc = {\ndata_valid : 0,\nblock_start :0,\ntemperature : 0,\nx_pos : 0,\ny_pos : 0,\nz_pos : 0,\nbaro_pressure : 0,\nlux : 0,\npkt_timer :0,\nrf_pwr : 0,\nsf_val : 0,\nrssi_up : 0,\nsnr_up : 0,\nrssi_dwn : 0,\nsnr_dwn :0 ,\nlat_deg : 0 ,\nlat_min : 0,\nlong_deg : 0,\nlong_min : 0,\nnum_sats : 0 ,\ngps_status : 0,\n};\n\ncontext.global.data_out = context.global.data_out || data_struc;\nvar pData = context.global.data_out;\n\nvar msg_pntr = 0;\nvar temp = 0;\n\nwhile (msg_pntr < msg.payload.length){\nswitch (msg.payload[msg_pntr]){\ncase data_type.lux_max:\ncase data_type.lux_min:\ncase data_type.lux_curr:\npData.lux = msg.payload[++msg_pntr] << 8\npData.lux |= msg.payload[++msg_pntr];\npData.lux = pData.lux * 0.24;\nmsg_pntr++;\nbreak;\ncase data_type.baro_max:\ncase data_type.baro_min:\ncase data_type.baro_curr:\npData.baro_pressure = msg.payload[++msg_pntr]<<16;\npData.baro_pressure |= msg.payload[++msg_pntr]<<8;\npData.baro_pressure |= msg.payload[++msg_pntr];\npData.baro_pressure = pData.baro_pressure * 0.25;\nmsg_pntr++;\nbreak;\ncase data_type.accel_max:\ncase data_type.accel_min:\ncase data_type.accel_curr:\npData.x_pos = ((msg.payload[++msg_pntr] << 24) >> 24) * 0.0625;\npData.y_pos = ((msg.payload[++msg_pntr] << 24) >> 24) * 0.0625;\npData.z_pos = ((msg.payload[++msg_pntr] << 24) >> 24) * 0.0625;\nmsg_pntr++;\nbreak;\ncase data_type.temp_min:\ncase data_type.temp_max:\ncase data_type.temp_curr:\npData.temperature = msg.payload[++msg_pntr] << 24;\npData.temperature |= msg.payload[++msg_pntr] << 16;\npData.temperature = (pData.temperature >> 16) * .0625;\nmsg_pntr++;\nbreak;\ncase data_type.configuration:\npData.pkt_timer = msg.payload[++msg_pntr];\nmsg_pntr++\nbreak;\ncase data_type.current_max:\ncase data_type.current_min:\ncase data_type.current_curr:\nmsg_pntr++;\nmsg_pntr++;\nmsg_pntr++;\nbreak;\ncase data_type.gps_latitude:\npData.lat_deg = (msg.payload[++msg_pntr] << 24) >> 24;\npData.lat_min = msg.payload[++msg_pntr];\ntemp = msg.payload[++msg_pntr] << 8\ntemp |= msg.payload[++msg_pntr];\npData.lat_min = pData.lat_min + (temp * 0.0001);\nmsg_pntr++;\nbreak;\ncase data_type.gps_longitude:\npData.long_deg = (msg.payload[++msg_pntr] << 24);\npData.long_deg |= (msg.payload[++msg_pntr] << 16);\npData.long_deg = pData.long_deg >> 16;\npData.long_min = msg.payload[++msg_pntr];\ntemp = msg.payload[++msg_pntr] << 8\ntemp |= msg.payload[++msg_pntr];\npData.long_min = pData.long_min + (temp * 0.0001);\nmsg_pntr++;\nbreak;\ncase data_type.gps_time:\nmsg_pntr++;\nmsg_pntr++;\nmsg_pntr++;\nmsg_pntr++;\nbreak;\ncase data_type.gps_date:\nmsg_pntr++;\nmsg_pntr++;\nmsg_pntr++;\nmsg_pntr++;\nbreak;\ncase data_type.gps_lock:\nmsg_pntr++;\npData.gps_status = msg.payload[msg_pntr] & 0x0F;\npData.num_sats = msg.payload[msg_pntr++] >> 4;\nbreak;\ncase data_type.qos_up:\npData.rssi_up = msg.payload[++msg_pntr] << 24;\npData.rssi_up |= msg.payload[++msg_pntr] << 16;\npData.rssi_up = pData.rssi_up >> 16;\npData.snr_up = (msg.payload[++msg_pntr] << 24) >> 24;\npData.rf_pwr = (msg.payload[++msg_pntr] << 24) >> 24;\nmsg_pntr++;\nbreak;\ncase data_type.qos_dwn:\npData.rssi_dwn = msg.payload[++msg_pntr] << 24;\npData.rssi_dwn |= msg.payload[++msg_pntr] << 16;\npData.rssi_dwn = pData.rssi_dwn >> 16;\npData.snr_dwn = ((msg.payload[++msg_pntr] << 24) >> 24) / 4;\nmsg_pntr++;\nbreak;\ncase data_type.rf_out:\npData.rf_pwr = (msg.payload[++msg_pntr] << 24) >> 24;\nmsg_pntr++;\nbreak;\ncase data_type.data_mark:\nif (msg_pntr == 0) {\npData = data_struc;\npData.block_start = 1;\nmsg_pntr++;\n}\nelse if (msg_pntr == (msg.payload.length - 1) && (pData.block_start === 1)) {\npData.data_valid = 1;\nmsg_pntr++;\n}\nelse {\npData = data_struc;\nmsg_pntr = msg.payload.length;\n}\nbreak;\ndefault:\npData = data_struc;\nmsg_pntr = msg.payload.length;\n}\n}\n\npData.sf_val = parseInt(msg.datr.replace(\"SF\",\" \"),10);\n\ncontext.global.data_out = pData;\n\nreturn {payload:pData};","outputs":1,"noerr":0,"x":442,"y":476,"wires":[["5ba941ae.ffd0a8"]]},{"id":"d180e5c2.0d6be","type":"function","z":"2cf143ce.b68414","name":"input","func":"return { \"tmst\": 3297610324, \"chan\": 7, \"rfch\": 1, \"freq\": 867.9, \"stat\": 1, \"modu\": \"LORA\", \"datr\": \"SF12BW125\", \"codr\": \"4/5\", \"lsnr\": 10.5, \"rssi\": -63, \"opts\": \"\", \"size\": 14, \"fcnt\": 319, \"cls\": 0, \"port\": 1, \"mhdr\": \"407714dd00003f01\", \"appeui\": \"e4-4d-04-7a-aa-db-e0-78\", \"deveui\": \"00-80-00-00-00-00-d9-c1\", \"ack\": false, \"adr\": false, \"gweui\": \"00-80-00-00-a0-00-2f-bc\", \"seqn\": 319, \"time\": \"2019-08-07T12:53:06.904012Z\", \"payload\": [ 14, 252, 0, 15, 8, 6, 28, 14, 5, 7, 35, 11, 1, 139 ], \"eui\": \"00-80-00-00-00-00-d9-c1\", \"_msgid\": \"c53dc386.3ac24\" }\n","outputs":1,"noerr":0,"x":302,"y":476,"wires":[["6589a58f.cf7774"]]},{"id":"def024d2.5743e8","type":"debug","z":"2cf143ce.b68414","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":798,"y":476,"wires":[]},{"id":"5ba941ae.ffd0a8","type":"change","z":"2cf143ce.b68414","name":"get temperature","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.temperature","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":612,"y":476,"wires":[["def024d2.5743e8"]]}]

Note that I have changed the function to output msg.payload instead of an object, see bottom of the function: return {payload:pData};