How to convert cayenne.getBuffer() to payload string in MicroPython

As I am quite new in MicroPython, my MicroPython program is as following:

from cayennelpp import CayenneLPP
cayenne = CayenneLPP()
cayenne.addTemperature(1, 23.54)
print (cayenne.getBuffer())

The print shows
bytearray(b’\x01g\x00\xeb’)

How to convert cayenne.getBuffer() to payload string like 01670164?

How to convert cayenne.getBuffer() to payload string like 01670164?

Assuming you only want this for debugging, then one of the many ways to display binary values in the human readable hexadecimal format:

import binascii
print binascii.hexlify(cayenne.getBuffer())

For MicroPython, you’d use ubinascii.

However…

How to convert cayenne.getBuffer() to payload string

If you want to convert this in order to send the payload, then: don’t. LoRaWAN payloads are not sent as human readable hexadecimal text. You should pass the binary result of cayenne.getBuffer() to whatever LoRaWAN library you’re using. For your example that will send 4 bytes, not 8.

Thank arjanvanb. You are my hero again. Your suggestion can solve my problem.
My use case is to put cayenne.getBuffer() in my AT command string.
It works like a charm now.

1 Like