client.publish error message "TypeError: object of type 'float' has no len()" #10919
Replies: 7 comments
-
temp_f, press_Hg and humd are floating point variables. Mqtt publish wants a bytearray, so you should pass |
Beta Was this translation helpful? Give feedback.
-
Drop-in replacement: def publish(topic, value):
if not isinstance(value, bytes):
value = str(value).encode()
print(f"{topic} -> {value}")
client.publish(topic, value)
print("Publish Done") It's also possible to use struct to send int, float, etc. def struct_publish(topic, value):
if isinstance(value, float):
data = struct.pack("<f", value) # float, 4 bytes
elif isinstance(value, int):
data = struct.pack("<i", value) # integer, 4 bytes
else:
data = str(value).encode("ascii")
print(f"{topic} -> {value}")
client.publish(topic, value)
print("Publish Done") |
Beta Was this translation helpful? Give feedback.
-
Excellent, that did it, both the way @bixb922 explained and the first example from @sosi-deadeye. I knew it had to do with the variables being floats so I had tried making them integers, but it didn't like that either, I didn't think about making it a string. One last question, I want to format the value I'm sending to mqtt as a float with 2 decimal places. I tried using the format I have in displaying the full reading, reading = 'Temperature: ' + f'{(temp_f): .2f}' but I've tried it several ways, mainly because I'm not sure of the proper way to configure that, and it's not working. Would I use this method or is there a different way? Thanks for the help and the education. |
Beta Was this translation helpful? Give feedback.
-
What problem is there when you say it is not working? F-strings are a good way to format, you can shorten to: If you need to send more complex data, there is a good review of serialization here (3.2): https://github.com/peterhinch/micropython-samples |
Beta Was this translation helpful? Give feedback.
-
Sorry I should have been clearer, using the format in the reading line of "f'{(temp_f): .2f}'" I replaced the temp_f with the string conversion to this "publish('swarm/temp', f'{(str(temp_f).encode()): .2f}')" and I get the following error File "", line 117, in obviously that is not the correct way. I tried the same format and set it to a second variable to publish but I get the same error. Thanks |
Beta Was this translation helpful? Give feedback.
-
It's either Both str and the F-string convert to string. Once you have the string, you encode it.
|
Beta Was this translation helpful? Give feedback.
-
Perfect, I had tried it with just the temp_f previously but I think I had the .encode() and the.2F reversed. It's hard trying to figure this stuff out on your own when all you have is some Google searches and YouTube videos. Although I've learnt a lot that way, I always get great help from the forums. Thanks again, greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a pico w with a BME280, a CMPS12 compass and a OLED display, I can read the sensors and display the values on the OLED.
I'm now trying to publish the values to a MQTT server. When I run the code I get the error message
connected
ip = 192.168.1.163
Temperature: 69.48 Humidity: 27.56 Pressure: 29.94
heading= 207.2
swarm/temp
69.476
Traceback (most recent call last):
File "", line 108, in
File "", line 56, in publish
File "/lib/umqtt/simple.py", line 112, in publish
TypeError: object of type 'float' has no len()
Here is the code
Why do I get that error?
Thanks
John
Beta Was this translation helpful? Give feedback.
All reactions