LoRaWAN adds \n to binary value in JSON

30 views
Skip to first unread message

Toni Juola

unread,
Jun 20, 2019, 11:25:25 AM6/20/19
to LoRaWAN Server Users
Hi!

I was just wondering why LoRaWAN adds "\n" this to start of binary value in JSON ? How to overcome this? I am pretty sure there is some easy workaround but just can't figure out what it is.

Example: Binary value is "0000" but in JSON as it is sent from connector to application it is "\n0000"

If someone could figure this out I would be very grateful. Thanks in advance!

Br,

Stonde

Petr Gotthard

unread,
Jun 20, 2019, 11:33:16 AM6/20/19
to Toni Juola, LoRaWAN Server Users

Hi. I am not aware of this. Could you share the entire JSON please?

 

Petr

--
You received this message because you are subscribed to the Google Groups "LoRaWAN Server Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lorawan-serve...@googlegroups.com.
To post to this group, send email to lorawan...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lorawan-server/4c6125e3-d83b-4457-bae1-c9712642f630%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Toni Juola

unread,
Jun 20, 2019, 12:26:20 PM6/20/19
to LoRaWAN Server Users
Hi!

Here (relevant parts bolded) :

Handler:

fun(#{port := 46}=Fields, <<Test:1/binary>>) ->
        Fields#{ data => #{ test => [#{ value => Test }]}};
     (Fields, _) ->
        Fields
end. 

JSON:

{"data":{"test":[{"value":"\u0000"}]}} 

Same happens with every value which is /binary.

PS. Only left the relevant code for the case here. For that part the received hex is 0000 from the LoRaWAN device.

Br,

Stonde

torstai 20. kesäkuuta 2019 18.33.16 UTC+3 Petr Gotthard kirjoitti:

Hi. I am not aware of this. Could you share the entire JSON please?

 

Petr

 

From: lorawan...@googlegroups.com [mailto:lorawan...@googlegroups.com] On Behalf Of Toni Juola
Sent: 20 June 2019 17:25
To: LoRaWAN Server Users <lorawan...@googlegroups.com>
Subject: LoRaWAN adds \n to binary value in JSON

 

Hi!

 

I was just wondering why LoRaWAN adds "\n" this to start of binary value in JSON ? How to overcome this? I am pretty sure there is some easy workaround but just can't figure out what it is.

 

Example: Binary value is "0000" but in JSON as it is sent from connector to application it is "\n0000"

 

If someone could figure this out I would be very grateful. Thanks in advance!

 

Br,

 

Stonde

--
You received this message because you are subscribed to the Google Groups "LoRaWAN Server Users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to lorawan...@googlegroups.com.

Toni Juola

unread,
Jun 20, 2019, 12:28:12 PM6/20/19
to LoRaWAN Server Users
Oh, and ofc adds \u. Midsummer Eve here now ;)

-Stonde

Petr Gotthard

unread,
Jun 20, 2019, 12:39:19 PM6/20/19
to Toni Juola, LoRaWAN Server Users

It’s a UNICODE character, thus ‘\uXXXX’.

You are likely inserting a non-printable binary value to a JSON, which is textual.

 

The default Uplink Fields get converted automatically, but if you want to have own field with a binary value, you need to convert it yourself using the “lorawan_utils:binary_to_hex” function, e.g.:

 

fun(#{port := 46}=Fields, <<Test:1/binary>>) ->

        Fields#{ data => #{ test => [#{ value => lorawan_utils:binary_to_hex(Test) }]}};

     (Fields, _) ->

        Fields

end.

 

Alternatively, if it’s a binary-encoded number (integer or float), not a binary value, parse it as a number, not a binary value. See Section 4.3 in http://erlang.org/documentation/doc-6.4/doc/programming_examples/bit_syntax.html. For example:

 

fun(#{port := 46}=Fields, <<Test:8>>) ->

        Fields#{ data => #{ test => [#{ value => Test }]}};

     (Fields, _) ->

        Fields

end.

 

 

Regards,

To unsubscribe from this group and stop receiving emails from it, send an email to lorawan-serve...@googlegroups.com.


To post to this group, send email to lorawan...@googlegroups.com.

Petr Gotthard

unread,
Jun 20, 2019, 1:08:10 PM6/20/19
to Toni Juola, LoRaWAN Server Users

You likely send 8 bits, which is represented by 2 hex-digits. In which encoding you want these 4 digits?

 

It’s it’s bits, why don’t you parse it as bits? The Erlang binary parsing is super-powerful. If you are getting 1 byte (8 bits), where the first 5 bits are useless and then the last 3 bits indicate what you said, then you can do:

 

fun(#{port := 46}=Fields, <<_:5,Batt:1,Temp:1,Smoke:1>>) ->

        Fields#{ data => #{ test => [#{ batt  => Batt, temp => Temp, smoke => Smoke }]}};

     (Fields, _) ->

        Fields

end.

 

 

Petr

 

From: Toni Juola [mailto:juol...@gmail.com]
Sent: 20 June 2019 18:56
To: Petr Gotthard <petr.g...@centrum.cz>
Subject: Re: LoRaWAN adds \n to binary value in JSON

 

Or hmmmmm.... not completely. I need this "0000" as straight "0000". This converts it to: "00" and the other one to plain "0"

 

Here some description:

 

Bit 0: smoke alarm status: bit0=1 represents there is a smoke alarm, bit0=0 represents there is no smoke alarm

Bit 1: temperature alarm status, bit1=1 represents there is high temperature alarm, bit1=0 represents there is no temperature alarm

Bit 2: low battery voltage status, bit2=1 represents there is a low battery level alarm, bit2=0 represents there is no battery level  alarm

 

The handling for the bits are done in the receiving side.

 

Br,

 

Stonde

 

to 20. kesäk. 2019 klo 19.48 Toni Juola (juol...@gmail.com) kirjoitti:

Thank you again Petr. You are the greatest. This one solved the issue:  lorawan_utils:binary_to_hex(Test)

 

-Toni

 

Petr Gotthard

unread,
Jun 20, 2019, 1:36:42 PM6/20/19
to Toni Juola, LoRaWAN Server Users

So you are pretty advanced, I didn’t know, sorry J

 

lorawan_utils:binary_to_hex() encodes X bytes to 2X hexa-digits, so if you want 4 digits, you need to send 2 bytes

if you want “00XX”, where 00 is fixed and XX are hex-digits based on this 1-byte you can do

lorawan_utils:binary_to_hex(<<0,Test/binary>>)

which prepends additional byte before the 1-byte Test, so it will produce 2*(n+1) hexa-digits, where n is the number of bytes in Test

 

Am I missing something?

 

 

Petr

 

From: Toni Juola [mailto:juol...@gmail.com]
Sent: 20 June 2019 19:23
To: Petr Gotthard <petr.g...@centrum.cz>
Subject: Re: LoRaWAN adds \n to binary value in JSON

 

The reason why I dont parse as plain bits is because I need only some values as bits. And for those bit values I provide extra field binary that the receiving end knows that it needs its own parsing in the receiving side.

 

This is the complete handler structure currently:

 

fun(#{port := 46}=Fields, <<16#0101:16, Smoke:8, Temp:16/big-signed-integer, Hum:8/unsigned-integer, Bat:8, Alarm:1/binary, Fault:1/binary, Pollution:8, Wvol:8>>) ->        

        Fields#{ sensor => 'AN-102C', data => #{smoke => [#{ index => 0, value => (Smoke/100), unit => 'dBm' }], temperature => [#{ index => 0, value => (Temp/100), unit => 'celsius' }], battery => [#{ index => 0, value => Bat, unit => 'percent' }], alarm => [#{ index => 0, value => Alarm, unit => 'binary' }], fault => [#{ index => 0, value => Fault, unit => 'binary' }], test => [#{ index => 0, value => 'null', unit => 'binary' }]}};
(Fields, <<16#0102:16, Test:1/binary>>) ->
        Fields#{ sensor => 'AN-102C', data => #{test => [#{ index => 0, value => Test, unit => 'binary' }], smoke => [#{ index => 0, value => 'null', unit => 'dBm' }], temperature => [#{ index => 0, value => 'null', unit => 'celsius' }], battery => [#{ index => 0, value => 'null', unit => 'percent' }], alarm => [#{ index => 0, value => 'null', unit => 'binary' }], fault => [#{ index => 0, value => 'null', unit => 'binary' }]}};
(Fields, <<16#0103:16, Alarm:1/binary, Fault:1/binary, Smoke:8, Temp:16/big-signed-integer, Hum:8/unsigned-integer, Bat:8, Pollution:8>>) ->
        Fields#{ sensor => 'AN-102C', data => #{alarm => [#{ index => 0, value => Alarm, unit => 'binary' }], fault => [#{ index => 0, value => Fault, unit => 'binary' }], smoke => [#{ index => 0, value => (Smoke/100), unit => 'dBm' }], temperature => [#{ index => 0, value => (Temp/100), unit => 'celsius' }], battery => [#{ index => 0, value => Bat, unit => 'percent' }], test => [#{ index => 0, value => 'null', unit => 'binary' }]}};


     (Fields, _) ->
        Fields
end.

 

All would be perfect without the "\n" at the start of the binary values when sending forward as JSON. For example for the test it creates JSON like this:

 

{"data":{"alarm":[{"index":0,"unit":"binary","value":null}],"battery":[{"index":0,"unit":"percent","value":null}],"fault":[{"index":0,"unit":"binary","value":null}],"smoke":[{"index":0,"unit":"dBm","value":null}],"temperature":[{"index":0,"unit":"celsius","value":null}],"test":[{"index":0,"unit":"binary","value":"\u0000"}]},"datetime":"2019-06-20T17:21:51Z","deveui":"FFFFFF10000074BA","port":46,"sensor":"AN-102C"}  

 

The only problem bolded. This is from the test button of the sensor.

 

-Stonde

Reply all
Reply to author
Forward
0 new messages