0

I am trying to send values from a esp32 on my network to my RPi via mqtt, which then evaluates these values and does some stuff with it.

I've written the code and tried it on my pc without any problems, but when executing the exact same code on the RPi it starts spitting out ValueErrors.

payload = str(message.payload)
payload = float(payload[2:-1])

When running the code like this, it says:

ValueError: could not convert string to float:

Trying to convert it to an integer first also returns an error:

ValueError: invalid literal for int() with base 10: ''

(The [2:-1] is needed because the payload/string that is to be converted is always received as b'payload')

It doesn't throw out any errors when used on the pc. I have also tried printing out the strings before converting them, which showed that they only consist of numbers.

Im running Python3.4 on both my pc and the RPi (tried 2.7 as well, didn't work.)

I hope anyone can explain this weird behaviour to me, thanks in advance.

0

1 Answer 1

1
  1. Slicing is not the correct way to convert a bytes array to str.

  2. string[2:-1] returns an empty string for any string shorter than 3 characters.

As a matter of fact, both int and float accept bytes arrays that represent numbers, so you shouldn't even bother.

float(message.payload) should work (in case it's not empty and represents a number of course).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.