esp32_bme280_w_arduino_ide | Last modified: 1751444452 | Edit | Home

ESP32 + BME280 with Arduino IDE

[[https://randomnerdtutorials.com/esp32-bme280-arduino-ide-pressure-temperature-humidity/]]

BMP280 vs BME280 -> https://www.wiki.lesfabriquesduponant.net/index.php?title=Capteur_BME280#Comment_distinguer_BME280_et_BMP280_.3F

5V vs 3.3V -> https://www.wiki.lesfabriquesduponant.net/index.php?title=Capteur_BME280#Connecter_BME_280_.C3.A0_l.27arduino

J'ai un BME280 5V :

Connexion BME -> ESP

BME280 ESP32-DevKitC V4
VCC 5V
GND GND
SDA GPIO21 (WIRE-SDA)
SCL GPIO22 (WIRE-SCL)

[[https://docs.espressif.com/projects/esp-dev-kits/en/latest/esp32/esp32-devkitc/user_guide.html#pin-layout]]

Code


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

Adafruit_BME280 bme; // I2C

void setup() {
    Serial.begin(115200);
    while(!Serial);    // time to get serial running

    unsigned status;

    status = bme.begin(0x76);  
    if (!status) {
        Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
        Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
        Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
        Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
        Serial.print("        ID of 0x60 represents a BME 280.\n");
        Serial.print("        ID of 0x61 represents a BME 680.\n");
        while (1) delay(10);
    }
    Serial.println();
}


void loop() { 
    printValues();
    delay(1000);
}


void printValues() {
    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature());
    Serial.println(" °C");

    Serial.print("Pressure = ");
    Serial.print(bme.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print("Humidity = ");
    Serial.print(bme.readHumidity());
    Serial.println(" %");

    Serial.println();
}

Webserver

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <WiFi.h>
#include <WebServer.h>

Adafruit_BME280 bme; // I2C

WebServer server(80);

void handleRoot() {
  String html = "<!DOCTYPE html>";
  html += "<html>\n";
  html += "<head>\n";
  html += "<title>Server ESP32</title>\n";
  html += "</head>\n";
  html += "<body>\n";
  html += "<p>" + String(bme.readTemperature()) + "</p>";
  html += "</body>\n";
  html += "</html>";
  server.send(200, "text/html", html);
}

void handleNotFound() {
  server.send(404, "text/plain", "File Not Found");
}

void setup() {

  Serial.begin(115200);
  while (!Serial) delay(100);

  unsigned status;

  status = bme.begin(0x76);  
  if (!status) {
    Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
    Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
    Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
    Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
    Serial.print("        ID of 0x60 represents a BME 280.\n");
    Serial.print("        ID of 0x61 represents a BME 680.\n");
    while (1) delay(10);
  }

  WiFi.begin("ssid", "passwd");

  Serial.print("Connecting to WiFi... ");
  while (WiFi.status() != WL_CONNECTED) delay(500);
  Serial.println("Ok.");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());



  server.on("/", handleRoot);

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");

}



void loop() {

  server.handleClient();
  delay(1);

}