{"id":99640,"date":"2020-11-23T16:47:52","date_gmt":"2020-11-23T16:47:52","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=99640"},"modified":"2020-11-23T16:47:55","modified_gmt":"2020-11-23T16:47:55","slug":"micropython-esp32-esp8266-bme680-web-server","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-bme680-web-server\/","title":{"rendered":"MicroPython: ESP32\/ESP8266 BME680 Web Server (Weather Station)"},"content":{"rendered":"\n<p>This tutorial is a step-by-step guide that covers how to build a standalone ESP32 or ESP8266 NodeMCU Web Server that displays BME680 sensor readings using MicroPython firmware. We\u2019ll create an ESP32\/ESP8266 Web Server that is mobile responsive and it can be accessed with any device with a browser in your local network.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/MicroPython-BME680-ESP32-ESP8266-Temperature-Humidity-Pressure-Gas-Web-Server-Guide.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython BME680 ESP32 ESP8266 Temperature Humidity Pressure Gas Web Server Guide\" class=\"wp-image-99613\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/MicroPython-BME680-ESP32-ESP8266-Temperature-Humidity-Pressure-Gas-Web-Server-Guide.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/MicroPython-BME680-ESP32-ESP8266-Temperature-Humidity-Pressure-Gas-Web-Server-Guide.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/MicroPython-BME680-ESP32-ESP8266-Temperature-Humidity-Pressure-Gas-Web-Server-Guide.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/MicroPython-BME680-ESP32-ESP8266-Temperature-Humidity-Pressure-Gas-Web-Server-Guide.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>If you want to learn how a relay module works, read our MicroPython Guide: <a href=\"https:\/\/randomnerdtutorials.com\/micropython-bme680-esp32-esp8266\/\">BME680 with ESP32 and ESP8266 (Temperature, Humidity, Pressure, Gas)<\/a>.<\/p>\n\n\n\n<p>We have similar guides using Arduino IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-bme680-sensor-arduino\/\">ESP32: BME680 Environmental Sensor using Arduino IDE (Gas, Pressure, Humidity, Temperature)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-bme680-sensor-arduino\/\">ESP8266 NodeMCU: BME680 Environmental Sensor using Arduino IDE (Gas, Pressure, Humidity, Temperature)<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your ESP32 or ESP8266 boards. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE or uPyCraft IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Thonny IDE:<ul><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Installing and getting started with Thonny IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/flashing-micropython-firmware-esptool-py-esp32-esp8266\/\">Flashing MicroPython Firmware with esptool.py<\/a><\/li><\/ul><\/li><li>uPyCraft IDE:<ul><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">Getting Started with uPyCraft IDE<\/a><\/li><li>Install uPyCraft IDE (<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-windows-pc-instructions\/\">Windows<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-mac-os-x-instructions\/\">Mac OS X<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-linux-ubuntu-instructions\/\">Linux<\/a>)<\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li><\/ul><\/li><\/ul>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about MicroPython:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266 eBook<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">BME680 Environmental Sensor Module<\/h2>\n\n\n\n<p>The BME680 is an environmental sensor that combines gas, pressure, humidity and temperature sensors. The gas sensor can detect a broad range of gases like volatile organic compounds (VOC). For this reason, the BME680 can be used in indoor air quality control.<\/p>\n\n\n\n<p>The BME680 supports I2C and SPI Interfaces.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-sensor-humidity-barometric-pressure-ambient-temperature-gas-air-quality-back.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME680 Gas sensor humidity barometric pressure ambient temperature gas air quality back\" class=\"wp-image-98119\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-sensor-humidity-barometric-pressure-ambient-temperature-gas-air-quality-back.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-sensor-humidity-barometric-pressure-ambient-temperature-gas-air-quality-back.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">BME680 I2C<\/h4>\n\n\n\n<p>This sensor communicates using I2C communication protocol, so the wiring is very simple. You can use the default ESP32 or ESP8266 I2C pins as shown in the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>BME680<\/strong><\/td><td><strong>ESP32<\/strong><\/td><td><strong>ESP8266<\/strong><\/td><\/tr><tr><td>Vin<\/td><td><span class=\"rnthl rntcred\">3.3V<\/span><\/td><td><span class=\"rnthl rntcred\">3.3V<\/span><\/td><\/tr><tr><td>GND<\/td><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><\/tr><tr><td>SCL<\/td><td><span class=\"rnthl rntclgray\">GPIO 22<\/span><\/td><td><span class=\"rnthl rntclgray\">GPIO 5<\/span> (D1) <\/td><\/tr><tr><td>SDA<\/td><td><span class=\"rnthl rntcyellow\">GPIO 21<\/span><\/td><td><span class=\"rnthl rntcyellow\">GPIO 4<\/span> (D2) <\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-Board-BME680-Gas-sensor-circuit-wiring-diagram-schematics.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Board BME680 Gas sensor circuit wiring diagram schematics\" class=\"wp-image-98142\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-Board-BME680-Gas-sensor-circuit-wiring-diagram-schematics.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-Board-BME680-Gas-sensor-circuit-wiring-diagram-schematics.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>For this project you need to wire the BME680 sensor module to the ESP32 or ESP8266 I2C pins. Here\u2019s a list of parts you need for this tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a style=\"font-size: inherit; background-color: initial;\" rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/bme680-gas-sensor-module\/\" target=\"_blank\">BME680 sensor module<\/a><\/li><li><a aria-label=\" (opens in a new tab)\" style=\"font-size: inherit; background-color: initial;\" rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a><span style=\"font-size: inherit; background-color: initial;\">&nbsp;or&nbsp;<\/span><a aria-label=\" (opens in a new tab)\" style=\"font-size: inherit; background-color: initial;\" rel=\"noreferrer noopener\" 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;\">&nbsp;(read&nbsp;<\/span><a aria-label=\" (opens in a new tab)\" style=\"font-size: inherit; background-color: initial;\" rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/esp32-vs-esp8266\/\" target=\"_blank\">ESP32 vs ESP8266<\/a><span style=\"font-size: inherit; background-color: initial;\">)<\/span><\/li><li><a aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><li><a aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" 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; ESP32<\/h2>\n\n\n\n<p>Follow the next schematic diagram if you\u2019re using an ESP32 board:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"669\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32_BME680_Wiring_Diagram_I2C.png?resize=786%2C669&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 BME680 Wiring Diagram I2C\" class=\"wp-image-98165\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32_BME680_Wiring_Diagram_I2C.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32_BME680_Wiring_Diagram_I2C.png?resize=300%2C255&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32_BME680_Wiring_Diagram_I2C.png?resize=768%2C654&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 Pinout Reference Guide<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Schematic &#8211; ESP8266<\/h2>\n\n\n\n<p>Follow the next schematic diagram if you\u2019re using an ESP8266 board:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"831\" height=\"531\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP8266-BME680-Environmental-Sensor-Wiring-Diagram-I2C.png?resize=831%2C531&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU BME680 Environmental Sensor Wiring Diagram I2C\" class=\"wp-image-98104\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP8266-BME680-Environmental-Sensor-Wiring-Diagram-I2C.png?w=831&amp;quality=100&amp;strip=all&amp;ssl=1 831w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP8266-BME680-Environmental-Sensor-Wiring-Diagram-I2C.png?resize=300%2C192&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP8266-BME680-Environmental-Sensor-Wiring-Diagram-I2C.png?resize=768%2C491&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 831px) 100vw, 831px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pinout-reference-gpios\/\">ESP8266 Pinout Reference Guide<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">BME680 MicroPython Library<\/h2>\n\n\n\n<p>The library to read from the BME680 sensor isn\u2019t part of the standard MicroPython library by default. So, you need to upload the following library to your ESP32\/ESP8266 board (save it with the name <em>bme680.py<\/em>).<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Spaces, comments and some functions have been removed from the original file to save memory\n# Original source: https:\/\/github.com\/adafruit\/Adafruit_CircuitPython_BME680\/blob\/master\/adafruit_bme680.py\nimport time\nimport math\nfrom micropython import const\nfrom ubinascii import hexlify as hex\ntry:\n  import struct\nexcept ImportError:\n  import ustruct as struct\n_BME680_CHIPID = const(0x61)\n_BME680_REG_CHIPID = const(0xD0)\n_BME680_BME680_COEFF_ADDR1 = const(0x89)\n_BME680_BME680_COEFF_ADDR2 = const(0xE1)\n_BME680_BME680_RES_HEAT_0 = const(0x5A)\n_BME680_BME680_GAS_WAIT_0 = const(0x64)\n_BME680_REG_SOFTRESET = const(0xE0)\n_BME680_REG_CTRL_GAS = const(0x71)\n_BME680_REG_CTRL_HUM = const(0x72)\n_BME280_REG_STATUS = const(0xF3)\n_BME680_REG_CTRL_MEAS = const(0x74)\n_BME680_REG_CONFIG = const(0x75)\n_BME680_REG_PAGE_SELECT = const(0x73)\n_BME680_REG_MEAS_STATUS = const(0x1D)\n_BME680_REG_PDATA = const(0x1F)\n_BME680_REG_TDATA = const(0x22)\n_BME680_REG_HDATA = const(0x25)\n_BME680_SAMPLERATES = (0, 1, 2, 4, 8, 16)\n_BME680_FILTERSIZES = (0, 1, 3, 7, 15, 31, 63, 127)\n_BME680_RUNGAS = const(0x10)\n_LOOKUP_TABLE_1 = (2147483647.0, 2147483647.0, 2147483647.0, 2147483647.0, 2147483647.0,\n  2126008810.0, 2147483647.0, 2130303777.0, 2147483647.0, 2147483647.0,\n  2143188679.0, 2136746228.0, 2147483647.0, 2126008810.0, 2147483647.0,\n  2147483647.0)\n_LOOKUP_TABLE_2 = (4096000000.0, 2048000000.0, 1024000000.0, 512000000.0, 255744255.0, 127110228.0,\n  64000000.0, 32258064.0, 16016016.0, 8000000.0, 4000000.0, 2000000.0, 1000000.0,\n  500000.0, 250000.0, 125000.0)\ndef _read24(arr):\n  ret = 0.0\n  for b in arr:\n    ret *= 256.0\n    ret += float(b &amp; 0xFF)\n  return ret\nclass Adafruit_BME680:\n  def __init__(self, *, refresh_rate=10):\n    self._write(_BME680_REG_SOFTRESET, [0xB6])\n    time.sleep(0.005)\n    chip_id = self._read_byte(_BME680_REG_CHIPID)\n    if chip_id != _BME680_CHIPID:\n      raise RuntimeError('Failed 0x%x' % chip_id)\n    self._read_calibration()\n    self._write(_BME680_BME680_RES_HEAT_0, [0x73])\n    self._write(_BME680_BME680_GAS_WAIT_0, [0x65])\n    self.sea_level_pressure = 1013.25\n    self._pressure_oversample = 0b011\n    self._temp_oversample = 0b100\n    self._humidity_oversample = 0b010\n    self._filter = 0b010\n    self._adc_pres = None\n    self._adc_temp = None\n    self._adc_hum = None\n    self._adc_gas = None\n    self._gas_range = None\n    self._t_fine = None\n    self._last_reading = 0\n    self._min_refresh_time = 1000 \/ refresh_rate\n  @property\n  def pressure_oversample(self):\n    return _BME680_SAMPLERATES[self._pressure_oversample]\n  @pressure_oversample.setter\n  def pressure_oversample(self, sample_rate):\n    if sample_rate in _BME680_SAMPLERATES:\n      self._pressure_oversample = _BME680_SAMPLERATES.index(sample_rate)\n    else:\n      raise RuntimeError(&quot;Invalid&quot;)\n  @property\n  def humidity_oversample(self):\n    return _BME680_SAMPLERATES[self._humidity_oversample]\n  @humidity_oversample.setter\n  def humidity_oversample(self, sample_rate):\n    if sample_rate in _BME680_SAMPLERATES:\n      self._humidity_oversample = _BME680_SAMPLERATES.index(sample_rate)\n    else:\n      raise RuntimeError(&quot;Invalid&quot;)\n  @property\n  def temperature_oversample(self):\n      return _BME680_SAMPLERATES[self._temp_oversample]\n  @temperature_oversample.setter\n  def temperature_oversample(self, sample_rate):\n    if sample_rate in _BME680_SAMPLERATES:\n      self._temp_oversample = _BME680_SAMPLERATES.index(sample_rate)\n    else:\n      raise RuntimeError(&quot;Invalid&quot;)\n  @property\n  def filter_size(self):\n    return _BME680_FILTERSIZES[self._filter]\n  @filter_size.setter\n  def filter_size(self, size):\n    if size in _BME680_FILTERSIZES:\n      self._filter = _BME680_FILTERSIZES[size]\n    else:\n      raise RuntimeError(&quot;Invalid&quot;)\n  @property\n  def temperature(self):\n    self._perform_reading()\n    calc_temp = (((self._t_fine * 5) + 128) \/ 256)\n    return calc_temp \/ 100\n  @property\n  def pressure(self):\n    self._perform_reading()\n    var1 = (self._t_fine \/ 2) - 64000\n    var2 = ((var1 \/ 4) * (var1 \/ 4)) \/ 2048\n    var2 = (var2 * self._pressure_calibration[5]) \/ 4\n    var2 = var2 + (var1 * self._pressure_calibration[4] * 2)\n    var2 = (var2 \/ 4) + (self._pressure_calibration[3] * 65536)\n    var1 = (((((var1 \/ 4) * (var1 \/ 4)) \/ 8192) *\n      (self._pressure_calibration[2] * 32) \/ 8) +\n      ((self._pressure_calibration[1] * var1) \/ 2))\n    var1 = var1 \/ 262144\n    var1 = ((32768 + var1) * self._pressure_calibration[0]) \/ 32768\n    calc_pres = 1048576 - self._adc_pres\n    calc_pres = (calc_pres - (var2 \/ 4096)) * 3125\n    calc_pres = (calc_pres \/ var1) * 2\n    var1 = (self._pressure_calibration[8] * (((calc_pres \/ 8) * (calc_pres \/ 8)) \/ 8192)) \/ 4096\n    var2 = ((calc_pres \/ 4) * self._pressure_calibration[7]) \/ 8192\n    var3 = (((calc_pres \/ 256) ** 3) * self._pressure_calibration[9]) \/ 131072\n    calc_pres += ((var1 + var2 + var3 + (self._pressure_calibration[6] * 128)) \/ 16)\n    return calc_pres\/100\n  @property\n  def humidity(self):\n    self._perform_reading()\n    temp_scaled = ((self._t_fine * 5) + 128) \/ 256\n    var1 = ((self._adc_hum - (self._humidity_calibration[0] * 16)) -\n      ((temp_scaled * self._humidity_calibration[2]) \/ 200))\n    var2 = (self._humidity_calibration[1] *\n      (((temp_scaled * self._humidity_calibration[3]) \/ 100) +\n       (((temp_scaled * ((temp_scaled * self._humidity_calibration[4]) \/ 100)) \/\n         64) \/ 100) + 16384)) \/ 1024\n    var3 = var1 * var2\n    var4 = self._humidity_calibration[5] * 128\n    var4 = (var4 + ((temp_scaled * self._humidity_calibration[6]) \/ 100)) \/ 16\n    var5 = ((var3 \/ 16384) * (var3 \/ 16384)) \/ 1024\n    var6 = (var4 * var5) \/ 2\n    calc_hum = (((var3 + var6) \/ 1024) * 1000) \/ 4096\n    calc_hum \/= 1000\n    if calc_hum &gt; 100:\n      calc_hum = 100\n    if calc_hum &lt; 0:\n      calc_hum = 0\n    return calc_hum\n  @property\n  def altitude(self):\n    pressure = self.pressure\n    return 44330 * (1.0 - math.pow(pressure \/ self.sea_level_pressure, 0.1903))\n  @property\n  def gas(self):\n    self._perform_reading()\n    var1 = ((1340 + (5 * self._sw_err)) * (_LOOKUP_TABLE_1[self._gas_range])) \/ 65536\n    var2 = ((self._adc_gas * 32768) - 16777216) + var1\n    var3 = (_LOOKUP_TABLE_2[self._gas_range] * var1) \/ 512\n    calc_gas_res = (var3 + (var2 \/ 2)) \/ var2\n    return int(calc_gas_res)\n  def _perform_reading(self):\n    if (time.ticks_diff(self._last_reading, time.ticks_ms()) * time.ticks_diff(0, 1)\n        &lt; self._min_refresh_time):\n      return\n    self._write(_BME680_REG_CONFIG, [self._filter &lt;&lt; 2])\n    self._write(_BME680_REG_CTRL_MEAS,\n      [(self._temp_oversample &lt;&lt; 5)|(self._pressure_oversample &lt;&lt; 2)])\n    self._write(_BME680_REG_CTRL_HUM, [self._humidity_oversample])\n    self._write(_BME680_REG_CTRL_GAS, [_BME680_RUNGAS])\n    ctrl = self._read_byte(_BME680_REG_CTRL_MEAS)\n    ctrl = (ctrl &amp; 0xFC) | 0x01\n    self._write(_BME680_REG_CTRL_MEAS, [ctrl])\n    new_data = False\n    while not new_data:\n      data = self._read(_BME680_REG_MEAS_STATUS, 15)\n      new_data = data[0] &amp; 0x80 != 0\n      time.sleep(0.005)\n    self._last_reading = time.ticks_ms()\n    self._adc_pres = _read24(data[2:5]) \/ 16\n    self._adc_temp = _read24(data[5:8]) \/ 16\n    self._adc_hum = struct.unpack('&gt;H', bytes(data[8:10]))[0]\n    self._adc_gas = int(struct.unpack('&gt;H', bytes(data[13:15]))[0] \/ 64)\n    self._gas_range = data[14] &amp; 0x0F\n    var1 = (self._adc_temp \/ 8) - (self._temp_calibration[0] * 2)\n    var2 = (var1 * self._temp_calibration[1]) \/ 2048\n    var3 = ((var1 \/ 2) * (var1 \/ 2)) \/ 4096\n    var3 = (var3 * self._temp_calibration[2] * 16) \/ 16384\n    self._t_fine = int(var2 + var3)\n  def _read_calibration(self):\n    coeff = self._read(_BME680_BME680_COEFF_ADDR1, 25)\n    coeff += self._read(_BME680_BME680_COEFF_ADDR2, 16)\n    coeff = list(struct.unpack('&lt;hbBHhbBhhbbHhhBBBHbbbBbHhbb', bytes(coeff[1:39])))\n    coeff = [float(i) for i in coeff]\n    self._temp_calibration = [coeff[x] for x in [23, 0, 1]]\n    self._pressure_calibration = [coeff[x] for x in [3, 4, 5, 7, 8, 10, 9, 12, 13, 14]]\n    self._humidity_calibration = [coeff[x] for x in [17, 16, 18, 19, 20, 21, 22]]\n    self._gas_calibration = [coeff[x] for x in [25, 24, 26]]\n    self._humidity_calibration[1] *= 16\n    self._humidity_calibration[1] += self._humidity_calibration[0] % 16\n    self._humidity_calibration[0] \/= 16\n    self._heat_range = (self._read_byte(0x02) &amp; 0x30) \/ 16\n    self._heat_val = self._read_byte(0x00)\n    self._sw_err = (self._read_byte(0x04) &amp; 0xF0) \/ 16\n  def _read_byte(self, register):\n    return self._read(register, 1)[0]\n  def _read(self, register, length):\n    raise NotImplementedError()\n  def _write(self, register, values):\n    raise NotImplementedError()\nclass BME680_I2C(Adafruit_BME680):\n  def __init__(self, i2c, address=0x77, debug=False, *, refresh_rate=10):\n    self._i2c = i2c\n    self._address = address\n    self._debug = debug\n    super().__init__(refresh_rate=refresh_rate)\n  def _read(self, register, length):\n    result = bytearray(length)\n    self._i2c.readfrom_mem_into(self._address, register &amp; 0xff, result)\n    if self._debug:\n      print(&quot;\\t${:x} read &quot;.format(register), &quot; &quot;.join([&quot;{:02x}&quot;.format(i) for i in result]))\n    return result\n  def _write(self, register, values):\n    if self._debug:\n      print(&quot;\\t${:x} write&quot;.format(register), &quot; &quot;.join([&quot;{:02x}&quot;.format(i) for i in values]))\n    for value in values:\n      self._i2c.writeto_mem(self._address, register, bytearray([value &amp; 0xFF]))\n      register += 1\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/bme680.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Follow the next set of instructions for the IDE you&#8217;re using:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A. Upload BME680 library with <strong>uPyCraft IDE<\/strong><\/li><li>B. Upload BME680 library with <strong>Thonny IDE<\/strong><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">A. Upload BME680 library with uPyCraft IDE<\/h3>\n\n\n\n<p>This section shows how to upload a library using uPyCraft IDE. If you&#8217;re using Thonny IDE, read the next section.<\/p>\n\n\n\n<p><strong>1.<\/strong> Create a new file by pressing the <strong>New File<\/strong> button (1).<\/p>\n\n\n\n<p><strong>2.<\/strong> Copy the BME680 library code into that file. The <a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/master\/Projects\/ESP-MicroPython\/bme680.py\" target=\"_blank\" rel=\"noreferrer noopener\">BME680 library code can be found here<\/a>.<\/p>\n\n\n\n<p><strong>3.<\/strong> After copying the code, save the file by pressing the <strong>Save <\/strong>button (2).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1002\" height=\"529\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/copy-and-save-bme280-library-upycraft-ide.png?resize=1002%2C529&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install BME280 library MicroPython ESP32 ESP8266 uPyCraft IDE step 1\" class=\"wp-image-86441\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/copy-and-save-bme280-library-upycraft-ide.png?w=1002&amp;quality=100&amp;strip=all&amp;ssl=1 1002w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/copy-and-save-bme280-library-upycraft-ide.png?resize=300%2C158&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/copy-and-save-bme280-library-upycraft-ide.png?resize=768%2C405&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1002px) 100vw, 1002px\" \/><\/figure><\/div>\n\n\n\n<p><strong>4.<\/strong> Call this new file \u201c<strong>bme680.py<\/strong>\u201d and press <strong>ok<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"112\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/bme680-new-micropython-file.png?resize=402%2C112&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"bme680.py new MicroPython file\" class=\"wp-image-99508\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/bme680-new-micropython-file.png?w=402&amp;quality=100&amp;strip=all&amp;ssl=1 402w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/bme680-new-micropython-file.png?resize=300%2C84&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 402px) 100vw, 402px\" \/><\/figure><\/div>\n\n\n\n<p><strong>5.<\/strong> Click the <strong>Download and Run<\/strong> button.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"41\" height=\"52\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/download-run.png?resize=41%2C52&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install BME280 library MicroPython ESP32 ESP8266 uPyCraft IDE step 3\" class=\"wp-image-75031\"\/><\/figure><\/div>\n\n\n\n<p>The file should be saved on the device folder with the name \u201c<strong>bme680.py<\/strong>\u201d as highlighted in the following figure.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"884\" height=\"567\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/uPyCraft-IDE-Saved-library-file-to-device-ESP32-ESP8266.png?resize=884%2C567&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"uPyCraft IDE Save library file to device ESP32 ESP8266\" class=\"wp-image-99606\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/uPyCraft-IDE-Saved-library-file-to-device-ESP32-ESP8266.png?w=884&amp;quality=100&amp;strip=all&amp;ssl=1 884w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/uPyCraft-IDE-Saved-library-file-to-device-ESP32-ESP8266.png?resize=300%2C192&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/uPyCraft-IDE-Saved-library-file-to-device-ESP32-ESP8266.png?resize=768%2C493&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 884px) 100vw, 884px\" \/><\/figure><\/div>\n\n\n\n<p>Now, you can use the library functionalities in your code by importing the library.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">B. Upload BME680 library with Thonny IDE <\/h3>\n\n\n\n<p>If you\u2019re using Thonny IDE, follow the next steps:<\/p>\n\n\n\n<p><strong>1.<\/strong> Copy the library code to a new file. The <a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/master\/Projects\/ESP-MicroPython\/bme680.py\" target=\"_blank\" rel=\"noreferrer noopener\">BME680 library code can be found here<\/a>.<\/p>\n\n\n\n<p><strong>2.<\/strong> Go to <strong>File<\/strong> &gt; <strong>Save as&#8230;<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"206\" height=\"294\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/Thonny-IDE-ESP32-ESP8266-MicroPython-Save-file-library-to-device-save-as.png?resize=206%2C294&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE ESP32 ESP8266 MicroPython Save file library to device save as\" class=\"wp-image-99603\"\/><\/figure><\/div>\n\n\n\n<p><strong>3.<\/strong> Select save to &#8220;<strong>MicroPython device<\/strong>&#8220;:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"220\" height=\"202\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/Thonny-IDE-ESP32-ESP8266-MicroPython-Save-file-library-to-device-select.png?resize=220%2C202&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE ESP32 ESP8266 MicroPython Save file library to device select\" class=\"wp-image-99605\"\/><\/figure><\/div>\n\n\n\n<p><strong>4.<\/strong> Name your file as <em>bme680.py<\/em> and press the OK button:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"553\" height=\"409\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/Thonny-IDE-ESP32-ESP8266-MicroPython-Save-file-library-to-device-name-file.png?resize=553%2C409&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE ESP32 ESP8266 MicroPython Save file library to device name file\" class=\"wp-image-99602\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/Thonny-IDE-ESP32-ESP8266-MicroPython-Save-file-library-to-device-name-file.png?w=553&amp;quality=100&amp;strip=all&amp;ssl=1 553w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/Thonny-IDE-ESP32-ESP8266-MicroPython-Save-file-library-to-device-name-file.png?resize=300%2C222&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><\/figure><\/div>\n\n\n\n<p>And that&#8217;s it. The library was uploaded to your board. To make sure that it was uploaded successfully, go to File &gt; Save as&#8230; and select the MicroPython device. Your file should be listed there:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"553\" height=\"409\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/Thonny-IDE-ESP32-ESP8266-MicroPython-Save-file-library-to-device-saved.png?resize=553%2C409&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE ESP32 ESP8266 MicroPython Save file library to device saved\" class=\"wp-image-99604\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/Thonny-IDE-ESP32-ESP8266-MicroPython-Save-file-library-to-device-saved.png?w=553&amp;quality=100&amp;strip=all&amp;ssl=1 553w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/Thonny-IDE-ESP32-ESP8266-MicroPython-Save-file-library-to-device-saved.png?resize=300%2C222&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><\/figure><\/div>\n\n\n\n<p>After uploading the library to your board, you can use the library functionalities in your code by importing the library.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code Web Server &#8211; BME680 Temperature, Humidity, Pressure, and Gas Air Quality<\/h2>\n\n\n\n<p>Now that you know how to get temperature, humidity, pressure and gas from the BME680 sensor, we&#8217;ll display the sensor readings on a web server that you can access on your local network.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-ESP8266-MicroPython-BME680-Web-Server-Test-mobile-responsive.jpg?resize=470%2C561&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 MicroPython BME680 Web Server Test mobile responsive\" class=\"wp-image-99612\" width=\"470\" height=\"561\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-ESP8266-MicroPython-BME680-Web-Server-Test-mobile-responsive.jpg?w=587&amp;quality=100&amp;strip=all&amp;ssl=1 587w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-ESP8266-MicroPython-BME680-Web-Server-Test-mobile-responsive.jpg?resize=252%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 252w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure><\/div>\n\n\n\n<p>For this example, you need three files:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong><em>bme680.py<\/em><\/strong>: this is the file that contains all the methods to use the BME680 sensor. That&#8217;s the file you&#8217;ve uploaded previously.<\/li><li><strong><em>boot.py<\/em><\/strong>: runs when the device starts and sets up several configuration options like your network credentials, importing libraries, setting the pins, etc.<\/li><li><strong><em>main.py<\/em><\/strong>: this is the main script where we&#8217;ll handle the web server. It executes immediately after the <em>boot.py<\/em>.<\/li><\/ul>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>Note: <\/strong> It is a good practice to include the boot.py and main.py files. However, if you prefer, you can include all the code in the main.py file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">boot.py<\/h3>\n\n\n\n<p>Create a new file in your IDE called <em>boot.py<\/em> and copy the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-bme680-esp32-esp8266\/\n\ntry:\n  import usocket as socket\nexcept:\n  import socket\n  \nfrom time import sleep\n\nfrom machine import Pin, I2C\nimport network\n\nimport esp\nesp.osdebug(None)\n\nimport gc\ngc.collect()\n\nfrom bme680 import *\n\n# ESP32 - Pin assignment\ni2c = I2C(scl=Pin(22), sda=Pin(21))\n# ESP8266 - Pin assignment\n#i2c = I2C(scl=Pin(5), sda=Pin(4))\n\nssid = 'REPLACE_WITH_YOUR_SSID'\npassword = 'REPLACE_WITH_YOUR_PASSWORD'\n\nstation = network.WLAN(network.STA_IF)\n\nstation.active(True)\nstation.connect(ssid, password)\n\nwhile station.isconnected() == False:\n  pass\n\nprint('Connection successful')\nprint(station.ifconfig())\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/esp_bme680_web_server_boot.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>This file imports the necessary libraries, defines the I2C pins to connect to the sensor and connects to your network.<\/p>\n\n\n\n<p>In the code, we&#8217;re using the ESP32 I2C pins:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>i2c = I2C(scl=Pin(22), sda=Pin(21))<\/code><\/pre>\n\n\n\n<p>If you&#8217;re using the ESP8266, comment the previous line and uncomment the following:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>i2c = I2C(scl=Pin(5), sda=Pin(4))<\/code><\/pre>\n\n\n\n<p>Then, insert your network credentials in the following variables:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>ssid = 'REPLACE_WITH_YOUR_SSID'\npassword = 'REPLACE_WITH_YOUR_PASSWORD'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">main.py<\/h3>\n\n\n\n<p>In the <em>main.py<\/em> file is where we&#8217;ll create the web server and handle the requests. Copy the following code to your <em>main.py<\/em> file. <\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-bme680-esp32-esp8266\/\n\ndef web_page():\n  bme = BME680_I2C(i2c=i2c)\n  \n  html = &quot;&quot;&quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;ESP with BME680&lt;\/title&gt;\n  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\n  &lt;link rel=&quot;icon&quot; href=&quot;data:,&quot;&gt;&lt;style&gt;body { text-align: center; font-family: &quot;Trebuchet MS&quot;, Arial;}\n  table { border-collapse: collapse; margin-left:auto; margin-right:auto; }\n  th { padding: 12px; background-color: #0043af; color: white; }\n  tr { border: 1px solid #ddd; padding: 12px; }\n  tr:hover { background-color: #bcbcbc; }\n  td { border: none; padding: 12px; }\n  .sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px;\n  &lt;\/style&gt;&lt;\/head&gt;&lt;body&gt;&lt;h1&gt;ESP with BME680&lt;\/h1&gt;\n  &lt;table&gt;&lt;tr&gt;&lt;th&gt;MEASUREMENT&lt;\/th&gt;&lt;th&gt;VALUE&lt;\/th&gt;&lt;\/tr&gt;\n  &lt;tr&gt;&lt;td&gt;Temp. Celsius&lt;\/td&gt;&lt;td&gt;&lt;span class=&quot;sensor&quot;&gt;&quot;&quot;&quot; + str(round(bme.temperature, 2)) + &quot;&quot;&quot; C&lt;\/span&gt;&lt;\/td&gt;&lt;\/tr&gt;\n  &lt;tr&gt;&lt;td&gt;Temp. Fahrenheit&lt;\/td&gt;&lt;td&gt;&lt;span class=&quot;sensor&quot;&gt;&quot;&quot;&quot; + str(round((bme.temperature) * (9\/5) + 32, 2))  + &quot;&quot;&quot; F&lt;\/span&gt;&lt;\/td&gt;&lt;\/tr&gt;\n  &lt;tr&gt;&lt;td&gt;Pressure&lt;\/td&gt;&lt;td&gt;&lt;span class=&quot;sensor&quot;&gt;&quot;&quot;&quot; + str(round(bme.pressure, 2)) + &quot;&quot;&quot; hPa&lt;\/span&gt;&lt;\/td&gt;&lt;\/tr&gt;\n  &lt;tr&gt;&lt;td&gt;Humidity&lt;\/td&gt;&lt;td&gt;&lt;span class=&quot;sensor&quot;&gt;&quot;&quot;&quot; + str(round(bme.humidity, 2)) + &quot;&quot;&quot; %&lt;\/span&gt;&lt;\/td&gt;&lt;\/tr&gt;\n  &lt;tr&gt;&lt;td&gt;Gas&lt;\/td&gt;&lt;td&gt;&lt;span class=&quot;sensor&quot;&gt;&quot;&quot;&quot; + str(round(bme.gas\/1000, 2)) + &quot;&quot;&quot; KOhms&lt;\/span&gt;&lt;\/td&gt;&lt;\/tr&gt;&lt;\/body&gt;&lt;\/html&gt;&quot;&quot;&quot;\n  return html\n\ns = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\ns.bind(('', 80))\ns.listen(5)\n\nwhile True:\n  try:\n    if gc.mem_free() &lt; 102000:\n      gc.collect()\n    conn, addr = s.accept()\n    conn.settimeout(3.0)\n    print('Got a connection from %s' % str(addr))\n    request = conn.recv(1024)\n    conn.settimeout(None)\n    request = str(request)\n    print('Content = %s' % request)\n    response = web_page()\n    conn.send('HTTP\/1.1 200 OK\\n')\n    conn.send('Content-Type: text\/html\\n')\n    conn.send('Connection: close\\n\\n')\n    conn.sendall(response)\n    conn.close()\n  except OSError as e:\n    conn.close()\n    print('Connection closed')\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/esp_bme680_web_server_main.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>This code creates a socket server that sends an HTML page with the latest sensor readings when it receives a request on the ESP32 or ESP8266 IP address.<\/p>\n\n\n\n<p>Basically, we have a function called <span class=\"rnthl rntliteral\">web_page()<\/span> that returns the HTML to build up the web page with the latest sensor readings. This HMTL text builds a table to display the readings:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def web_page():\n  bme = BME680_I2C(i2c=i2c)\n  \n  html = \"\"\"&lt;html>&lt;head>&lt;title>ESP with BME680&lt;\/title>\n  &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  &lt;link rel=\"icon\" href=\"data:,\">&lt;style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}\n  table { border-collapse: collapse; margin-left:auto; margin-right:auto; }\n  th { padding: 12px; background-color: #0043af; color: white; }\n  tr { border: 1px solid #ddd; padding: 12px; }\n  tr:hover { background-color: #bcbcbc; }\n  td { border: none; padding: 12px; }\n  .sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px;\n  &lt;\/style>&lt;\/head>&lt;body>&lt;h1>ESP with BME680&lt;\/h1>\n  &lt;table>&lt;tr>&lt;th>MEASUREMENT&lt;\/th>&lt;th>VALUE&lt;\/th>&lt;\/tr>\n  &lt;tr>&lt;td>Temp. Celsius&lt;\/td>&lt;td>&lt;span class=\"sensor\">\"\"\" + str(round(bme.temperature, 2)) + \"\"\" C&lt;\/span>&lt;\/td>&lt;\/tr>\n  &lt;tr>&lt;td>Temp. Fahrenheit&lt;\/td>&lt;td>&lt;span class=\"sensor\">\"\"\" + str(round((bme.temperature) * (9\/5) + 32, 2))  + \"\"\" F&lt;\/span>&lt;\/td>&lt;\/tr>\n  &lt;tr>&lt;td>Pressure&lt;\/td>&lt;td>&lt;span class=\"sensor\">\"\"\" + str(round(bme.pressure, 2)) + \"\"\" hPa&lt;\/span>&lt;\/td>&lt;\/tr>\n  &lt;tr>&lt;td>Humidity&lt;\/td>&lt;td>&lt;span class=\"sensor\">\"\"\" + str(round(bme.humidity, 2)) + \"\"\" %&lt;\/span>&lt;\/td>&lt;\/tr>\n  &lt;tr>&lt;td>Gas&lt;\/td>&lt;td>&lt;span class=\"sensor\">\"\"\" + str(round(bme.gas\/1000, 2)) + \"\"\" KOhms&lt;\/span>&lt;\/td>&lt;\/tr>&lt;\/body>&lt;\/html>\"\"\"\n  return html<\/code><\/pre>\n\n\n\n<p>Then, we create a socket server that sends the HTML when it gets a request. The HTML text is then saved on the <span class=\"rnthl rntliteral\">response<\/span> variable:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>response = web_page()<\/code><\/pre>\n\n\n\n<p>And sent to the client:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>conn.sendall(response)<\/code><\/pre>\n\n\n\n<p>We&#8217;ve explained in great detail how these kind of web servers work in previous tutorials. So, if you want to learn how it works, you can read the following articles:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-dht11-dht22-web-server\/\">MicroPython: ESP32\/ESP8266 with DHT11\/DHT22 Web Server<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-micropython-web-server\/\">ESP32\/ESP8266 MicroPython Web Server \u2013 Control Outputs<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Web Server Demonstration<\/h2>\n\n\n\n<p>Upload all the previous files to your ESP32 or ESP8266 board in the following order:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><em>bme680.py<\/em><\/li><li><em>boot.py<\/em><\/li><li><em>main.py<\/em><\/li><\/ol>\n\n\n\n<p>If you don&#8217;t know how to upload code, you can read our getting started guides with uPyCraft IDE, or Thonny IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Getting Started with Thonny MicroPython (Python) IDE for ESP32 and ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">Getting Started with MicroPython on ESP32 and ESP8266 (uPyCraft IDE)<\/a><\/li><\/ul>\n\n\n\n<p>After uploading the code, your ESP32 or ESP8266 IP address should be displayed on the Serial Monitor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"823\" height=\"201\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-ESP8266-MicroPython-IP-Address.png?resize=823%2C201&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 MicroPython IP Address\" class=\"wp-image-99610\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-ESP8266-MicroPython-IP-Address.png?w=823&amp;quality=100&amp;strip=all&amp;ssl=1 823w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-ESP8266-MicroPython-IP-Address.png?resize=300%2C73&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-ESP8266-MicroPython-IP-Address.png?resize=768%2C188&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 823px) 100vw, 823px\" \/><\/figure><\/div>\n\n\n\n<p>Open a web browser in your local network and type your ESP IP address (in our example the IP is http:\/\/192.168.1.114). You should get a page with the latest sensor readings as shown in the following figure.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"814\" height=\"522\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-ESP8266-MicroPython-BME680-Web-Server-Test.png?resize=814%2C522&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 MicroPython BME680 Web Server Test Demonstration\" class=\"wp-image-99609\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-ESP8266-MicroPython-BME680-Web-Server-Test.png?w=814&amp;quality=100&amp;strip=all&amp;ssl=1 814w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-ESP8266-MicroPython-BME680-Web-Server-Test.png?resize=300%2C192&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-ESP8266-MicroPython-BME680-Web-Server-Test.png?resize=768%2C493&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 814px) 100vw, 814px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Auto refresh web page<\/h3>\n\n\n\n<p>With the web server script provided in this project, you need to refresh the web page to see the latest readings. If you add the next meta tag inside the HTML <span class=\"rnthl rntliteral\"><span style=\"color: #333399;\">&lt;head&gt;&lt;\/head&gt;<\/span><\/span> tags, your web page will auto refresh every 10 seconds:<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;meta http-equiv=\"refresh\" content=\"10\"><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. We have other projects and tutorials with MicroPython that you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/low-power-weather-station-datalogger-using-esp8266-bme280-micropython\/\">Low Power Weather Station Datalogger using ESP8266 and BME280 with MicroPython<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/micropython-oled-display-esp32-esp8266\/\">MicroPython: OLED Display with ESP32 and ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-dht11-dht22-web-server\/\">MicroPython: ESP32\/ESP8266 with DHT11\/DHT22 Web Server<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-ws2812b-addressable-rgb-leds-neopixel-esp32-esp8266\/\">MicroPython: WS2812B Addressable RGB LEDs with ESP32 and ESP8266<\/a><\/li><\/ul>\n\n\n\n<p>If you want to learn more about programming the ESP32 and ESP8266 boards with MicroPython, get access to our eBook:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\"><strong>MicroPython Programming with ESP32 and ESP8266<\/strong><\/a><strong>. <\/strong><\/p>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial is a step-by-step guide that covers how to build a standalone ESP32 or ESP8266 NodeMCU Web Server that displays BME680 sensor readings using MicroPython firmware. We\u2019ll create an &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython: ESP32\/ESP8266 BME680 Web Server (Weather Station)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-bme680-web-server\/#more-99640\" aria-label=\"Read more about MicroPython: ESP32\/ESP8266 BME680 Web Server (Weather Station)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":99613,"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":[310,309,264],"tags":[],"class_list":["post-99640","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-micropython","category-0-esp32-micropython","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/MicroPython-BME680-ESP32-ESP8266-Temperature-Humidity-Pressure-Gas-Web-Server-Guide.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\/99640","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=99640"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/99640\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/99613"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=99640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=99640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=99640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}