Mitch K
Published © CC BY-NC-SA

Getting Started with the MH-Z14A CO2 Detector

This is a short tutorial for connecting and setting up the MH-Z14A carbon dioxide sensor in Arduino w/OTA updates and Blynk.

BeginnerFull instructions provided1 hour11,159
Getting Started with the MH-Z14A CO2 Detector

Things used in this project

Hardware components

SparkFun ESP8266 Thing - Dev Board
SparkFun ESP8266 Thing - Dev Board
×1
MH-Z14A CO Sensor
×1

Software apps and online services

Blynk
Blynk

Story

Read more

Schematics

CO2 Monitor

Code

CO2 Monitor

Arduino
#include <Arduino.h>
#include <ESP8266WiFi.h>  // for OTA, Blynk, & restart command
#include <BlynkSimpleEsp8266.h>  // for blynk
#include <ESP8266mDNS.h>  // for OTA
#include <WiFiUdp.h>  // for OTA
#include <ArduinoOTA.h>  // for OTA

char auth[] = "token";
char ssid[] = "IoT";
char pass[] = "password";

byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};  // get gas command
byte cmdCal[9] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78};  // calibrate command
char response[9];  // holds the recieved data

int CO2ppm = 0;

unsigned long warmingTimer = 0;
unsigned long timer1 = 0;

void setup()
{
	Serial.begin(9600);  // Setup a serial connection with the sensor

	Blynk.begin(auth, ssid, pass);

	wifi_station_set_hostname("ESP-CO2 Monitor");  // network device hostname
	ArduinoOTA.setHostname("ESP-CO2 Monitor");  // for OTA
	ArduinoOTA.begin();  // for OTA

	warmingTimer = millis();  // initilize warmup timer
}

void loop()
{
	Blynk.run();
	ArduinoOTA.handle(); // for OTA

	while (millis() - warmingTimer < 310000)
	{
		Blynk.run();
		ArduinoOTA.handle();  // for OTA

		if (millis() - timer1 > 1000)
		{
			Blynk.virtualWrite(V2, (310000 - (millis() - warmingTimer)) / 1000);  // counts down till the sensor is ready
			timer1 = millis();
		}
	}

	if (millis() - timer1 > 15000)  // runs every 15 sec
	{
		getReadings();
		maint();
		timer1 = millis();
	}

} // loop

void getReadings()
{
	while (Serial.available())  // this clears out any garbage in the RX buffer
	{
		int garbage = Serial.read();
	}

	Serial.write(cmd, 9);  // Sent out read command to the sensor
	Serial.flush();  // this pauses the sketch and waits for the TX buffer to send all its data to the sensor

	while (!Serial.available())  // this pauses the sketch and waiting for the sensor responce
	{
		delay(0);
	}

	Serial.readBytes(response, 9);  // once data is avilable, it reads it to a variable
	int responseHigh = (int)response[2];
	int responseLow = (int)response[3];
	CO2ppm = (256 * responseHigh) + responseLow;

	Blynk.virtualWrite(V0, CO2ppm);
}

void calibrate()
{
	Serial.write(cmdCal, 9);
	delay(3000);
}

void maint()
{
	int rssi = wifi_station_get_rssi();
	Blynk.virtualWrite(V12, rssi);
}

BLYNK_WRITE(V10)
{
	byte cali = param.asInt();

	if (cali)
	{
		calibrate();
	}
}

BLYNK_WRITE(V11) // restart chip
{
	byte reset = param.asInt();

	if (reset)
	{
		ESP.restart();
	}
}

Credits

Mitch K

Mitch K

10 projects • 34 followers
Maker, designer, & all around fun to be around!

Comments