MHZ-19 CO2 ppm

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Poh_Roman
Posts: 3
Joined: Tue Aug 27, 2019 12:25 pm

MHZ-19 CO2 ppm

Post by Poh_Roman » Tue Aug 27, 2019 1:04 pm

HI, help me to create micropython script to read data from PWM for MHZ-19 CO2.
Example on C:
Serial3.setup(9600, {rx: P0, tx: P1});
function print_data(data) {
a = [];
for (var i=0; i < data.length; i++) { a.push(data.charCodeAt(i)); }
crc = 256 - (a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7])%256;
if (crc == a[8]) { console.log("temp: " + (a[4]-40) + ", co2: " + a[2] * 256 + a[3]); }
}

function check_co2() {
Serial3.write("\xFF\x01\x86\x00\x00\x00\x00\x00\x79");
print_data(Serial3.read(9));
}

setInterval(check_co2, 10000);

// Serial3.write("\xFF\x01\x87\x00\x00\x00\x00\x00\x78"); ZERO POINT CALIBRATION
// Serial3.write("\xFF\x01\x79\x00\x00\x00\x00\x00\x86"); ABC logic off
// Serial3.write("\xFF\x01\x79\xA0\x00\x00\x00\x00\xE6"); ABC logic on

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: MHZ-19 CO2 ppm

Post by kevinkk525 » Tue Aug 27, 2019 1:42 pm

Your example uses uart, not PWM.

http://docs.micropython.org/en/v1.9.3/p ... .UART.html

However you will have difficulties using the esp2866 because it only has one UART for the micropython repl. You can use that for your device but you would need to disable the repl and use the webrepl. You'll find other threads about that on the forum.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Poh_Roman
Posts: 3
Joined: Tue Aug 27, 2019 12:25 pm

Re: MHZ-19 CO2 ppm

Post by Poh_Roman » Wed Aug 28, 2019 8:05 am

[quote=kevinkk525 post_id=38934 time=1566913368 user_id=3684]
Your example uses uart, not PWM.

[url]http://docs.micropython.org/en/v1.9.3/p ... .UART.html[/url]

However you will have difficulties using the esp2866 because it only has one UART for the micropython repl. You can use that for your device but you would need to disable the repl and use the webrepl. You'll find other threads about that on the forum.
[/quote]

Thanks, i need any example by PWM
C++:
int MHZ19::getPwmData()
{
unsigned long th, tl, ppm = 0;

do
{
th = pulseIn(_pwm_pin, HIGH, 1004000) / 1000;
tl = 1004 - th;
ppm = 5000 * (th - 2) / (th + tl - 4);
} while (th == 0);

return ppm;
}

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: MHZ-19 CO2 ppm

Post by kevinkk525 » Wed Aug 28, 2019 1:25 pm

I'm not sure there is a good way to count pwm pulses yet but haven't needed that myself yet.

The other option would be to use an Arduino to do that and then send the data from the arduino to the esp8266
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply