[SOLVED] UART and MH-Z16 CO2 sensor
-
@robert-hh Thank you! :)
I got it now. It's work like it's should.
Maybe I should to open the window :D -
@robert-hh said in [SOLVED] UART and MH-Z16 CO2 sensor:
@tttadam Using struct.unpack, you can also use:
from machine import UART from struct import unpack uart = UART(1, 9600) uart.write(b'\xFF\x01\x86\x00\x00\x00\x00\x00\x79') # This is what should trigger the measure response = uart.read(9) cmd, concentration, check = unpack(">xBh4xB", response) checksum = (256 - sum(response[1:-1])) % 256 print(concentration, checksum == check)FYI, nowadays when I try this code I got ValueError: bad typecode error. I am not sure if this was the case back in May.
-
@tttadam It looks like the x type code is not supported by MicroPython. Use:
d1, cmd, concentration, d2, check = unpack(">bBhiB", s)d1 and d2 are dummy values, which can be ignored.
-
@robert-hh Yep, Thank You! :)
-
I found an interesting bug/glitch today. The sensor only works one time after I upload the code.
When I plug it out, and hook it to a power brick, or back to the computer it does not work. sends back false data. every other uart and I2c sensor is OK, but not this.Any tipp how this is possible? It is so weird I don't even know how to start the investigation :D
-
@tttadam Is there a difference in how you start the code?
a) After uploading. How do you upload? And how do you start the code? Do you for instance push reset?
b) After reset: The code should be executed normally. Can you verify after reset, that the code is really loaded permanently to the device. For verification, do not use the tools you used for uploading. Use something different. If you uploaded for instance with pymakr, use ftp/Filezilla for verification. -
@robert-hh Well, I hit upload in pymakr, and then the code start by itself. Or when I plug in to the usb, or to a powerbrick then it's also start by itself.
The device sends data over mqtt, so even when it's not connected to the pc I have some sense what's going on. (That how I realised that the data what goes to the server is corrupted) -
@tttadam I think I find the issue.
If I shut the pycom down by pluging it out (like the 99,9% of the cases) the sensor wont work.
But if I reboot it after I pluged-in then it does work.
So my "solution"/workaround is that I check in the boot.py if the checksum is false, if it is, then I do a machine.reset() . Any more elegant solution is welcomed :D -
@robert-hh I have run your code in rpi 3B but it causes an error as the machine is not found what should we write instead?
-
@saman-naz This code is not made for a linux based RPI.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login