Introduction: ESP8266 Temperature Controlled Relay
A friend of mine is a scientist who does experiments that are very sensitive to air temperature and humidity. The incubator room has a small ceramic heater but the heater's thermostat was not nearly accurate enough, only able to maintain the temperature within 10-15 degrees.
Commercial devices that log temperature and humidity can be quite expensive, and getting the data from the device can be difficult. Plus, they can't control the temperature, only log the data. He asked how hard would it be to build a device that could accurately control the heater through a relay while logging the temperature and humidity. Sounded easy enough.
Grabbing an ESP8266, relay, DHT22, and some online IoT platform, we're off.
Step 1: Supplies
This project uses a handful of supplies, all of which are pretty common and you may already have them on-hand today. Here is a full list of what I used, feel free to adjust as needed to meet your project needs.
- ESP8266 ESP-01 (or similar ESP8266 board)
- DHT-22 Temperature and Humidity sensor
- LM317 variable voltage regulator (or a standard 3.3V regulator would be easier)
- 5V High current relay (I started with a 10A but did blow it out within 2-days)
- Various resistors and capacitors
- Jumper wires
- Standard electrical outlet and cover
- Electric Gang box
- Old USB plug with adaptor
- Old electrical plug
In retrospect, using a NodeMCU instead of the ESP-01 would have made a lot more sense. I didn't have one at the time so I made do with what I had.
Step 2: Outlet Construction
While I technically started with the microcontroller and code, it makes sense to start with the AC outlet first. For this project, I used a single gang box, a standard 2-plug outlet, and the power cord from an old power strip.
The electrical socket gets wired up with the two white wires joined together and the two ground wires joined together. The two black wires going through the high-side of the relay. Make sure you get the terminals screwed down well and none of the strands are going to short out, I put a little solder on the wires so that the stands stayed together.
Be careful with the high-voltage and double check each connection. It is a good idea to put electrical tape on your wire lungs so they don't wiggle loose.
Step 3: Curciut Design
The circuit is pretty straightforward but if you use the ESP-01 like I did, you will need to add a voltage regulator to get a 3.3V. Standard relays do require 5V so you'll need a 3.3V and 5.0V rail.
My circuit used an LM317 voltage regulator with a set of resistors to get a constant 3.3V rail, I tapped the USB 5V to power the relay. There are 3.3V relays but not for high-current relays needed if you are going to power a small space heater.
The DHT22 requires a 4.7k pull-up resistor.
Step 4: Solder the Board
Layout and solder all of the components. This can be a bit tricky but pre-plan the traces with a piece of graph paper will help.
I used a USB board for a power plug but it was a pretty weak and replaced it with two header pins instead. I used two female headers on the board and soldered two male-header pin directly to an old USB plug. This proved to be more reliable and solid. The USB wiring colors are:
Black --> Ground
Red --> 5V
I also used male headers to expose the DHT-22 and Relay pins on my perfboard to connect them with standard jumper wires.
Make sure you label each pin, power and ground connector in case it gets unplugged later.
Step 5: Mount the Circuit Board
On the side of the gang box, mount the circuit board with screws and/or hot-glue. Make sure the placement is made so that the jumper wires reach around to your relay mounted inside of the box and you can easily plug in your power connector.
Add a jumper wire with heat shrink to your DHT-22 sensor with the appropriate length for your situation. Mine was about 8 inches long. I used some CAT5 cable instead so that the leads could be slightly bent into position and would be free-standing.
Step 6: Arudino Code
The Arduino code uses my SensorBase class, which is available on my Github page. You do not need to use my SensorBase code, you can write directly to the MQTT server and Thingspeak.
This project features three key software features:
- A local webserver to set and view values
- Remote MQTT server to send and store data
- Thingspeak dashboard for graphing data
You can use one or more of these features, just adjust the code as necessary. This is the specific set of code I used, you will need to adjust passwords and API keys.
Step 7: Thingspeak Dashboard
Setup a free Thingspeak account and define a new dashboard. You will need to use the same order of the items as I have listed below, the names don't matter but the order does.
If you do want to add or remove items, just adjust the Thingspeak parameters in the Arduino code, it is pretty straight-forward and well documented on their website.
Step 8: CloudMQTT Setup
Any MQTT service, or similar IOT service like Blynk, would work but I choose to use CloudMQTT for this project. I have used CloudeMQTT for many projects in the past and since this project will be handed over to a friend, it makes sense to create a new account that can also be transferred.
Create a CloudMQTT account and then create a new "instance", choose the "Cute Cat" size since we are only using it for control, no logging. CloudMQTT will provide you with a server name, username, password, and port number. (Note that the port number is not the standard MQTT port). Transfer all of these values into your ESP8266 code in the corresponding locations, making sure that the case is correct. (seriously, just copy/paste the values)
You can use the "Websocket UI" panel on CloudMQTT to see your device's connections, button pushes, and, in the odd scenario that you get an error, an error message.
You WILL need these settings when configuring the Android MQTT client as well, so note the values if you need to. Hopefully, your password is not too complicated to type in on your phone, you cannot set that in CloudMQTT.
Step 9: Final Testing
Now we just need to test the final device.
Before you test anything, double check EACH wire and use your multimeter in continuity mode to trace all wires. Make sure everything is connected to where you think it is connected. Because the relay is isolating the high-voltage from the low-voltage, you don't need to worry about shorting out your microcontroller.
I used a simple electricians circuit tester to verify everything was wired properly on the high-voltage side and it also worked well to test my relay.
Add your ESP2866 on your wifi network by connecting to the device through your phone or laptop. This uses the standard WifiManager library and he has all of the necessary documentation on his Github page.
Using an incandescent light bulb, I placed my DHT-22 sensor next to the bulb and plugged the lamp into the outlet. This allowed the temperature to heat up quickly, triggering the relay to turn off the lamp, and repeat the process. This was very helpful to test everything, including my wifi-connection.
Your device should properly turn the relay on when the temperature is too low and turn it off once the temperature reaches the high value. In my testing, this has been able to keep our lab space temperature within 1 degree Celcius 24/hours a day.