zefanja / aqi

Measure AQI based on PM2.5 or PM10 with a Raspberry Pi and a SDS011 particle sensor
GNU General Public License v3.0
94 stars 46 forks source link

How to make it working with Python3 #13

Open pierrot10 opened 4 years ago

pierrot10 commented 4 years ago

Hello, Thank a lot for that code. I need to make it working with Python3 but it generate this error message

File "aqi.py", line 130, in cmd_set_sleep(0) File "aqi.py", line 104, in cmd_set_sleep ser.write(construct_command(CMD_SLEEP, [0x1, mode])) File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 518, in write d = to_bytes(data) File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 63, in to_bytes raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq)) TypeError: unicode strings are not supported, please encode to bytes: 'ª´\x06\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00ÿÿ\x06«'

Is there a adapted script for Python3?

Thank a lot

wallarug commented 4 years ago

Hi @pierrot10 ,

This is not a Python3 specific error. This is caused by not converting the string to bytes correctly.

You will need to encode whatever you are sending as bytes using string.encode("hex"). Here is a quick example.

>>> s = "this is a string"
>>> data = s.encode("hex")
'54686973206973206120737472696e67'
>>> data.decode("hex")

If you want to copy lines 100 to 110 in this issue it might be easier to find out why this error is being thrown.

snarksandrec commented 2 years ago

I'm getting the same error for the same section of code (sleep): Traceback (most recent call last): File "/home/pi/./aqi.py", line 112, in <module> cmd_set_sleep(0) File "/home/pi/./aqi.py", line 86, in cmd_set_sleep ser.write(construct_command(CMD_SLEEP, [0x1, mode])) File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 598, in write d = to_bytes(data) File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 65, in to_bytes raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq)) TypeError: unicode strings are not supported, please encode to bytes: 'ª´\x06\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00ÿÿ\x06«'

I used the same code in the aqi.py file in this repo, so the issue is in the block beginning on line 84. I'm not sure what I should be encoding/decoding and where. Anyone with slightly more specific instructions to follow?

Thanks!

RugaAlberto commented 1 year ago

I have the same error and I'm going crazy, by any chance did you solve it? or found any workarounds? Thanks for any replies

Alberto Ruga

japanoy commented 3 months ago

Year 2024-May and am also having this exact issue.

Wish somebody knowledgeable in Python3 initiate a fix.

From extensive googling, looks like the only fix for us noob is to use Python2.

zefanja commented 3 months ago

Maybe checkout https://sensor.community/en/sensors/airrohr/

We use it since many years and it runs very reliable (and is also cheaper). Instead of the SDS011 we use a PMS5003 PM2.5 sensor.