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! :)