BME280 Module
This module provides an easy-to-use interface for communicating with the BME280 chip, a popular environmental sensor that measures temperature, humidity, and pressure.
The Bosch Sensortec BME280/BMP280 temperature/air pressure/humidity sensor communicates with the ESP32 using the I2C interface. See the BME280 Lua Source Code for details.
Usage
To use this module, you first need to create a BME280 object by calling the create
function and passing the following parameters:
module.create()
bme=require"bme280".create(port, address, sda, scl, settings)
Create a BME280 object.
Parameters:
port
(int
): I2C port number, e.g.,0
address
(int
): the BME280 I2C address, typically0x76
sda
(int
): the GPIO number used for the I2C Serial Datascl
(int
): the GPIO number used for the I2C Serial Clocksettings
(table
, optional): a Lua table with settings. See the WeatherStation demo for example code.
Returns:
A new BME280 object.
BME280 Object Methods
bme:read()
local temperature, humidity, pressure = bme:read()
Reads the temperature, humidity, and pressure from the BME280 sensor.
Returns:
Three values: the temperature (in degrees Celsius), the humidity (as a percentage), and the pressure (in Pascals).
BME280 Examples
Basic example
Here’s an example of how to use the bme280
module:
local bmeModule = require"bme280"
local port = 0
local address = 0x76
local sda = 5
local scl = 6
local bme = bmeModule.create(port, address, sda, scl)
local temp, hum, pres = bme:read()
This example creates a new BME280 object, reads the temperature, humidity, and pressure from the sensor, and stores the values in the temp
, hum
, and pres
variables. Note that we’ve assumed that the I2C port, address, SDA, and SCL pins are specified as local variables. You may need to adjust these variables to fit your specific use case.
MQTT Sparkplug Enabled Weather Station
The MQTT Sparkplug Enabled Weather Station example available on GitHub is a ready-to-run example that demonstrates the power and capabilities of MQTT Sparkplug. One of the key components used in this example is the BME280 Lua module.
The example code demonstrates how to use the BME280 Lua module to read environmental data from the BME280 chip and publish it to an MQTT broker using the Sparkplug protocol. This data can then be consumed by other Sparkplug-enabled devices or applications.
