This repository contains easily reusable code to read the BME280 temperature, pressure and humidity sensor by an ESP8266 microcontroller.
This code was tested on Wemos D1 Mini with the following wiring:
(Photo from esp8266learning.com.)
-
Add the
Adafruit BME280 Library
andAdafruit Unified Sensor
library to your project. -
Copy the
bme280.cpp
andbme280.h
files to your project folder. -
Include the
bme280.h
header file into your project:
#include "bme280.h"
- Create an instance of the sensor (uses the default
0x76
address):
BME280 bme;
- Initialize the sensor when your device boots up:
void setup() {
bme.init();
}
- Read the sensor values:
void loop() {
BME280::Measurement measurement = bme.getMeasuredData();
}
- Get the temperature, pressure, altitude and humidity values:
Serial.printf("Temperature: %.1f *C\n", measurement.temperature);
Serial.printf("Pressure: %.1f hPa\n", measurement.pressure);
Serial.printf("Altitude: %.1f m\n", measurement.altitude);
Serial.printf("Humidity: %.1f %\n", measurement.humidity);
For a complete example check the esp8266-bme280.ino
file.
Optional: you can use the getTemperature()
, getPressure()
, getAltitude()
and getHumidity()
functions of the BME280
class individually as well.
This BME280
class acts as a wrapper around the Adafruit BME280 Library to provide easier setup, and encapsulate all logic, includes and constants related to this sensor.
Currently this class provides the most frequently used functionality of the sensor, but the sensor provides additional options to further customize its behavior. Check this example to see various scenarios.
This project is maintained by György Balássy.