BME680  v1.0.12
Arduino Library for Bosch BME680 Temperature, Humidity, and Pressure
ESP32FeatherWiFiDemo.ino File Reference
#include <SD.h>
#include <SPI.h>
#include <WebServer.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include "WebPageContents.h"

Functions

WebServer server (80)
 Instantiate a web server on port 80.
 
void setup ()
 
void handleRoot ()
 
void handleADC ()
 
void loop ()
 
void getSensorData ()
 

Variables

const char * FILE_NAME = "/BME_680.csv"
 < WiFi Client library More...
 
const char * HOSTNAME = "BME680"
 Give the device a name.
 
const char * WIFI_SSID = "NetworkSSID"
 Network SSID for connection.
 
const char * WIFI_PASSWORD = "Password"
 Network authentication code.
 
const uint32_t SERIAL_SPEED = 115200
 Set the baud rate for Serial I/O.
 
const uint8_t POWER_PIN = A13
 Supply voltage through a divider.
 
const uint8_t LED_PIN = 13
 This pin is the on-board red LED.
 
const uint8_t SD_CARD_SPI_CS_PIN = 21
 (general GPIO) on ESP32 for chip select
 
const uint8_t SD_CARD_SPI_CD_PIN = A5
 (general GPIO) on ESP32 for carrier detect
 
const uint32_t SD_LOG_INTERVAL = 1000
 Milliseconds between writes to SD-Card.
 
const uint8_t SD_FLUSH_INTERVAL = 60
 do a "flush" after this number of writes
 
const float SEA_LEVEL_PRESSURE = 1013.25
 Standard atmosphere sea level pressure.
 
BME680_Class BME680
 Create an instance of the BME680 class.
 
File dataFile
 Class for a SD-Card file.
 
int32_t temperature
 BME680 temperature value.
 
int32_t humidity
 BME680 humidity value.
 
int32_t pressure
 BME680 pressure value.
 
int32_t gas
 BME680 gas resistance value.
 
int32_t start_pressure
 Initial pressure reading.
 
bool sd_card_present = false
 Switch set when SD-Card detected.
 
String jsonData
 JSON data string.
 
uint16_t loopCounter = 0
 Counter for number of write operations since startup.
 

Detailed Description

Description

Example program for using an Arduino ESP32 based system (this sketch was developed and tested on a ESP32 Huzzah32 Feather board from https://www.adafruit.com/product/3405) along with a BME680 connected via I2C to monitor the temperature, pressure and humidity and report the values in a dynamic chart on a web page hosted by the ESP32 and connected to a local network.

Prior to compiling the program, the contents of the include file "Authentication.h" need to be updated to reflect the local WiFi network to use and the corresponding authentication code.

Once started, the IP-Address is set by the WiFi router and displayed on the serial output of the ESP32, this IP address should then be entered as the URL in a web browser of a computer attached to the same network and the data should be presented there, updated every 10 seconds.

The Bosch BME680 sensor measures temperature, pressure, humidity and air quality and is described at https://www.bosch-sensortec.com/bst/products/all_products/BME680. The datasheet is available from Bosch at https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME680_DS001-11.pdf

The most recent version of the BME680 library is available at https://github.com/Zanduino/BME680 and the documentation of the library as well as example programs are described in the project's wiki pages located at https://github.com/Zanduino/BME680/wiki.

The BME680 is an extremely small physical package that is so tiny as to be impossible to solder at home, hence it will be used as part of a third-party breakout board. There are several such boards

available at this time, for example
Company
Link
Sparkfun https://www.sparkfun.com/products/14570
BlueDot https://www.bluedot.space/sensor-boards/bme680/

Adafruit | https://learn.adafruit.com/adafruit-BME680-humidity-barometric-pressure-temperature-sensor-breakout

Bosch supplies sample software that runs on various platforms, including the Arduino family; this can be downloaed at https://github.com/BoschSensortec/BSEC-Arduino-library . This software is part of the Bosch "BSEC" (Bosch Sensortec Environmental Cluster) framework and somewhat bulky and unwieldy for typical Arduino applications, hence the choice to make a more compact and rather less abstract library.

The pressure reading needs to be adjusted for altitude to get the adjusted pressure reading. There are numerous sources on the internet for formulae converting from standard sea-level pressure to altitude, see the data sheet for the BME180 on page 16 of http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf. Rather than put a floating-point function in the library which may not be used but which would use space, an example altitude computation function has been added to this example program to show how it might be done.

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author

Written by Arnd Arnd@.nosp@m.Zand.nosp@m.uino..nosp@m.Com at https://www.github.com/SV-Zanshin

Changelog

Version Date Developer Comments
1.0.1 2020-07-04 SV-Zanshin Issue #25 - implement clang-formatting
1.0.0 2020-06-02 SV-Zanshin Ready to commit and publish as Issue #20
1.0.0b 2020-05-30 SV-Zanshin Initial coding

Function Documentation

◆ getSensorData()

void getSensorData ( )

Function to read the BME680 data and optionally write it to the SD-Card

The BME680 data is read in and if an SD-Card is present the values are written to it. The "jsonData" string is also put together here so that a client request can immediately send the current data. This is done because the frequency with which the data is written to the SD-Card is higher than that of the web page refresh.

Returns
void

◆ handleADC()

void handleADC ( )

Called when a "readADC"" request is sent from a browser client

When the browser client sends a GET request for a "readADC" this ISR is called, which then returns the current value in the jsonData string, which is continuously updated in the main loop

Returns
void

◆ handleRoot()

void handleRoot ( )

Called when a request is sent from a browser client

When the ESP32 gets an IP address and that address is entered in a browser then this ISR get called to handle that request. The response is to send the HTML page which is contained in the variable "MAIN_page" which is set in the "WebPageContents.h" file

Returns
void

◆ loop()

void loop ( )

Arduino method for the main program loop

This is the main program for the Arduino IDE, it is an infinite loop and keeps on repeating. The timed measurements from the BME680 are handled here, while the actual web page serving and responses are handled by the ISRs "handleADC()" and "handleRoot()"

Returns
void

< Millis() value for next SD-Card write time

◆ setup()

void setup ( )

Arduino method called once at startup to initialize the system

This is an Arduino IDE method which is called first upon boot or restart. It is only called one time and then control goes to the main "loop()" method, from which control never returns. The BME680, then the SD-Card (if present, if not present then it is ignored), then the Wi-Fi connection are initialized and configured here.

Returns
void

< countdown to 0 to detect card

Variable Documentation

◆ FILE_NAME

const char* FILE_NAME = "/BME_680.csv"

< WiFi Client library

< Include the SD Card standard library < Include the SPI standard library < WiFi Web Server library < ESP32 WiFi LibraryFilename on SD-Card