# main.ino

- Project: [esp32-bme280](https://flashesp.com/explore/KtWu6PzaA6oVvPFexqb0Y.md) by Mikrobot.pl
- Board: ESP32 Dev Module
- Language: Arduino (C++)
- Web page: https://flashesp.com/explore/KtWu6PzaA6oVvPFexqb0Y/files/EzqmTp5VDmqafbOLEPH8N

```cpp
#include <WiFi.h>
#include <WebServer.h>
#include <DNSServer.h>
#include <Wire.h>

#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

// ===== WiFi Captive Portal =====
const char* ssid = "ESP32_BME280";   // open AP (no password)
DNSServer dnsServer;
WebServer server(80);

// ===== BME280 =====
Adafruit_BME280 bme; // I2C
bool bme_ok = false;

// Try both common I2C addresses: 0x76, 0x77
bool initBME() {
  if (bme.begin(0x76)) return true;
  if (bme.begin(0x77)) return true;
  return false;
}

// ===== HTML =====
String makePage(float t, float h, float p_hpa) {
  String page = "<!doctype html><html><head>"
                "<meta name='viewport' content='width=device-width, initial-scale=1'>"
                "<meta http-equiv='refresh' content='2'>"   // auto refresh every 2s
                "<style>"
                "body{font-family:Arial; margin:20px;}"
                ".card{padding:16px; border:1px solid #ddd; border-radius:12px; max-width:360px;}"
                ".v{font-size:28px; margin:10px 0;}"
                ".l{color:#666; font-size:14px;}"
                "</style>"
                "</head><body>";

  page += "<h2>ESP32 BME280 Captive Portal</h2>";

  if (!bme_ok) {
    page += "<div class='card'><div class='v'>BME280 not detected</div>"
            "<div class='l'>Check wiring (SDA=GPIO21, SCL=GPIO22) and I2C address (0x76/0x77).</div></div>";
  } else {
    page += "<div class='card'>";
    page += "<div class='l'>Temperature</div><div class='v'>" + String(t, 1) + " &deg;C</div>";
    page += "<div class='l'>Humidity</div><div class='v'>" + String(h, 1) + " %</div>";
    page += "<div class='l'>Pressure</div><div class='v'>" + String(p_hpa, 1) + " hPa</div>";
    page += "<div class='l'>Auto-refresh: 2s</div>";
    page += "</div>";
  }

  page += "</body></html>";
  return page;
}

// ===== Handlers =====
void handleRoot() {
  float t = NAN, h = NAN, p_hpa = NAN;

  if (bme_ok) {
    t = bme.readTemperature();
    h = bme.readHumidity();
    p_hpa = bme.readPressure() / 100.0F; // Pa -> hPa
  }

  server.send(200, "text/html", makePage(t, h, p_hpa));
}

// Optional: JSON endpoint (useful for debugging / integrations)
void handleJson() {
  if (!bme_ok) {
    server.send(200, "application/json", "{\"ok\":false,\"error\":\"BME280 not detected\"}");
    return;
  }

  float t = bme.readTemperature();
  float h = bme.readHumidity();
  float p_hpa = bme.readPressure() / 100.0F;

  String json = "{";
  json += "\"ok\":true,";
  json += "\"temperature_c\":" + String(t, 2) + ",";
  json += "\"humidity_pct\":" + String(h, 2) + ",";
  json += "\"pressure_hpa\":" + String(p_hpa, 2);
  json += "}";

  server.send(200, "application/json", json);
}

void setup() {
  Serial.begin(115200);

  // I2C init (ESP32 default pins)
  Wire.begin(21, 22); // SDA, SCL

  bme_ok = initBME();

  // Start open AP
  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid); // no password
  IPAddress myIP = WiFi.softAPIP();

  // DNS catch-all (captive portal)
  dnsServer.start(53, "*", myIP);

  // Web routes
  server.on("/", handleRoot);
  server.on("/json", handleJson);

  // Redirect everything else to portal
  server.onNotFound([&]() {
    server.sendHeader("Location", String("http://") + WiFi.softAPIP().toString(), true);
    server.send(302, "text/plain", "");
  });

  server.begin();
}

void loop() {
  dnsServer.processNextRequest();
  server.handleClient();
}
```
