{"id":99689,"date":"2020-11-20T15:34:39","date_gmt":"2020-11-20T15:34:39","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=99689"},"modified":"2020-11-20T15:59:07","modified_gmt":"2020-11-20T15:59:07","slug":"micropython-esp32-esp8266-ds18b20-web-server","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-ds18b20-web-server\/","title":{"rendered":"MicroPython: ESP32\/ESP8266 DS18B20 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 DS18B20 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\/ESP32-ESP8266-MicroPython-DS18B20-Temperature-Web-Server.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 MicroPython DS18B20 Temperature Web Server Guide\" class=\"wp-image-99629\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-ESP8266-MicroPython-DS18B20-Temperature-Web-Server.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-ESP8266-MicroPython-DS18B20-Temperature-Web-Server.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\/ESP32-ESP8266-MicroPython-DS18B20-Temperature-Web-Server.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\/ESP32-ESP8266-MicroPython-DS18B20-Temperature-Web-Server.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-ds18b20-esp32-esp8266\/\">DS18B20 with ESP32 and ESP8266 (Temperature Readings)<\/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-ds18b20-temperature-arduino-ide\/\">ESP32 DS18B20 Temperature Sensor with Arduino IDE (Single, Multiple, Web Server)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide\/\">ESP8266 DS18B20 Temperature Sensor with Arduino IDE (Single, Multiple, Web Server)<\/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\">Introducing DS18B20 Temperature Sensor<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-temperature-sensor-2\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">DS18B20 temperature sensor<\/a> is a one-wire digital temperature sensor. This means that it just requires one data line (and GND) to communicate with your ESP32 or ESP8266.<\/p>\n\n\n\n<p>It can be powered by an external power supply or it can derive power from the data line (called &#8220;parasite mode&#8221;), which eliminates the need for an external power supply.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"490\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-tempeature-sensor-pinout.jpg?resize=750%2C490&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Temperature Sensor Pinout Pins\" class=\"wp-image-86735\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-tempeature-sensor-pinout.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-tempeature-sensor-pinout.jpg?resize=300%2C196&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>To follow this tutorial you need the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32 <\/a>or <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266 <\/a>(read <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/esp32-vs-esp8266\/\" target=\"_blank\">ESP32 vs ESP8266<\/a>)<\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-temperature-sensor-2\/\" target=\"_blank\">DS18B20 temperature sensor<\/a> (one or multiple sensors) \u2013&nbsp;<a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-digital-temperature-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">waterproof version<\/a> <\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">4.7k Ohm resistor<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li><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><\/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>If you&#8217;re using an ESP32, follow the next diagram.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_single_normal.png?resize=563%2C612&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Temperature Sensor with ESP32 Normal Mode Wiring Schematic Diagram\" class=\"wp-image-86565\" width=\"563\" height=\"612\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_single_normal.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp32_single_normal.png?resize=276%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 276w\" sizes=\"(max-width: 563px) 100vw, 563px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Schematic &#8211; ESP8266<\/h2>\n\n\n\n<p>If you&#8217;re using an ESP8266, follow the next schematic diagram.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp8266_single_normal_F.png?resize=419%2C572&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Temperature Sensor with ESP8266 Normal Mode Wiring Schematic Diagram\" class=\"wp-image-86567\" width=\"419\" height=\"572\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp8266_single_normal_F.png?w=559&amp;quality=100&amp;strip=all&amp;ssl=1 559w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp8266_single_normal_F.png?resize=220%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 220w\" sizes=\"(max-width: 419px) 100vw, 419px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> in this tutorial we&#8217;re connecting the DS18B20 data line to GPIO 4, but you can use any other suitable GPIO.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code Web Server &#8211; DS18B20 Temperature Readings<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><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-DS18B20-Temperature-Sensor-MicroPython-Web-Server.jpg?resize=750%2C420&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Display DS18B20 Temperature Readings on Web Server MicroPython\" class=\"wp-image-87105\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-ESP8266-DS18B20-Temperature-Sensor-MicroPython-Web-Server.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-DS18B20-Temperature-Sensor-MicroPython-Web-Server.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>For this example, you need two files:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><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\u2019ll handle the web server. It executes immediately after the&nbsp;<em>boot.py<\/em>.<\/li><\/ul>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note:&nbsp;<\/strong>it is a good practice to include the <em>boot.py<\/em> and <em>main.py<\/em> files. However, if you prefer, you can include all the code in the <em>main.py<\/em> file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">boot.py (DS18B20 web server)<\/h3>\n\n\n\n<p>Create a new file in your IDE called&nbsp;<em>boot.py<\/em>&nbsp;and copy the following code.<br><\/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\nfrom machine import Pin\nimport onewire, ds18x20\n\nimport network\n\nimport esp\nesp.osdebug(None)\n\nimport gc\ngc.collect()\n\nds_pin = Pin(4)\nds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))\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_ds18b20_web_server_boot.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>This file imports the required libraries, sets up the DS18B20 sensor and connects to your network.<\/p>\n\n\n\n<p>Here, we&#8217;re setting the DS18B20 data pin on <span class=\"rnthl rntclblue\">GPIO 4<\/span> but you can use any other suitable pin:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>ds_pin = Pin(4)\nds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))<\/code><\/pre>\n\n\n\n<p>You should insert your network credentials in the following variables so that the ESP is able to connect to your network.<\/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 (DS18B20 web server) <\/h3>\n\n\n\n<p>In the&nbsp;<em>main.py<\/em>&nbsp;file is where we\u2019ll create the web server and handle the requests. Copy the following code to your&nbsp;<em>main.py<\/em>&nbsp;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 read_ds_sensor():\n  roms = ds_sensor.scan()\n  print('Found DS devices: ', roms)\n  print('Temperatures: ')\n  ds_sensor.convert_temp()\n  for rom in roms:\n    temp = ds_sensor.read_temp(rom)\n    if isinstance(temp, float):\n      msg = round(temp, 2)\n      print(temp, end=' ')\n      print('Valid temperature')\n      return msg\n  return b'0.0'\n  \ndef web_page():\n  temp = read_ds_sensor()\n  html = &quot;&quot;&quot;&lt;!DOCTYPE HTML&gt;&lt;html&gt;&lt;head&gt;\n  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\n  &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/use.fontawesome.com\/releases\/v5.7.2\/css\/all.css&quot; integrity=&quot;sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr&quot; crossorigin=&quot;anonymous&quot;&gt;\n  &lt;style&gt; html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center; }\n    h2 { font-size: 3.0rem; } p { font-size: 3.0rem; } .units { font-size: 1.2rem; } \n    .ds-labels{ font-size: 1.5rem; vertical-align:middle; padding-bottom: 15px; }\n  &lt;\/style&gt;&lt;\/head&gt;&lt;body&gt;&lt;h2&gt;ESP with DS18B20&lt;\/h2&gt;\n  &lt;p&gt;&lt;i class=&quot;fas fa-thermometer-half&quot; style=&quot;color:#059e8a;&quot;&gt;&lt;\/i&gt; \n    &lt;span class=&quot;ds-labels&quot;&gt;Temperature&lt;\/span&gt;\n    &lt;span id=&quot;temperature&quot;&gt;&quot;&quot;&quot; + str(temp) + &quot;&quot;&quot;&lt;\/span&gt;\n    &lt;sup class=&quot;units&quot;&gt;&amp;deg;C&lt;\/sup&gt;\n  &lt;\/p&gt;\n    &lt;p&gt;&lt;i class=&quot;fas fa-thermometer-half&quot; style=&quot;color:#059e8a;&quot;&gt;&lt;\/i&gt; \n    &lt;span class=&quot;ds-labels&quot;&gt;Temperature&lt;\/span&gt;\n    &lt;span id=&quot;temperature&quot;&gt;&quot;&quot;&quot; + str(round(temp * (9\/5) + 32.0, 2)) + &quot;&quot;&quot;&lt;\/span&gt;\n    &lt;sup class=&quot;units&quot;&gt;&amp;deg;F&lt;\/sup&gt;\n  &lt;\/p&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_ds18b20_web_server_main.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Reading the DS18B20 Sensor<\/h4>\n\n\n\n<p>The code starts by creating a function called <span class=\"rnthl rntliteral\">read_ds_sensor()<\/span> that gets the temperature from the DS18B20 temperature sensor. If you&#8217;ve followed the previous section, you should be familiar with the methods used here.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def read_ds_sensor():\n  roms = ds_sensor.scan()\n  print('Found DS devices: ', roms)\n  ds_sensor.convert_temp()\n  for rom in roms:\n    temp = ds_sensor.read_temp(rom)\n    if isinstance(temp, float):\n      temp = round(temp, 2)\n      print('Valid temperature')\n      return temp\n  return '0'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Web Page<\/h4>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">web_page()<\/span> function returns the HTML page with the latest temperature readings.<\/p>\n\n\n\n<p>We&#8217;ve built a similar web page on a previous tutorial. So, if you want to learn how the HTML works, you can read this article: <a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-dht11-dht22-web-server\/\">MicroPython: ESP32\/ESP8266 with DHT11\/DHT22 Web Server<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"> Creating the web server<\/h4>\n\n\n\n<p>After that, make the usual procedures to create a socket server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>while 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')<\/code><\/pre>\n\n\n\n<p>For an in-depth explanation of this procedure,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-micropython-web-server\/\">refer to this tutorial<\/a>. <\/p>\n\n\n\n<p>Basically, when the ESP receives a request, we send the web page with the latest readings as a response:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>response = web_page()\nconn.send('HTTP\/1.1 200 OK\\n')\nconn.send('Content-Type: text\/html\\n')\nconn.send('Connection: close\\n\\n')\nconn.sendall(response)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Web Server Demonstration<\/h2>\n\n\n\n<p>After uploading the <em>boot.py<\/em> and <em>main.py<\/em> files to your board. Click the ESP RST button to run the code.<\/p>\n\n\n\n<p>Then, open your browser and type the ESP IP address. You can access the web page with the latest sensor readings in Celsius and Fahrenheit degrees: <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"687\" height=\"461\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-Web-Server.png?resize=687%2C461&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Temperature Readings on ESP32 ESP8266 Web Server\" class=\"wp-image-86800\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-Web-Server.png?w=687&amp;quality=100&amp;strip=all&amp;ssl=1 687w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-Web-Server.png?resize=300%2C201&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 687px) 100vw, 687px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>We hope you&#8217;ve found this MicroPython guide about the DS18B20 temperature sensor with the ESP32 and ESP8266 useful. We have other projects with the ESP and MicroPython that you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-bme280-esp32-esp8266\/\">MicroPython: BME280 with ESP32 and ESP8266 (Pressure, Temperature, Humidity)<\/a><br><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-mqtt-esp32-esp8266\/\">MicroPython: Getting Started with MQTT on ESP32\/ESP8266<\/a><\/li><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 href=\"https:\/\/randomnerdtutorials.com\/micropython-oled-display-esp32-esp8266\/\">MicroPython: OLED Display with ESP32 and ESP8266<\/a><\/li><\/ul>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about MicroPython with ESP32 and ESP8266 with our eBook: <a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\"><strong>MicroPython Programming with ESP32 and ESP8266<\/strong><\/a><\/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 DS18B20 sensor readings using MicroPython firmware. We\u2019ll create an &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython: ESP32\/ESP8266 DS18B20 Web Server (Weather Station)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-ds18b20-web-server\/#more-99689\" aria-label=\"Read more about MicroPython: ESP32\/ESP8266 DS18B20 Web Server (Weather Station)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":99629,"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-99689","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\/ESP32-ESP8266-MicroPython-DS18B20-Temperature-Web-Server.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\/99689","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=99689"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/99689\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/99629"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=99689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=99689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=99689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}