Downlink from TTN to LoPy4 - Buffer too small error?

Guys,
Apologies for yet another post. I figured it would be best to put this in a seperate thread.

I want to be able to send a byte from TTN to my LoPy4, read it and then process it.

My code on the LoPy4 follows the Core-Electronics example:

if len(rx_pkt) > 0:
    print("Downlink data on port 200:", rx_pkt)
    print('length:', len(rx_pkt))
    print("Unpacked Downlink_packet is:", ustruct.unpack('f',rx_pkt)[0])   # This is the line that errors out
    pycom.rgbled(0xffa500)
#    #input("Downlink recieved, press Enter to continue")
    utime.sleep(3)
    pycom.heartbeat(False)

On the TTN website I can send a Downlink byte of 01, it schedules it and sends. The LoPy4 receives it and displays the following in the REPL.

In short, its the ustruct.unpack() that Im struggling with - but after all I learnt previously I have no idea why. Why is a buffer too small?

My TTN encoder is left unchanged from Default, ie:

function Encoder(object, port) {
  // Encode downlink messages sent as
  // object to an array or buffer of bytes.
  var bytes = [];

  // if (port === 1) bytes[0] = object.led ? 1 : 0;

  return bytes;
}

Im wondering if this could be the problem? Although if it is, again I do not know why because I am getting what I expect on the REPL before decoding.

Thoughts please.

Thanks
Jon

Aren’t you asking struct.unpack(‘f’) to unpack a float? A float is bigger than a byte, so the buffer is too small. Unpack a byte or a char.

https://docs.python.org/3/library/struct.html

1 Like

Thanks. I think I was looking at it the wrong way round, thinking that a float was bigger than the value sent and therefore thinking it should fit into it.

I’ll try later with a ‘c’ and b’ instead. I only need to send a 0 or 1 (and maybe a 2, which would rule out Boolean!)

Thx
Jon

I will point out that the struct.unpack(‘b’) is a Boolean and I don’t know if a ‘c’ is valid. The link I posted does have table of valid values. :grin:

Thanks Robin, from the table of values in the link you sent, ‘c’ is a “char” of 1 byte, ‘b’ is a “signed char” of 1 byte and ‘?’ is Boolean, again 1 byte in length.

A quick try suggested that ‘c’ and ‘?’ do not work, ‘b’ allows 00 through 09 to be sent and decoded correctly.

Thanks for your assistance / pointing me in the right direction.

R
Jon

1 Like

You’re welcome Jon. Looks like the limited MicroPython documentation is wrong. ‘c’ is not listed as a valid format. Either that or I’m going blind, or senile, maybe both…