How do I can connect MQTT BROKER with NODE RED and show the device data in web browser

I’ll add what you asked me personally:

Again, my package is able to send a valid lorawan message to a broker. What you do with it is up to you.

That said, it’ll depend on how you encode/decode your data, as a data payload is nothing but an array of bytes. For example, the simplest case is where you take only positive integral temperature, so you can represent from 0 up to 255 degrees on a single byte without any encoding.

But what if you want to be able to send negative temperatures? You could use 2’s complement to represent the sign, which will let you encode from -127 to 127 degrees, again only with one byte. And this is really easy to represent, as all you need to do at loraserver’s side is write a decoding function that checks if the value you got is greater than 127 and in that case convert it like this:

if value > 127 {
  temp = value - 2*127
}

But then maybe you don’t want to be limited to integers, so instead you send your temperature using two bytes: one represents the integral part and the sign, and the other byte gives you 8 bits of precision. How do you use them to represent your value? There’s a lot of ways, so just search a bit, pick your favourite and then implement the encoding on your program that is sending the lorawan message. Just as an example, we’ve used this format to encode arbitrary precision float data (latitude, longitude, temperature, rpessure, etc.).

After that, at lora-app-server go to the application where your device is registered, and at Application configuration select javascript for the payload codec and write a function that decodes the bytes array that you’ll get. This function should do exactly the opposite than the encoding one, so if we take the simple example from before, the original encoding (i.e., at the sending script) function should have done something like this:

if temp < 0 {
  value = temp + 2*127
}

Of course, the payload codec needs to return a JS object, so your decoding function at lora-app-server should resemble this:

function Decode(fPort, bytes) {

  var temp = bytes[0];
  if temp > 127 {
    temp = temp - 2*127;
  }
  var dataout = {
  "temperature": {
      "value":temp,
    },
  };

  return dataout;

}

Finally, at node-red you can subscribe to the topic application/[applicationID]/device/[devEUI]/rx and you’ll get a nice json that’ll contain the key object for your data:

{
...
 "object": {                    
        "temperature": {"value": 30},
    }
...
}

Hope that clears up things.

2 Likes