Custom Smart Plug - Controlling A Space Heater With Ecobee

About the project

With a relay, you can control your household devices. And with the right data, they can be automated. Here I use Ecobee's API to get the temperature from a room sensor, then use that to control a space heater. This regulates the temperature in a room with poor airflow. The same process can be applied to many project ideas.

Project info

Difficulty: Moderate

Platforms: Arduino

Estimated time: 1 day

License: GNU General Public License, version 3 or later (GPL3+)

Items used in this project

Hardware components

NodeMCU ESP8266 Microcontroller NodeMCU ESP8266 Microcontroller Find at: https://amzn.to/37TAnL0 (Affiliated Link) x 1
Yeeco 30A Relay Yeeco 30A Relay Because I'm powering a 12A space heater, I chose a high current relay. If you choose a different relay, make sure it is power rated for your devices! Find at: https://amzn.to/2OcG3Zq (Affiliated Link) x 1
Project Enclosure Project Enclosure I chose this enclosure with the intention of using a larger Solid State Relay. You may be able to get away with a smaller one. Find at: https://amzn.to/3q11KsP (Affiliated Link) x 1
Outlet Socket (15A Rating) Outlet Socket (15A Rating) Find at: https://amzn.to/3dULIyo (Affiliated Link) x 1
Inlet Socket (15A Rating) Inlet Socket (15A Rating) Find at: https://amzn.to/3r2JmRR (Affiliated Link) x 1
USB Splitter USB Splitter Find at: https://amzn.to/2O5ZGCK (Affiliated Link) x 1
AC Power Cord AC Power Cord Find at: https://amzn.to/3dVtegX (Affiliated Link) x 1
Wire Connectors Wire Connectors Find at: https://amzn.to/3r4YpKy (Affiliated Link) x 1
Perfboard Perfboard Find at: https://amzn.to/3oNUel7 (Affiliated Link) x 1
10k Ohm Resistor 10k Ohm Resistor Find at: https://amzn.to/3nL7N3t (Affiliated Link) x 1
150 Ohm Resistor 150 Ohm Resistor Find at: https://amzn.to/3nL7N3t (Affiliated Link) x 1
Mounting Standoffs Mounting Standoffs Find at: https://amzn.to/3nO8DN8 (Affiliated Link) x 1
Breadboard Breadboard For prototyping. Find at:https://amzn.to/35Iy6Bt (Affiliated Link) x 1
Ecobee Thermostat with Room Sensors Ecobee Thermostat with Room Sensors Find at: https://amzn.to/3bR1k3n (Affiliated link) x 1
Logic Level NFET Transistor Logic Level NFET Transistor Find at: https://amzn.to/3pZTzgl (Affiliated Link) x 1
Heat Shrink Tubing Heat Shrink Tubing x 1

View all

Software apps and online services

Ecobee API Ecobee API Get started here: https://www.ecobee.com/home/developer/api/introduction/index.shtml
Arduino IDE Arduino IDE
Postman Postman https://www.postman.com/
Arduino JSON Assistant Arduino JSON Assistant https://arduinojson.org/v6/assistant/

Hand tools and fabrication machines

Soldering Iron Soldering Iron Find at: https://amzn.to/2LRqi9t (Affiliated Link) x 1
Wood Burning Iron Wood Burning Iron I used this because it was compatible with hot knife tips, which I used to cut square holes. There are certainly other methods. Find at: https://amzn.to/3nH9TBC (Affiliated Link) x 1
Hot Knife Tips Hot Knife Tips Compatible with wood burning iron. Find at: https://amzn.to/3bHpjDu (Affiliated Link) x 1
Power Drill Power Drill x 1
Utility Knife Utility Knife x 1

Story

*If you prefer a video tutorial, jump down to the bottom. 

*This project involves working with high voltages and high current. Use caution. Do not attempt unless you are comfortable with this.

The Idea

My son's nursery is one of those rooms at the far end of the house where the heating barely reaches. We have an Ecobee thermostat with multiple room sensors, but that doesn't help much when the vent is putting out room-temp air, and it just ends up overheating the rest of the house. 

(My son, trying to sleep every night in his cold nursery)

Step 2 was to add a borrowed space heater, an older model, but even on low, it ended up making the small nursery too warm. 

Of course, I could have bought an upgraded heater with a built in thermostat, but why not spend that money on parts to build a custom smart controller that integrates the Ecobee? Because it's still pretty expensive, you say? Well, that's not going to stop an engineer with an design in mind. 

Speaking of designs, this is the block diagram I first drew up: 

Let me try to break that down for you. There is a Wi-Fi connected microcontroller (MCU) that is communicating with Ecobee's API, receiving data formatted as JSON, which is decoded to get the temperature. Based on this, it uses a Solid State Relay (SSR) to control the heater (crudely drawn on the right). Also, the MCU is saving data to its built-in flash memory, and it hosts a web server (Web) with configuration controls and a log dump. Make sense? 

Note: After the first prototype, I ditched the SSR for a mechanical relay. It was generating way too much heat to feel comfortable leaving it in the nursery overnight. 

I chose a NodeMCU ESP8266 for the microcontroller so I'd have built-in Wi-Fi.

Circuit Diagram

After  that, I refined the design into a detailed schematic (I'll use the term "refined" lightly, since I still drew it in MS Paint).

This is the version with the mechanical relay. Notice that the relay can't be driven by the microcontroller alone, so there is an NFET transistor driver circuit that pulls power from a USB phone charger. Info about how that circuit works can be found here: https://arduinodiy.wordpress.com/2015/12/29/using-fets-to-switch-a-load/

The microcontroller itself is also powered by a phone charger - I managed to find a micro-USB splitter cable, and I use one end to power the microcontroller and cut the other end to wire to the driver circuit. I clamped some wire connectors to the prongs of the phone charger and hard wired it to the socket that plugs into the wall. 

Software

Then it was just a matter of writing code and putting it all together. 

In short, every 5 minutes the microcontroller accesses Ecobee's API to get temperature data. It may receive an error that my authentication tokens are expired (those are used to prove that I'm really supposed to have access to that particular thermostat). If they have expired, I send another API request to refresh them. I save those tokens and some other configuration info (current temperature, some timer settings, etc) to flash memory - this way, if I ever unplug the device, I don't lose important data. Meanwhile, the microcontroller is also hosting a web server of its own with fields to configure settings and a log page (so I can monitor how everything is working and check on any errors that may have occurred). There is also a timer and a few other features.

Grab the code from the repo if you're curious, and see the video below for more detailed explanations about some of the code functionality. 

Future Work

There is certainly some room for improvement here. For one thing, the code is all synchronous, meaning that if the processor is tied up waiting on an API request, the Web Server will be inaccessible until it finishes. Also, in some places of the code I pull the actual time from online, and in other places I use an internal timer library. This could probably be consolidated so that all actions are based on real time - the internal timer also gets blocked by API requests, so timing ends up being inconsistent. I may also wire in a fuse as an extra bit of safety.

How To Build It

If you want to build this project as-is, here's what you do.

  1. You'll need to go to Ecobee's site to register your device and set up an App. I'll let them explain it here: https://www.ecobee.com/home/developer/api/introduction/index.shtml (save the AppId and access tokens you get from them).
  2. Clone the project repository.
  3. Unzip the Data folder in the same directory as the Arduino code (.ino file).
  4. Edit the ssid and password variables in the code with your own WiFi login info.
  5. Edit the appId variable using the AppID you got from ecobee. 
  6. Copy the access and refresh tokens you got from ecobee into their respective files in the Data folder.
  7. Replace the contents of the sensor.txt file with the ecobee Sensor name you are using.
  8. Install the ESP8266 Sketch Data Upload tool.
  9. Use that tool to upload the contents of the data folder to memory on the microcontroller.
  10. Upload the sketch to the microcontroller.
  11. Wire up the circuit based on the schematic (see video below for some construction methods).
  12. Use the Serial Monitor in the Arduino IDE to monitor the code execution, and it will tell you the IP address of the device. Consider making this a static address through your router settings. 
  13. Copy that address to a browser, and you can access the config/log web server. 
  14. Mount the circuitry in a box, and  you're done.Tip: To cut square holes in the enclosure, I cut an X from corner to corner with a hot knife, then score the perimeter with a utility knife, then snap the triangle flaps out. 

Conclusion

This is a fairly specific application, but I intended for this project to serve as a template for any Smart Home/Internet of Things device you might want to build. Use what I've written to access any other API and deserialize the data it sends you. Then use that data to control whatever devices you want. The relay can handle turning stuff on or off, but maybe replace that with a motor or a LED screen. You could even gather data from one API and use it to send signals to another API - creating custom Smart Home actions. 

Video

And as promised, here is a video of the full design/build/test process.

Schematics, diagrams and documents

Schematic

Code

Space Heater Controller Repo

Credits

Photo of AllPartsCombined

AllPartsCombined

I like to make stuff and make videos about it.

   

Leave your feedback...