dmascord / micropython

Micropython fork with SoftUART support
MIT License
3 stars 1 forks source link

SoftUART with MH-Z19B #2

Open hcet14 opened 3 years ago

hcet14 commented 3 years ago

Hello Damien, I tried your softuart with two (one at a time) MH-Z19 Co2 sensor https://www.winsen-sensor.com/d/files/infrared-gas-sensor/ndir-co2-sensor/mh-z19b-co2-manual(ver1_6).pdf. Doesn't work.

I did some further investigations, it reads CO2 values, but my code is very different from the commands stated in datasheet (200105).

Communication between ESP8266 and PC over FTDI UART to USB works flawless.

No it does not. The behavior is hard to describe, lets say some sequences of bytes are ok (200105).

When I hook up the sensor, the answers from MH-Z19 don't make sense. My initialization with readout goes like this:

from machine import SoftUART from machine import Pin import time softuart = SoftUART(Pin(4), Pin(5), baudrate=9600, timeout=1, timeout_char=2) time.sleep(2) softuart.write(b"\xFF\x01\x86\x00\x00\x00\x00\x00\x79") time.sleep(1) r=softuart.read(9) print(r)

FF 01 86 00 00 00 00 00 79 (command to readout)

FF 86 02 3E 46 00 00 00 F4 (answer from 1st sensor over FTDI UART) FF 86 01 9A 00 00 00 00 DF (answer from 1st sensor over FTDI UART)

Both make sense.

b'\xff\x86\x02&G\x00\x00\x00\x0b' (answer from 1st sensor over ESP8266) b'\xff\x86\x02XH\x00\x00\x00\xd8' (answer from 1st sensor over ESP8266)

Well.

Setting timeout to 1 gives values "closer to good". Playing with timeout_char has almost no influence.

MH-Z19 uses a STM32 µC.

Maybe you give it a try with https://github.com/plerup/espsoftwareserial which runs pretty good and is still supported. There are some examples on github which use a MH-Z19 and an ESP8266 on the Arduino platform. I'm positiv that it's possible to communicate with an ESP8266 over software UART on the Micropython platform to devices.

I really appreciate your good work.

hcet14

dmascord commented 3 years ago

Hi hcet14,

Apologies for not noticing this before! Let me see if I can get a MH-Z19 to test it.

Cheers,

Damien

dmascord commented 3 years ago

Hi hcet14,

I can't replicate this issue testing with your exact characters using - miniterm /dev/ttyUSB1 115200 --encoding hexlify

The data being sent from the esp8266 is fine, and the data being read from the serial copy/paste of "FF 86 02 3E 46 00 00 00 F4" shows b'\xff\x86\x02>F\x00\x00\x00\xf4' similar to what you are seeing, but with the correct characters.

Not sure what is going on on your side, but my testing on 9600 and 115200 seems fine to me.

Testing with the following code:

import machine
import binascii
ser = machine.SoftUART(machine.Pin(5),machine.Pin(4), baudrate=9600, timeout=1000, timeout_char=10)
ser.write(b"\xFF\x01\x86\x00\x00\x00\x00\x00\x79")
while True:
    data = ser.read(9)
    if data:
        print(binascii.hexlify(data,' '))
    else:
        print "Looping."`

Cheers,

Damien

dmascord commented 3 years ago

I ordered one of these devices, so I will try it in a few weeks once it arrives.

hcet14 commented 3 years ago

Hi Damien,

I almost forgot about my issue. Cool, that you even want to buy a sensor. I really appreciate it!

I bought the sensor to build a little warning app to inform us in the office to open the windows again, when the CO2 (aerosol) concentration is getting high again (Corona).

This site has good information about the sensor https://revspace.nl/MH-Z19B. Pay attention about the fake MH-Z19 and never use ABC (Auto Base Calibration), I recommend to turn it off (0x79 = 0x00)! The UART of the fake sensor is working fine, but the measurements are nonsense. I bought a good one and a fake one.

I wrote python scripts to use it with ESP32 boards with hard UART and ESP8266 boards with your soft UART. Long time not used, I have to look for the scripts and will provide access if someone is interested and my search is successful.

My sensors work fine regarding communication with my ESP32 or hooked to a PC.

What makes more sense? Communicate here on GitHub, in here https://forum.micropython.org/viewtopic.php?f=16&t=2204&hilit=MH+Z19#p12516, or opening a new thread here https://forum.micropython.org/viewforum.php?f=16?

I wish you success!

hcet14

dmascord commented 3 years ago

Hi,

I received my MH-Z19C, which looks like a real one, and am testing with the following code:

import machine
import binascii
import time
ser = machine.SoftUART(machine.Pin(5),machine.Pin(4), baudrate=9600, timeout=1000, timeout_char=10)

while True:
    ser.write(b"\xFF\x01\x86\x00\x00\x00\x00\x00\x79")
    data = ser.read(9)
    if data:
        print(binascii.hexlify(data,' '))
    else:
        print("Looping.")
    time.sleep(5)

and I am getting the output

b'ff 86 13 88 39 00 00 00 a6'
b'ff 86 13 88 39 00 00 00 a6'
b'ff 86 13 88 39 00 00 00 a6'
b'ff 86 13 88 39 00 00 00 a6'
b'ff 86 13 88 39 00 00 00 a6'
b'ff 86 13 88 39 00 00 00 a6'
b'ff 86 13 88 39 00 00 00 a6'
b'ff 86 13 88 39 00 00 00 a6'
b'ff 86 13 88 39 00 00 00 a6'
b'ff 86 13 88 38 00 00 00 a7'
b'ff 86 13 88 39 00 00 00 a6'
b'ff 86 13 88 39 00 00 00 a6'

I am not sure what data it is meant to be returning, but it looks reasonable to me ?

Testing using miniterm reveals

FF 86 13 88 3B 00 00 00 A4 
FF 86 13 88 3B 00 00 00 A4 
FF 86 13 88 3B 00 00 00 A4 
FF 86 13 88 3B 00 00 00 A4 
FF 86 13 88 3A 00 00 00 A5

So it doesn't look like there is any data corruption with the soft uart library.