{"id":86860,"date":"2019-07-09T09:44:31","date_gmt":"2019-07-09T09:44:31","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=86860"},"modified":"2020-07-30T14:10:53","modified_gmt":"2020-07-30T14:10:53","slug":"esp8266-bme280-arduino-ide","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp8266-bme280-arduino-ide\/","title":{"rendered":"ESP8266 with BME280 using Arduino IDE (Pressure, Temperature, Humidity)"},"content":{"rendered":"\n<p>This guide shows how to use the BME280 sensor module with the ESP8266 to read pressure, temperature, humidity and estimate altitude using Arduino IDE. The BME280 sensor uses I2C or SPI communication protocol to exchange data with a microcontroller.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-BME280.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 with BME280 using Arduino IDE (Pressure, Temperature, Humidity)\" class=\"wp-image-86888\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-BME280.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-BME280.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-BME280.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-BME280.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>We\u2019ll show you how to wire the sensor to the ESP8266, install the required libraries, and write a simple sketch that displays the sensor readings. We&#8217;ll also build a web server example to display the latest pressure, temperature and humidity readings.<\/p>\n\n\n\n<p>Before proceeding with this tutorial you should have the ESP8266 add-on installed in your Arduino IDE. <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-esp8266-board-arduino-ide\/\">Install the ESP8266 Board in Arduino IDE<\/a><\/li><\/ul>\n\n\n\n<p>You might also like reading other BME280 guides:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><span style=\"font-size: inherit; background-color: initial;\"><a href=\"https:\/\/randomnerdtutorials.com\/esp32-bme280-arduino-ide-pressure-temperature-humidity\/\">ESP32 with BME280 Sensor using Arduino IDE<\/a><\/span><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-with-bme280-mini-weather-station\/\">ESP32 Web Server with BME280 \u2013 Weather Station<\/a><\/li><li><a style=\"font-size: inherit; background-color: initial;\" href=\"https:\/\/randomnerdtutorials.com\/micropython-bme280-esp32-esp8266\/\">ESP32\/ESP8266 with BME280 using MicroPython<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/bme280-sensor-arduino-pressure-temperature-humidity\/\">Arduino Board with BME280<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing BME280 Sensor Module<\/h2>\n\n\n\n<p>The <a rel=\"noreferrer noopener\" aria-label=\"BME280 sensor (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/bme280-sensor-module\/\" target=\"_blank\">BME280 sensor<\/a> module reads barometric pressure, temperature, and humidity. Because pressure changes with altitude, you can also estimate altitude. There are several versions of this sensor module. We\u2019re using the module illustrated in the figure below.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-sensor.jpg?resize=750%2C462&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 Sensor I2C Module reads pressure, temperature, and humidity\" class=\"wp-image-86437\" width=\"750\" height=\"462\"\/><\/figure><\/div>\n\n\n\n<p>This sensor communicates using I2C communication protocol, so the wiring is very simple. You can use the default ESP8266 I2C pins as shown in the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>BME280<\/strong><\/td><td><strong>ESP8266<\/strong><\/td><\/tr><tr><td>Vin<\/td><td><span class=\"rnthl rntcred\">3.3V<\/span><\/td><\/tr><tr><td>GND<\/td><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><\/tr><tr><td>SCL<\/td><td><span class=\"rnthl rntcyellow\">GPIO 5<\/span><\/td><\/tr><tr><td>SDA<\/td><td><span class=\"rnthl rntclgray\">GPIO 4<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>There are other versions of this sensor that can use either SPI or I2C communication protocols, like the module shown in the next figure:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/BME280-SPI.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 Sensor Module SPI or I2C communication protocols\" class=\"wp-image-86630\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/BME280-SPI.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/BME280-SPI.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>If you&#8217;re using one of these sensors, to use I2C communication protocol, use the following pins:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>BME280<\/strong><\/td><td><strong>ESP8266<\/strong><\/td><\/tr><tr><td>SCK (SCL Pin)<\/td><td> <span class=\"rnthl rntcyellow\">GPIO 5<\/span><\/td><\/tr><tr><td>SDI (SDA pin) <\/td><td> <span class=\"rnthl rntclgray\">GPIO 4<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>If you use SPI communication protocol, you need to use the following pins:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>BME280<\/strong><\/td><td><strong>ESP8266<\/strong><\/td><\/tr><tr><td> SCK (SPI Clock)<\/td><td><span class=\"rnthl rntcyellow\">GPIO 14<\/span><\/td><\/tr><tr><td> SDO&nbsp;(MISO)<\/td><td><span class=\"rnthl rntclblue\">GPIO 12<\/span><\/td><\/tr><tr><td> SDI (MOSI)<\/td><td><span class=\"rnthl rntclgray\">GPIO 13<\/span><\/td><\/tr><tr><td> CS (Chip Select) <\/td><td><span class=\"rnthl rntcbrown\">GPIO 15<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>To complete this tutorial you need the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/bme280-sensor-module\/\" target=\"_blank\">BME280 sensor module<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\"ESP8266&nbsp; (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266<\/a><span style=\"font-size: inherit; background-color: initial;\"><a rel=\"noreferrer noopener\" aria-label=\"ESP8266&nbsp; (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">&nbsp;<\/a>(read <a href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Best ESP8266 development boards (opens in a new tab)\">Best ESP8266 development boards<\/a>)<\/span><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Schematic &#8211; ESP8266 with BME280 using I2C<\/h2>\n\n\n\n<p>We\u2019re going to use I2C communication with the BME280 sensor module. For that, wire the sensor to the ESP8266&nbsp;<span class=\"rnthl rntclgray\">SDA<\/span>&nbsp;and&nbsp;<span class=\"rnthl rntcyellow\">SCL<\/span>&nbsp;pins, as shown in the following schematic diagram. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"532\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP8266-BME280-Arduino-IDE.png?resize=705%2C532&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Schematic Wiring diagram ESP8266 with BME280 using I2C\" class=\"wp-image-85980\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP8266-BME280-Arduino-IDE.png?w=705&amp;quality=100&amp;strip=all&amp;ssl=1 705w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP8266-BME280-Arduino-IDE.png?resize=300%2C226&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 705px) 100vw, 705px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pinout-reference-gpios\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"ESP8266 Pinout Reference Guide (opens in a new tab)\">ESP8266 Pinout Reference Guide<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing the BME280 library<\/h2>\n\n\n\n<p>To get readings from the BME280 sensor module you need to use the <a href=\"https:\/\/github.com\/adafruit\/Adafruit_BME280_Library\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Adafruit_BME280 library<\/a>. Follow the next steps to install the library in your Arduino IDE:<\/p>\n\n\n\n<p>Open your Arduino IDE and go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>. The Library Manager should open.<\/p>\n\n\n\n<p>Search for \u201c<strong>adafruit bme280<\/strong> \u201d on the Search box and install the library.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/install-bme280.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing BME280 library in Arduino IDE\" class=\"wp-image-70233\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/install-bme280.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/install-bme280.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/install-bme280.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Installing the Adafruit_Sensor library<\/h2>\n\n\n\n<p>To use the BME280 library, you also need to install the <a href=\"https:\/\/github.com\/adafruit\/Adafruit_Sensor\" target=\"_blank\" rel=\"noreferrer noopener\">Adafruit_Sensor library<\/a>. Follow the next steps to install the library in your Arduino IDE:<\/p>\n\n\n\n<p>Go to <strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong> and type \u201c<strong>Adafruit Unified Sensor<\/strong>\u201d in the search box. Scroll all the way down to find the library and install it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing Adafruit Unified Sensor Driver library\" class=\"wp-image-84295\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p>After installing the libraries, restart your Arduino IDE.<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Reading Pressure, Temperature, and Humidity <\/h2>\n\n\n\n<p>To read pressure, temperature, and humidity we&#8217;ll use a sketch example from the library.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/BME280-ESP8266-Breadboard.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 with BME280 I2C sensor module on Breadboard\" class=\"wp-image-86887\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/BME280-ESP8266-Breadboard.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/BME280-ESP8266-Breadboard.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>After installing the BME280 library, and the Adafruit_Sensor library, open the Arduino IDE and, go to <strong>File<\/strong> &gt; <strong>Examples<\/strong> &gt; <strong>Adafruit BME280 library<\/strong> &gt; <strong>bme280 test<\/strong>.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Complete project details at http:\/\/randomnerdtutorials.com  \n*********\/\n\n#include &lt;Wire.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;\n#include &lt;Adafruit_BME280.h&gt;\n\n\/*#include &lt;SPI.h&gt;\n#define BME_SCK 14\n#define BME_MISO 12\n#define BME_MOSI 13\n#define BME_CS 15*\/\n\n#define SEALEVELPRESSURE_HPA (1013.25)\n\nAdafruit_BME280 bme; \/\/ I2C\n\/\/Adafruit_BME280 bme(BME_CS); \/\/ hardware SPI\n\/\/Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); \/\/ software SPI\n\nunsigned long delayTime;\n\nvoid setup() {\n  Serial.begin(9600);\n  Serial.println(F(&quot;BME280 test&quot;));\n\n  bool status;\n\n  \/\/ default settings\n  \/\/ (you can also pass in a Wire library object like &amp;Wire2)\n  status = bme.begin(0x76);  \n  if (!status) {\n    Serial.println(&quot;Could not find a valid BME280 sensor, check wiring!&quot;);\n    while (1);\n  }\n\n  Serial.println(&quot;-- Default Test --&quot;);\n  delayTime = 1000;\n\n  Serial.println();\n}\n\nvoid loop() { \n  printValues();\n  delay(delayTime);\n}\n\nvoid printValues() {\n  Serial.print(&quot;Temperature = &quot;);\n  Serial.print(bme.readTemperature());\n  Serial.println(&quot; *C&quot;);\n  \n  \/\/ Convert temperature to Fahrenheit\n  \/*Serial.print(&quot;Temperature = &quot;);\n  Serial.print(1.8 * bme.readTemperature() + 32);\n  Serial.println(&quot; *F&quot;);*\/\n  \n  Serial.print(&quot;Pressure = &quot;);\n  Serial.print(bme.readPressure() \/ 100.0F);\n  Serial.println(&quot; hPa&quot;);\n\n  Serial.print(&quot;Approx. Altitude = &quot;);\n  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));\n  Serial.println(&quot; m&quot;);\n\n  Serial.print(&quot;Humidity = &quot;);\n  Serial.print(bme.readHumidity());\n  Serial.println(&quot; %&quot;);\n\n  Serial.println();\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/ESP8266_BME280_Test.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>We&#8217;ve made a few modifications to the sketch to make it fully compatible with the ESP8266.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>Continue reading this section to learn how the code works, or skip to the &#8220;Demonstration&#8221; section.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Libraries<\/h3>\n\n\n\n<p>The code starts by including the needed libraries: the <span class=\"rnthl rntliteral\">wire<\/span> library to use I2C, and the <span class=\"rnthl rntliteral\">Adafruit_Sensor<\/span> and <span class=\"rnthl rntliteral\">Adafruit_BME280<\/span> libraries to interface with the BME280 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Wire.h>\n#include &lt;Adafruit_Sensor.h>\n#include &lt;Adafruit_BME280.h><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">SPI communication<\/h3>\n\n\n\n<p>As we&#8217;re going to use I2C communication, the following lines that define the SPI pins are commented:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/*#include &lt;SPI.h>\n#define BME_SCK 14\n#define BME_MISO 12\n#define BME_MOSI 13\n#define BME_CS 15*\/<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong> if you&#8217;re using SPI communication, use the ESP8266 default SPI pins:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>MOSI<\/strong><\/td><td><strong>MISO<\/strong><\/td><td><strong>CLK<\/strong><\/td><td><strong>CS<\/strong><\/td><\/tr><tr><td>GPIO 13<\/td><td>GPIO 12<\/td><td>GPIO 14<\/td><td>GPIO 15<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Sea level pressure<\/h3>\n\n\n\n<p>A variable called <span class=\"rnthl rntliteral\">SEALEVELPRESSURE_HPA<\/span> is created.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define SEALEVELPRESSURE_HPA (1013.25)<\/code><\/pre>\n\n\n\n<p>This variable saves the pressure at the sea level in hectopascal (is equivalent to milibar). This variable is used to estimate the altitude for a given pressure by comparing it with the sea level pressure. This example uses the default value, but for more accurate results, replace the value with the current sea level pressure at your location.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">I2C<\/h3>\n\n\n\n<p>This example uses I2C communication protocol by default. As you can see, you just need to create an <span class=\"rnthl rntliteral\">Adafruit_BME280<\/span> object called <span class=\"rnthl rntliteral\">bme<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_BME280 bme; \/\/ I2C<\/code><\/pre>\n\n\n\n<p>To use SPI, you need to comment this previous line and uncomment one of the following lines.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/Adafruit_BME280 bme(BME_CS); \/\/ hardware SPI\n\/\/Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); \/\/ software SPI<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">setup()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, start a serial communication:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(9600);<\/code><\/pre>\n\n\n\n<p>And initialize the sensor:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>status = bme.begin(0x76); \nif (!status) {\n  Serial.println(\"Could not find a valid BME280 sensor, check wiring!\");\n  while (1);\n}<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclblue\">We initialize the sensor with the <span class=\"rnthl rntliteral\">0x76<\/span> address. In case you&#8217;re not getting sensor readings, check the I2C address of your sensor. With the BME280 sensor wired to your ESP8266, run <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/master\/Projects\/LCD_I2C\/I2C_Scanner.ino\" target=\"_blank\">this I2C scanner sketch<\/a> to check the address of your sensor. Then, change the address if needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Printing values<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, the <span class=\"rnthl rntliteral\">printValues()<\/span> function reads the values from the BME280 and prints the results in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop() { \n  printValues();\n  delay(delayTime);\n}<\/code><\/pre>\n\n\n\n<p>Reading temperature, humidity, pressure, and estimate altitude is as simple as using the following methods on the <span class=\"rnthl rntliteral\">bme<\/span> object:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><span class=\"rnthl rntliteral\">bme.readTemperature()<\/span> &#8211; reads temperature in Celsius;<\/li><li><span class=\"rnthl rntliteral\">bme.readHumidity()<\/span> &#8211; reads absolute humidity;<\/li><li><span class=\"rnthl rntliteral\">bme.readPressure()<\/span> &#8211; reads pressure in hPa (hectoPascal = millibar);<\/li><li><span class=\"rnthl rntliteral\">bme.readAltitude(SEALEVELPRESSURE_HPA)<\/span> &#8211; estimates altitude in meters based on the pressure at the sea level.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Upload the code to your ESP8266, and open the Serial Monitor at a baud rate of 9600. Press the on-board RST button to run the code. You should see the readings displayed on the Serial Monitor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"469\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-readings-arduino-ide.png?resize=666%2C469&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Printing BME280 pressure, temperature and humidity readings with ESP32 in Arduino IDE Serial Monitor\" class=\"wp-image-86486\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-readings-arduino-ide.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-readings-arduino-ide.png?resize=300%2C211&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">ESP8266 Web Server with BME280 Sensor<\/h2>\n\n\n\n<p>The BME280 sensor measures temperature, humidity, and pressure. So, you can easily build a compact weather station and monitor the measurements using a web server built with the ESP8266 &#8211; that&#8217;s what we&#8217;re going to do in this section<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"420\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-ESP8266-BME280-web-server-temperature-humidity-pressure.jpg?resize=750%2C420&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Web Server with BME280 Sensor in Arduino IDE\" class=\"wp-image-86891\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-ESP8266-BME280-web-server-temperature-humidity-pressure.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-ESP8266-BME280-web-server-temperature-humidity-pressure.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>Copy the following code to your Arduino IDE. Don\u2019t upload it yet. First, you need to include your SSID and password.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/randomnerdtutorials.com  \n*********\/\n\n\/\/ Load Wi-Fi library\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;Wire.h&gt;\n#include &lt;Adafruit_BME280.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;\n\n\/\/uncomment the following lines if you're using SPI\n\/*#include &lt;SPI.h&gt;\n#define BME_SCK 14\n#define BME_MISO 12\n#define BME_MOSI 13\n#define BME_CS 15*\/\n\n#define SEALEVELPRESSURE_HPA (1013.25)\n\nAdafruit_BME280 bme; \/\/ I2C\n\/\/Adafruit_BME280 bme(BME_CS); \/\/ hardware SPI\n\/\/Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); \/\/ software SPI\n\n\/\/ Replace with your network credentials\nconst char* ssid     = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n\/\/ Set web server port number to 80\nWiFiServer server(80);\n\n\/\/ Variable to store the HTTP request\nString header;\n\nvoid setup() {\n  Serial.begin(115200);\n  bool status;\n\n  \/\/ default settings\n  \/\/ (you can also pass in a Wire library object like &amp;Wire2)\n  \/\/status = bme.begin();  \n  if (!bme.begin(0x76)) {\n    Serial.println(&quot;Could not find a valid BME280 sensor, check wiring!&quot;);\n    while (1);\n  }\n\n  \/\/ Connect to Wi-Fi network with SSID and password\n  Serial.print(&quot;Connecting to &quot;);\n  Serial.println(ssid);\n  WiFi.begin(ssid, password);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  \/\/ Print local IP address and start web server\n  Serial.println(&quot;&quot;);\n  Serial.println(&quot;WiFi connected.&quot;);\n  Serial.println(&quot;IP address: &quot;);\n  Serial.println(WiFi.localIP());\n  server.begin();\n}\n\nvoid loop(){\n  WiFiClient client = server.available();   \/\/ Listen for incoming clients\n\n  if (client) {                             \/\/ If a new client connects,\n    Serial.println(&quot;New Client.&quot;);          \/\/ print a message out in the serial port\n    String currentLine = &quot;&quot;;                \/\/ make a String to hold incoming data from the client\n    while (client.connected()) {            \/\/ loop while the client's connected\n      if (client.available()) {             \/\/ if there's bytes to read from the client,\n        char c = client.read();             \/\/ read a byte, then\n        Serial.write(c);                    \/\/ print it out the serial monitor\n        header += c;\n        if (c == '\\n') {                    \/\/ if the byte is a newline character\n          \/\/ if the current line is blank, you got two newline characters in a row.\n          \/\/ that's the end of the client HTTP request, so send a response:\n          if (currentLine.length() == 0) {\n            \/\/ HTTP headers always start with a response code (e.g. HTTP\/1.1 200 OK)\n            \/\/ and a content-type so the client knows what's coming, then a blank line:\n            client.println(&quot;HTTP\/1.1 200 OK&quot;);\n            client.println(&quot;Content-type:text\/html&quot;);\n            client.println(&quot;Connection: close&quot;);\n            client.println();\n            \n            \/\/ Display the HTML web page\n            client.println(&quot;&lt;!DOCTYPE html&gt;&lt;html&gt;&quot;);\n            client.println(&quot;&lt;head&gt;&lt;meta name=\\&quot;viewport\\&quot; content=\\&quot;width=device-width, initial-scale=1\\&quot;&gt;&quot;);\n            client.println(&quot;&lt;link rel=\\&quot;icon\\&quot; href=\\&quot;data:,\\&quot;&gt;&quot;);\n            \/\/ CSS to style the table \n            client.println(&quot;&lt;style&gt;body { text-align: center; font-family: \\&quot;Trebuchet MS\\&quot;, Arial;}&quot;);\n            client.println(&quot;table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }&quot;);\n            client.println(&quot;th { padding: 12px; background-color: #0043af; color: white; }&quot;);\n            client.println(&quot;tr { border: 1px solid #ddd; padding: 12px; }&quot;);\n            client.println(&quot;tr:hover { background-color: #bcbcbc; }&quot;);\n            client.println(&quot;td { border: none; padding: 12px; }&quot;);\n            client.println(&quot;.sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px; }&quot;);\n            \n            \/\/ Web Page Heading\n            client.println(&quot;&lt;\/style&gt;&lt;\/head&gt;&lt;body&gt;&lt;h1&gt;ESP8266 with BME280&lt;\/h1&gt;&quot;);\n            client.println(&quot;&lt;table&gt;&lt;tr&gt;&lt;th&gt;MEASUREMENT&lt;\/th&gt;&lt;th&gt;VALUE&lt;\/th&gt;&lt;\/tr&gt;&quot;);\n            client.println(&quot;&lt;tr&gt;&lt;td&gt;Temp. Celsius&lt;\/td&gt;&lt;td&gt;&lt;span class=\\&quot;sensor\\&quot;&gt;&quot;);\n            client.println(bme.readTemperature());\n            client.println(&quot; *C&lt;\/span&gt;&lt;\/td&gt;&lt;\/tr&gt;&quot;);  \n            client.println(&quot;&lt;tr&gt;&lt;td&gt;Temp. Fahrenheit&lt;\/td&gt;&lt;td&gt;&lt;span class=\\&quot;sensor\\&quot;&gt;&quot;);\n            client.println(1.8 * bme.readTemperature() + 32);\n            client.println(&quot; *F&lt;\/span&gt;&lt;\/td&gt;&lt;\/tr&gt;&quot;);       \n            client.println(&quot;&lt;tr&gt;&lt;td&gt;Pressure&lt;\/td&gt;&lt;td&gt;&lt;span class=\\&quot;sensor\\&quot;&gt;&quot;);\n            client.println(bme.readPressure() \/ 100.0F);\n            client.println(&quot; hPa&lt;\/span&gt;&lt;\/td&gt;&lt;\/tr&gt;&quot;);\n            client.println(&quot;&lt;tr&gt;&lt;td&gt;Approx. Altitude&lt;\/td&gt;&lt;td&gt;&lt;span class=\\&quot;sensor\\&quot;&gt;&quot;);\n            client.println(bme.readAltitude(SEALEVELPRESSURE_HPA));\n            client.println(&quot; m&lt;\/span&gt;&lt;\/td&gt;&lt;\/tr&gt;&quot;); \n            client.println(&quot;&lt;tr&gt;&lt;td&gt;Humidity&lt;\/td&gt;&lt;td&gt;&lt;span class=\\&quot;sensor\\&quot;&gt;&quot;);\n            client.println(bme.readHumidity());\n            client.println(&quot; %&lt;\/span&gt;&lt;\/td&gt;&lt;\/tr&gt;&quot;); \n            client.println(&quot;&lt;\/body&gt;&lt;\/html&gt;&quot;);\n            \n            \/\/ The HTTP response ends with another blank line\n            client.println();\n            \/\/ Break out of the while loop\n            break;\n          } else { \/\/ if you got a newline, then clear currentLine\n            currentLine = &quot;&quot;;\n          }\n        } else if (c != '\\r') {  \/\/ if you got anything else but a carriage return character,\n          currentLine += c;      \/\/ add it to the end of the currentLine\n        }\n      }\n    }\n    \/\/ Clear the header variable\n    header = &quot;&quot;;\n    \/\/ Close the connection\n    client.stop();\n    Serial.println(&quot;Client disconnected.&quot;);\n    Serial.println(&quot;&quot;);\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/BME280_Web_Server\/ESP8266_BME280_Web_Server.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>Continue reading this section to learn how the code works or skip to the &#8220;Demonstration&#8221; section.<\/p>\n\n\n\n<p>This sketch is very similar with the sketch used in the&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/esp8266-web-server\/\">ESP8266 Web Server Tutorial<\/a>. First, you include the <span class=\"rnthl rntliteral\">ESP8266WiFi<\/span> library and the needed libraries to read from the BME280 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Load Wi-Fi library\n#include &lt;ESP8266WiFi.h>\n#include &lt;Wire.h>\n#include &lt;Adafruit_BME280.h>\n#include &lt;Adafruit_Sensor.h><\/code><\/pre>\n\n\n\n<p>The next line defines a variable to save the pressure at the sea level. For more accurate altitude estimation, replace the value with the current sea level pressure at your location.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define SEALEVELPRESSURE_HPA (1013.25)<\/code><\/pre>\n\n\n\n<p>In the following line you create an <span class=\"rnthl rntliteral\">Adafruit_BME280<\/span> object called <span class=\"rnthl rntliteral\">bme<\/span> that by default establishes a communication with the sensor using I2C.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_BME280 bme; \/\/ I2C<\/code><\/pre>\n\n\n\n<p>As mentioned previously, you need to insert your ssid and password in the following lines inside the double quotes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid     = \"\";\nconst char* password = \"\";<\/code><\/pre>\n\n\n\n<p>Then, you set your web server to port 80.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Set web server port number to 80\nWiFiServer server(80);<\/code><\/pre>\n\n\n\n<p>The following line creates a variable to store the header of the HTTP request:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String header;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">setup()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, we start a serial communication at a baud rate of 115200 for debugging purposes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>You check that the BME280 sensor was successfully initialized.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (!bme.begin(0x76)) {\n  Serial.println(\"Could not find a valid BME280 sensor, check wiring!\");\n  while (1);<\/code><\/pre>\n\n\n\n<p>The following lines begin the Wi-Fi connection with <span class=\"rnthl rntliteral\">WiFi.begin(ssid, password)<\/span>, wait for a successful connection and print the ESP IP address in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Connect to Wi-Fi network with SSID and password\nSerial.print(\"Connecting to \");\nSerial.println(ssid);\nWiFi.begin(ssid, password);\nwhile (WiFi.status() != WL_CONNECTED) {\n  delay(500);\n  Serial.print(\".\");\n}\n\/\/ Print local IP address and start web server\nSerial.println(\"\");\nSerial.println(\"WiFi connected.\");\nSerial.println(\"IP address: \");\nSerial.println(WiFi.localIP());\nserver.begin();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">loop()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we program what happens when a new client establishes a connection with the web server. The ESP is always listening for incoming clients with this line:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFiClient client = server.available(); \/\/ Listen for incoming clients<\/code><\/pre>\n\n\n\n<p>When a request is received from a client, we\u2019ll save the incoming data. The&nbsp;while loop that follows will be running as long as the client stays connected.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (client) { \/\/ If a new client connects,\n  Serial.println(\"New Client.\"); \/\/ print a message out in the serial port\n  String currentLine = \"\"; \/\/ make a String to hold incoming data from the client\n  while (client.connected()) { \/\/ loop while the client's connected\n    if (client.available()) { \/\/ if there's bytes to read from the client,\n      char c = client.read(); \/\/ read a byte, then\n      Serial.write(c); \/\/ print it out the serial monitor\n      header += c;\n      if (c == '\\n') { \/\/ if the byte is a newline character\n        \/\/ if the current line is blank, you got two newline characters in a row.\n        \/\/ that's the end of the client HTTP request, so send a response:\n        if (currentLine.length() == 0) {\n          \/\/ HTTP headers always start with a response code (e.g. HTTP\/1.1 200 OK)\n          \/\/ and a content-type so the client knows what's coming, then a blank line:\n          client.println(\"HTTP\/1.1 200 OK\");\n          client.println(\"Content-type:text\/html\");\n          client.println(\"Connection: close\");\n          client.println();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Displaying the HTML web page<\/h3>\n\n\n\n<p>The next thing you need to do is sending a response to the client with the HTML text to build the web page.<\/p>\n\n\n\n<p>The web page is sent to the client using this expression <span class=\"rnthl rntliteral\">client.println()<\/span>. You should enter what you want to send to the client as an argument.<\/p>\n\n\n\n<p>The following code snippet sends the web page to display the sensor readings in a table.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>client.println(\"&lt;!DOCTYPE html>&lt;html>\");\nclient.println(\"&lt;head>&lt;meta name=\\\"viewport\\\" content=\\\"width=device-width, initial-scale=1\\\">\");\nclient.println(\"&lt;link rel=\\\"icon\\\" href=\\\"data:,\\\">\");\n\/\/ CSS to style the on\/off buttons \n\/\/ Feel free to change the background-color and font-size attributes to fit your preferences\nclient.println(\"&lt;style>body { text-align: center; font-family: \\\"Trebuchet MS\\\", Arial;}\");\nclient.println(\"table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }\");\nclient.println(\"th { padding: 12px; background-color: #0043af; color: white; }\");\nclient.println(\"tr { border: 1px solid #ddd; padding: 12px; }\");\nclient.println(\"tr:hover { background-color: #bcbcbc; }\");\nclient.println(\"td { border: none; padding: 12px; }\");\nclient.println(\".sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px; }\");\n \n\/\/ Web Page Heading\nclient.println(\"&lt;\/style>&lt;\/head>&lt;body>&lt;h1>ESP32 with BME280&lt;\/h1>\");\nclient.println(\"&lt;table>&lt;tr>&lt;th>MEASUREMENT&lt;\/th>&lt;th>VALUE&lt;\/th>&lt;\/tr>\");\nclient.println(\"&lt;tr>&lt;td>Temp. Celsius&lt;\/td>&lt;td>&lt;span class=\\\"sensor\\\">\");\nclient.println(bme.readTemperature());\nclient.println(\" *C&lt;\/span>&lt;\/td>&lt;\/tr>\"); \nclient.println(\"&lt;tr>&lt;td>Temp. Fahrenheit&lt;\/td>&lt;td>&lt;span class=\\\"sensor\\\">\");\nclient.println(1.8 * bme.readTemperature() + 32);\nclient.println(\" *F&lt;\/span>&lt;\/td>&lt;\/tr>\"); \nclient.println(\"&lt;tr>&lt;td>Pressure&lt;\/td>&lt;td>&lt;span class=\\\"sensor\\\">\");\nclient.println(bme.readPressure() \/ 100.0F);\nclient.println(\" hPa&lt;\/span>&lt;\/td>&lt;\/tr>\");\nclient.println(\"&lt;tr>&lt;td>Approx. Altitude&lt;\/td>&lt;td>&lt;span class=\\\"sensor\\\">\");\nclient.println(bme.readAltitude(SEALEVELPRESSURE_HPA));\nclient.println(\" m&lt;\/span>&lt;\/td>&lt;\/tr>\"); \nclient.println(\"&lt;tr>&lt;td>Humidity&lt;\/td>&lt;td>&lt;span class=\\\"sensor\\\">\");\nclient.println(bme.readHumidity());\nclient.println(\" %&lt;\/span>&lt;\/td>&lt;\/tr>\"); \nclient.println(\"&lt;\/body>&lt;\/html>\");<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong> you can <a href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/blob\/master\/Projects\/ESP8266\/BME280_Web_Server\/table_with_css.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"click here to view the full HTML web page (opens in a new tab)\">click here to view the full HTML web page<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Displaying the Sensor Readings<\/h3>\n\n\n\n<p>To display the sensor readings on the table, we just need to send them between the corresponding <span style=\"color: #333399;\">&lt;td&gt;<\/span> and <span style=\"color: #333399;\">&lt;\/td&gt;<\/span> tags. For example, to display the temperature:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>client.println(\"&lt;tr>&lt;td>Temp. Celsius&lt;\/td>&lt;td>&lt;span class=\\\"sensor\\\">\");\nclient.println(bme.readTemperature());\nclient.println(\" *C&lt;\/span>&lt;\/td>&lt;\/tr>\");<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>: the <span style=\"color: #333399;\">&lt;span&gt;<\/span> tag is useful to style a particular part of a text. In this case, we&#8217;re using the <span style=\"color: #333399;\">&lt;span&gt;<\/span> tag to include the sensor reading in a class called &#8220;sensor&#8221;. This is useful to style that particular part of text using CSS.<\/p>\n\n\n\n<p>By default the table is displaying the temperature readings in both Celsius degrees and Fahrenheit. You can comment the following three lines, if you want to display the temperature only in Fahrenheit degrees.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/*client.println(\"&lt;tr>&lt;td>Temp. Celsius&lt;\/td>&lt;td>&lt;span class=\\\"sensor\\\">\");\nclient.println(bme.readTemperature());\nclient.println(\" *C&lt;\/span>&lt;\/td>&lt;\/tr>\");*\/<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Closing the Connection<\/h3>\n\n\n\n<p>Finally, when the response ends, we clear the <span class=\"rnthl rntliteral\">header<\/span> variable, and stop the connection with the client with <span class=\"rnthl rntliteral\">client.stop()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Clear the header variable\nheader = \"\";\n\/\/ Close the connection\nclient.stop();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Web Server Demonstration<\/h2>\n\n\n\n<p>After inserting your network credentials you can upload the code to your board.<\/p>\n\n\n\n<p>Check that you have the right board and COM port selected, and upload the code to your ESP8266. After uploading, open the Serial Monitor at a baud rate of 115200, and copy the ESP8266 IP address.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"626\" height=\"445\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/IP-address-ESP8266.png?resize=626%2C445&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 IP address Serial Monitor\" class=\"wp-image-86865\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/IP-address-ESP8266.png?w=626&amp;quality=100&amp;strip=all&amp;ssl=1 626w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/IP-address-ESP8266.png?resize=300%2C213&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 626px) 100vw, 626px\" \/><\/figure><\/div>\n\n\n\n<p>Open your browser, paste the IP address, and you should see the latest sensor readings.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"731\" height=\"487\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/esp8266-bme280-web-server.png?resize=731%2C487&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 Web Server with ESP8266 demonstration\" class=\"wp-image-86864\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/esp8266-bme280-web-server.png?w=731&amp;quality=100&amp;strip=all&amp;ssl=1 731w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/esp8266-bme280-web-server.png?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 731px) 100vw, 731px\" \/><\/figure><\/div>\n\n\n\n<p>To update the readings, you just need to refresh the web page. <\/p>\n\n\n\n<p><strong>You may also like:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"ESP8266 with DHT11 Sensor - Asynchronous Web Server with Auto Updates (opens in a new tab)\">ESP8266 with DHT11 Sensor &#8211; Asynchronous Web Server with Auto Updates<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This article was an in-depth guide on how to get pressure, temperature and humidity readings from a BME280 sensor with the ESP8266 using Arduino IDE and display the readings on a web server.<\/p>\n\n\n\n<p>Now, you can take this project further and display your sensor readings in an OLED display; create a datalogger; save the readings in your own database or send the readings to your Home Automation platform using MQTT. Here&#8217;s some projects and tutorials that might help you implement these ideas:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-publish-sensor-readings-to-google-sheets\/\">ESP32 Publish Sensor Readings to Google Sheets<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/low-power-weather-station-datalogger-using-esp8266-bme280-micropython\/\">Low Power Weather Station Datalogger (MicroPython) <\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-mysql-database-php\/\">ESP32\/ESP8266 Insert Data into MySQL Database using PHP and Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-and-node-red-with-mqtt\/\">ESP8266 and Node-RED with MQTT (Publish and Subscribe)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/what-is-mqtt-and-how-it-works\/\">What is MQTT and How It Works<\/a><\/li><\/ul>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about the ESP8266 with our course: <a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\"><strong>Home Automation using ESP8266<\/strong><\/a><\/p>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide shows how to use the BME280 sensor module with the ESP8266 to read pressure, temperature, humidity and estimate altitude using Arduino IDE. The BME280 sensor uses I2C or &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP8266 with BME280 using Arduino IDE (Pressure, Temperature, Humidity)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-bme280-arduino-ide\/#more-86860\" aria-label=\"Read more about ESP8266 with BME280 using Arduino IDE (Pressure, Temperature, Humidity)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":86888,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[265,214,246,300,240,264],"tags":[],"class_list":["post-86860","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266-project","category-esp8266","category-esp8266-arduino-ide","category-0-esp8266","category-esp8266-projects","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-BME280.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/86860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=86860"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/86860\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/86888"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=86860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=86860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=86860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}