xtaran / ttn-console-adeunis-lorawan-ftd-decode

TheThingsNetwork Console Decoder for Adeunis LoRaWAN Field Test Devices
MIT License
1 stars 4 forks source link

Wrong element parsing #1

Open ddewaele opened 6 years ago

ddewaele commented 6 years ago

This may be related to the 2 payload formats supported by the device

Looking at the Adeunis Payload docs, I noticed for example that temperature is located in the second byte of the payload, and a simple decimal conversion should show the temperature in celcius.

However, I was a bit confused by the parsing as it seems to do something different :

Sample 1 : Temperature should be 28 (hex:1C), not -98

[ 142, 28, 0, 0, 16, 102 ]
{ payload_decrypt: '8e1c001066',
  ranger: 
   { status: 
      { temperature: 1,
        trigger_acc: 0,
        trigger_button: 0,
        gps: 0,
        up_ctr: 1,
        dn_ctr: 1,
        battery: 1 },
     T: -98,
     up: 0,
     dn: 16,
     battery: 102 } }

Sample 2 : Temperature should be 31 (hex:1F), not -124

[ 191, 31, 71, 34, 130, 16, 0, 131, 38, 16, 22, 1, 1, 16, 87, 38, 8 ]
{ payload_decrypt: 'bf1f47228210083261016111057268',
  ranger: 
   { status: 
      { temperature: 1,
        trigger_acc: 0,
        trigger_button: 1,
        gps: 1,
        up_ctr: 1,
        dn_ctr: 1,
        battery: 1 },
     T: -124,
     gps: '4722821008326101',
     lat: 47.38035,
     lon_d: 8,
     lon_m: 32,
     lon_s: 61,
     lon_dir: 'E',
     lon: 8.5435,
     up: 22,
     dn: 17,
     battery: 2135 } }

Also on TTN I'm getting weird results with payload 8F1903030E740B07

{
  "payload_decrypt": "8f1933e74b7",
  "ranger": {
    "T": -78,
    "battery": 607,
    "dn": 231,
    "status": {
      "battery": 1,
      "dn_ctr": 1,
      "gps": 0,
      "temperature": 1,
      "trigger_acc": 0,
      "trigger_button": 0,
      "up_ctr": 1
    },
    "up": 51
  }
}
xtaran commented 6 years ago

Now that went fast. I didn't even announce the repo anywhere yet and got the first bug report. :-)

I currently have my device in the office, so I can't check before next week which setting it has.

But chances are indeed high that the code so far only supports one of these modes: I've written my modifications for the code a few months ago and mostly modified the way the functions are called and the syntax to make it run in Otto and work with the TTN Console API. And since it IIRC gave sane values for my device, I didn't much bother about potential other use cases. (And I haven't touched the actual code since then except for adding the README file…)

ddewaele commented 6 years ago

Once it's out there it's out there right ? :)

Perhaps you can get some inspiration from https://github.com/claudyus/node-red-contrib-adeunis/blob/master/adeunis-rf.js . That seems to be working on my device. If you need help testing let me know !

ChaDup commented 5 years ago

I also had this problem with temperature.

I figured out that the Hex2Bin function was not working, as it gave wrong binary output. This what I used instead: function Hex2Bin(n){if(!checkHex(n))return 0; return (parseInt(n, 16).toString(2)).padStart(8, '0');}

Also the longitude was incorrect because the degrees are on 3 bites and not 2. gps quality signal, satelites number, rssi et snr were also missing from the payload.

This how i modified the end of code to get the right longitude and other outputs.

        if(payload.data.status.gps){
            // Get latitude
            payload.data.gps = str.substr(i/4,16);
            var lat_d = parseInt(str.substr(i/4,2));
            i += 8;
            var lat_m = parseInt(str.substr(i/4,2));
            i += 8;
            var lat_s = (parseInt(str.substr(i/4,3)));
            i += 12;
            payload.data.lat_d = lat_d;
            payload.data.lat_m = lat_m;
            payload.data.lat_s = lat_s;
            var lat_sign = (str.substr(1/4,1)=="1" ? -1 : 1);
            payload.data.lat_dir = (lat_sign == -1 ? "S" : "N");
            i += 4;
            payload.data.lat = lat_sign*(lat_d+(lat_m/60)+(lat_s/60000));

            // Get longitude
            var lon_d = parseInt(str.substr(i/4,3));
            i += 12;
            var lon_m = parseInt(str.substr(i/4,2));
            i += 8;
            var lon_s = parseInt(str.substr(i/4,2));
            i += 8;
            payload.data.lon_d = lon_d;
            payload.data.lon_m = lon_m;
            payload.data.lon_s = lon_s;
            if(typeof lon_s!=="number") lon_s = 0;
            if(typeof lon_m!=="number") lon_m = 0;
            if(typeof lon_d!=="number") lon_d = 0;
            var lon_sign = (str.substr(i/4,1)=="1" ? -1 : 1);
            payload.data.lon_dir = (lon_sign == -1 ? "W" : "E");
            i += 4;
            payload.data.lon = lon_sign*(lon_d+(lon_m/60)+(lon_s/6000));

            var gps_qual = Bin2Dec(Hex2Bin(str.substr(i/4,1)));
            if (gps_qual == 1 ) gps_qual = "good";
            if (gps_qual == 2 ) gps_qual = "average";
            if (gps_qual == 3 ) gps_qual = "poor";
            payload.data.gps_qual = gps_qual;
            i += 4;

            payload.data.sat_num = Bin2Dec(Hex2Bin(str.substr(i/4,1)));
            i += 4;

        }
        if(payload.data.status.up_ctr){
            payload.data.up = Bin2Dec(Hex2Bin(str.substr(i/4,2)));
            i += 8;
        }
        if(payload.data.status.dn_ctr){
            payload.data.dn = Bin2Dec(Hex2Bin(str.substr(i/4,2)));
            i += 8;
        }
        if(payload.data.status.battery){
            payload.data.battery = Bin2Dec(Hex2Bin(str.substr(i/4,4)));
            i += 16;
        }
        if(payload.data.status.rssiSnr){
            payload.data.rssi = Bin2Dec(Hex2Bin(str.substr(i/4,2)))*-1;
            i += 8;
            payload.data.snr = Bin2Dec(Hex2Bin(str.substr(i/4,2)));
            i += 8;
        }
        return payload;
    }