From f873251887ee504561e80b33ef23f244d23cb6d4 Mon Sep 17 00:00:00 2001 From: DeflateAwning <11021263+DeflateAwning@users.noreply.github.com> Date: Thu, 22 Sep 2022 13:25:35 -0600 Subject: [PATCH] Add WioLoRaWANFieldTester decoder --- WioLoRaWANFieldTester/decoder.js | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 WioLoRaWANFieldTester/decoder.js diff --git a/WioLoRaWANFieldTester/decoder.js b/WioLoRaWANFieldTester/decoder.js new file mode 100644 index 0000000..3072361 --- /dev/null +++ b/WioLoRaWANFieldTester/decoder.js @@ -0,0 +1,41 @@ +function Decoder(bytes, port) { + // source: https://github.com/disk91/WioLoRaWANFieldTester/blob/master/doc/ConfigureCargo.md#setup-a-decoding-function + + var decoded = {}; + + if ( port != 1 ) return decoded; + + var lonSign = (bytes[0]>>7) & 0x01 ? -1 : 1; + var latSign = (bytes[0]>>6) & 0x01 ? -1 : 1; + + var encLat = ((bytes[0] & 0x3f)<<17)+ + (bytes[1]<<9)+ + (bytes[2]<<1)+ + (bytes[3]>>7); + + var encLon = ((bytes[3] & 0x7f)<<16)+ + (bytes[4]<<8)+ + bytes[5]; + + var hdop = bytes[8]/10; + var sats = bytes[9]; + + const maxHdop = 2; + const minSats = 5; + + if ((hdop < maxHdop) && (sats >= minSats)) { + // Send only acceptable quality of position to mappers + decoded.latitude = latSign * (encLat * 108 + 53) / 10000000; + decoded.longitude = lonSign * (encLon * 215 + 107) / 10000000; + decoded.altitude = ((bytes[6]<<8)+bytes[7])-1000; + decoded.accuracy = (hdop*5+5)/10 + decoded.hdop = hdop; + decoded.sats = sats; + } else { + decoded.error = "Need more GPS precision (hdop must be <"+maxHdop+ + " & sats must be >= "+minSats+") current hdop: "+hdop+" & sats:"+sats; + } + + return decoded; +} +