Handlers, Erlang, and hex strings...

165 views
Skip to first unread message

co...@perceptivethings.com

unread,
Jan 25, 2018, 5:30:29 PM1/25/18
to LoRaWAN Server Users
I've been having a difficult time getting my Multitech mDot's data to parse in a handler.  I get consistent parse_failed errors and I don't understand why.  Specifically, the payload is 18 bytes, but the values are really 9 bytes, with each ASCII character being represented with two bytes as a hexadecimal value (i.e. 0x31 = 1, 0x32 = 2, etc).  I think this is a "hex string" in Erlang?  When converted to ASCII characters, first byte is a boolean ("1" or "0"), the next four bytes are a float temperature reading, and the last four are a float battery voltage reading.  As I mentioned, the values are sent as hexadecimal ASCII characters represented.  For example, a payload of "3132352E31332E3334" is...

3132352E31332E3334 --> 31 34 35 2E 31 33 2E 33 34 --> 125.13.34 --> 1, 25.1, 3.34

so my resulting object would look like:

{sensor: 1, temp: 25.1,  batt: 3.34}

I've tried all sorts of ways to parse the data, and the most promising in my mind is this function:

fun(Fields, <<Data/binary>>) ->
Fields#{sensor => binary_part(Data, {0,1}), temp => binary_part(Data, {2,4}), batt => binary_part(Data, {8,4})}
end.

This should just pull the ASCII characters out of the binary based on position...like a substring.  Unfortunately, I only get "badarg" errors from this function, but I'm at a loss as to why. This may not be the ideal way of doing it, but my other attempt based on the documentation always fails with a "function_clause" parse error:

fun(Fields, <<SENSOR, Temp:4/binary, Voltage:4/binary>>) ->
Fields#{sensor => SENSOR, temp => Temp, batt => Voltage}
end.

Any help would be appreciated!


Petr Gotthard

unread,
Jan 26, 2018, 5:04:21 AM1/26/18
to co...@perceptivethings.com, LoRaWAN Server Users

First of all, are you using the release 0.4.x or 0.5? The documentation under ‘master’ branch is for 0.5; if you have 0.4.x you need to switch to ‘stable’.

 

If your data is as you say, then <<Sensor:1/binary, Temp:4/binary, Voltage:4/binary>> shall work. The difference to your last function is that “Sensor” is a binary too.

 

 

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/15911b85-50f3-47ab-b0e7-2508d976bb81%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

co...@perceptivethings.com

unread,
Jan 29, 2018, 1:05:23 PM1/29/18
to LoRaWAN Server Users
We're testing 0.5, but your solution worked!  Changing the first field to "Sensor:1/binary" seems to have done the trick. Thanks for your help and amazing work on this project.
Reply all
Reply to author
Forward
0 new messages