ESP8266 Air quality sensor

Everything about esp8266 and more.

Moderator: leecollings

Post Reply
User avatar
Corvax
Posts: 2
Joined: Friday 24 June 2016 20:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

ESP8266 Air quality sensor

Post 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;
}


Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: ESP8266 Air quality sensor

Post by Derik »

a printscreen from Domoticz?
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
User avatar
gizmocuz
Posts: 2352
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: ESP8266 Air quality sensor

Post by gizmocuz »

Great job!
But its an expensive sensor ;)
Quality outlives Quantity!
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: ESP8266 Air quality sensor

Post by sincze »

Great coding.
However I agree, it must be able to achieve this under $5 :D
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
User avatar
Corvax
Posts: 2
Joined: Friday 24 June 2016 20:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: ESP8266 Air quality sensor

Post 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 17845 times
Co2 sensor.jpeg
Co2 sensor.jpeg (48.3 KiB) Viewed 17845 times
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: ESP8266 Air quality sensor

Post 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?
I am not active on this forum anymore.
madrian
Posts: 231
Joined: Saturday 27 August 2016 1:18
Target OS: -
Domoticz version:
Contact:

Re: ESP8266 Air quality sensor

Post by madrian »

ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: ESP8266 Air quality sensor

Post 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.
I am not active on this forum anymore.
nixalsschrott
Posts: 39
Joined: Tuesday 11 July 2017 10:26
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Location: Germany
Contact:

Re: ESP8266 Air quality sensor

Post 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
Domoticz Beta | Aeon Labs Z-Wave USB Stick Gen5 | Cyrus SmartHome 4-in-1 Multisensor | Qubino ZMNHCD1 - Roller Shutter | NEO COOLCAM NAS-WR01ZE Power Plug | Fibaro FGRGB-101 | WEMOS D1 MINI (ESP8266) Sensors CO2/TEMP/BARO |
randytsuch
Posts: 90
Joined: Sunday 20 March 2016 18:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: LA, Ca USA
Contact:

Re: ESP8266 Air quality sensor

Post 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
woodtrix
Posts: 34
Joined: Saturday 18 February 2017 17:54
Target OS: -
Domoticz version:
Contact:

Re: ESP8266 Air quality sensor

Post 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.
manjh
Posts: 708
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: ESP8266 Air quality sensor

Post 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?
Hans
Toulon7559
Posts: 843
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: ESP8266 Air quality sensor

Post by Toulon7559 »

Could this reasonably priced dust-measuring device be meaningful for a DIY-realisation for measuring air-quality?
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
manjh
Posts: 708
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: ESP8266 Air quality sensor

Post 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
Hans
manjh
Posts: 708
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: ESP8266 Air quality sensor

Post 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.
Hans
User avatar
LouiS22
Posts: 433
Joined: Friday 27 February 2015 13:21
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: ESP8266 Air quality sensor

Post 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).
woodtrix
Posts: 34
Joined: Saturday 18 February 2017 17:54
Target OS: -
Domoticz version:
Contact:

Re: ESP8266 Air quality sensor

Post 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?
Topcase1
Posts: 3
Joined: Sunday 29 October 2017 13:47
Target OS: Raspberry Pi / ODroid
Domoticz version: v4.9700
Location: Maassluis, Zuid-Holland
Contact:

Re: ESP8266 Air quality sensor

Post 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
SweetPants

Re: ESP8266 Air quality sensor

Post 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!!
Topcase1
Posts: 3
Joined: Sunday 29 October 2017 13:47
Target OS: Raspberry Pi / ODroid
Domoticz version: v4.9700
Location: Maassluis, Zuid-Holland
Contact:

Re: ESP8266 Air quality sensor

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests