ESP8266 DS18B20 Temperature Sensor Web Server with Arduino IDE

In this project you’ll create a standalone web server with an ESP8266 that displays the temperature with a DS18B20 temperature sensor.

Recommended Resources:

If you like the ESP and you want to do more projects you can read my eBook Home Automation Using ESP8266.

Let’s get started!

Installing the libraries

You’ll need to install the OneWire Library and DallasTemperature Library.

Installing the OneWire Library

  1. Click here to download the OneWire library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get OneWire-master folder
  3. Rename your folder from OneWire-master to OneWire
  4. Move the OneWire folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Installing the DallasTemperature Library

  1. Click here to download the DallasTemperature library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get Arduino-Temperature-Control-Library-master folder
  3. Rename your folder from Arduino-Temperature-Control-Library-master to DallasTemperature
  4. Move the DallasTemperature folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Uploading your ESP8266 code

Having the ESP8266 add-on for the Arduino IDE installed (How to Install the ESP8266 Board in Arduino IDE).

Go to Tools and select “Generic ESP8266 Module” or “ESP-12E”.

Arduino IDE select esp8266

Copy the sketch below to your Arduino IDE. Replace the SSID and password with your own credentials.

After modifying my sketch upload it to your ESP8266 (if you can’t upload code to your ESP8266, read this troubleshooting guide).

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/

// Including the ESP8266 WiFi library
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Replace with your network details
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// Data wire is plugged into pin D1 on the ESP8266 12-E - GPIO 5
#define ONE_WIRE_BUS 5

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature DS18B20(&oneWire);
char temperatureCString[7];
char temperatureFString[7];

// Web Server on port 80
WiFiServer server(80);

// only runs once on boot
void setup() {
  // Initializing serial port for debugging purposes
  Serial.begin(115200);
  delay(10);

  DS18B20.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement
  
  // Connecting to WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Starting the web server
  server.begin();
  Serial.println("Web server running. Waiting for the ESP IP...");
  delay(10000);
  
  // Printing the ESP IP address
  Serial.println(WiFi.localIP());
}

void getTemperature() {
  float tempC;
  float tempF;
  do {
    DS18B20.requestTemperatures(); 
    tempC = DS18B20.getTempCByIndex(0);
    dtostrf(tempC, 2, 2, temperatureCString);
    tempF = DS18B20.getTempFByIndex(0);
    dtostrf(tempF, 3, 2, temperatureFString);
    delay(100);
  } while (tempC == 85.0 || tempC == (-127.0));
}

// runs over and over again
void loop() {
  // Listenning for new clients
  WiFiClient client = server.available();
  
  if (client) {
    Serial.println("New client");
    // bolean to locate when the http request ends
    boolean blank_line = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        
        if (c == '\n' && blank_line) {
            getTemperature();
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            // your actual web page that displays temperature
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            client.println("<head></head><body><h1>ESP8266 - Temperature</h1><h3>Temperature in Celsius: ");
            client.println(temperatureCString);
            client.println("*C</h3><h3>Temperature in Fahrenheit: ");
            client.println(temperatureFString);
            client.println("*F</h3></body></html>");  
            break;
        }
        if (c == '\n') {
          // when starts reading a new line
          blank_line = true;
        }
        else if (c != '\r') {
          // when finds a character on the current line
          blank_line = false;
        }
      }
    }  
    // closing the client connection
    delay(1);
    client.stop();
    Serial.println("Client disconnected.");
  }
}   

View raw code

Schematics

To complete this project you need the following components:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Here’s the schematics:

ESP8266_ds18b20_arduino

ESP8266 IP Address

Open the Arduino IDE serial monitor at a baud rate of 115200. After a few seconds your IP address should appear. In my case it’s 192.168.1.105.

serial monitor

Demonstration

For the final demonstration open any browser from a device that is connected to the same router that your ESP is. Then, type the IP address and press Enter:

web server window

Now you can see temperature in Celsius and Fahrenheit in your web server. To see the latest readings simply refresh the web page.

Do you have any questions? Leave a comment down below.

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my Facebook Page.

P.S. If you got stuck during this tutorial make sure you read “ESP8266 Troubleshooting Guide

P.P.S. To lear more about the sensor used in this tutorial, you can read Guide for DS18B20 Temperature Sensor with Arduino


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

70 thoughts on “ESP8266 DS18B20 Temperature Sensor Web Server with Arduino IDE”

  1. Cool item, like to see more of this 🙂 and if I may,… some more articles with ESP modules in low power/power save mode (where the whole solution is powered by 1 18650 battery…

  2. Hi, thanks for the neat project, it looks very useful, and I may well build one of these for outdoor temp while we are living in our motor home this winter. Question though, could one add another sensor of the same kind for outdoor and use one for indoor?? Thus two Dallas temp sensors? Also could one also add a LCD display to display the temps locally as well as on the network?

  3. Hi, if I try to compile this code on my IDE I get this error:

    Temperature_Sensor:17: error: ‘D1’ was not declared in this scope

    #define ONE_WIRE_BUS D1

    It seems that D1 is not recognize. What do you think about?

  4. Hi. Great tutorial as usual. I have a suggestion for another ESP tutorial and that is to be able to pass data to a MySQL db. I have one set up on one of my RasPi’s and it receives data from my other RasPis but I also have ESP modules that I would like to collect data from but can’t figure out how to do that. I think this would be a great general interest topic to cover…if it can be done. 🙂
    Thanks,
    Bruce

  5. Hi Rui,

    I want to try this with ESP8266-01, GPIO2, so what should I use instead of “D1” ?

    Thanks in advance

  6. Hoi Rui

    Would you please explain to me what to change in the sketch so that the web-page is refreshing every x seconds ?

    regards Ton.

  7. Another great tutorial Rui, Thanks! I usually use an ESP07 or ESP12 in projects. Can you tell me how the GPIO numbers relate to the “Dn” numbering of the Nodemcu 1.0 Module. Thanks.

  8. Hello Rui, I built your project with the esp and DS1820B and it works fine with my mobile device but when I use Chrome or Firefox I don’t get a response. The ESP sees the connect but the browser just sits there. Any ideas?

  9. Yes !!!…as always…good job!!!!
    I am waiting for a light in the path from you to write a tutorial about FTP and this ESP8266 Node MCU, if you have time, one day maybe?

  10. bonsoir
    j’ai lu avec attention votre tuto
    peut on le mettre en place avec un réseau adhoc
    cordialement
    pierre

  11. Realy nice and informative thank you somuch for your kindness to freely over flow the knowldge. It is help to poors who cannot spend money for learning. God will take care of you. Thanks for shearing.

  12. I have just created an account for your home automation server and have set up a led and its working perfectly – Thank you.

    I also wanted to monitor room temperature, but cant see how to do this on the home automation server – is this possible?

  13. Great tutorial !!
    Btw, I have a question, how can I update the temparature data, when it changes, to the webserver without reloading the page ??
    Thank you for reading !

    • You can either assign a fixed IP address in your router to your ESP’s MAC address.
      Or simply Google ESP8266 Arduino IDE fixed IP address and you’ll find a snippet of code that you can use

  14. Many thanks for posting the article and code. Works like a charm on my basic ESP2866 and the DS18B20 board that I had from a Pi project. My first couple of days with the ESP8266, what an amazing piece of kit!

  15. Hi Rui, Merry Christmas.

    First of all, thanks for another great tutorial.

    I built the project with the esp and DS1820B and it works fine.

    Now I’m building another project, reads the data with ESP, and displays in serial terminal, showing like in the browser, but I see trash.
    How do I translate the view, it would be like in the browser?
    And how I present data also in LCD?
    Can you give me some aimed please?

    Thanks a lot.

  16. Hi Rui, Thank you for your comment.
    The Baud rate is OK.
    I see that the results of the measurement, but it probably displays HTML with a lot of other data.
    I would like to isolate the data of temperature, how do I do it?

    Thanks.

  17. Hello Rui
    i use fdti, esp8266-01, ds18b20
    I modified the line
    #define ONE_WIRE_BUS 5
    to
    #define ONE_WIRE_BUS 2 (gpio2)
    It worked once until “New client”
    Without being able to access the web
    Since the script stops after the ip address
    why ?
    Best regards
    Pierre

  18. As I am typing this reply the browser is still trying to connect to the ESP-12. It’s now more then 5 minutes. The serial monitor gives an IP and as soon as I type in the IP it shows: New client but nothing happens. As a matter of fact, It is (the browser) still trying to connect.

    • It looks like you’re not entering the right IP address in your web browser or your ESP is not establishing a Wi-Fi connection with your router.
      Make sure you double-check the serial monitor and it prints the IP address that you’re using to access in your web browser.

      • Dear Rui,
        ESP is connected to wifi, I know IP address but WWW page isn’t available, also serial moitor shows nothing. Any ideas what could be the problem ?

        Thx
        Marcin

  19. @marc and @marcin

    Try using an other pin for the ds18B20 sensor. I had to connect to D5 instead of D1. The page loaded instantly when connected to the right port.

  20. Hello!

    I’ve did this project but now i want to learn something new. For example i want to add an LED which indicates readings from ds18b20, how to do that?

    Regards,
    Hrvoje

  21. Hi,
    Thank you for your tutorial.
    Will the same code work in all the development board for ESP8266?
    If not what changes do we need to make?

    Thanking you in advance.
    GK

    • Hi.
      This code works for ESP8266.
      It should work in all models.
      The only thing you may need to change is the pin that the sensor is connected to.
      In our example it is connected to D1(GPIO 5). You may need to connect to another pin, in case your specific development board doesn’t have that pin available.
      I hope this answers your question.
      Regards,
      Sara

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.