Page 1 of 10

ESP8266 Air quality sensor

Posted: Friday 24 June 2016 20:40
by Corvax
Hi,

I have "made" an air quality sensor by using a MH-Z19 module bought on Aliexpress for around 25 USD and a ESP8266 201 board.

Sensor
http://www.aliexpress.com/wholesale?cat ... ext=MH-z19

The code is updating a dummy air quality sensor around every 30 seconds.


The software is a mashup from different sources...and some written by me.......probably some room for some more tweaking.....

Code: Select all


#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
const char* ssid     = "YourSSID";  
const char* password = "YourPassword";
const char* idx      = "300";				//IDX of dummy air quality sensor
const char* host     = "192.168.1.40"; //     	//IP Address of domoticz server

SoftwareSerial mySerial(12, 13); // RX, TX


void(* resetFunc) (void) = 0; //declare reset function @ address 0


void setup() {

      
   Serial.begin(115200);

    mySerial.begin(9600);       //C02 sensor read serial


 
  delay(1000);
  
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected");  
  Serial.println("IP address: " + WiFi.localIP());

}

void loop() {
      
  Serial.print("connecting to ");
  Serial.println(host);
  
  WiFiClient client;
  const int httpPort = 8080;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");

    Serial.println("Reset everything");
   ESP.reset();
    return;
  }

  int ppm = readCO2();

   delay(1000);
  bool dataError = false;
  Serial.println("  PPM = " + String(ppm));

  if (ppm < 100 || ppm > 6000)
  {
    Serial.println("PPM not valid");

    return;         //start over again
  }

  delay(1000);

  

  client.print(String("GET ") + "/json.htm?type=command&param=udevice&idx=" + idx + "&nvalue=" + ppm + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");

  delay(1000); // wait for server to respond

  // read response

  String section="header";

  while(client.available()){
    
    String line = client.readStringUntil('\r');
    Serial.print(line);


 if (section=="header") { // headers..
      Serial.print(".");
      if (line=="\n") { // skips the empty space at the beginning 
        section="json";
      }
    }
    else if (section=="json") {  // print the good stuff
      section="ignore";
      String result = line.substring(1);

      // Parse JSON
      int size = result.length() + 1;
      char json[size];
      result.toCharArray(json, size);
      StaticJsonBuffer<200> jsonBuffer;
      JsonObject& json_parsed = jsonBuffer.parseObject(json);
      if (!json_parsed.success())
      {
        Serial.println("parseObject() failed");
        return;
      }
       if (strcmp(json_parsed["status"], "OK") == 0) {
        Serial.println("Returned OK");
       
      }
      else if (strcmp(json_parsed["status"], "ERR") == 0){
                Serial.println("Returned ERR or");
      }
      else{
                Serial.println("Not working at all");
      }

  }


  }
   
  Serial.println("closing connection");
  client.stop();
  delay(20000);
  
}

int readCO2()
{

  byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
  // command to ask for data
  char response[9]; // for answer

  mySerial.write(cmd, 9); //request PPM CO2
  mySerial.readBytes(response, 9);
  if (response[0] != 0xFF)
  {
    Serial.println("Wrong starting byte from co2 sensor!");
    return -1;
  }

  if (response[1] != 0x86)
  {
    Serial.println("Wrong command from co2 sensor!");
    return -1;
  }

  int responseHigh = (int) response[2];
  int responseLow = (int) response[3];
  int ppm = (256 * responseHigh) + responseLow;
  return ppm;
}



Re: ESP8266 Air quality sensor

Posted: Friday 24 June 2016 22:57
by Derik
a printscreen from Domoticz?

Re: ESP8266 Air quality sensor

Posted: Sunday 26 June 2016 13:10
by gizmocuz
Great job!
But its an expensive sensor ;)

Re: ESP8266 Air quality sensor

Posted: Sunday 26 June 2016 13:43
by sincze
Great coding.
However I agree, it must be able to achieve this under $5 :D

Re: ESP8266 Air quality sensor

Posted: Monday 27 June 2016 17:43
by Corvax
Derik wrote:a printscreen from Domoticz?
Hi, I see attached devices setup and a chart.

regards
Devices.png
Devices.png (140.5 KiB) Viewed 17832 times
Co2 sensor.jpeg
Co2 sensor.jpeg (48.3 KiB) Viewed 17832 times

Re: ESP8266 Air quality sensor

Posted: Saturday 13 August 2016 21:59
by ThinkPad
How accurate is this sensor compared to something like a more expensive sensor like a SenseAir K30 ? Any idea?
And what about calibration? Does it need calibration?

Re: ESP8266 Air quality sensor

Posted: Sunday 04 September 2016 22:04
by madrian

Re: ESP8266 Air quality sensor

Posted: Monday 05 September 2016 7:28
by ThinkPad
MQ135 sucks if i have to believe blogs online. Really hard to get a trustworthy result.
MH-Z19 is most valueable option for now, good price and readings seem to be accurate.

Re: ESP8266 Air quality sensor

Posted: Monday 14 August 2017 11:39
by nixalsschrott
As I understand, you are using WIFI only, right?
Is there any option to build this CO2 sensor with z-wave plus compatibility to integrate into z-wave plus meshed network?
Perhaps also with temperature and humidity reporting?

Kind regards
matty

Re: ESP8266 Air quality sensor

Posted: Monday 14 August 2017 16:45
by randytsuch
nixalsschrott wrote: Monday 14 August 2017 11:39 As I understand, you are using WIFI only, right?
Is there any option to build this CO2 sensor with z-wave plus compatibility to integrate into z-wave plus meshed network?
Perhaps also with temperature and humidity reporting?

Kind regards
matty
zwave is a proprietary interface, you have to pay a large license fee to use it, so really no DIY options with zwave.
ESP's use the wifi network you already have, they are really nice, inexpensive little boards

Randy

Re: ESP8266 Air quality sensor

Posted: Tuesday 15 August 2017 3:42
by woodtrix
I currently using a couple of vlads co2 monitors

http://vair-monitor.com

and quite happy with it... he offers various co2 sensors (also sells the individual chips for diy) options include dustsensors humiditybetc, the works...

the dude building them is also on this forum.

Re: ESP8266 Air quality sensor

Posted: Friday 18 August 2017 9:55
by manjh
I am using a Senseair S8 coupled to an ESP unit, and can see the values coming into Domoticz when I use a "user defined" sensor device. But when I link it to an "Air quality" device, values stay zero. What is wrong?

Re: ESP8266 Air quality sensor

Posted: Tuesday 22 August 2017 12:57
by Toulon7559
Could this reasonably priced dust-measuring device be meaningful for a DIY-realisation for measuring air-quality?

Re: ESP8266 Air quality sensor

Posted: Tuesday 22 August 2017 13:31
by manjh
Toulon7559 wrote: Tuesday 22 August 2017 12:57 Could this reasonably priced dust-measuring device be meaningful for a DIY-realisation for measuring air-quality?
Air quality is determined by a number of things. The most important are CO2 percentage and dust.
Using a dust sensor alone does not cut it: you could have very clean air (in the sense of dust content) but a very high CO2 percentage.
I currently have a CO2 sensor running, but I have a dust sensor on order and will add that to the ESP as soon as I have it.
https://www.banggood.com/DIY-GP2Y1010AU ... mds=search

Re: ESP8266 Air quality sensor

Posted: Tuesday 22 August 2017 13:32
by manjh
manjh wrote: Friday 18 August 2017 9:55 I am using a Senseair S8 coupled to an ESP unit, and can see the values coming into Domoticz when I use a "user defined" sensor device. But when I link it to an "Air quality" device, values stay zero. What is wrong?
I solved it by using a json command in the rules section of ESPEasy. Not really a solution, but a good workaround.

Re: ESP8266 Air quality sensor

Posted: Tuesday 22 August 2017 16:19
by LouiS22
woodtrix wrote: Tuesday 15 August 2017 3:42 I currently using a couple of vlads co2 monitors

http://vair-monitor.com

and quite happy with it... he offers various co2 sensors (also sells the individual chips for diy) options include dustsensors humiditybetc, the works...

the dude building them is also on this forum.
Sadly he recently stopped building and selling new devices (will continue it later).

Re: ESP8266 Air quality sensor

Posted: Wednesday 06 September 2017 21:10
by woodtrix
Did any of ever thinking about how to expand this?

I hvae CO, CO2 and dust sensors all over (and of the course humidity and temperature) to monitor and control the ventilation system (& the vacuum robot, air purifier, heating)

But there are two areas in the air quality that I have not figured out yet...

I want to know the difference between nr 1 and nr 2, in a bathroom which has no windows,... so i was thinking a methane sensor (or something like that, that works... (no idea of any of the mq series works) placed at the bottom of the resevoir... ass to sensor is like 20-30 centimeters... and would be close enough to detect early...

(I already detect flushings, by using a flood sensor without the alarm beeb (broken off), and exploiting the internal flushing mechanism, such that when you flush the sensor makes contact, but it would be nice to be a little earlier with nr 2, and do nothing, or maybe a lower setting, when you have to piss

Second I want to know when cooking takes place so when one bake some sausages,the ventilation goes on, I could attach a humidity sensor (i would also like to detect things like burning a patato)

Do you guys have any thoughts or ideas on this?

Re: ESP8266 Air quality sensor

Posted: Sunday 29 October 2017 14:13
by Topcase1
Omdat ik hier nieuw ben zeg ik Hallo tegen iedereen,

De luchtkwaliteitssensor gemaakt van een ESP8266 en een MH-Z19 staat mij wel aan.
Wat ik begrijp is dat je zo'n combinatie "stand alone" kunt gebruiken en koppelen via wifi aan mijn Raspberrypi waarop Domoticz staat?
Nu zag ik de afdruk van een CO2 grafiek en dat wil ik ook.
Maar op diezelfde afdruk zie ik als hardware de Volcraft co-20 in Domoticz staan.
Kan iemand mij uitleggen hoe dit zit? Is het zo dat als je de software inlaad vanzelf als hardware Volcraft in Domoticz hebt?

Alvast bedankt,

Topcase1

Re: ESP8266 Air quality sensor

Posted: Sunday 29 October 2017 14:16
by SweetPants
Topcase1 wrote: Sunday 29 October 2017 14:13 Omdat ik hier nieuw ben zeg ik Hallo tegen iedereen,

De luchtkwaliteitssensor gemaakt van een ESP8266 en een MH-Z19 staat mij wel aan.
Wat ik begrijp is dat je zo'n combinatie "stand alone" kunt gebruiken en koppelen via wifi aan mijn Raspberrypi waarop Domoticz staat?
Nu zag ik de afdruk van een CO2 grafiek en dat wil ik ook.
Maar op diezelfde afdruk zie ik als hardware de Volcraft co-20 in Domoticz staan.
Kan iemand mij uitleggen hoe dit zit? Is het zo dat als je de software inlaad vanzelf als hardware Volcraft in Domoticz hebt?

Alvast bedankt,

Topcase1
English please!!

Re: ESP8266 Air quality sensor

Posted: Sunday 29 October 2017 14:23
by Topcase1
Because I'm new here I say Hello to everyone,

The air quality sensor made of an ESP8266 and an MH-Z19 is true to me.
What I understand is that you can use and pair this combination of stand alone with Wi-Fi to my Raspberrypi on which Domoticz stands?
Now I saw the print of a CO2 chart, and I want that too.
But on the same print, I see hardware as the Volcraft co-20 in Domoticz.
Can anyone explain how this is? Is it that if you download the software by itself, if you have Volcraft in Domoticz?

Thanks in advance,

Topcase1