Creating a temperature web server using ESP8266

ESP8266 is a fantastic micro controller unit, with built-in Wifi and is programmable via the Arduino IDE (or LUA if you wish). By using this with a DS18B20 we are going to run a web server on it, with which we can use any browser to get the current temperature.

Getting started

Before we can start writing the code, we will need to have the following hardware:

  • ESP8266 Micro controller unit (doesn’t matter which model)
  • Breadboard
  • 4k6 ohm resistor
  • Cables
  • DS18B20

There are different kinds of both ESP8266 and DS18B20. The ESP8266 I am going to use is NodeMCU, which has built-in USB and is breadboard-friendly. The DS18B20 I am going to use for now is the non-waterproof which comes without a cable and is much smaller. You could of course use other variants of both.

These are some of the ESP8266 that you could use:

And a picture with all of the hardware I am going to use (I’m not going to use the DS18B20 with a cable though):

Connecting everything

If you are using a ESP8266 with built-in USB, you shouldn’t have to do that much in this step. If you however are using a ESP8266 without it, you need to connect a couple of wires (I recommend using a USB to TTL cable), and remember to lower the voltage to 3.3 volts or you may destroy the ESP8266! You could use a power source with 3.3 volts for VCC and a Logic Level Converter between TX on the ESP8266 and RX on the USB to TTL cable. If you are using a NodeMCU or another ESP8266 with USB, you don’t have to care about that.

Now it’s time to connect the wires. This is how I did it:

ESP8266 -> DS18B20
VCC -> 3.3 volts
GND -> GND
Data -> D1

You also have to add a 4k7 ohm pull-up resistor between Data and VCC on the DS18B20.

And a picture of it:

Writing the software

Before we can start writing the software, you have to add ESP8266 in the board manager in Arduino IDE. You also have to add DallasTemperature and OneWire from the libraries manager.

I am attaching the full source code below:

Before you build it, make sure you change the Wifi SSID and password to your own. You should also open the Serial Monitor to get the IP Address used by the ESP8266.

As you can see in the source code, we have three different levels on temperature (cold, mediumhot and hot). Depending on the temperature, the background colors will change automatically. You could also change that to background images (for example a beach when it’s hot, and a snow man when it’s really cold J).

When it’s built and I navigate to the IP address on my phone, this is what I get:

Next step

When this is up and running, there are some things we could do. We could for example send the temperature to a Raspberry Pi with a database which is logging the temperature, once per hour. We could also try to get the ESP8266 to sleep mode, which means it will draw much less current, and thereby be able to run on two AA batteries.

Or maybe add another sensor, such as the DHT22 which is able to measure both temperature and humidity.

Please let me know in the comments if you have made any improvements! :)

14 Comments

  • Thanks, for your explanation about that. This was great for me.
    Now I will try to read some sensors in the same screen.

  • Great Job - Many Thanks for your efforts.

  • Someone tried to read two sensors? Just need to add another variable like (temp = DS18B20.getTempCByIndex(0);)

    Regards,

  • hi bro i upload your sketch to my nedemcu .
    i can ping ip.
    no answer on by android & browser 192.168.0.14 ?
    have you any tip for me.
    http://i.hizliresim.com/7AlXYl.jpg




  • serial screen
    http://i.hizliresim.com/MkOg06.png

  • Bro Thanks All Works Thank You.
    2 Question works With ESP-01 ESP8266.

  • tested on esp8266-01 and works fine,
    how to change code to obtain a self refresh web page after about 3-5 seconds?
    regards

  • I found a good project with 2 sensors : http://stafferier.com/?p=107

  • Great stuff, thankyou!
    I've modified your code to work with 3 temp sensors for my project, plus added some extra bits to measure an external battery voltage using the ESP's ADC & a resistor network.



  • Great stuff, thankyou!
    I've modified your code to work with 3 temp sensors for my project, plus added some extra bits to measure an external battery voltage using the ESP's ADC & a resistor network.
    Drift - Sunday, July 2, 2017 12:53:07 PM

    Could you provide me your Sketch ? I also like to use this nice Code for three sensors. It is better to have one node MCU for 3 sensor together, than for each sensor one Node MCU.

    Thank´s

    Thomas

  • Hi!

    I can ping IP but I cannot reach the webserver, serial monitor puts out "server running". Any hint for me?

    Thank you!

    Andreas

  • hello,

    same for me. server running but I cannot reach th ewebserver .

    any idea ?

    thanks,

  • I can't see the IP

  • Serialmonitor says;

    Connected to Mikes
    IP address: 192.168.122.107
    Temperature web server started!

    but i am unable to connect to this ip by entering it in the address bar of my browser. Maybe my ds18b20 is not working?

Add a Comment

As it will appear on the website

Not displayed

Your website