Intro: IoT Wall Outlet With Arduino and ESP8266
In this instructable, I show you how to add enhanced accessibility, intelligence and connectivity to an ordinary wall outlet. This is done with a combination of microcontrollers, Arduino, various sensors, ESP8266 and a mix of software packages. As I have designed this project in a sort of a modular fashion, you can try to build the whole project as it is or parts of it or even extend this project with more functionality.
Fan Speed Regulator
IR Remote Control
Log to Cloud Platforms
This is the second instructable in the series of IoT related instructables that I am writing on automating my house.
The first instructable is:
An inexpensive IoT enabler using ESP8266
Step 1: Design Considerations
This project was conceived when I felt the need to automatically reduce the speed of an AC ceiling fan in the bedroom as temperature drops through the night. In Bangalore, where I live, during winters, I like to switch on the fan when I go to sleep. But early mornings, when the temperature drops, the fan makes it even colder. Which disturbs my sleep and I don't want to or cant get up to reduce the speed or switch off the fan since the wall socket is not close to the bed. So I needed a way to automatically shutoff the fan or reduce its speed in the mornings and thought of building one myself since there aren't any commercial products already available in the market. I know, I know, there are bigger problems to solve in this world, but I guess this problem needs solving too, so what the heck :)
Along with this initial requirement, the following were also considered while designing the project.
- Should automatically reduce the speed of the fan based on the time elapsed or temperature drop.
- Should still be able to operate physical switches.
- Should be able to remotely control light and fan. IR remote is a necessity. Smartphone app is perhaps an indulgence.
- Should be able to function independently.
- If the board breaks down for any reason, before it can be fixed or properly uninstalled, it should still be possible to use the physical switches to operate the light and the fan without any changes.
- Need to be able to switch off the board, when the house is going to be empty for extended periods of time (say vacation), but when switched on again, should again function properly without needing any manual intervention.
- The board should update the state of the devices controlled properly irrespective of the mode of control - IR remote, smartphone app or physical switches.
- Should not look too ugly or messy.
Step 2: Basics
What is an Arduino?
Arduino is an open-source electronic prototyping platform to create interactive electronic objects.
What is ATMEGA1284P-PU?
This is the microcontroller IC used in this project.
What is ESP8266?
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 to offload all Wi-Fi networking functions from another application processor.
What is MQTT?
MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. For example, it has been used in sensors communicating to a broker via satellite link, over occasional dial-up connections with healthcare providers, and in a range of home automation and small device scenarios.
What is node-red?
Node-RED is a tool for wiring together hardware devices, APIs and online services in new and interesting ways.
What is NetIO?
NetIO is a multi platform smartphone application, a generic remote controller for almost everything. It simply sends and reads strings over a network socket. You can easily communicate with microcontrollers or PCs connected to your LAN!
What is Thingspeak?
ThingSpeak is an open source “Internet of Things” application and API to store and retrieve data from things using HTTP over the Internet or via a Local Area Network. With ThingSpeak, you can create sensor logging applications, location tracking applications, and a social network of things with status updates.
Step 3: Proof of Concept
There is hardly any information on the web regarding how to control the speed of an AC fan electronically. One of the links I found, copied below, shows an image of a ceiling fan remote control circuit, but it does not contain an actual circuit diagram. It uses triacs to select the capacitor or combination of capacitors. Based on how much capacitance is in the circuit, the speed of the fan can be varied. For max speed, the input is directly connected to the fan.
Then I bought a regular off-the-shelf fan regulator and took it apart. Found that it also uses capacitors to control the speed of a fan and the principle of operation is similar to the one in the link above.
Now I needed a triac circuit to mimic the turn of the knob on the off-the-shelf fan regulator. Found this link that talks about a simple way to turn on/off AC appliances using triacs:
Based on the above details, created a circuit that uses MOC3041 optocouplers to trigger BT136 triacs. Salvaged the capacitors from the off-the-shelf regulator and used them in this circuit. Based on which triacs are triggered, the amount of capacitance in the circuit can be varied thereby controlling the speed of the fan. In this proof of concept, I just added a IR receiver and a LM35 temperature sensor. It worked well. But there is no IoT connectivity and it was not ready for proper installation.
Please note that this is the first proof of concept board that I made. The rest of this instructable does not contain information about this, rather, it contains the details of the prototype that I made that also has IoT connectivity as well as many other features.
Step 4: Parts
ATMEGA1284P-PU microcontroller chip
ESP8266 ESP-01 module
ILI9341 TFT Touch SPI LCD screen
0.96" OLED i2c LCD screen
DHT11 or DHT22 sensor
WS2812B LED strip
4N25 zero cross detectors
Breakaway male and female headers
220v AC to 5v DC 2 Amps power supply module
LM1117 3.3v LDO voltage regulator
FTDI USB to TTL adapter cable
In addition to the above, another Arduino board (any model should be fine) is needed to install the bootloader on the ATMEGA1284P chip.
Step 5: Circuit
The circuit is split into two major parts:
1. First is the main microcontroller circuit. Only DC here.
2. Second is for all the AC components. Mix of AC and DC here.
This is a combination of various circuits I have found on the web. The only part of the circuit that I had to struggle with, as explained earlier, is the one for controlling the speed of a AC fan. Although I found a few variations of this circuit, I did not get a proper schematic. Hence, this part of the circuit needs a little explanation.
The basic idea is that the speed of the AC fan can be varied by using 250v AC capacitors in parallel. Depending on how much capacitance is in the circuit, the speed of the fan could be increased. Actually, it is the impedance or the capacitive reactance or some such thing that actually does the trick, but since I have little knowledge about that, I will refrain from even making an attempt to explain it here. In my circuit, you can notice that there are four MOC3041/BTA136 triacs. Each of the first three triacs is connected to capacitors of 1uF, 2uF and 3uF capacitors and then to the fan. The last is directly connected to the fan.
With this circuit, its possible to have 7 different speeds by triggering the triacs as needed.
- Speed 0: None of the MOC3041 triacs are triggered.
- Speed 1: Only the MOC3041 connected to 1uF is triggered.
- Speed 2: Only the MOC3041 connected to 2uF is triggered.
- Speed 3: Only the MOC3041 connected to 3uF is triggered.
- Speed 4: MOC3041s connected to 1uF and 3uF are triggered.
- Speed 5: MOC3041s connected to 2uF and 3uF are triggered.
- Speed 6: MOC3041s connected to 1uF, 2uF and 3uF are triggered.
- Speed 7: Only the MOC3041 directly connected to the fan is triggered.
Another thing about this circuit are the two zero cross detectors - one for the light and the the other for the fan. These are needed to check if the light or fan is actually switched on or off. Note that there are two physical 2-way switches on the wall socket that can still be used. When the state of the light or the fan is changed by/through the infrared remote or the smartphone app or the physical switches, it is the the zero cross detector circuits that actually detects whether the light or the fan is actually in the on or the off state.
The Kicad files of the circuits are available at https://github.com/tangophi/iot_wall_socket/tree/m...
Step 6: Wiring
There was a 2-way switch for the light on this board as well as a 2-way switch on another wall socket near the door. The fan had only a 1-way switch and a fan speed regulator.
In the new scheme, the 2-way switch for the light near the door has been retained as it was. But the other 2-way switch for the light has been moved for fan's use. And the 1-way switch that was previously used for the fan is now used to control power to the IoT board.
Step 7: Code
The Arduino sketch for the ATMEGA1284P microcontroller and the source code for the firmware of the ESP8266 module is available at
The node-red code is available at
In addition to the above, the NetIO based smarphone app also needs to be build. But not sure how to share that code here. It is pretty easy to build the app. See the section on the smartphone app for more details.
Step 8: DIP Switch Settings
There are three DIP switches on the board. These need to be set correctly for various modes of operation:
Uploading sketch to Arduino
Uploading firmware to ESP8266
Step 9: Flashing the Firmware
Make sure the DIP switches are set properly and use Arduino IDE to upload the sketch to the ATMEGA1284p. And use ESP8266 SDK to flash the firmware to the ESP8266 module.
Instructions for both of these are available widely on the net and hence have not included them here.
Step 10: Node-red
node-red running on a Raspberry Pi acts as a coordinator and manages various IoT boards in the house. It does this by getting/sending MQTT messages from/to various boards as well as socket messages from a NetIO based smartphone app. It also acts as an interface to send data to thingspeak.com and to send tweet alerts.
The node-red flow in the repo contains nodes related to the IoT wall socket as well as nodes of other projects. The IoT wall socket related entities are labeled as socket1 in the node-red flow.
If a Raspberry Pi is not available to run node-red, a cloud platform such as IBM Bluemix can be used to host node-red. One advantage of using a cloud platform is that the IoT boards can be controlled from anywhere in the world easily.
Step 11: Smartphone App
The smartphone app is built using the NetIO Design Editor at http://netio.davideickhoff.de/editor
Once the app is designed, the NetIO app should be downloaded to your smartphone. Note that this is a paid app, but it does not cost much.
Whenever a button/switch/slider is pressed in the app, it will send a message through sockets to the Rpi box that is running node-red. The node-red application will process the messages and then send MQTT messages to the wall socket board.
For the smartphone app to work it should be in the same network as the IoT wall socket board.
It is possible to use the smartphone app from outside the local network. If the ISP allows incoming connections, it is just a simple matter of forwarding the socket port on the router. If the ISP does not allow incoming connections, then VPN can be used as a workaround.
Step 12: How It Works
This diagram shows a high level overview of this project.
When the infrared remote is used, the IR signals generated by it are detected by the IR receiver in the board. And depending on the command, relays are either turned on or off to switch on/off the light or the fan. Also, various triacs are triggered to set the speed of the fan.
The NetIo based app talks to node-red on the Raspberry Pi through sockets. Based on the command sent by the app, node-red triggers MQTT messages. Which are processed by the broker and sent to the ESP8266 module. Which in turn send the command through the serial port to the Arduino as json formatted strings.
Data such as temperature, humidity and light/fan states are sent by the Arduino to the ESP8266 module as json strings. The ESP8266 modules makes MQTT messages out of this data and sends them to node-red via the broker. For temperature and humidity readings, node-red collates them and sends one set of readings per a predetermined interval to thingspeak.com via http.
When the security alarm goes off, node-red sends an alert to Twitter and the account holder as well as all those who are following him will get a tweet.
If SMS notifications is enabled in Twitter account settings, then whenever a alert is sent, a SMS alert is also sent to the registered mobile number.
Step 13: Operations
The board can be operated in any of the following ways. Some functionality that is available in one method may not be available in others.
1. Touch screen operation
- The light or the fan can be switched on/off just by touching the relevant image on the screen.
- The speed of the fan can be increased or decreased by touching the Fast or Slow images.
- Time alarm can be set by pressing the clock display on the screen for more than 2 seconds.
- Light and Fan options can be accessed by pressing the relevant image for more than 2 seconds.
- Security alarm can be activated by pressing the Alarm logo for more than 2 seconds.
- Led strip can be switched on/off by pressing the LED logo. But its mode cannot be changed.
- Debug messages can be enabled by pressing the bottom edge of the screen for more than 2 seconds.
2. IR Remote Control
- Light/Fan/Led canbe switched on/off by pressing the relevant key.
- Fan speed can be set directly by pressing keys 1 to 7 or by pressing the increase or decrease keys.
- LED mode can be changed.
- Big clock display can be activated.
- Needs line of sight to work - similar to how normal TV remote works.
3. Smartphone App
- Light/Fan/Led canbe switched on/off by sliding the relevant switches
- Fan speed can be set directly by using the slider.
- LED mode can be changed.
- LED color can be set
- Can be used from anywhere in the network or even from outside the network - possibly from anywhere in the world.
4. Physical switches
- Light and Fan can be switched on/off using the 2-way switches in the wall socket.
- The board itself can be switched off using the switch on the wall socket.
- If the board is switched off, the physical fan regulator can be used to change the fan speed.
- If the board is switched on, the physical fan regulator should be set to max speed. The actual fan speed will be controlled by the board.
Step 14: Monitoring
The board logs temperature, humidity and power consumption readings once every 10 minutes to thingspeak.com. It also logs state of the light and the state and speed of the fan whenever they are changed.
Presently only Thingspeak.com is configured. If needed, nodes for other cloud services can also be added.
The following links have information on how to add nodes for various IoT cloud services:
Step 15: Smart Algorithms
1. Automatic Fan Speed Control
Various options to set the fan speed are available
- Decrease fan speed by 1 step for every 1 degree drop in temperature.
- Decrease fan speed by 2 steps for every 1 degree drop in temperature.
- Decrease fan speed by 1 step every 90 minutes.
- Decrease fan speed by 2 steps every 90 minutes.
- Stop fan if temperature falls below the specified temperature.
2. Time alarm options
When the clock arm goes off, along with the buzzer, the following options can be set.
- Switch on light.
- Switch off fan.
- Switch on fan and set it to maximum speed.
3. Automatic full screen clock display
During nights (when the fan is switched on and the light is switched off), if a movement is detected after 10 minutes of idleness, show the current time in full screen so that it is visible clearly from the bed.
Step 16: Security Alarm
The PIR motion detector can be used as a security alarm. The alarm can be "armed" when going out. And when the alarm is armed, and if motion is detected, the screen will show a keypad so that the security code can be entered. If the correct security code is entered within a minuted then the alarm is disarmed. But if the correct code is not entered within a minute, a tweet is sent along with activating the buzzer.
Also, a SMS message will be sent to the mobile number associated with the twitter account. This is achieved by enabling SMS notifications in Twitter account settings.
Step 17: Clock/Alarm
There is no RTC module on the board to provide current time. Instead, an event is triggered every minute that will send the current timestamp as a MQTT message to the board. And we extract the required info from the timestamp and use it to update time.
- Normally the clock is displayed in small font at the top right of the display. But to see the time from a distance, a key can be pressed on the IR remote to show just the current time in big font.
- A clock alarm with options like switch on/off fan/light when it goes off can be configured by long press of the time window.
- At nights, when the fan is on and the light is off, if movement is detected after an 10 minutes without detecting motion, the board will automatically display the clock in big font. So that it is readable from the bed.
Step 18: Mood Lighting
There is a strip of WS282b LEDS around the enclosure. These can be turned on to create various colors or patterns.
This mood lighting can be controlled through the following means:
1. Smartphone app can be used to turn on/off the LEDs, change pattern as well as changing the color.
2. IR remote can be used to turn on/off the LEDs and change the pattern.
3. Touchscreen can be used to just turn on/off the LEDS.
Step 19: Issues
Most of the functionality works fine. There are a couple of issues however:
1. A couple of BTA136 triacs used in this project have gone bad twice over the last couple of months. I replaced just those triacs and everything seems to be fine. However, I have not been able to determine what exactly caused these triacs to short out or if there is something inherently wrong with my circuit.
2. Power Consumption readings are mostly wrong. I guess there is too much noise in the circuit, since there are three relays and no opto-isolation for them. Guess the current transformer circuit needs to be isolated to prevent noise from other parts of the circuit interfering with the readings.
3. The PIR motion sensor is sometimes erratic. Even when there is no-one in the room, it detects motion. Probably due to the fact that its next to the window and the window shades. Tried tuning the sensor, but so far no luck.
Any help/suggestions on the above would be highly appreciated.
Step 20: Future Ideas
This is the second IoT project that I have done. The first one is a simple ESP8266 board with a relay and a temperature sensor. Details of the first project are available at
I have also created a mood lighting project for my living room. Also modified two power strips by combining them with Arduino/ESP8266 and relays so that individual outlets can be remotely switched on or off. Photos of these two projects are included here. I will write instructables for them in the near future.
Further, I am also planning to automate the main wall socket in the living room similar to how i have done in this instructable for the bed room. In addition to all the existing features, I am going to add a IR transmitters as well more environmental sensors to that project. Later on, probably add some flame/gas sensors in the kitchen.
When all the hardware components are in place, several new algorithms/functionalities can be added. Such as:
1. At night, when the fan is switched on and the light is switched off in the bed room (meaning you are going to sleep), all appliances in other rooms should be automatically switched off.
2. When the security alarm is activated (when you are leaving the house), switch off all the appliances. Also, when the security alarm is deactivated (when you come back), turn on the appliances which normally would be turned on (bit of machine learning here).
3. Monitor sleep pattern by analyzing the data logged to the cloud. For example the time between the fan switched on in the night and it being switched off in the morning can be roughly counted as the sleep time.
4. Appliances like TV, cable, sound system and media player all need their own remote control (mostly IR based) and its cumbersome to deal with them. Instead write a smartphone app and map the keys of all the remotes by reading the code emitted by them. Then when a button is pressed in the app, it will send a message to the IR transmitter in the Arduino board to send the relevant code as IR signals to the appliances. One app to rule them all :)
5. Include footage from a security camera inside the house when sending intruder alarm alerts instead of just a plain text message.
Step 21: Conclusion
If you find this instructable useful in implementing any of your projects, it would be great if you could post some details as well as photos of them as comments.
If you have any questions/doubts, please kindly post them as a comments instead of sending a mail to my inbox. If its posted as a comment, it will be useful to all and I can reply as a comment as well.
Suggestions to improve this project as well as ideas for new features/functionalities are always welcome.