{"id":25036,"date":"2019-07-16T08:27:46","date_gmt":"2019-07-16T08:27:46","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=25036"},"modified":"2025-03-17T12:29:33","modified_gmt":"2025-03-17T12:29:33","slug":"esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide\/","title":{"rendered":"ESP8266 DS18B20 Temperature Sensor with Arduino IDE (Single, Multiple, Web Server)"},"content":{"rendered":"\n<p>This is a in-depth guide for the DS18B20 temperature sensor with ESP8266 using Arduino IDE. We&#8217;ll cover how to wire the sensor, install the required libraries, and write the code to get the sensor readings from one and multiple sensors. Finally, we&#8217;ll build a simple web server to display the 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\/07\/ESP8266-DS18B20-Temperature-Sensor-Arduino-IDE-Web-Server.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 with DS18B20 Temperature Sensor using Arduino IDE\" class=\"wp-image-87102\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-DS18B20-Temperature-Sensor-Arduino-IDE-Web-Server.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-DS18B20-Temperature-Sensor-Arduino-IDE-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\/2019\/07\/ESP8266-DS18B20-Temperature-Sensor-Arduino-IDE-Web-Server.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-DS18B20-Temperature-Sensor-Arduino-IDE-Web-Server.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<p>Throughout this tutorial we\u2019ll cover the following topics:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Read temperature from one DS18B20 temperature sensor;<\/li>\n\n\n\n<li>Read temperature from multiple DS18B20 temperature sensors;<\/li>\n\n\n\n<li>Display DS18B20 sensor readings on a web server.<\/li>\n<\/ol>\n\n\n\n<p>You might also like reading other DS18B20 guides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-ds18b20-temperature-arduino-ide\/\">ESP32 DS18B20 Temperature Sensor with Arduino IDE<\/a><\/li>\n\n\n\n<li><a style=\"background-color: initial; font-size: inherit;\" href=\"https:\/\/randomnerdtutorials.com\/micropython-ds18b20-esp32-esp8266\/\">ESP32\/ESP8266 DS18B20 Temperature Sensor with MicroPython<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-multiple-ds18b20-temperature-sensors\/\">ESP32 with Multiple DS18B20 Temperature Sensors<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-ds18b20-temperature-sensor-with-arduino\/\">DS18B20 Temperature Sensor with Arduino<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about the ESP8266 with our course:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266<\/a>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing DS18B20 Temperature Sensor<\/h2>\n\n\n\n<p>The&nbsp;<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>&nbsp;is a one-wire digital temperature sensor. This means that it just requires one data line (and GND) to communicate with your 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 \u201cparasite mode\u201d), which eliminates the need for an external power supply.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><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><\/div>\n\n\n<p>Each DS18B20 temperature sensor has a unique 64-bit serial code. This allows you to wire multiple sensors to the same data wire. So, you can get temperature from multiple sensors using just one GPIO.<\/p>\n\n\n\n<p>The DS18B20 temperature sensor is also available in&nbsp;<a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-digital-temperature-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\">waterproof version<\/a>.<\/p>\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\/07\/ds18b20-waterproof.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Temperature Sensor Waterproof version\" class=\"wp-image-86674\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20-waterproof.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-waterproof.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>Here\u2019s a summary of the most relevant specs of the DS18B20 temperature sensor:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Communicates over one-wire bus<\/li>\n\n\n\n<li>Power supply range: 3.0V to 5.5V<\/li>\n\n\n\n<li>Operating temperature range: -55\u00baC to +125\u00baC<\/li>\n\n\n\n<li>Accuracy +\/-0.5 \u00baC (between the range -10\u00baC to 85\u00baC)<\/li>\n<\/ul>\n\n\n\n<p>For more information consult the&nbsp;<a href=\"https:\/\/datasheets.maximintegrated.com\/en\/ds\/DS18B20.pdf\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">DS18B20 datasheet<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>To complete this tutorial you need the following components:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266<\/a> &#8211; read <a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\">Best ESP8266 Wi-Fi Development Boards<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-digital-temperature-sensor\/\" target=\"_blank\">DS18B20 temperature sensor&nbsp;<\/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>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">4.7k Ohm Resistor<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" 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\" 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\">ESP8266 with DS18B20 Schematic Diagram<\/h2>\n\n\n\n<p>As mentioned previously, the DS18B20 temperature sensor can be powered through the VDD pin (<strong>normal mode<\/strong>), or it can derive its power from the data line (<strong>parasite mode<\/strong>). You can choose either modes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parasite Mode<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp8266_single_parasite.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"558\" height=\"763\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp8266_single_parasite.png?resize=558%2C763&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Temperature Sensor with ESP8266 Parasite Mode Wiring Schematic Diagram\" class=\"wp-image-86568\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp8266_single_parasite.png?w=558&amp;quality=100&amp;strip=all&amp;ssl=1 558w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp8266_single_parasite.png?resize=219%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 219w\" sizes=\"(max-width: 558px) 100vw, 558px\" \/><\/a><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Normal Mode<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp8266_single_normal_F.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"559\" height=\"763\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ds18b20_esp8266_single_normal_F.png?resize=559%2C763&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Temperature Sensor with ESP8266 Normal Mode Wiring Schematic Diagram\" class=\"wp-image-86567\" 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: 559px) 100vw, 559px\" \/><\/a><\/figure><\/div>\n\n\n<p><strong>Note:&nbsp;<\/strong>in this tutorial we\u2019re connecting the DS18B20 data line to <span class=\"rnthl rntclblue\">GPIO 4<\/span>, but you can use any other suitable GPIO.<\/p>\n\n\n\n<p>Read our&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pinout-reference-gpios\/\">ESP8266 GPIO Reference Guide<\/a>&nbsp;to learn more about the ESP8266 GPIOs.<\/p>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> if you\u2019re using an <a href=\"https:\/\/makeradvisor.com\/tools\/esp-01-wi-fi-board\/\">ESP-01<\/a>,&nbsp;GPIO 2&nbsp;is the most suitable pin to connect to the DS18B20 data pin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing Your Arduino IDE<\/h2>\n\n\n\n<p> We\u2019ll program the ESP8266 using Arduino IDE, so make sure you have the ESP8266 add-on installed before proceeding:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp8266-nodemcu-arduino-ide-2-0\/\" title=\"\">Install ESP8266 Board in Arduino IDE (Windows, Mac OS X, and Linux instructions)<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Installing Libraries for DS18B20<\/h3>\n\n\n\n<p>To interface with the DS18B20 temperature sensor, you need to install the&nbsp;<a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/github.com\/PaulStoffregen\/OneWire\" target=\"_blank\">One Wire library by Paul Stoffregen<\/a>&nbsp;and the&nbsp;<a href=\"https:\/\/github.com\/milesburton\/Arduino-Temperature-Control-Library\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Dallas Temperature library<\/a>. Follow the next steps to install those libraries.<\/p>\n\n\n\n<p>1. Open your Arduino IDE and go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>. The Library Manager should open.<\/p>\n\n\n\n<p>2. Type \u201c<strong>onewire<\/strong>\u201d in the search box and install the OneWire library by Paul Stoffregen.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-onewire-library.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install OneWire library by Paul Stoffregen in Arduino IDE\" class=\"wp-image-86581\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-onewire-library.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-onewire-library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-onewire-library.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n<p>3. Then, search for &#8220;<strong>Dallas<\/strong>&#8221; and install the Dallas Temperature library by Miles Burton.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-dallas-temperature.png?resize=787%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install DallasTemperature library by Miles Burton in Arduino IDE\" class=\"wp-image-86582\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-dallas-temperature.png?w=787&amp;quality=100&amp;strip=all&amp;ssl=1 787w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-dallas-temperature.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/install-dallas-temperature.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><\/figure><\/div>\n\n\n<p>After installing the libraries, restart your Arduino IDE.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Code (Single DS18B20)<\/h2>\n\n\n\n<p>After installing the required libraries, you can upload the following code to the ESP8266. The code reads temperature from the DS18B20 temperature sensor and displays the readings on the Arduino IDE Serial Monitor. <\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com  \n*********\/\n\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;\n\n\/\/ GPIO where the DS18B20 is connected to\nconst int oneWireBus = 4;     \n\n\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(oneWireBus);\n\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);\n\nvoid setup() {\n  \/\/ Start the Serial Monitor\n  Serial.begin(115200);\n  \/\/ Start the DS18B20 sensor\n  sensors.begin();\n}\n\nvoid loop() {\n  sensors.requestTemperatures(); \n  float temperatureC = sensors.getTempCByIndex(0);\n  float temperatureF = sensors.getTempFByIndex(0);\n  Serial.print(temperatureC);\n  Serial.println(&quot;\u00baC&quot;);\n  Serial.print(temperatureF);\n  Serial.println(&quot;\u00baF&quot;);\n  delay(5000);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_DS18B20_Single.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>There are many different ways to get the temperature from DS18B20 temperature sensors. However, if you&#8217;re using just one single sensor, this is one of the easiest and simplest ways. <\/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=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-single-ds18b20.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Single OneWire Temperature Sensor with ESP32 and Arduino IDE on Breadboard\" class=\"wp-image-87077\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-single-ds18b20.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-single-ds18b20.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<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>Start by including the <span class=\"rnthl rntliteral\">OneWire<\/span> and the <span class=\"rnthl rntliteral\">DallasTemperature<\/span> libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;<\/code><\/pre>\n\n\n\n<p>Create the instances needed for the temperature sensor. The temperature sensor is connected to <span class=\"rnthl rntclblue\">GPIO 4<\/span>. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ GPIO where the DS18B20 is connected to\nconst int oneWireBus = 4;\n\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(oneWireBus);\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial Monitor at a baud rate of 115200.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Initialize the DS18B20 temperature sensor:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.begin();<\/code><\/pre>\n\n\n\n<p>Before actually getting the temperature, you need to call the <span class=\"rnthl rntliteral\">requestTemperatures()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.requestTemperatures(); <\/code><\/pre>\n\n\n\n<p>Then, get the temperature in Celsius by using the <span class=\"rnthl rntliteral\">getTempCByIndex()<\/span> method as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float temperatureC = sensors.getTempCByIndex(0);<\/code><\/pre>\n\n\n\n<p>Or use the <span class=\"rnthl rntliteral\">getTempFByIndex()<\/span> to get the temperature in Fahrenheit.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float temperatureF = sensors.getTempFByIndex(0);<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">getTempCByIndex()<\/span> and the <span class=\"rnthl rntliteral\">getTempFByIndex()<\/span> methods accept the index of the temperature sensor. Because we&#8217;re using just one sensor its index is 0. If you want to read more than one sensor, you use index 0 for one sensor, index 1 for other sensor and so on.<\/p>\n\n\n\n<p>Finally, print the results in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(temperatureC);\nSerial.println(\"\u00baC\");\nSerial.print(temperatureF);\nSerial.println(\"\u00baF\");<\/code><\/pre>\n\n\n\n<p>New temperature readings are requested every 5 seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>delay(5000);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>After uploading the code, open the Arduino IDE Serial Monitor&nbsp;at a 115200 baud rate. You should get the temperature displayed in both Celsius and Fahrenheit: <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-ESP8266-Basic-Temperature-Example.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"665\" height=\"525\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-ESP8266-Basic-Temperature-Example.png?resize=665%2C525&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-102955\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-ESP8266-Basic-Temperature-Example.png?w=665&amp;quality=100&amp;strip=all&amp;ssl=1 665w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/DS18B20-ESP8266-Basic-Temperature-Example.png?resize=300%2C237&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 665px) 100vw, 665px\" \/><\/a><\/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\" id=\"multiple-sensors\">Getting Temperature from Multiple DS18B20 Temperature Sensors<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-ds18b20-multiple-sensors-1.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Multiple Temperature Sensors OneWire with ESP32 and Arduino IDE\" class=\"wp-image-87081\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-ds18b20-multiple-sensors-1.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-ds18b20-multiple-sensors-1.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>The DS18B20 temperature sensor communicates using one-wire protocol and each sensor has a unique 64-bit serial code, so you can read the temperature from multiple sensors using just one single digital Pin.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic<\/h3>\n\n\n\n<p>To read the temperature from multiple sensors, you just need to wire all data lines together as shown in the next schematic diagram:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1020\" height=\"806\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_esp8266_multiple_bb.png?resize=1020%2C806&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS18B20 Multiple Temperature Sensors with ESP32 Wiring Schematic Diagram\" class=\"wp-image-87082\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_esp8266_multiple_bb.png?w=1020&amp;quality=100&amp;strip=all&amp;ssl=1 1020w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_esp8266_multiple_bb.png?resize=300%2C237&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ds18b20_esp8266_multiple_bb.png?resize=768%2C607&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1020px) 100vw, 1020px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Code (Multiple DS18B20s)<\/h3>\n\n\n\n<p>Then, upload the following code. It scans for all devices on <span class=\"rnthl rntclblue\">GPIO 4<\/span> and prints the temperature for each one. This sketch is based on the example provided by the DallasTemperature library.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com  \n*********\/\n\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;\n\n\/\/ Data wire is plugged TO GPIO 4\n#define ONE_WIRE_BUS 4\n\n\/\/ Setup a oneWire instance to communicate with any OneWire devices (not just Maxim\/Dallas temperature ICs)\nOneWire oneWire(ONE_WIRE_BUS);\n\n\/\/ Pass our oneWire reference to Dallas Temperature. \nDallasTemperature sensors(&amp;oneWire);\n\n\/\/ Number of temperature devices found\nint numberOfDevices;\n\n\/\/ We'll use this variable to store a found device address\nDeviceAddress tempDeviceAddress; \n\nvoid setup(){\n  \/\/ start serial port\n  Serial.begin(115200);\n  \n  \/\/ Start up the library\n  sensors.begin();\n  \n  \/\/ Grab a count of devices on the wire\n  numberOfDevices = sensors.getDeviceCount();\n  \n  \/\/ locate devices on the bus\n  Serial.print(&quot;Locating devices...&quot;);\n  Serial.print(&quot;Found &quot;);\n  Serial.print(numberOfDevices, DEC);\n  Serial.println(&quot; devices.&quot;);\n\n  \/\/ Loop through each device, print out address\n  for(int i=0;i&lt;numberOfDevices; i++){\n    \/\/ Search the wire for address\n    if(sensors.getAddress(tempDeviceAddress, i)){\n      Serial.print(&quot;Found device &quot;);\n      Serial.print(i, DEC);\n      Serial.print(&quot; with address: &quot;);\n      printAddress(tempDeviceAddress);\n      Serial.println();\n    } else {\n      Serial.print(&quot;Found ghost device at &quot;);\n      Serial.print(i, DEC);\n      Serial.print(&quot; but could not detect address. Check power and cabling&quot;);\n    }\n  }\n}\n\nvoid loop(){ \n  sensors.requestTemperatures(); \/\/ Send the command to get temperatures\n  \n  \/\/ Loop through each device, print out temperature data\n  for(int i=0;i&lt;numberOfDevices; i++){\n    \/\/ Search the wire for address\n    if(sensors.getAddress(tempDeviceAddress, i)){\n      \/\/ Output the device ID\n      Serial.print(&quot;Temperature for device: &quot;);\n      Serial.println(i,DEC);\n      \/\/ Print the data\n      float tempC = sensors.getTempC(tempDeviceAddress);\n      Serial.print(&quot;Temp C: &quot;);\n      Serial.print(tempC);\n      Serial.print(&quot; Temp F: &quot;);\n      Serial.println(DallasTemperature::toFahrenheit(tempC)); \/\/ Converts tempC to Fahrenheit\n    }\n  }\n  delay(5000);\n}\n\n\/\/ function to print a device address\nvoid printAddress(DeviceAddress deviceAddress) {\n  for (uint8_t i = 0; i &lt; 8; i++){\n    if (deviceAddress[i] &lt; 16) Serial.print(&quot;0&quot;);\n      Serial.print(deviceAddress[i], HEX);\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_DS18B20_Multiple.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the code works<\/h3>\n\n\n\n<p>The code uses several useful methods to handle multiple DS18B20 sensors.<\/p>\n\n\n\n<p>You use the <span class=\"rnthl rntliteral\">getDeviceCount()<\/span> method to get the number of DS18B20 sensors on the data line.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>numberOfDevices = sensors.getDeviceCount();<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">getAddress()<\/span> method finds the sensors addresses:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if(sensors.getAddress(tempDeviceAddress, i)){<\/code><\/pre>\n\n\n\n<p>The address is unique for each sensor. So each sensor can be identified by its address.<\/p>\n\n\n\n<p>Then, you use the <span class=\"rnthl rntliteral\">getTempC()<\/span> method that accepts as argument the device address. With this method, you can get the temperature from a specific sensor:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float tempC = sensors.getTempC(tempDeviceAddress);<\/code><\/pre>\n\n\n\n<p>To get the temperature in Fahrenheit degrees, you can use the <span class=\"rnthl rntliteral\">getTempF()<\/span>. Alternatively, you can convert the temperature in Celsius to Fahrenheit as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>DallasTemperature::toFahrenheit(tempC)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>After uploading the code, open your Serial Monitor at a baud rate of 115200. You should get all your sensors readings displayed as shown below:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"454\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/multiple-ds18b20-sensor-serial-monitor-115200.png?resize=708%2C454&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Multiple DS18B20 Temperature Sensors Serial Monitor Print readings\" class=\"wp-image-87090\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/multiple-ds18b20-sensor-serial-monitor-115200.png?w=708&amp;quality=100&amp;strip=all&amp;ssl=1 708w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/multiple-ds18b20-sensor-serial-monitor-115200.png?resize=300%2C192&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 708px) 100vw, 708px\" \/><\/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 DS18B20 Temperature Readings in a Web Server<\/h2>\n\n\n\n<p>To build the web server we\u2019ll use the\u00a0<a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncWebServer\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncWebServer <\/a>library that provides an easy way to build an asynchronous web server. Building an asynchronous web server has several advantages. We recommend taking a quick look at the <a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncWebServer\/wiki\" target=\"_blank\" rel=\"noopener\" title=\"\">library documentation on its GitHub page<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing the Web Server Libraries<\/h3>\n\n\n\n<p>We&#8217;ll build the web server using the following libraries:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncWebServer\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncWebServer by ESP32Async<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncTCP\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncTCP&nbsp;by ESP32Async<\/a><\/li>\n<\/ul>\n\n\n\n<p>You can install these libraries using the Arduino Library Manager. Go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>&nbsp;and search for the libraries&#8217; names.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code (DS18B20 Async Web Server)<\/h3>\n\n\n\n<p>Open your Arduino IDE and copy the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com  \n*********\/\n\n\/\/ Import required libraries\n#ifdef ESP32\n  #include &lt;WiFi.h&gt;\n  #include &lt;ESPAsyncWebServer.h&gt;\n#else\n  #include &lt;Arduino.h&gt;\n  #include &lt;ESP8266WiFi.h&gt;\n  #include &lt;Hash.h&gt;\n  #include &lt;ESPAsyncTCP.h&gt;\n  #include &lt;ESPAsyncWebServer.h&gt;\n#endif\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;\n\n\/\/ Data wire is connected to GPIO 4\n#define ONE_WIRE_BUS 4\n\n\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(ONE_WIRE_BUS);\n\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);\n\n\/\/ Variables to store temperature values\nString temperatureF = &quot;&quot;;\nString temperatureC = &quot;&quot;;\n\n\/\/ Timer variables\nunsigned long lastTime = 0;  \nunsigned long timerDelay = 30000;\n\n\/\/ Replace with your network credentials\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n\/\/ Create AsyncWebServer object on port 80\nAsyncWebServer server(80);\n\nString readDSTemperatureC() {\n  \/\/ Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus\n  sensors.requestTemperatures(); \n  float tempC = sensors.getTempCByIndex(0);\n\n  if(tempC == -127.00) {\n    Serial.println(&quot;Failed to read from DS18B20 sensor&quot;);\n    return &quot;--&quot;;\n  } else {\n    Serial.print(&quot;Temperature Celsius: &quot;);\n    Serial.println(tempC); \n  }\n  return String(tempC);\n}\n\nString readDSTemperatureF() {\n  \/\/ Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus\n  sensors.requestTemperatures(); \n  float tempF = sensors.getTempFByIndex(0);\n\n  if(int(tempF) == -196){\n    Serial.println(&quot;Failed to read from DS18B20 sensor&quot;);\n    return &quot;--&quot;;\n  } else {\n    Serial.print(&quot;Temperature Fahrenheit: &quot;);\n    Serial.println(tempF);\n  }\n  return String(tempF);\n}\n\nconst char index_html[] PROGMEM = R&quot;rawliteral(\n&lt;!DOCTYPE HTML&gt;&lt;html&gt;\n&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;\n    html {\n     font-family: Arial;\n     display: inline-block;\n     margin: 0px auto;\n     text-align: center;\n    }\n    h2 { font-size: 3.0rem; }\n    p { font-size: 3.0rem; }\n    .units { font-size: 1.2rem; }\n    .ds-labels{\n      font-size: 1.5rem;\n      vertical-align:middle;\n      padding-bottom: 15px;\n    }\n  &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;h2&gt;ESP DS18B20 Server&lt;\/h2&gt;\n  &lt;p&gt;\n    &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 Celsius&lt;\/span&gt; \n    &lt;span id=&quot;temperaturec&quot;&gt;%TEMPERATUREC%&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;\n    &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 Fahrenheit&lt;\/span&gt;\n    &lt;span id=&quot;temperaturef&quot;&gt;%TEMPERATUREF%&lt;\/span&gt;\n    &lt;sup class=&quot;units&quot;&gt;&amp;deg;F&lt;\/sup&gt;\n  &lt;\/p&gt;\n&lt;\/body&gt;\n&lt;script&gt;\nsetInterval(function ( ) {\n  var xhttp = new XMLHttpRequest();\n  xhttp.onreadystatechange = function() {\n    if (this.readyState == 4 &amp;&amp; this.status == 200) {\n      document.getElementById(&quot;temperaturec&quot;).innerHTML = this.responseText;\n    }\n  };\n  xhttp.open(&quot;GET&quot;, &quot;\/temperaturec&quot;, true);\n  xhttp.send();\n}, 10000) ;\nsetInterval(function ( ) {\n  var xhttp = new XMLHttpRequest();\n  xhttp.onreadystatechange = function() {\n    if (this.readyState == 4 &amp;&amp; this.status == 200) {\n      document.getElementById(&quot;temperaturef&quot;).innerHTML = this.responseText;\n    }\n  };\n  xhttp.open(&quot;GET&quot;, &quot;\/temperaturef&quot;, true);\n  xhttp.send();\n}, 10000) ;\n&lt;\/script&gt;\n&lt;\/html&gt;)rawliteral&quot;;\n\n\/\/ Replaces placeholder with DS18B20 values\nString processor(const String&amp; var){\n  \/\/Serial.println(var);\n  if(var == &quot;TEMPERATUREC&quot;){\n    return temperatureC;\n  }\n  else if(var == &quot;TEMPERATUREF&quot;){\n    return temperatureF;\n  }\n  return String();\n}\n\nvoid setup(){\n  \/\/ Serial port for debugging purposes\n  Serial.begin(115200);\n  Serial.println();\n  \n  \/\/ Start up the DS18B20 library\n  sensors.begin();\n\n  temperatureC = readDSTemperatureC();\n  temperatureF = readDSTemperatureF();\n\n  \/\/ Connect to Wi-Fi\n  WiFi.begin(ssid, password);\n  Serial.println(&quot;Connecting to WiFi&quot;);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  Serial.println();\n  \n  \/\/ Print ESP Local IP Address\n  Serial.println(WiFi.localIP());\n\n  \/\/ Route for root \/ web page\n  server.on(&quot;\/&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    request-&gt;send(200, &quot;text\/html&quot;, index_html, processor);\n  });\n  server.on(&quot;\/temperaturec&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    request-&gt;send(200, &quot;text\/plain&quot;, temperatureC.c_str());\n  });\n  server.on(&quot;\/temperaturef&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    request-&gt;send(200, &quot;text\/plain&quot;, temperatureF.c_str());\n  });\n  \/\/ Start server\n  server.begin();\n}\n \nvoid loop(){\n  if ((millis() - lastTime) &gt; timerDelay) {\n    temperatureC = readDSTemperatureC();\n    temperatureF = readDSTemperatureF();\n    lastTime = millis();\n  }  \n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_DS18B20_Async_Web_Server.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Insert your network credentials in the following variables, and the code will work straight away.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>In the following paragraphs we\u2019ll explain how the code works. Keep reading if you want to learn more or jump to the &#8220;Demonstration&#8221; section to see the final result.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Importing libraries<\/h4>\n\n\n\n<p>First, import the required libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Arduino.h&gt;\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;Hash.h&gt;\n#include &lt;ESPAsyncTCP.h&gt;\n#include &lt;ESPAsyncWebServer.h&gt;\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Instantiate DS18B20 Sensor<\/h4>\n\n\n\n<p>Define the GPIO that the DS18B20 data pin is connected to. In this case, it\u2019s connected to <span class=\"rnthl rntclblue\">GPIO 4<\/span>(D1).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define ONE_WIRE_BUS 4<\/code><\/pre>\n\n\n\n<p>Instantiate the instances needed to initialize the sensor:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(ONE_WIRE_BUS);\n\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);<\/code><\/pre>\n\n\n\n<p>Create variables that will hold the temperature and humidity as String values:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String temperatureF = \"\";\nString temperatureC = \"\";<\/code><\/pre>\n\n\n\n<p>We&#8217;ll get new sensor readings every 30 seconds. You can change that in the <span class=\"rnthl rntliteral\">timerDelay<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>unsigned long lastTime = 0;  \nunsigned long timerDelay = 30000;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Setting your network credentials<\/h4>\n\n\n\n<p>Insert your network credentials in the following variables, so that the ESP8266 can connect to your local network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<p>Create an&nbsp;AsyncWebServer object on port 80.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>AsyncWebServer server(80);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Read Temperature Functions<\/h4>\n\n\n\n<p>Then, we create two functions to read the temperature.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">readDSTemperatureC()<\/span> function returns the readings in Celsius degrees.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String readDSTemperatureC() {\n  \/\/ Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus\n  sensors.requestTemperatures(); \n  float tempC = sensors.getTempCByIndex(0);\n\n  if(tempC == -127.00){\n    Serial.println(\"Failed to read from DS18B20 sensor\");\n    return \"--\";\n  } else {\n    Serial.print(\"Temperature Celsius: \");\n    Serial.println(tempC); \n  }\n  return String(tempC);\n}<\/code><\/pre>\n\n\n\n<p>In case the sensor is not able to get a valid reading, it returns -127. So, we have an if statement that returns two dashes (\u2013-) in case the sensor fails to get the readings. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if(tempC == -127.00){\n  Serial.println(\"Failed to read from DS18B20 sensor\");\n  return \"--\";<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">reaDSTemperatureF()<\/span> function works in a similar way but returns the readings in Fahrenheit degrees.<\/p>\n\n\n\n<p>The readings are returned as string type. To convert a float to a string, use the <span class=\"rnthl rntliteral\">String()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>return String(tempC);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Building the Web Page<\/h4>\n\n\n\n<p>The next step is building the web page. The HTML and CSS needed to build the web page are saved on the <span class=\"rnthl rntliteral\">index_html<\/span> variable.<\/p>\n\n\n\n<p>In the HTML text we have <span class=\"rnthl rntliteral\">TEMPERATUREC<\/span> and <span class=\"rnthl rntliteral\">TEMPERATUREF<\/span> between <strong>%<\/strong> signs. This is a placeholder for the temperature values.<\/p>\n\n\n\n<p>This means that this <span class=\"rnthl rntliteral\"><strong>%TEMPERATUREC%<\/strong><\/span> text is like a variable that will be replaced by the actual temperature value from the sensor. The placeholders on the HTML text should go between <strong>%<\/strong> signs. <\/p>\n\n\n\n<p>We&#8217;ve explained in great detail how the HTML and CSS used in this web server works in a previous tutorial. So, if you want to learn more, refer to the next project:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide\/\">DHT11\/DHT22 Temperature and Humidity Web Server with Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Processor<\/h4>\n\n\n\n<p>Now, we need to create the <span class=\"rnthl rntliteral\">processor()<\/span> function, that will replace the placeholders in our HTML text with the actual temperature values.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Replaces placeholder with DS18B20 values\nString processor(const String&amp; var){\n  \/\/Serial.println(var);\n  if(var == \"TEMPERATUREC\"){\n    return temperatureC;\n  }\n  else if(var == \"TEMPERATUREF\"){\n    return temperatureF;\n  }\n  return String();\n}<\/code><\/pre>\n\n\n\n<p>When the web page is requested, we check if the HTML has any placeholders. If it finds the <span class=\"rnthl rntliteral\">%TEMPERATUREC%<\/span> placeholder, we return the temperature in Celsius by calling the <span class=\"rnthl rntliteral\">readDSTemperatureC()<\/span> function created previously.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if(var == \"TEMPERATUREC\"){\n  return temperatureC;\n}<\/code><\/pre>\n\n\n\n<p>If the placeholder is <span class=\"rnthl rntliteral\">%TEMPERATUREF%<\/span>, we return the temperature in Fahrenheit.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>else if(var == \"TEMPERATUREF\"){\n  return temperatureF;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial Monitor for debugging purposes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Initialize the DS18B20 temperature sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.begin();<\/code><\/pre>\n\n\n\n<p>Get the current temperature values:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>temperatureC = readDSTemperatureC();\ntemperatureF = readDSTemperatureF();<\/code><\/pre>\n\n\n\n<p>Connect to your local network and print the ESP8266 IP address.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFi.begin(ssid, password);\nSerial.println(\"Connecting to WiFi\");\nwhile (WiFi.status() != WL_CONNECTED) {\n  delay(500);\n  Serial.print(\".\");\n}\nSerial.println();\n  \n\/\/ Print ESP8266 Local IP Address\nSerial.println(WiFi.localIP());<\/code><\/pre>\n\n\n\n<p>Finally, add the next lines of code to handle the web server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/html\", index_html, processor);\n});\nserver.on(\"\/temperaturec\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/plain\", temperatureC.c_str());\n});\nserver.on(\"\/temperaturef\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/plain\", temperatureF.c_str());\n});<\/code><\/pre>\n\n\n\n<p>When we make a request on the root URL, we send the HTML text that is stored in the <span class=\"rnthl rntliteral\">index_html<\/span> variable. We also need to pass the <span class=\"rnthl rntliteral\">processor<\/span> function, that will replace all the placeholders with the right values.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/html\", index_html, processor);\n});<\/code><\/pre>\n\n\n\n<p>We need to add two additional handlers to update the temperature readings. When we receive a request on the&nbsp;<em>\/temperature<\/em>c&nbsp;URL, we simply need to send the updated temperature value. It is plain text, and it should be sent as a char, so, we use the <span class=\"rnthl rntliteral\">c_str()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/temperaturec\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/plain\", temperatureC.c_str());\n});<\/code><\/pre>\n\n\n\n<p>The same process is repeated for the temperature in Fahrenheit.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/temperaturef\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request-&gt;send_P(200, \"text\/plain\", temperatureF.c_str());\n});<\/code><\/pre>\n\n\n\n<p>Lastly, we can start the server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.begin();<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, update the temperature values every 30 seconds (<span class=\"rnthl rntliteral\">timerDelay<\/span> variable).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop(){\n  if ((millis() - lastTime) &gt; timerDelay) {\n    temperatureC = readDSTemperatureC();\n    temperatureF = readDSTemperatureF();\n    lastTime = millis();\n  }  \n}<\/code><\/pre>\n\n\n\n<p>That\u2019s pretty much how the code works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>After uploading the code, open the Arduino IDE Serial Monitor at a baud rate of 115200.&nbsp;After a few seconds your IP address should show up.<\/p>\n\n\n\n<p>In your local network, open a browser and type&nbsp;the ESP8266 IP address.<\/p>\n\n\n\n<p>Now you&nbsp;can see&nbsp;temperature in Celsius and Fahrenheit in your web server. The sensor readings update automatically without the need to refresh the web page.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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=\"ESP8266 Web Server with DS18B20 OneWire Temperature Sensor Arduino IDE\" 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<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This was an in-depth guide on how to use the DS18B20 temperature sensor with the ESP8266 and display the readings on a web server.<\/p>\n\n\n\n<p>The DS18B20 communicates via one-wire protocol and each sensor has a unique 64-bit serial code, which means you can read many sensors on the same data pin.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. We have other tutorials with the ESP8266 that you may also like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-bme280-arduino-ide\/\">ESP8266 with BME280 using Arduino IDE (Pressure, Temperature, Humidity)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-dht-temperature-and-humidity-oled-display\/\">ESP8266: DHT Temperature and Humidity Readings in OLED Display<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/category\/0-esp8266\/\">ESP8266 0.96 inch OLED Display with Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-adc-reading-analog-values-with-nodemcu\/\">ESP8266 DHT11\/DHT22 Temperature and Humidity Web Server with Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP8266 with <a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\"><strong>Home Automation using ESP8266 (eBook + Video Course)<\/strong><\/a>.<\/p>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a in-depth guide for the DS18B20 temperature sensor with ESP8266 using Arduino IDE. We&#8217;ll cover how to wire the sensor, install the required libraries, and write the code &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP8266 DS18B20 Temperature Sensor with Arduino IDE (Single, Multiple, Web Server)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide\/#more-25036\" aria-label=\"Read more about ESP8266 DS18B20 Temperature Sensor with Arduino IDE (Single, Multiple, Web Server)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":87102,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[265,246,300,264,215,257],"tags":[],"class_list":["post-25036","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266-project","category-esp8266-arduino-ide","category-0-esp8266","category-project","category-sensors","category-web-server-2"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP8266-DS18B20-Temperature-Sensor-Arduino-IDE-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\/25036","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=25036"}],"version-history":[{"count":4,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/25036\/revisions"}],"predecessor-version":[{"id":168095,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/25036\/revisions\/168095"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/87102"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=25036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=25036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=25036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}