Controlling an electric heater with a Raspberry Pi
It’s getting closer to winter and you are freezing at home, the first thing you do is turn on your shabby electric heater on, right?
An important factor to take into consideration when talking about our heating system is the electricity costs and how to make the process more efficient. We could keep it on all day on, the home would be always warm, but we would receive a fun bill at the end of the month we might not want to pay.
With that in mind we may want to automate a bit more when is the heater switching on/off.
This project aims to solve this using a Rasperry Pi, a Woox smart plug and a few economical components.
Hardware
- Raspberry Pi 3
- Woox Smart Plug (I am using R4026)
- LCD1602 (display)
- DTH-11 (digital temperature and humidity sensor)
- Breadboard (optional)
Circuit design
The first thing we have to do is build the circuit below. A breadboard would make it easier but it’s not mandatory.
After mounting all components together and turning on the Raspberry Pi, you should see how the LCD display is propagating a blinding blue light, that’s a good sign though.
Code
Now that we have all pieces joined and working we can proceed to the software. We are going to use Python 3 since it contains all necessary libraries to make our life a bit easier when dealing with these components.
LCD display
To display text on the screen we are going to use a library that contains all necessary code for interfacing the LCD1602 I2C display. All we have to do is clone it and install all necessary dependencies.
git clone https://github.com/the-raspberry-pi-guy/lcd
cd lcd
sudo sh install.sh
After installing all dependencies your Raspberry Pi will reboot. Now we can test that the display works properly, in order to do that, we can run the following simple script from the repository:
python3 demo_lcd.py
We should see these lines printed:
Now that we have our display working the next step is getting the temperature in the current room.
Temperature and humidity
We have the sensor connected, we just need to read its data. Bear in mind that this sensor has a temperature accuracy of ±2 °C, but it’s accurate enough for this project.
First we have to import the DTH-11 sensor library. We can install it using pip running the following command:
sudo pip3 install Adafruit_DHT
For more info: https://github.com/adafruit/Adafruit_Python_DHT
IFTTT
All this would not be possible without the connector between the Raspberry Pi and Woox (plug), this is IFTTT. This free service lets us configure hundreds of automations with a lot of flexibility.
For this project we need to:
- Create an IFTTT account.
- Add Smart Life service, and follow the steps to connect it to your Woox account.
- Add Webhooks service. Important to copy our API key to use it later.
- Create a new Applet.
- Use Webhooks as trigger (This). We want to receive a web request with an Event Name of “temperature_low”.
- As action (That) we are going to switch on our smart plug because we’ll send this event when temperature it’s too cold in the current room.
In order to do that we need to add Smart Life with the action, Turn on. Then we can select our Woox plug with its respective name. - Connect this Applet.
Application
The following code is all we need to have all our pieces working. Bear in mind that this is just a proof of concept, and there is a lot of room for extending this as desired.
Copy this snippet and store it in the same folder where you cloned the lcd project with this name heater_controller.py.
Now you need to find your Webhooks API key (from IFTTT) that you will find in its settings, and replace it here:
WEBHOOKS_KEY = "YOUR_WEBHOOKS_KEY"
You can also adjust the variable TEMP_LOW_THRESHOLD to your desired one.
TEMP_LOW_THRESHOLD = 16
Finally, we run it!
python3 heater_controller.py
If the temperature was below the threshold at one point you will see this event printed in the terminal. This means that the event was sent to IFTTT. \o/
Extension ideas
- Switch off the heater if temperature is high enough.
- Avoid sending duplicate events, just change the state of the heater if it’s different than the last event sent.
- Average last minutes temperature points instead of relying on a single value.
- Control an AC/fan.