I've got a no-doubt very newbie question.
I have a sensor, which gives off a hex string as being the payload. So far I've been able to extract -within a function node- the data, put it in variables and process it as I want it to be. After this however, I got stuck.
What I basically want to accomplish, is to put the variables in a "msg.payload.", so that I'll end up with one function which accepts the initial payload and outputs one msg with several topics.
After a few hours of frustration and knowing it must be something simple which I oversee, my temperature sensor can pickup my body temperature...
If anybody can help...
The following is basically the working part..except for the outgoing part.
let payload = msg.payload;
let batteryRaw = payload.substr(4,4);
let humidityRaw = payload.substr(12,2);
let pressureRaw = payload.substr(18,4);
let temperatureRaw = payload.substr(26,4);
let gasRaw = payload.substr(34,4);
let batteryDec = (parseInt(batteryRaw, 16)*0.01);
let humidityDec = (parseInt(humidityRaw, 16)*0.5);
let pressureDec = (parseInt(pressureRaw, 16)*0.1);
let temperatureDec = (parseInt(temperatureRaw, 16)*0.1);
let gasDec = (parseInt(gasRaw, 16)*0.01);
return {payload:temperatureDec};
I think you are looking for something like this. Not multiple topics but multiple different payloads, bad way of wording this but I'm at a loss for a better description
msg = {
topic: "data",
payload: "sensors",
battery: batteryraw,
pressure: pressureraw,
gas: gasraw,
}
return msg;
and so on, this gives you all your data in one msg but lots of sub-payloads if you will. If this isn't what you are looking for please let us know, I triple checked my syntax but I may have a typo, sometimes hard to see everything on my monitor.
If you put a debug node on the output of the function node and set it to complete msg object you'll see all the data in the output. You can have as many of these sub-parts as you want in the payload.
Thanks for the fast reply, it indeed was the exact thing I wanted to accomplish! After seeing many errors, looking for many (obiously wrong-) terms on Google, I'm now "back on the road"! (and seeking a quick course in js
The complete code for the functionnode is as below. (for the ones interested, it is to decode the RAK7204 environmental sensor, without "decrypting" it through TTN, ChirpStack, or the like.)
Gerry: thanks!!!
let payload = msg.payload;
let batteryRaw = payload.substr(4,4);
let humidityRaw = payload.substr(12,2);
let pressureRaw = payload.substr(18,4);
let temperatureRaw = payload.substr(26,4);
let gasRaw = payload.substr(34,4);
let batteryDec = (parseInt(batteryRaw, 16)*0.01);
let humidityDec = (parseInt(humidityRaw, 16)*0.5);
let pressureDec = (parseInt(pressureRaw, 16)*0.1);
let temperatureDec = (parseInt(temperatureRaw, 16)*0.1);
let gasDec = (parseInt(gasRaw, 16)*0.01);
msg = {
topic: "RAK7204",
payload: payload,
battery: batteryDec,
humidity: humidityDec,
pressure: pressureDec,
temperature: temperatureDec,
gas: gasDec,
}
return msg;