mqttloralorawanmqtt.js

Lora packet data is encoded as hex string but decoding to ASCII is showing nonsense


I am using 2 LHT65N sensors, a Wisgate Edge Lite 2 Gateway, forwarding the payload to a Mosquitto broker, and to display data I am using mqtt.js.

I am receiving packets from the sensors and the data I get is a hex string, but when I am decoding it to plaintext I get no useful data.

So this is the object I get:

{
"applicationID": "1",
"applicationName": "ptxd_temp_humid",
"devEUI": "a84041a777864399",
"deviceName": "sensor_uphill",
"timestamp": 1674051438,
"fCnt": 179,
"fPort": 2,
"data": "CBCF09200203017FFF7FFF",
"data_encode": "hexstring",
"adr": true,
"rxInfo": [
    {
        "gatewayID": "ac1f09fffe08bf79",
        "loRaSNR": 0,
        "rssi": -59,
        "location": {
            "latitude": 0,
            "longitude": 0,
            "altitude": 0
        }
    }
],
"txInfo": {
    "frequency": 868800000,
    "dr": 7
}

You can see that the packet data is encoded as a hex string.

I am using this method:

decoder(bytes: any) {
  var batt_v = ((bytes[0]<<8 | bytes[1]) & 0x3FFF)/1000;
  var temp_int = ((bytes[2]<<24>>16 | bytes[3])/100).toFixed(2);
  var temp_ext = ((bytes[7]<<24>>16 | bytes[8])/100).toFixed(2);
  var hum_int = ((bytes[4]<<8 | bytes[5])/10).toFixed(1);
  var ext_sen = 
     {
       "0":"No external sensor",
       "1":"Temperature Sensor",
     }[bytes[6]&0x7F];

  return {
    Ext_sensor: ext_sen,
    BatV: batt_v,
    TempC_SHT: temp_int,
    Hum_SHT: hum_int,
    TempC_DS: temp_ext,
  };

To achieve this:

{
   "BatV": 1,
   "TempC_SHT": "27.56",
   "Hum_SHT": "50.6",
   "TempC_DS": "25.23"
 }

But I get:

{
  "BatV": 0,
  "TempC_SHT": "0.00",
  "Hum_SHT": "0.9",
  "TempC_DS": "0.00"
}

I feel that the problem is the gateway. The system log of the gateway is already receiving the encoded data. I tried a hex converter and also the payload formatter of the LHT65N but neither gave me the correct data. I also triple checked the API key, but it is correct.

Is there any configuration that I have to do to receive the correct data?

Solution

I was passing the wrong type into the method. I was confused because I compared my data to others and mine appeared to be too short so I was pretty confused. I am decoding my hexstring to a UInt8Array now and it's working as expected.

    client.on('message', (topic, message: Buffer, packet: 
              mqtt.IPublishPacket) => {
    
      let payload = JSON.parse(message.toString())
      console.log(Buffer.from(payload.data, 'hex'))
      this.data = this.Decoder(Buffer.from(payload.data, 'hex'));
      console.log(this.data)
  
    })

Solution

  • The hex string "data": "CBCF09200203017FFF7FFF" that's received decodes properly with the code you showed. Running it against the hex string, I get:

    {
      Ext_sensor: 'Temperature Sensor',
      BatV: 3.023,
      TempC_SHT: '23.36',
      Hum_SHT: '51.5',
      TempC_DS: '327.67'
    }
    

    So that seems to be working. If this arrives via the gateway all the way to the MQTT broker, the problem isn't with the gateway then. It is with either the broker, or the decoder itself, but then again, as I said, the decoder's code seems to be working. It looks like you are getting wrong data, somewhere between it is sent to the decoder and the time it is returned to you.