Hello everyone. I am facing the following problem:
In MQTT, i'm suscribe to a topic and i'm constantly receiving a frame consisting in three bytes like so: (SoF) ( byte1) (byte2).
Where (SoF) represents the start of the frame ( it's just a char) and (byte1),(byte2) the most significant and less significant bits of a uint16.
My question is, how can I "sense" when a frame is comming and then concatenate the byte1 and the byte2 in a single unsigned int variable?
In C it's quite easy to do it, but since this is my first time programming in .js I have no clue of where to start.
Not sure what you mean by that bit. Each time there is a new value for the subscribed topic in mqtt the MQTT In node will pass on a message, which you can feed into a function node to do your manipulation.
If you need further help then feed the output of the mqtt node into a debug node and paste what you get here so we can see exactly what the format of the data is.
That is what i already achieved. Know what i need to do Is to program the funtion that "understand" that when a (SoF) is coming the next two bytes that comes are the bits of a 16 bits variable.
I don't have a clue of how to shift those bits into a new variable in .js ( and particulary with the payload).
Again you have not made it clear what you mean by "understand that when a (SoF) is coming the next two bytes that comes are the bits of a 16 bits variable". Do you mean that you get other types of messages coming in and you need to differentiate them? If so then you can use a Switch node for that. To access the payload in a function node you use msg.payload. Have a look at the nodered docs there are is a lot of useful information there about accessing data in messages and so on.
As for how to shift data in javascript an internet search for 'javascript bit shift' will take you to sites such as https://www.w3schools.com/js/js_bitwise.asp