While there are tons of smart home devices available in the market, the truth is that you’ll need a big budget to invest in a sophisticated home automation setup off the shelf. For instance, a standard LIFX Smart Bulb costs $59, and considering an average home has at least 12-15 light points, you’ll need to spend a staggering $885 on bulbs.
And that’s not it. If you wish to combine that with an additional temperature sensor or motion sensor, you’ll also have to buy a central automation controller, which could take the total cost to around $1000. Don’t believe the math? Have a look at the below screenshot from Amazon for the above home automation setup:
But what if I told you that it’s possible to build a DIY home automation system at a fractional cost, like, as low as $40? Won’t that be awesome? Of course, creating such a setup requires a fair bit of technical expertise, but for people who are willing to climb up the learning curve, the ROI is exponentially high.
What You’ll Get:
By the end of the tutorial, you’ll be able to build an affordable, scalable and intuitive home automation system that has realistic home automation use-cases, and which can be controlled easily from your iOS/Android device.
Using the DIY home automation setup, you’ll be able be monitor room temperature, track motion, dim lights/fans, and turn on any electrical device from your computer or smartphone. To give you an idea of how the user interface would look, take a look at the screenshot below (it’s exactly the kind of interface you’ll get, if you follow the tutorial).
Stuff You’ll Need for the project:
Here’s a list of all the items that you’ll need to purchase for creating a home automation setup. All the items can easily be purchased from Amazon (the links have been attached):
- 1x NodeMCU board (Price: $8).
- 1x Relay (Price: $6)
- 1x TMP36 Temperature sensor (Price: $ 2)
- 1x HC-SR501 PIR Sensor (Price: $2)
- 9-12V 1.0A DC Adapter (Price: $6)
- 3.3V/5V power supply module (Price: $6)
- 1x Breadboard (Price: $5)
- Jumper/electric wires (Price: $1)
- Bidirectional logic-level shifter (Price: $1)
- 1x bulb/appliance that you want to control (Price: $2)
ESP8266 is a highly integrated chip designed for the needs of a new connected world. It offers a complete and self-contained Wi-Fi networking solution, allowing it to either host the application or ...Available: In stock
The Arduino 5V relay module is suitable for SCM development, home appliance control. It is with 5V~12V TIL control signal which can control DC / AC signal. The module is perfect for common Arduino ...Available: In stock
Enter your model number to make sure this fits.; High accuracy solid state temperature sensor; Voltage Input: 2.7V to 5.5V DC; Output range: 0.1V (-40°C) to 2.0V (150°C); Temperature range: -40°C to ...Available: In stock
ProTechTrader ™ Universal AC/DC Power Adapter with Multiple Voltage Output (3v-12v) Allows you to easily replace your old wall wort transformer, and allows you the flexibility to use it with multiple ...Available: In stock
For MB102 bread board, two way independent control, Can be switched to 0V, 3.3V, 5V. On-board Two sets of 3.3V, 5V DC output pins, Facilitate the external leads. A plastic lens is used for high ...Available: In stock
This breadboard is ideal for making electrical connections and for testing purposes. The breadboard has 400 point with standard 0.1" (2.54mm) pitch. Packaged in a zip lock bag.Available: In stock
If you’ve ever tried to connect a 3.3V device to a 5V system, you know what a challenge it can be. This bi-directional logic level converter is a small device that safely steps down 5V signals to ...Available: In stock
Crunching the above numbers, the cost of running your home automation pilot project comes down to around $39. That’s freaking awesome, considering a typical automation system costs a lot more, and can’t be custom-made to suit your individual needs.
DISCLAIMER: Please note that the NodeMCU based system solves a basic home automation use-case (controlling devices and monitoring environment), and it’s NOT a replacement/alternative to mass market automation products.
Of course, we can build up on the basic use-case, and create more intricate setups, like IFTTT integration or inter-node communication, but they’re out of the scope for this article. We will be adding more advanced DIY home automation tutorials in the future.
Setting up the Hardware
Once you have all the above mentioned hardware parts, the next step is to connect them. Now, instead of showing you the exact circuit diagram of the final circuit, I thought it would be a better idea to gradually build it, so that you have a better understanding of how to connect additional components in the future.
First, we start by connecting a single relay with our NodeMCU controller. Here’s the circuit diagram for it:
As you can see from the above diagram, I powered up the NodeMCU from a 3.3V/5V power module. Now you might be wondering why I’m using an external 3.3V/5V power supply, when I can get the power directly from NodeMCU. Well, I know some of you might have already guessed it, here’s exactly why I went for an external power supply:
- The relay needs 5V input signal and has a high current drawing tendency, especially while switching.
- Since we would be connecting a temperature and motion sensor at later point, it makes sense to have a dedicated power supply for them.
- The external power module can be powered from a general-purpose 12V, 1.0A DC adapter, and can provide two different voltage levels (5.0V and 3.3V) for powering the circuitry (convenient for expanding the scope).
Next, I connected the IN pin of the SMD relay to a bi-directional level shifter, instead of connecting it directly to D7 pin of NodeMCU. Again, the reason for this choice is simple. As the relay operates on 5V, we would need to level shift the HIGH signal (3.3V) coming from NodeMCU to relay’s HIGH (5.0V).
Lastly, I have connected the relay in series with my bulb/electrical appliance, so that it acts as a switch (or circuit breaker!). Here’s a more simplified diagram of the connected relay circuit:
Now, there are three terminals on the relay: NO (Normally Open), Normally Connected (NC) and Common (C). As shown in the diagram, the C terminal of the relay would be connected to live wire on the power supply, while the NO terminal would get directly connected to the bulb. In case you’re wondering why I have connected them in such a manner, take a look at the pictorial representation of how these terminals work:
As you can see, the NC terminal is connected to the C terminal when the circuit is open, while the NO terminal gets connected to the C terminal, only when the circuit gets closed. Since we need to turn on the bulb when D7 pin gets high, we need to use the NO terminal.
Connecting it to an IoT Cloud service
Now that we are done with setting up the hardware, the next step is to connect the circuit with an IoT cloud service. But how do we do that? After all, in order to connect to any cloud service, we would need to be first connected to the Internet, right?
Well, here’s exactly where the magic of NodeMCU unveils. The nifty little microcontroller comes with an ESP8266 onboard, which means it can communicate through Wi-Fi and operate on the TCP/IP stack. That means we already have a Network layer for our setup, so we can go ahead and run our application on it.
For the setup, we would be using a drag-and-drop IoT builder service from a startup called myDevices. The platform is extremely easy-to-use (when compared to other IoT cloud platforms), and absolutely FREE for makers.
In order to connect with Cayenne, we would need to create an account and obtain the unique identifier for our device. Here’s how to do it:
- Go to https://cayenne.mydevices.com/cayenne/login and click on SIGN UP.
- Sign up on the platform using your email id and password. Once you’re logged in, you should be able to a screen like this:
- Click on Arduino→ Next → Arduino Uno → Wi-Fi shield. Once you select the option, a popup should open up with the code. Copy the code and paste it in a notepad file. [Note: Leave the window open. Do not close it. We will come back to it after running our code on NodeMCU.]
Uploading the code
Prerequisite: Arduino IDE and NodeMCU drivers
Now, the next step is to upload the code on your NodeMCU. In order to do that, you’ll first need to download Arduino IDE on your machine. You can download it, here. After downloading the IDE, based on the system that you’re using, you would need to install the drivers for NodeMCU. You can download the needed drivers from here.
Once you have installed the IDE and drivers, open the Arduino IDE on your machine and do the following:
- Click on Sketch→ Include Library → Manage Libraries → Search for “Cayenne”. Install the library. (Don’t download the CayenneMQTT library. You don’t need it, for this tutorial.)
- Now, click on File→ New, and copy the code from the popup, here. In the code, there would be a line, which reads something like this:
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.Set the ssid variable to your Wi-Fi network’s SSID and enter the password for the network. Save the file.
char token = "xyzxyzxyz";
// Your network name and password.
char ssid = "NetworkSSID";
char password = "NetworkPassword";
- Next, go to Tools → Boards→ Board Manager → Search for “NodeMCU”. Install the “esp8266” board family on your IDE.
- Once the installation completes, go to Tools → Board → NodeMCU. Keep CPU frequency as 80 Mhz, Flash size as 4M, and upload speed as 115200 (You can change these variables based on your model, but the above configurations should work).
- Next, go ahead and select the port in Tools → Port. It should ideally be the last option, with the name “USB-TTL”.
- Once you’ve selected the board, click on Sketch → Upload to upload the code on your NodeMCU. It should take a few seconds for the code to be uploaded on NodeMCU.
After you have uploaded the code on NodeMCU, click on Tools → Serial Monitor, it should open up a window, like this one:
If you can see an output that reads something like this: “Connect Success… Ready”
Congratulations! You have successfully connected your NodeMCU to cloud. You can go ahead and go back to the Cayenne dashboard window in the browser. You should now be able to see a screen, like this:
Running the setup on your smartphone
Now that you have connected the setup to cloud, you can go ahead and download the Cayenne app on your iOS or Android device.
- Open the app and sign into your account. You would get a single option on the Devices screen, labelled “Arduino Uno”. Click on it.
- You’ll be navigated to a blank screen. Click on the “Add” button icon on the top.
- Go to Actuators → Relay. It should open up a window like this one:
Select the following:
I/O → DigitalPin → D13 (I know we have connected it to D7 pin on NodeMCU, but the cloud platform thinks we are using an Arduino. And here’s how the mapping works:
Device Widget → ButtonInvert Logic → OffChoose Icon → LightOnce you have configured the relay, click “Add”. Wait for a few seconds, as it might take a while. Once done, it should display a message that reads something like “Relay successfully added to your project”. Woah! You just connected your relay with smartphone.
Now go to your dashboard, and tap the switch. You should now be able to turn light on/off from your smartphone. Pretty cool, right?
You can go a step further and add temperature and motion sensor to your automation setup. Here’s how I managed to hook up a TMP36 temperature sensor and PIR motion sensor with NodeMCU:
You can go ahead and just connect the parts as shown above. I have used breadboard for prototyping, but once you have tested it, you can solder the components on a PCB board. For mapping the data pins of NodeMCU with Cayenne digital pins, you can refer the PIN definition diagram given above.
For instance, D1 on NodeMCU → GPIO5 on Arduino, and that’s why you need to select D5 when configuring PIR sensor. Similarly D2 on NodeMCU → GPIO4, so you need to select D4 for temperature sensor. You can add theses sensors from the app itself, just like you added the relay to your project. It’s a fairly straight-forward process, with no coding involved.
[Note: While configuring TMP36 temperature sensor, you’ll get an additional option, whether you wish to use Virtual pin or Analog Pin. Select Analog Pin→ A0.]
Setting triggers and notifications
Cayenne also allows you to set triggers and notifications, which means you can get instant notifications when the PIR motion sensor detects movement, or the temperature sensor measures a reading high above the threshold.
While the above setup was all about active control, you can use triggers to gain passive control over the system, or monitor activity in the background.
In order to set a trigger, go to Cayenne→ Triggers. In the If <Trigger> then <Notify > tab, tap on Trigger. Click on if (+) icon. Once you’re on the screen, it should give you all the possible options. For instance, you can select Motion Sensor and then have it configured for motion events (1-> Motion, 0-> No Motion). You can even click on the ”Test” icon at top, to simulate the event, and know whether the trigger works. For now, the system supports only email notifications, but hopefully, we would be able to do a lot more in the future.
Over to you
I hope the guide helped you in kicking start with a basic home automation setup. Please note that above tutorial provides just boilerplate code for creating something awesome, and we expect you to take it a step further. For instance, you can use IFTTT’s Maker channel to connect your home automation setup with thousands of other platforms, including Amazon Echo, Nest, SmartThings, LIFX, Philips Hue, and much more.
Also, in case you feel stuck or have an opinion that you would like to share with us, please feel free to give a shout in the comments section below. We’d love to help.