CO2 monitor
MH-Z19B CO2 meter with an OLED and a ventilation alert, handy for bedrooms and offices.
BME280 plus OLED with a local web page and JSON API. A clean base for any climate monitoring.
Level: Beginner Board: ESP32 DevKit V1 (WROOM-32) Climate
Powered from USB only; nothing here touches mains.
Buy the parts kit · Estimated total: $11.00
Other countries: order these parts on AliExpress with the links below.
Some links are affiliate links: we may earn a commission at no extra cost to you.
| Module | Module pin | ESP32 pin | Note |
|---|---|---|---|
| BME280 SDA / SCL | I2C | GPIO21 / GPIO22 | address 0x76 or 0x77 |
| OLED SDA / SCL | I2C | GPIO21 / GPIO22 | address 0x3C |
| VCC / GND | 3V3 / GND |
Arduino IDE → Tools → Manage Libraries, then install:
#include <WiFi.h>
const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";
bool connectWiFi(uint32_t timeoutMs = 20000) {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
uint32_t t0 = millis();
while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
delay(250);
Serial.print(".");
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.print("IP: ");
Serial.println(WiFi.localIP());
return true;
}
return false;
}
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <Adafruit_SSD1306.h>
Adafruit_BME280 bme;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
float t = NAN, h = NAN, p = NAN;
#include <WebServer.h>
WebServer server(80);
String buildJson(); // implemented in the project below
String num(float v, int d = 1) { return isnan(v) ? String("null") : String(v, d); }
const char PAGE_HTML[] PROGMEM = R"rawliteral(
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>ESP32</title>
<style>body{font-family:system-ui;background:#0b0f14;color:#e6edf3;margin:20px}table{border-collapse:collapse}td{padding:6px 14px;border-bottom:1px solid #223}button,input{font-size:16px;padding:8px 14px;margin:4px;border-radius:6px;border:1px solid #345;background:#16202b;color:#e6edf3}</style>
</head><body><h2>ESP32</h2><table id="t"></table>
<script>
function load(){fetch('/json').then(r=>r.json()).then(d=>{let h='';for(const k in d){h+='<tr><td>'+k+'</td><td>'+d[k]+'</td></tr>';}document.getElementById('t').innerHTML=h;});}
load();setInterval(load,2000);
</script></body></html>
)rawliteral";
void startWeb() {
server.on("/", []() { server.send_P(200, "text/html", PAGE_HTML); });
server.on("/json", []() { server.send(200, "application/json", buildJson()); });
server.begin();
}
String buildJson() { return String("{\"temp\":") + num(t) + ",\"hum\":" + num(h, 0) + ",\"hPa\":" + num(p, 0) + "}"; }
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
if (!bme.begin(0x76) && !bme.begin(0x77)) Serial.println("BME280 not found");
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
connectWiFi();
startWeb();
}
void loop() {
server.handleClient();
static uint32_t tm = 0;
if (millis() - tm > 2000) {
tm = millis(); t = bme.readTemperature(); h = bme.readHumidity(); p = bme.readPressure() / 100.0;
oled.clearDisplay(); oled.setTextColor(WHITE); oled.setTextSize(2);
oled.setCursor(0, 0); oled.print(t, 1); oled.print(" C");
oled.setCursor(0, 24); oled.print(h, 0); oled.print(" %");
oled.setCursor(0, 48); oled.print(p, 0); oled.print(" hPa");
oled.display();
}
}Flash a ready firmware from the browser Paste your error into the AI Error Doctor
MH-Z19B CO2 meter with an OLED and a ventilation alert, handy for bedrooms and offices.
PMS5003 particle sensor plus BME280: PM1 / PM2.5 / PM10, temperature and humidity on a web page.
Two probes at the top and the bottom of a closet with the router, switch and NAS, and an alarm before the equipment overheats in summer.
Comments
No comments yet. Share your experience or ask a question.