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¶m=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;
}