Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

ByteArray to String Problem
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

ByteArray to String Problem

by dsheeran on Sun May 03, 2020 2:11 pm

I am using a Metro M4 Express with a RFM69 radio transceiver to receive commands from a custom built remote control that is also using a RFM69 to send commands.
I am getting responses for only 1 type of command (instead of about 14 different possibilities) but that could be for several reasons.

My question today is how to convert the result of the command received into a string so I can take action on it.

The received string looks like this....

bytearray(b'\x06\x89\xb4\xe9\x92\x0b\xde\x9aH\x19O\x9b')

I cannot seem to decode it. I've used the example in the tutorial for the RFM69 without success. I get a UNICODE exception for that.

I'm attaching a picture of Mu where I get the exception along with the relevant code.

Please help.

byte array.png
byte array.png (64.43 KiB) Viewed 18 times

dsheeran
 
Posts: 134
Joined: Wed Aug 02, 2017 1:20 pm

Re: ByteArray to String Problem

by danhalbert on Sun May 03, 2020 2:42 pm

It looks like you expect the returned bytearray to be alphabetic letters (e.g., like "safety". However, the example you gave is a mix of non alphanumeric or even printing characters.

So I think something else is going wrong, or the remote control is not actually sending readable text back: it's sending some encoded form.

If you just print(packet), what do you see?

You can always do: ` if somebytes == b'safety' `, but that doesn't appear to be what is coming back anyway.

danhalbert
 
Posts: 2056
Joined: Tue Aug 08, 2017 12:37 pm

Re: ByteArray to String Problem

by dsheeran on Sun May 03, 2020 2:55 pm

Strange. The same approach worked in Arduino code. The remote is sending text like "safety".

When I do as you suggested and print(packet) I get this back...

bytearray(b'\x86\x89\xb4\xe9\x92\x0b\xde\x9a\xc8\x99\xcf\x9b')

And other ideas?

dsheeran
 
Posts: 134
Joined: Wed Aug 02, 2017 1:20 pm

Re: ByteArray to String Problem

by danhalbert on Sun May 03, 2020 3:43 pm

Could you post the Arduino code you are using? There seems to be some difference between what it is receiving and what rfm69.receive() is returning.

danhalbert
 
Posts: 2056
Joined: Tue Aug 08, 2017 12:37 pm

Re: ByteArray to String Problem

by dsheeran on Sun May 03, 2020 4:10 pm

Got it working!

It was the encryption key. It is set in the remote but I hadn't seen it in the tutorial until just now. I set it in the receiver code and voila! Everything is working as designed / expected.

Thanks!

dsheeran
 
Posts: 134
Joined: Wed Aug 02, 2017 1:20 pm

Re: ByteArray to String Problem

by danhalbert on Sun May 03, 2020 4:50 pm

Great! Glad you got it working!

danhalbert
 
Posts: 2056
Joined: Tue Aug 08, 2017 12:37 pm

Please be positive and constructive with your questions and comments.