Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make it working with Python3 #13

Open
pierrot10 opened this issue Apr 15, 2020 · 5 comments
Open

How to make it working with Python3 #13

pierrot10 opened this issue Apr 15, 2020 · 5 comments

Comments

@pierrot10
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

japanoy commented May 6, 2024

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
Copy link
Owner

zefanja commented May 6, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants