This circuit is designed to monitor the concentration of CO2 in an environment using a SenseAir S8 CO2 sensor and an ESP-8266 microcontroller. The ESP-8266 reads the CO2 levels and controls a set of LEDs (red, green, yellow) and a buzzer to indicate air quality levels. The microcontroller is also capable of sending the CO2 data to a remote server via Wi-Fi for real-time monitoring. A push switch is included to reset the system.
#include <AirGradient.h>
#include <ESP8266WiFi.h>
#include "SoftwareSerial.h"
#include <ThingSpeak.h>
const char* ssid = "INFINITUM010D_2.4";
const char* password ="JrxgMmuYt6";
const char* server = "api.thingspeak.com";
String apiKey = "C11FT8M5PFLKQPRM";
int duration = 60; // Delay between each data measure and uploading
int led1V = D1; // Green LED
int led2A = D0; // Yellow LED
int led3R = D5; // Red LED
int led4Buzzer = D6; // Buzzer
#define RX_PIN D7
#define TX_PIN D8
WiFiClient client;
SoftwareSerial co2SensorSerial(RX_PIN, TX_PIN);
byte co2Response[] = {0, 0, 0, 0, 0, 0, 0};
void setup() {
pinMode(led1V, OUTPUT);
pinMode(led2A, OUTPUT);
pinMode(led3R, OUTPUT);
pinMode(led4Buzzer, OUTPUT);
Serial.begin(115200);
co2SensorSerial.begin(9600);
Serial.println();
Serial.print("Wifi connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println();
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
void loop() {
int co2 = requestCO2();
Serial.printf("CO2 %d ppm\n", co2);
delay(5000);
if (client.connect(server, 80)) {
String postStr = apiKey;
postStr += "&field1=";
postStr += String(co2);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.println("%. Send to Thingspeak.");
}
// LED and buzzer logic based on CO2 levels
if (co2 >= 400 && co2 < 700) {
digitalWrite(led1V, HIGH);
digitalWrite(led2A, LOW);
digitalWrite(led3R, LOW);
} else if (co2 >= 701 && co2 < 999) {
digitalWrite(led2A, HIGH);
digitalWrite(led1V, LOW);
digitalWrite(led3R, LOW);
digitalWrite(led4Buzzer, LOW);
} else if (co2 >= 1000) {
digitalWrite(led3R, HIGH);
digitalWrite(led2A, LOW);
digitalWrite(led1V, LOW);
delay(5000);
digitalWrite(led4Buzzer, LOW);
}
if (co2 > 1500 && co2 < 65533) {
digitalWrite(led3R, HIGH);
digitalWrite(led2A, LOW);
digitalWrite(led1V, LOW);
digitalWrite(led4Buzzer, LOW);
delay(5000);
digitalWrite(led4Buzzer, LOW);
}
}
int requestCO2() {
static byte readCommand[] = {0xFE, 0x44, 0x00, 0x08, 0x02, 0x9F, 0x25};
byte cmd_s8[]= {0xFE, 0x04, 0x00, 0x03, 0x00, 0x01, 0xD5, 0xC5};
byte abc_s8[]= {0xFE, 0x03, 0x00, 0x1F, 0x00, 0x01, 0xA1, 0xC3};
byte response_s8[7] = {0, 0, 0, 0, 0, 0, 0};
int readAttempt = 0;
while (!co2SensorSerial.available()) {
co2SensorSerial.write(readCommand, 7);
readAttempt++;
delay(1000);
if (readAttempt >= 5) {
Serial.println(F("Failed to request CO2. Skipping it..."));
return 0;
}
}
int timeout = 0;
while (co2SensorSerial.available() < 7) {
timeout++;
if (timeout > 10) {
while (co2SensorSerial.available()) {
co2SensorSerial.read();
}
break;
}
delay(50);
}
for (int i = 0; i < 7; i++) {
co2Response[i] = co2SensorSerial.read();
}
int high = co2Response[3];
int low = co2Response[4];
int val = high * 256 + low;
return val * 1;
client.stop();
Serial.println("Waiting…");
// thingspeak needs minimum 15 sec delay between updates
delay(duration * 1000);
}
The additional documentation provided in the code input is a detailed explanation of the importance of CO2 monitoring for health and safety, particularly in the context of preventing the spread of COVID-19. It describes how CO2 levels can indicate the quality of ventilation in a space and how this relates to the potential for airborne transmission of the virus. It also explains the operation of CO2 meters, particularly non-dispersive infrared (NDIR) sensors, and how they can be used to ensure a safe environment by alerting when CO2 concentrations reach levels that may indicate poor ventilation.