Page 1 of 1

MHZ-19 CO2 ppm

Posted: Tue Aug 27, 2019 1:04 pm
by Poh_Roman
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

Re: MHZ-19 CO2 ppm

Posted: Tue Aug 27, 2019 1:42 pm
by kevinkk525
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.

Re: MHZ-19 CO2 ppm

Posted: Wed Aug 28, 2019 8:05 am
by Poh_Roman
[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;
}

Re: MHZ-19 CO2 ppm

Posted: Wed Aug 28, 2019 1:25 pm
by kevinkk525
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