Monitoring temperatures around your house can be done fairly easily and there is a range of commercial products out there. You might wonder why you would want to monitor the temperature of your house. One of the reasons is that it can help you control your central heating more efficiently. In this post, we describe how to build a simple wireless temperature monitor with ESP8266 and Raspberry Pi using WiFi and UDP protocols.
- ESP8266 (ESP-07 used here)
- FT232 converter to program the ESP
- Raspberry Pi zero W (Used for it’s built-in WiFi capability)
- ili9341 SPI LCD to display temperature
- DS18B20 sensor
- 4.7 k resistor
- IoTbear ESP breakout board (Optional)
- A router to which both the Pi and the ESP will be connected to.
The idea is that a DS18b20 sensor is connected to the ESP8266 and the temperature readings are sent to a Raspberry Pi using UDP through WiFi. The Raspberry Pi then processes that temperature data and displays it on an TFT LCD screen. Of course, both, the ESP8266 and the Raspberry Pi are connected to the same router or are on the same network. It is also possible to send data across network/over the internet if you configure your DNS properly to make the devices visible to each other. In this post, they are simply connected to the same router.
For information about the TFT LCD screen and how to connect it to a Raspberry Pi, visit https://behindthesciences.com/electronics/connecting-ili9341-SPI-TouchScreen-lcd-to-a-raspberry-pi-in-python/
For getting started with ESP8266 and UDP protocol, have a look at our previous post.
Connecting DS18b20 to the ESP8266
To keep thing simple, we are going to use the Arduino environment and the OneWire library to program the ESP8266.
The wiring of the DS18d20 is as follows, with the DS18d20 wired to GPIO 0:
In our actual setup, we used a Li-ion battery and the charger described in a previous post.
Uploading code to the ESP8266
Download the ESP8266 code (.ino) from our Github, open it in the Arduino Environment and edit the ssid and password lines to match your router.
const char* ssid = "Your_Router_SSID";
const char* password = "Your_Router_Password";
One thing that you need to edit is the broadcast IP. In our case, our router gateway is 192.168.1.1 and our broadcast IP is 192.168.1.255
The UDP port we are using is 64123. Feel free to pick a different one.
Compile and upload.
Disconnect your FT232 from the ESP-07 and connect your 3.3V supply.
Testing your DS18b20 and ESP8266
To test that all is good, get yourself a copy of PacketSender and open it up.
The ESP8266 waits for a start command before it starts to send the temperature data over UDP broadcast. In this case, the start command is ASCII “BehindTheSciences.com”
The temperature UDP data is sent every 5 seconds using a delay(5000) function. There is also a stop command which stops the UDP data broadcast; ASCII “STOP”.
Here is a quick demo of the Temperature data being received on a Windows 10 PC using Packet Sender:
Configuring your Raspberry Pi
Now that you have setup your ESP8266 and you have verified that it is sending the DS18b20 temperature data over UDP, you can start working to get the data to be received by your Pi.
This section assumes that you have already done the initial setup described here for the LCD.
The code to read the UDP data sent from the ESP8266 is written entirely in python. The code setups the ili9341 SPI Touchscreen LCD, initialises a UDP connection, monitors the UDP port and displays the temperature value received from the ESP8266.
Download all the .py files from our GitHub, upload it to your Pi in a suitable location. You will need to edit the IP of the ESP in the python code. In our case, it is set to 192.168.1.2. Change it with yours.
Communication between ESP8266 and Raspberry Pi
Now let’s test that the temperature is received by the Raspberry Pi from the ESP8266. Once you have powered the ESP, run the following command on your Pi through SSH/Putty
You should, first of all, see the display active:
After a few seconds, you will start to see Temperature readings in the top right corner. To check if you do see temperature changes, hold the DS18b20 between your thumb and index finger, you should see the temperature rise on the Raspberry Pi TFT LCD. 🙂
PS: The red rectangles shown on display are for future touch screen functions; more to come.
If you have any questions, comment below or contact us at email@example.com