Decoding a float value in a payload function


(Jonathanmf) #21

Hi !

I have some difficult with the payload function.
Here is my Payload: 32352E3736302E32
This is for a DHT22 sensor. When i use python I can read values:

>>> binascii.unhexlify('32352E37')
b'25.7'
>>> binascii.unhexlify('36302E32')
b'60.2'

But I can’t make readable on the decoder. What can I do I tried … a lot !

thx (and sorry for my english)


(Arjan) #22

That’s decoding text (where the hexadecimal 0x32 is the character 2, 0x35 is 5, 0x2E is the dot, and 0x37 is 7). But your node should not send text as that needs 8 bytes for just two values with a very limited range.

Still then, while you’ve not changed your node’s code to use a better encoding, and assuming that always 4 characters per value are used, so if:

  • no negative values are sent
  • zero is sent as 00.0, or as 0.0 with a leading space
  • values below 10 are sent with a leading space or zero, like 08.1
  • integer values are sent with a fractional part, like 12.0

…then you could decode the text using:

function Decoder(bytes, port) {
  // test with 32352E3736302E32
  var text1 = String.fromCharCode.apply(null, bytes.slice(0, 4));
  var text2 = String.fromCharCode.apply(null, bytes.slice(4, 8));
    
  return {
    text1: text1,
    float1: parseFloat(text1),
    text2: text2,
    float2: parseFloat(text2)
  }
}

…which will get you:

{
  "float1": 25.7,
  "float2": 60.2,
  "text1": "25.7",
  "text2": "60.2"
}

(Jonathanmf) #23

Oh I understand, so I need to optimise the code. I’m totally “noob” I need to learn. Thanks for your explanation.

BTW, here is the code :slight_smile:

void onEvent (ev_t ev) {
    Serial.print(os_getTime());
    Serial.print(": ");
    switch(ev) {
        case EV_SCAN_TIMEOUT:
            Serial.println(F("EV_SCAN_TIMEOUT"));
            break;
        case EV_BEACON_FOUND:
            Serial.println(F("EV_BEACON_FOUND"));
            break;
        case EV_BEACON_MISSED:
            Serial.println(F("EV_BEACON_MISSED"));
            break;
        case EV_BEACON_TRACKED:
            Serial.println(F("EV_BEACON_TRACKED"));
            break;
        case EV_JOINING:
            Serial.println(F("EV_JOINING"));
            break;
        case EV_JOINED:
            Serial.println(F("EV_JOINED"));
            break;
        case EV_RFU1:
            Serial.println(F("EV_RFU1"));
            break;
        case EV_JOIN_FAILED:
            Serial.println(F("EV_JOIN_FAILED"));
            break;
        case EV_REJOIN_FAILED:
            Serial.println(F("EV_REJOIN_FAILED"));
            break;
        case EV_TXCOMPLETE:
            Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
            if (LMIC.txrxFlags & TXRX_ACK)
              Serial.println(F("Received ack"));
            if (LMIC.dataLen) {
              Serial.println(F("Received "));
              Serial.println(LMIC.dataLen);
              Serial.println(F(" bytes of payload"));
            }
            // Schedule next transmission
            os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
            break;
        case EV_LOST_TSYNC:
            Serial.println(F("EV_LOST_TSYNC"));
            break;
        case EV_RESET:
            Serial.println(F("EV_RESET"));
            break;
        case EV_RXCOMPLETE:
            // data received in ping slot
            Serial.println(F("EV_RXCOMPLETE"));
            break;
        case EV_LINK_DEAD:
            Serial.println(F("EV_LINK_DEAD"));
            break;
        case EV_LINK_ALIVE:
            Serial.println(F("EV_LINK_ALIVE"));
            break;
         default:
            Serial.println(F("Unknown event"));
            break;
    }
}

void do_send(osjob_t* j){
    byte buffer[8];
    float t = dht.readTemperature();
    float h = dht.readHumidity();
    dtostrf(t, 2, 1, buffer);
    dtostrf(h, 2, 1, &buffer[4]);
    String res = buffer;
    res.getBytes(buffer, res.length() + 1);
    Serial.println("");
    Serial.print("Sending - temperature: ");
    Serial.print(t);
    Serial.print(", humidity: ");
    Serial.print(h);
    Serial.println("");

    // Check if there is not a current TX/RX job running
    if (LMIC.opmode & OP_TXRXPEND) {
        Serial.println(F("OP_TXRXPEND, not sending"));
    } else {
        // Prepare upstream data transmission at the next possible time.
        LMIC_setTxData2(1, (uint8_t*) buffer, res.length(), 0);
        Serial.println(F("Packet queued"));
    }
    // Next TX is scheduled after TX_COMPLETE event.
}

(Arjan) #24

This goes horribly wrong for any value that does not convert to exactly four characters. (If you found that code elsewhere, please refer the author to this answer…)

Instead, use something like explained above:

// Read sensor values and multiply by 100 to effectively keep 2 decimals
// Signed 16 bits integer, -32767 up to +32767
int16_t t = dht.readTemperature() * 100;
// Unsigned 16 bits integer, 0 up to 65535
uint16_t h = dht.readHumidity() * 100;

This can be encoded into just 4 bytes for the 2 values, using some “shifting” as explained in more detail in Decrypting messages for dummies:

byte buffer[4];
buffer[0] = t >> 8;
buffer[1] = t;
buffer[2] = h >> 8;
buffer[3] = h;
LMIC_setTxData2(1, buffer, sizeof(buffer), 0);

And decoded like also explained in more detail in the link above:

function Decoder(bytes, port) {
  var t = (bytes[0] & 0x80 ? 0xFFFF<<16 : 0) | bytes[0]<<8 | bytes[1];
  var h = bytes[2]<<8 | bytes[3];
  return {
    temperature: t / 100,
    humidity: h / 100
  }
}

You could probably even send humidity without any decimals, saving yet another byte:

// Unsigned 8 bits integer, 0 up to 255
uint8_t h = dht.readHumidity();

…with:

byte buffer[3];
buffer[0] = t >> 8;
buffer[1] = t;
buffer[2] = h;
LMIC_setTxData2(1, buffer, sizeof(buffer), 0);

…and:

function Decoder(bytes, port) {
  var t = (bytes[0] & 0x80 ? 0xFFFF<<16 : 0) | bytes[0]<<8 | bytes[1];
  var h = bytes[2];
  return {
    temperature: t / 100,
    humidity: h
  }
}

(Jonathanmf) #25

Work Perfectly (with the optimized version for humidity) ! Big thank for all your explanations ! Clear and working. I’m getting smarter today :stuck_out_tongue:


(thanasis Kotsiopoulos) #26

Any ideas i could do what you are describing?
Let me first tell you that i use loriot server instead of ttn.
I tried to connect loriot backend with node red and ibm bluemix but i am struggling with javascript.
So any ideas about decoding with python and then connecting to an application server?


#27

and you asked questions on the TTN forum ? … strange :tired_face:


#29

your welcome :wink:


(Arjan) #30

Just for future reference, another option to decode an IEEE-754 floating point in Decode float sent by Lopy as Node.


(Brady Aiello) #31

I pulled my answer from here:

So my payload function, making up for the ATSAMD21G18’s opposite endianness looks like this:


(Mat Lynx) #33

This was very helpful! Thanks.


#34

Thanks to @arjanvanb we updated our payload decoder code:
https://github.com/KELLERAGfuerDruckmesstechnik/KellerAgTheThingsNetworkPayloadDecoder