Payload Codec Java Script

Hi,

Can anyone help me with the java code to decode the base64 encoded data? Actually I will be using that for decoding the base64 encoded data that I receive in the Application page of LoRa Built-in server.

I am receiving in the device-data the data as “TXkgbmFtZSBpcyBTb25hbSBwaHVudHNobw==” but I want to get the data as my original data sent from the node which is “My name is Sonam Phuntsho” but I did not know how to program my logic in the Payload Codec.

If any one can help me please.

I am new to LoRa Server as well as Java.

Thank you

Hi men,
You must build your own decoder for your payload in “Javascript”, not in Java.
I found this example of a YouTube channel.

// Decode decodes an array of bytes into an object.
//  - fPort contains the LoRaWAN fPort number
//  - bytes is an array of bytes, e.g. [225, 230, 255, 0]
// The function must return an object, e.g. {"temperature": 22.5}

function hex_to_ascii(str1)
 {
	var hex  = str1.toString();
	var str = '';
	for (var n = 0; n < hex.length; n += 2) {
		str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
	}
	return str;
 }

function toHexString(bytes) {
    return bytes.map(function(byte) {
        return ("00" + (byte & 0xFF).toString(16)).slice(-2)
      }).join('')
}

function Decode(fPort, bytes) {

    var tohex = toHexString(bytes);
	var toascii = hex_to_ascii(tohex);
  
    return {"mydata": toascii};

}

It converts the payload to HexString and after that to Ascii.
I hope it works for you.
Best Regards

1 Like

Thanks man ( oscard1)
More examples like this is what we need.
but! if possible; complete examples, from arduino to the chirpstack application…
It seems complicated to get!

Thank you oscard1 , because I had been stuck on this for 2 or 3 months, so it has helped me a lot.
Good and simple examples like this can get you out of a rut… I can now follow something that works…

JSON up: {“rxpk”:[{“tmst”:4254943203,“chan”:2,“rfch”:1,“freq”:868.500000,“stat”:1,“modu”:“LORA”,“datr”:“SF7BW125”,“codr”:“4/5”,“lsnr”:6.8,“rssi”:-36,“size”:15,“data”:“QEStMAGABQABGVjRlzle”}]}

application/2/device/0000000000000001/event/up {“applicationID”:“2”,“applicationName”:“1apli”,“deviceName”:“1dis”,“devEUI”:“AAAAAAAAAAE=”,“rxInfo”:[],“txInfo”:{“frequency”:868500000,“modulation”:“LORA”,“loRaModulationInfo”:{“bandwidth”:125,“spreadingFactor”:7,“codeRate”:“4/5”,“polarizationInversion”:false}},“adr”:true,“dr”:5,“fCnt”:5,“fPort”:1,“data”:“MDE=”,“objectJSON”:"{“mydata”:“01”}",“tags”:{},“confirmedUplink”:false,“devAddr”:“ATCtRA==”}

Hi @paco,

You can find examples of decoders usually in devices user manuals.
Here you are one more of a sensor to measure temperature, humidity, and other signals depending on how it is setup.

function Decode(fPort, bytes){

var data = {  
       //External sensor
       Ext_sensor:
       {
         "0":"No external sensor",
         "1":"Temperature Sensor",
         "4":"Interrupt Sensor send",
         "5":"Illumination Sensor",
         "6":"ADC Sensor",
         "7":"Interrupt Sensor count",
       }[bytes[6]&0x7F],
       
       //Battery,units:V
       BatV:((bytes[0]<<8 | bytes[1]) & 0x3FFF)/1000,
       
       //SHT20,temperature,units:
       TempC_SHT:((bytes[2]<<24>>16 | bytes[3])/100).toFixed(2),
       
       //SHT20,Humidity,units:%
       Hum_SHT:((bytes[4]<<8 | bytes[5])/10).toFixed(1),
       
       //DS18B20,temperature,units:
       TempC_DS:
       {
         "1":((bytes[7]<<24>>16 | bytes[8])/100).toFixed(2),
       }[bytes[6]&0xFF],       
       
       //Exti pin level,PA4
       Exti_pin_level:
       {
         "4":bytes[7] ? "High":"Low",
       }[bytes[6]&0x7F], 
       
       //Exit pin status,PA4
       Exti_status:
       {
         "4":bytes[8] ? "True":"False",
       }[bytes[6]&0x7F],    
       
       //BH1750,illumination,units:lux
       ILL_lux:
       {
         "5":bytes[7]<<8 | bytes[8],
       }[bytes[6]&0x7F],  

        //ADC,PA4,units:V
        ADC_V:
       {
         "6":(bytes[7]<<8 | bytes[8])/1000,
       }[bytes[6]&0x7F],  
       
        //Exti count,PA4,units:times
        Exit_count:
        {
          "7":bytes[7]<<8 | bytes[8],
        }[bytes[6]&0x7F],  
        
        //Applicable to working mode 4,5,6 & 7,and working mode 4,6 & 7 requires short circuit PA9 and PA10
        No_connect:
        {
          "1":"Sensor no connection",
        }[(bytes[6]&0x80)>>7],  
  };
	return data;
}

Thanks pulidoj! take note of this too, the RFM95 devices I bought didn’t come with a user manual.
It’s also complicated to see arduino-lmic examples on the internet that include HTU21D, BMP280, etc sensors, as only LMiC-v1.5 seems to work fine on my pro mini arduinos, maybe I should try again with LMiC-v3.3, although this library seems too heavy for the pro mini, any advice on that!
If you have more examples of this kind, can send them to me.

Hi @paco

But for what you tell, I think that your problem is another one.
RFM95 is not a finished product that transmits a certain LoRa message, with a defined payload that you have to decode.
What I sent you (the same as @oscard1) is the decoding of that payload.

With Arduino and RFM95 is a development product where you have to build the information to transmit, you have to build the payload to send, that afterwards, when it arrives to the server or to de Application Server, has to be decoded to be used.

I guess you are looking for how to build this info, is it?

You’re right Jesus, I see that you understand me perfectly, this is where I think most of us get stuck, programming is a complicated issue… we’ll have to go little by little.
By the way! yesterday I tested again the LMiC-v3.3 and it works fine, a bit heavier than LMiC-v1.5, but not much more.
The test shows both of them working, one with LMiC-v1.5, and the second one with LMiC-v3.3.
I’m looking for how to build that information and it’s difficult.

application/2/device/0000000000000001/event/up {“applicationID”:“2”,“applicationName”:“1apli”,“deviceName”:“1dis”,“devEUI”:“AAAAAAAAAAE=”,“rxInfo”:[],“txInfo”:{“frequency”:868500000,“modulation”:“LORA”,“loRaModulationInfo”:{“bandwidth”:125,“spreadingFactor”:7,“codeRate”:“4/5”,“polarizationInversion”:false}},“adr”:true,“dr”:5,“fCnt”:13,“fPort”:1,“data”:“MDE=”,“objectJSON”:"{“mydata”:“01”}",“tags”:{},“confirmedUplink”:false,“devAddr”:“AQfBQw==”}
application/2/device/0000000000000002/event/up {“applicationID”:“2”,“applicationName”:“1apli”,“deviceName”:“2dis”,“devEUI”:“AAAAAAAAAAI=”,“rxInfo”:[],“txInfo”:{“frequency”:868500000,“modulation”:“LORA”,“loRaModulationInfo”:{“bandwidth”:125,“spreadingFactor”:7,“codeRate”:“4/5”,“polarizationInversion”:false}},“adr”:true,“dr”:5,“fCnt”:17,“fPort”:1,“data”:“MDI=”,“objectJSON”:"{“mydata”:“02”}",“tags”:{},“confirmedUplink”:false,“devAddr”:“ABuFaw==”}
^C
$ base64 -d <<< MDI= | hexdump -C
00000000 30 32 |02|
00000002

Great,

From what I see, it is nearly finished.
You can build your data as you want but I suggest you to do it like this, without passing the names of the variables you are sending.

For instance, suppose you want to pass the temperature and humidity of a sensor, just this.
Suppose we have the following values:

  • 32.4 ºC
  • 56.2 % HR

We are going to build our payload in this way:

  • 2 first bytes for temperature with 1 decimal
  • 2 last bytes for humidity with 1 decimal

Let’s convert the values to HEX:

  • 324 -> 01 44
  • 562 -> 02 0C
    So our payload in HEX will be: 0144020C

Now we convert it from HEX into Base64: AUQCDA==

This is what you have to send in your DATA variable of the message.

Afterthat, when it arrives at Chirstack you can decode it making the reverse way, and perhaps you made a user manual explaining that your 2 first bytes are temperature with 1 decimal and the last 2 are humidity with 1 decimal.

Thanks Jesus, but it seems to me that I’m further behind than you think, I think I understand it, but it’s another thing to put it into practice, hence the importance of complete examples or videos… .
I will have to start for example; adding to the arduino-lmic sketch the HTU21D sensor, this in particular is what I meant when I said that I understood… examples of these are not there, they are almost all made for other platforms such as the ESP82…
or they follow other paths that are not chirpstack.
I think the best there is on lorawan is chirpstack, so I’ll keep trying…
324 144
562 232 this is what I get…

If this is the case, it might be close to the target;
Just copy and paste the second one into the first one and that’s it. Obviously in an orderly way by this I mean all the libraries together up to the top, what is in the setup of the second one put it in the setup of the first one and the same with the loop.
1 single setup 1 single loop

I seem to be getting the hang of it… heh heh heh heh… no decimals in the data.
the data is repeated, “data”: “ABk9”, “objectJSON”:"{“H”:61, “T”:25}" or is that the way it is?
thanks pulidoj for the indications;

application/2/device/0000000000000001/event/up {“applicationID”:“2”,“applicationName”:“1apli”,“deviceName”:“1dis”,“devEUI”:“AAAAAAAAAAE=”,“rxInfo”:[],“txInfo”:{“frequency”:868500000,“modulation”:“LORA”,“loRaModulationInfo”:{“bandwidth”:125,“spreadingFactor”:7,“codeRate”:“4/5”,“polarizationInversion”:false}},“adr”:true,“dr”:5,“fCnt”:5,“fPort”:1,“data”:“ABk9”,“objectJSON”:"{“H”:61,“T”:25}",“tags”:{},“confirmedUplink”:false,“devAddr”:“AVRXrA==”}

frequency:868500000
modulation:“LORA”

bandwidth:125
spreadingFactor:7
codeRate:"4/5"
polarizationInversion:false

adr:true
dr:5
fCnt:5
fPort:1
data:“ABk9”
H:61
T:25
tags:

    confirmedUplink:false