-
-
Notifications
You must be signed in to change notification settings - Fork 345
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
Add MHZ19 CO2 sensor library #2415
Conversation
@Vity01 Could you try this out on your hardware? |
e0b4bf4
to
b99de0d
Compare
You are the boss! Thank you. I will try it tomorrow. |
@Vity01 were you able to test this PR? |
@slaff I spent several several hours on it to get it work, I made some progress of understanding that thing, but I need more time... |
I am confused with the wiring from the example.
but I don't see usage for the pins |
That's correct, the definitions aren't used so they're purely for information - I'll add a comment: The call to
This is for debug output using UART1. The RX pin isn't accessible - it conflicts with one of the flash data lines. |
This is a whole sketch I use: #include <SmingCore.h>
#include <MHZ19.h>
#define PWM_PIN 14
#define SERIAL_TX_PIN 2
#define MHZ19_TX_PIN 15
#define MHZ19_RX_PIN 13
namespace
{
HardwareSerial serial0(0);
MHZ19::Uart mhz19(serial0);
MHZ19::PwmReader pwmReader;
SimpleTimer timer;
void initHardware()
{
Serial.begin();
Serial.println("Init HW");
serial0.begin();
serial0.swap();
Serial.println("Swap done");
Serial.setPort(1);
Serial.println("Begin second serial");
Serial.begin(SERIAL_BAUD_RATE, SERIAL_8N1, SERIAL_TX_ONLY, SERIAL_TX_PIN);
Serial.systemDebugOutput(true);
Serial.println("Pwm Reader PIN");
pwmReader.begin(PWM_PIN);
}
void takeMeasurement()
{
Serial.println("Take Measurement");
mhz19.getMeasurement([](MHZ19::Measurement& m) {
Serial.print("Measurement result: ");
Serial.println(toString(m.error));
if(m.error != MHZ19::Error::success) {
return;
}
Serial.print("co2 ");
Serial.print(m.co2_ppm);
Serial.print(", temp ");
Serial.print(m.temperature);
Serial.print(", status ");
Serial.println(m.status);
});
/* This method of PWM reading is not recommended */
// int co2ppm = MHZ19::pwmRead(PWM_PIN);
// Serial.print("co2 via PWM: ");
// Serial.println(co2ppm);
}
} // namespace
void init()
{
initHardware();
mhz19.setAutoCalibration(false);
timer.initializeMs<2000>(takeMeasurement).start();
pwmReader.setCallback([](uint16_t ppm) {
Serial.print(_F("PWM reader says "));
Serial.print(ppm);
Serial.println(_F("ppm"));
});
Serial.println("Note: Sensor requires 3 minutes to warm up.");
} I tried this combination of wiring
In both cases there is no output to the console and the sketch is crashing probably during |
Connections should be: MHZ19 PWM -> GPIO14 (D5) Note that UART1 outputs on GPIO2, but the USB/serial chip on your nodeMCU is connected to GPIO1. I ran your code unmodified and has no crashing issues on my nodeMCU (similar to yours). Also note the blue LED on the ESP12F module is connected to GPIO2 so should flash when there's debug output being printed, If the chip is rebooting, you'd see some serial output at 74880 baud, which is what the ROM code defaults to. The serial port initialisation order is important: void initHardware()
} PS. Software serial is something I'd only consider as a last resort as it's less robust than using hardware, even at relatively low speeds. Anything which relies on disabling interrupts for extended periods raises a red flag. PPS: I messed up the PWM reader a bit in the last couple of commits, but that won't affect serial operation. |
Thank you Mike for the info. We are getting close :-). I made some progress. Now I see the output:
The PWM value seems to be correct. When I blow at the sensor, ppm grows. But it stops to show measurement once it reaches > 2000. I am not sure if it's a bug or feature :-) (there is 2000ppm range). And as you noticed, the measurement does not work. Anything I can try? |
Checksum failed means data is all wrong! Try building with verbose debugging to see what's happening:
|
Update: I am studying protocol doc, the command is correct.
|
Bug in checksum calculation now fixed (missed a digit!) Didn't cause problem in testing because of zero packet bytes - just shows how ineffective that method is! |
Datasheet not particular clear about range setting, but I'm sure you can figure that bit out! |
We are almost there :-). I am aware of your blind programming and you are doing great job! This is the output when the detectionRange is 2000. Looks correct 👍 (I have to investigate what the status value means)
|
Thanks :-) When I added All this will vary depending on the variant being used - I've seen MH-Z19, MH-Z19B and MH-Z19C so far... If you can procure some reliable information for configuring your particular device we can add that to the documentation.
More likely the values fall out of spec. as per the Also see CYCLE_MS and CYCLE_TOLERANCE values. For wire-waggle-testing I set CYCLE_TOLERANCE to 1000 but again try widening the ranges to see what happens.
Not in software. Get your soldering iron out! https://www.electrodragon.com/w/images/f/f4/Schematic_esp-12-q.png |
Actually yes of course when PWM input is at maximum the pulse will be on those limits so should have a check for that and return maximum value. |
Hmmms, after cca 2 hours of measurement and writing to the console it started to print just a mess (checked also with another terminal in Putty). The diode was blinking (measurement is probably working OK), but the output to the terminal/console was broken. No idea what could be wrong... |
@mikee47 is this PR ready for merging? |
@Vity01 How would you like to proceed with this PR? Do you have any further suggestions before merging it? |
Have you checked with different baud rates? Re. above comment try 74880 to see if it's rebooting. Maybe a snippet of your console dump will help diagnose what's happening. |
I am trying to reproduce the "output mess" problem. So far it's working. I don't think the problem was in rebooting, just the output to the terminal. Suggestion: |
So much of this is undocumented. Have a look at https://github.com/WifWaf/MH-Z19 it handles various undocumented commands. |
New asynchronous library for Sming