{"id":86432,"date":"2019-07-05T11:07:31","date_gmt":"2019-07-05T11:07:31","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=86432"},"modified":"2023-09-22T17:45:27","modified_gmt":"2023-09-22T17:45:27","slug":"micropython-bme280-esp32-esp8266","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-bme280-esp32-esp8266\/","title":{"rendered":"MicroPython: BME280 with ESP32 and ESP8266 (Pressure, Temperature, Humidity)"},"content":{"rendered":"\n<p>In this guide you&#8217;ll learn how to use the BME280 sensor module with the ESP32 and ESP8266 to get pressure, temperature and humidity readings using MicroPython firmware. We&#8217;ll build a simple example to get you familiar with the sensor and a web server to display your sensor readings.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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\/06\/bme280-esp32-esp8266-micropython.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython BME280 with ESP32 and ESP8266 Temperature, Humidity and Pressure\" class=\"wp-image-86436\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-esp32-esp8266-micropython.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-esp32-esp8266-micropython.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-esp32-esp8266-micropython.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-esp32-esp8266-micropython.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<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\">\n<li>Thonny IDE:\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Installing and getting started with Thonny IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/flashing-micropython-firmware-esptool-py-esp32-esp8266\/\">Flashing MicroPython Firmware with esptool.py<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>uPyCraft IDE:\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">Getting Started with uPyCraft IDE<\/a><\/li>\n\n\n\n<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>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li>\n<\/ul>\n<\/li>\n<\/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<p>You might also like reading other BME280 guides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<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>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-with-bme280-mini-weather-station\/\">ESP32 Web Server with BME280 \u2013 Weather Station<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-bme280-arduino-ide\/\">ESP8266 with BME280 using Arduino IDE<\/a><\/li>\n\n\n\n<li><a style=\"font-size: inherit; background-color: initial;\" href=\"https:\/\/randomnerdtutorials.com\/bme280-sensor-arduino-pressure-temperature-humidity\/\">Arduino Board with BME280<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing the BME280 Sensor Module<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/makeradvisor.com\/tools\/bme280-sensor-module\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"BME280 sensor module (opens in a new tab)\">BME280 sensor module<\/a> reads barometric pressure, temperature and humidity. Because pressure changes with altitude, you can also estimate altitude. There are several versions of this sensor module, but we\u2019re using the one shown in the figure below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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%2C408&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 sensor temperature, humidity, and pressure\" class=\"wp-image-86437\" width=\"750\" height=\"408\"\/><\/figure><\/div>\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>BME280<\/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<div class=\"wp-block-image\">\n<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\/06\/esp32-bme280-breadboard.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython BME280 with ESP32 and ESP8266 Parts Required\" class=\"wp-image-86465\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/esp32-bme280-breadboard.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/esp32-bme280-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<p>For this project you need to wire the BME280 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\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/bme280-sensor-module\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"BME280 sensor module\u2028 (opens in a new tab)\">BME280 sensor module<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" style=\"font-size: inherit; background-color: initial;\" 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 rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" style=\"font-size: inherit; background-color: initial;\" 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 rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" style=\"font-size: inherit; background-color: initial;\" href=\"https:\/\/makeradvisor.com\/esp32-vs-esp8266\/\" target=\"_blank\">ESP32 vs ESP8266<\/a><span style=\"font-size: inherit; background-color: initial;\">)<\/span><\/li>\n\n\n\n<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>\n\n\n\n<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>\n<\/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<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"669\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-bme280_schematic.jpg?resize=768%2C669&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 BME280 Sensor Module Wiring Schematics Diagram\" class=\"wp-image-86439\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-bme280_schematic.jpg?w=768&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-bme280_schematic.jpg?resize=300%2C261&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\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<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"672\" height=\"539\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP8266-bme280_schematic.jpg?resize=672%2C539&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 BME280 Sensor Module Wiring Schematics Diagram\" class=\"wp-image-86440\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP8266-bme280_schematic.jpg?w=672&amp;quality=100&amp;strip=all&amp;ssl=1 672w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP8266-bme280_schematic.jpg?resize=300%2C241&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 672px) 100vw, 672px\" \/><\/figure><\/div>\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\">BME280 MicroPython Library<\/h2>\n\n\n\n<p>The library to read from the BME280 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>BME280.py<\/em>).<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\">from machine import I2C\nimport time\n\n# BME280 default address.\nBME280_I2CADDR = 0x76\n\n# Operating Modes\nBME280_OSAMPLE_1 = 1\nBME280_OSAMPLE_2 = 2\nBME280_OSAMPLE_4 = 3\nBME280_OSAMPLE_8 = 4\nBME280_OSAMPLE_16 = 5\n\n# BME280 Registers\n\nBME280_REGISTER_DIG_T1 = 0x88  # Trimming parameter registers\nBME280_REGISTER_DIG_T2 = 0x8A\nBME280_REGISTER_DIG_T3 = 0x8C\n\nBME280_REGISTER_DIG_P1 = 0x8E\nBME280_REGISTER_DIG_P2 = 0x90\nBME280_REGISTER_DIG_P3 = 0x92\nBME280_REGISTER_DIG_P4 = 0x94\nBME280_REGISTER_DIG_P5 = 0x96\nBME280_REGISTER_DIG_P6 = 0x98\nBME280_REGISTER_DIG_P7 = 0x9A\nBME280_REGISTER_DIG_P8 = 0x9C\nBME280_REGISTER_DIG_P9 = 0x9E\n\nBME280_REGISTER_DIG_H1 = 0xA1\nBME280_REGISTER_DIG_H2 = 0xE1\nBME280_REGISTER_DIG_H3 = 0xE3\nBME280_REGISTER_DIG_H4 = 0xE4\nBME280_REGISTER_DIG_H5 = 0xE5\nBME280_REGISTER_DIG_H6 = 0xE6\nBME280_REGISTER_DIG_H7 = 0xE7\n\nBME280_REGISTER_CHIPID = 0xD0\nBME280_REGISTER_VERSION = 0xD1\nBME280_REGISTER_SOFTRESET = 0xE0\n\nBME280_REGISTER_CONTROL_HUM = 0xF2\nBME280_REGISTER_CONTROL = 0xF4\nBME280_REGISTER_CONFIG = 0xF5\nBME280_REGISTER_PRESSURE_DATA = 0xF7\nBME280_REGISTER_TEMP_DATA = 0xFA\nBME280_REGISTER_HUMIDITY_DATA = 0xFD\n\n\nclass Device:\n  &quot;&quot;&quot;Class for communicating with an I2C device.\n\n  Allows reading and writing 8-bit, 16-bit, and byte array values to\n  registers on the device.&quot;&quot;&quot;\n\n  def __init__(self, address, i2c):\n    &quot;&quot;&quot;Create an instance of the I2C device at the specified address using\n    the specified I2C interface object.&quot;&quot;&quot;\n    self._address = address\n    self._i2c = i2c\n\n  def writeRaw8(self, value):\n    &quot;&quot;&quot;Write an 8-bit value on the bus (without register).&quot;&quot;&quot;\n    value = value &amp; 0xFF\n    self._i2c.writeto(self._address, value)\n\n  def write8(self, register, value):\n    &quot;&quot;&quot;Write an 8-bit value to the specified register.&quot;&quot;&quot;\n    b=bytearray(1)\n    b[0]=value &amp; 0xFF\n    self._i2c.writeto_mem(self._address, register, b)\n\n  def write16(self, register, value):\n    &quot;&quot;&quot;Write a 16-bit value to the specified register.&quot;&quot;&quot;\n    value = value &amp; 0xFFFF\n    b=bytearray(2)\n    b[0]= value &amp; 0xFF\n    b[1]= (value&gt;&gt;8) &amp; 0xFF\n    self.i2c.writeto_mem(self._address, register, value)\n\n  def readRaw8(self):\n    &quot;&quot;&quot;Read an 8-bit value on the bus (without register).&quot;&quot;&quot;\n    return int.from_bytes(self._i2c.readfrom(self._address, 1),'little') &amp; 0xFF\n\n  def readU8(self, register):\n    &quot;&quot;&quot;Read an unsigned byte from the specified register.&quot;&quot;&quot;\n    return int.from_bytes(\n        self._i2c.readfrom_mem(self._address, register, 1),'little') &amp; 0xFF\n\n  def readS8(self, register):\n    &quot;&quot;&quot;Read a signed byte from the specified register.&quot;&quot;&quot;\n    result = self.readU8(register)\n    if result &gt; 127:\n      result -= 256\n    return result\n\n  def readU16(self, register, little_endian=True):\n    &quot;&quot;&quot;Read an unsigned 16-bit value from the specified register, with the\n    specified endianness (default little endian, or least significant byte\n    first).&quot;&quot;&quot;\n    result = int.from_bytes(\n        self._i2c.readfrom_mem(self._address, register, 2),'little') &amp; 0xFFFF\n    if not little_endian:\n      result = ((result &lt;&lt; 8) &amp; 0xFF00) + (result &gt;&gt; 8)\n    return result\n\n  def readS16(self, register, little_endian=True):\n    &quot;&quot;&quot;Read a signed 16-bit value from the specified register, with the\n    specified endianness (default little endian, or least significant byte\n    first).&quot;&quot;&quot;\n    result = self.readU16(register, little_endian)\n    if result &gt; 32767:\n      result -= 65536\n    return result\n\n  def readU16LE(self, register):\n    &quot;&quot;&quot;Read an unsigned 16-bit value from the specified register, in little\n    endian byte order.&quot;&quot;&quot;\n    return self.readU16(register, little_endian=True)\n\n  def readU16BE(self, register):\n    &quot;&quot;&quot;Read an unsigned 16-bit value from the specified register, in big\n    endian byte order.&quot;&quot;&quot;\n    return self.readU16(register, little_endian=False)\n\n  def readS16LE(self, register):\n    &quot;&quot;&quot;Read a signed 16-bit value from the specified register, in little\n    endian byte order.&quot;&quot;&quot;\n    return self.readS16(register, little_endian=True)\n\n  def readS16BE(self, register):\n    &quot;&quot;&quot;Read a signed 16-bit value from the specified register, in big\n    endian byte order.&quot;&quot;&quot;\n    return self.readS16(register, little_endian=False)\n\n\nclass BME280:\n  def __init__(self, mode=BME280_OSAMPLE_1, address=BME280_I2CADDR, i2c=None,\n               **kwargs):\n    # Check that mode is valid.\n    if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,\n                    BME280_OSAMPLE_8, BME280_OSAMPLE_16]:\n        raise ValueError(\n            'Unexpected mode value {0}. Set mode to one of '\n            'BME280_ULTRALOWPOWER, BME280_STANDARD, BME280_HIGHRES, or '\n            'BME280_ULTRAHIGHRES'.format(mode))\n    self._mode = mode\n    # Create I2C device.\n    if i2c is None:\n      raise ValueError('An I2C object is required.')\n    self._device = Device(address, i2c)\n    # Load calibration values.\n    self._load_calibration()\n    self._device.write8(BME280_REGISTER_CONTROL, 0x3F)\n    self.t_fine = 0\n\n  def _load_calibration(self):\n\n    self.dig_T1 = self._device.readU16LE(BME280_REGISTER_DIG_T1)\n    self.dig_T2 = self._device.readS16LE(BME280_REGISTER_DIG_T2)\n    self.dig_T3 = self._device.readS16LE(BME280_REGISTER_DIG_T3)\n\n    self.dig_P1 = self._device.readU16LE(BME280_REGISTER_DIG_P1)\n    self.dig_P2 = self._device.readS16LE(BME280_REGISTER_DIG_P2)\n    self.dig_P3 = self._device.readS16LE(BME280_REGISTER_DIG_P3)\n    self.dig_P4 = self._device.readS16LE(BME280_REGISTER_DIG_P4)\n    self.dig_P5 = self._device.readS16LE(BME280_REGISTER_DIG_P5)\n    self.dig_P6 = self._device.readS16LE(BME280_REGISTER_DIG_P6)\n    self.dig_P7 = self._device.readS16LE(BME280_REGISTER_DIG_P7)\n    self.dig_P8 = self._device.readS16LE(BME280_REGISTER_DIG_P8)\n    self.dig_P9 = self._device.readS16LE(BME280_REGISTER_DIG_P9)\n\n    self.dig_H1 = self._device.readU8(BME280_REGISTER_DIG_H1)\n    self.dig_H2 = self._device.readS16LE(BME280_REGISTER_DIG_H2)\n    self.dig_H3 = self._device.readU8(BME280_REGISTER_DIG_H3)\n    self.dig_H6 = self._device.readS8(BME280_REGISTER_DIG_H7)\n\n    h4 = self._device.readS8(BME280_REGISTER_DIG_H4)\n    h4 = (h4 &lt;&lt; 24) &gt;&gt; 20\n    self.dig_H4 = h4 | (self._device.readU8(BME280_REGISTER_DIG_H5) &amp; 0x0F)\n\n    h5 = self._device.readS8(BME280_REGISTER_DIG_H6)\n    h5 = (h5 &lt;&lt; 24) &gt;&gt; 20\n    self.dig_H5 = h5 | (\n        self._device.readU8(BME280_REGISTER_DIG_H5) &gt;&gt; 4 &amp; 0x0F)\n\n  def read_raw_temp(self):\n    &quot;&quot;&quot;Reads the raw (uncompensated) temperature from the sensor.&quot;&quot;&quot;\n    meas = self._mode\n    self._device.write8(BME280_REGISTER_CONTROL_HUM, meas)\n    meas = self._mode &lt;&lt; 5 | self._mode &lt;&lt; 2 | 1\n    self._device.write8(BME280_REGISTER_CONTROL, meas)\n    sleep_time = 1250 + 2300 * (1 &lt;&lt; self._mode)\n\n    sleep_time = sleep_time + 2300 * (1 &lt;&lt; self._mode) + 575\n    sleep_time = sleep_time + 2300 * (1 &lt;&lt; self._mode) + 575\n    time.sleep_us(sleep_time)  # Wait the required time\n    msb = self._device.readU8(BME280_REGISTER_TEMP_DATA)\n    lsb = self._device.readU8(BME280_REGISTER_TEMP_DATA + 1)\n    xlsb = self._device.readU8(BME280_REGISTER_TEMP_DATA + 2)\n    raw = ((msb &lt;&lt; 16) | (lsb &lt;&lt; 8) | xlsb) &gt;&gt; 4\n    return raw\n\n  def read_raw_pressure(self):\n    &quot;&quot;&quot;Reads the raw (uncompensated) pressure level from the sensor.&quot;&quot;&quot;\n    &quot;&quot;&quot;Assumes that the temperature has already been read &quot;&quot;&quot;\n    &quot;&quot;&quot;i.e. that enough delay has been provided&quot;&quot;&quot;\n    msb = self._device.readU8(BME280_REGISTER_PRESSURE_DATA)\n    lsb = self._device.readU8(BME280_REGISTER_PRESSURE_DATA + 1)\n    xlsb = self._device.readU8(BME280_REGISTER_PRESSURE_DATA + 2)\n    raw = ((msb &lt;&lt; 16) | (lsb &lt;&lt; 8) | xlsb) &gt;&gt; 4\n    return raw\n\n  def read_raw_humidity(self):\n    &quot;&quot;&quot;Assumes that the temperature has already been read &quot;&quot;&quot;\n    &quot;&quot;&quot;i.e. that enough delay has been provided&quot;&quot;&quot;\n    msb = self._device.readU8(BME280_REGISTER_HUMIDITY_DATA)\n    lsb = self._device.readU8(BME280_REGISTER_HUMIDITY_DATA + 1)\n    raw = (msb &lt;&lt; 8) | lsb\n    return raw\n\n  def read_temperature(self):\n    &quot;&quot;&quot;Get the compensated temperature in 0.01 of a degree celsius.&quot;&quot;&quot;\n    adc = self.read_raw_temp()\n    var1 = ((adc &gt;&gt; 3) - (self.dig_T1 &lt;&lt; 1)) * (self.dig_T2 &gt;&gt; 11)\n    var2 = ((\n        (((adc &gt;&gt; 4) - self.dig_T1) * ((adc &gt;&gt; 4) - self.dig_T1)) &gt;&gt; 12) *\n        self.dig_T3) &gt;&gt; 14\n    self.t_fine = var1 + var2\n    return (self.t_fine * 5 + 128) &gt;&gt; 8\n\n  def read_pressure(self):\n    &quot;&quot;&quot;Gets the compensated pressure in Pascals.&quot;&quot;&quot;\n    adc = self.read_raw_pressure()\n    var1 = self.t_fine - 128000\n    var2 = var1 * var1 * self.dig_P6\n    var2 = var2 + ((var1 * self.dig_P5) &lt;&lt; 17)\n    var2 = var2 + (self.dig_P4 &lt;&lt; 35)\n    var1 = (((var1 * var1 * self.dig_P3) &gt;&gt; 8) +\n            ((var1 * self.dig_P2) &gt;&gt; 12))\n    var1 = (((1 &lt;&lt; 47) + var1) * self.dig_P1) &gt;&gt; 33\n    if var1 == 0:\n      return 0\n    p = 1048576 - adc\n    p = (((p &lt;&lt; 31) - var2) * 3125) \/\/ var1\n    var1 = (self.dig_P9 * (p &gt;&gt; 13) * (p &gt;&gt; 13)) &gt;&gt; 25\n    var2 = (self.dig_P8 * p) &gt;&gt; 19\n    return ((p + var1 + var2) &gt;&gt; 8) + (self.dig_P7 &lt;&lt; 4)\n\n  def read_humidity(self):\n    adc = self.read_raw_humidity()\n    # print 'Raw humidity = {0:d}'.format (adc)\n    h = self.t_fine - 76800\n    h = (((((adc &lt;&lt; 14) - (self.dig_H4 &lt;&lt; 20) - (self.dig_H5 * h)) +\n         16384) &gt;&gt; 15) * (((((((h * self.dig_H6) &gt;&gt; 10) * (((h *\n                          self.dig_H3) &gt;&gt; 11) + 32768)) &gt;&gt; 10) + 2097152) *\n                          self.dig_H2 + 8192) &gt;&gt; 14))\n    h = h - (((((h &gt;&gt; 15) * (h &gt;&gt; 15)) &gt;&gt; 7) * self.dig_H1) &gt;&gt; 4)\n    h = 0 if h &lt; 0 else h\n    h = 419430400 if h &gt; 419430400 else h\n    return h &gt;&gt; 12\n\n  @property\n  def temperature(self):\n    &quot;Return the temperature in degrees.&quot;\n    t = self.read_temperature()\n    ti = t \/\/ 100\n    td = t - ti * 100\n    return &quot;{}.{:02d}C&quot;.format(ti, td)\n\n  @property\n  def pressure(self):\n    &quot;Return the temperature in hPa.&quot;\n    p = self.read_pressure() \/\/ 256\n    pi = p \/\/ 100\n    pd = p - pi * 100\n    return &quot;{}.{:02d}hPa&quot;.format(pi, pd)\n\n  @property\n  def humidity(self):\n    &quot;Return the humidity in percent.&quot;\n    h = self.read_humidity()\n    hi = h \/\/ 1024\n    hd = h * 100 \/\/ 1024 - hi * 100\n    return &quot;{}.{:02d}%&quot;.format(hi, hd)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/raw\/master\/code\/WiFi\/HTTP_Client_IFTTT_BME280\/BME280.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\">\n<li>A. Upload BME280 library with <strong>uPyCraft IDE<\/strong><\/li>\n\n\n\n<li>B. Upload BME280 library with <strong>Thonny IDE<\/strong><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">A. Upload BME280 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 BME280 library code into that file. The <a href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/blob\/master\/code\/WiFi\/HTTP_Client_IFTTT_BME280\/BME280.py\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"BME280 library code can be found here (opens in a new tab)\">BME280 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<div class=\"wp-block-image\">\n<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<p><strong>4.<\/strong> Call this new file \u201c<strong>BME280.py<\/strong>\u201d and press <strong>ok<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"112\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-bme280-library.png?resize=402%2C112&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install BME280 library MicroPython ESP32 ESP8266 uPyCraft IDE step 2\" class=\"wp-image-86442\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-bme280-library.png?w=402&amp;quality=100&amp;strip=all&amp;ssl=1 402w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-bme280-library.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<p><strong>5.<\/strong> Click the <strong>Download and Run<\/strong> button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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<p>The file should be saved on the device folder with the name \u201c<strong>BME280.py<\/strong>\u201d as highlighted in the following figure.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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\/bme280-installed-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 4\" class=\"wp-image-86443\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-installed-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\/bme280-installed-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\/bme280-installed-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<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 BME280 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:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/blob\/master\/code\/WiFi\/HTTP_Client_IFTTT_BME280\/BME280.py\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">BME280 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<div class=\"wp-block-image\">\n<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<p><strong>3.<\/strong> Select save to &#8220;<strong>MicroPython device<\/strong>&#8220;:<\/p>\n\n\n<div class=\"wp-block-image\">\n<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<p><strong>4.<\/strong> Name your file as <em>BME280.py<\/em> and press the OK button:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"553\" height=\"277\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/bme280-library-new-MicroPython-file-Thonny-IDE.png?resize=553%2C277&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 library new MicroPython file Thonny IDE\" class=\"wp-image-99626\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/bme280-library-new-MicroPython-file-Thonny-IDE.png?w=553&amp;quality=100&amp;strip=all&amp;ssl=1 553w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/bme280-library-new-MicroPython-file-Thonny-IDE.png?resize=300%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><\/figure><\/div>\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<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"553\" height=\"280\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/bme280-library-MicroPython-file-created-Thonny-IDE.png?resize=553%2C280&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 library MicroPython file created Thonny IDE\" class=\"wp-image-99625\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/bme280-library-MicroPython-file-created-Thonny-IDE.png?w=553&amp;quality=100&amp;strip=all&amp;ssl=1 553w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/bme280-library-MicroPython-file-created-Thonny-IDE.png?resize=300%2C152&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><\/figure><\/div>\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 &#8211; BME280 Pressure, Temperature, and Humidity<\/h2>\n\n\n\n<p>After uploading the library to the ESP32 or ESP8266, copy the following code to the <em>main.py<\/em> or <em>boot.py<\/em> file. It simply prints the temperature, humidity and pressure into the shell every 5 seconds.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\n\nfrom machine import Pin, I2C\nfrom time import sleep\nimport BME280\n\n# ESP32 - Pin assignment\ni2c = I2C(scl=Pin(22), sda=Pin(21), freq=10000)\n# ESP8266 - Pin assignment\n#i2c = I2C(scl=Pin(5), sda=Pin(4), freq=10000)\n\nwhile True:\n  bme = BME280.BME280(i2c=i2c)\n  temp = bme.temperature\n  hum = bme.humidity\n  pres = bme.pressure\n  # uncomment for temperature in Fahrenheit\n  #temp = (bme.read_temperature()\/100) * (9\/5) + 32\n  #temp = str(round(temp, 2)) + 'F'\n  print('Temperature: ', temp)\n  print('Humidity: ', hum)\n  print('Pressure: ', pres)\n\n  sleep(5)\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_bme280_readings.py\" 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>First, you need to import the necessary libraries, including the <span class=\"rnthl rntliteral\">BME280<\/span> module you&#8217;ve imported previously.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from machine import Pin, I2C\nfrom time import sleep\nimport BME280<\/code><\/pre>\n\n\n\n<p>Set the I2C pins. In this case, we&#8217;re using the default I2C pins. If you&#8217;re using the ESP32, set the pins as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>i2c = I2C(scl=Pin(22), sda=Pin(21), freq=10000)<\/code><\/pre>\n\n\n\n<p>If you&#8217;re using the ESP8266, comment the previous line and uncomment the following so that you have:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>i2c = I2C(scl=Pin(5), sda=Pin(4), freq=10000)<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">while<\/span> loop, create a <span class=\"rnthl rntliteral\">BME280<\/span> object called <span class=\"rnthl rntliteral\">bme<\/span> with the I2C pins defined earlier:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>bme = BME280.BME280(i2c=i2c)<\/code><\/pre>\n\n\n\n<p>Reading temperature, humidity and pressure is as simple as using the <span class=\"rnthl rntliteral\">temperature<\/span>, <span class=\"rnthl rntliteral\">humidity<\/span> and <span class=\"rnthl rntliteral\">pressure<\/span> methods on the <span class=\"rnthl rntliteral\">bme<\/span> object. <\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>temp = bme.temperature\nhum = bme.humidity\npres = bme.pressure<\/code><\/pre>\n\n\n\n<p>Finally, print the readings on the shell:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>print('Temperature: ', temp)\nprint('Humidity: ', hum)\nprint('Pressure: ', pres)<\/code><\/pre>\n\n\n\n<p>In the end, we add a delay of 5 seconds:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>sleep(5)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>After uploading the code to your board, press the RST button to run the code. New BME280 sensor readings should be displayed every 5 seconds.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"891\" height=\"651\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-esp32-esp8266-bme280-1.png?resize=891%2C651&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 MicroPython ESP32 ESP8266 Printing Readings in Python Shell\" class=\"wp-image-86462\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-esp32-esp8266-bme280-1.png?w=891&amp;quality=100&amp;strip=all&amp;ssl=1 891w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-esp32-esp8266-bme280-1.png?resize=300%2C219&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-esp32-esp8266-bme280-1.png?resize=768%2C561&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 891px) 100vw, 891px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Display BME280 Readings on Web Server<\/h2>\n\n\n\n<p>Now that you know how to get pressure, temperature and humidity from the BME280 sensor, we&#8217;ll display the sensor readings on a web server that you can access on your local network.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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=\"Display BME280 Readings on Web Server with ESP32 or ESP8266\" 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<p>For this example, you need three files:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>BME280.py<\/em><\/strong>: this is the file that contains all the methods to use the BME280 sensor. That&#8217;s the file you&#8217;ve uploaded previously.<\/li>\n\n\n\n<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>\n\n\n\n<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>\n<\/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\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\nimport BME280\n\n# ESP32 - Pin assignment\ni2c = I2C(scl=Pin(22), sda=Pin(21), freq=10000)\n# ESP8266 - Pin assignment\n#i2c = I2C(scl=Pin(5), sda=Pin(4), freq=10000)\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_bme280_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(5), sda=Pin(4), freq=10000)<\/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(22), sda=Pin(21), freq=10000)<\/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\n\ndef web_page():\n  bme = BME280.BME280(i2c=i2c)\n  \n  html = &quot;&quot;&quot;&lt;html&gt;&lt;head&gt;&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; width:35%; 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 BME280&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(bme.temperature) + &quot;&quot;&quot;&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.read_temperature()\/100.0) * (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(bme.pressure) + &quot;&quot;&quot;&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(bme.humidity) + &quot;&quot;&quot;&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_bme280_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>html = \"\"\"&lt;html&gt;&lt;head&gt;&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;\n  &lt;link rel=\"icon\" href=\"data:,\"&gt;&lt;style&gt;body { text-align: center; font-family: \"Trebuchet MS\", Arial;}\n  table { border-collapse: collapse; width:35%; 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 BME280&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=\"sensor\"&gt;\"\"\" + str(bme.temperature) + \"\"\"&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=\"sensor\"&gt;\"\"\" + str(round((bme.read_temperature()\/100.0) * (9\/5) + 32, 2)) + \"\"\"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=\"sensor\"&gt;\"\"\" + str(bme.pressure) + \"\"\"&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=\"sensor\"&gt;\"\"\" + str(bme.humidity) + \"\"\"&lt;\/span&gt;&lt;\/td&gt;&lt;\/tr&gt; \n  &lt;\/body&gt;&lt;\/html&gt;\"\"\"<\/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 response 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\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-dht11-dht22-web-server\/\">MicroPython: ESP32\/ESP8266 with DHT11\/DHT22 Web Server<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-micropython-web-server\/\">ESP32\/ESP8266 MicroPython Web Server \u2013 Control Outputs<\/a><\/li>\n<\/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\">\n<li><em>BME280.py<\/em><\/li>\n\n\n\n<li><em>boot.py<\/em><\/li>\n\n\n\n<li><em>main.py<\/em><\/li>\n<\/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\">\n<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>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">Getting Started with MicroPython on ESP32 and ESP8266 (uPyCraft IDE)<\/a><\/li>\n<\/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<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"838\" height=\"196\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/micropython-ip-address.png?resize=838%2C196&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-86761\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/micropython-ip-address.png?w=838&amp;quality=100&amp;strip=all&amp;ssl=1 838w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/micropython-ip-address.png?resize=300%2C70&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/micropython-ip-address.png?resize=768%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 838px) 100vw, 838px\" \/><\/figure><\/div>\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.71). You should get a page with the latest sensor readings as shown in the following figure.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"759\" height=\"473\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-ESP8266-BME280-web-server.png?resize=759%2C473&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 Web Server MicroPython with ESP8266 or ESP32\" class=\"wp-image-86763\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-ESP8266-BME280-web-server.png?w=759&amp;quality=100&amp;strip=all&amp;ssl=1 759w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-ESP8266-BME280-web-server.png?resize=300%2C187&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><\/figure>\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 add the next meta tag inside the HTML &lt;head&gt;&lt;\/head&gt; 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\"&gt;<\/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\">\n<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>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/micropython-oled-display-esp32-esp8266\/\">MicroPython: OLED Display with ESP32 and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-dht11-dht22-web-server\/\">MicroPython: ESP32\/ESP8266 with DHT11\/DHT22 Web Server<\/a><\/li>\n\n\n\n<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>\n<\/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>In this guide you&#8217;ll learn how to use the BME280 sensor module with the ESP32 and ESP8266 to get pressure, temperature and humidity readings using MicroPython firmware. We&#8217;ll build a &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython: BME280 with ESP32 and ESP8266 (Pressure, Temperature, Humidity)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-bme280-esp32-esp8266\/#more-86432\" aria-label=\"Read more about MicroPython: BME280 with ESP32 and ESP8266 (Pressure, Temperature, Humidity)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":86436,"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-86432","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\/2019\/06\/bme280-esp32-esp8266-micropython.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\/86432","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=86432"}],"version-history":[{"count":1,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/86432\/revisions"}],"predecessor-version":[{"id":136810,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/86432\/revisions\/136810"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/86436"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=86432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=86432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=86432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}