IoT Wall Outlet with Arduino and ESP8266

How to add enhanced accessibility, intelligence and connectivity to an ordinary wall outlet

In this DIY, I show you how to take an ordinary wall outlet and add intelligence using 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.

Features
Light On/Off
Fan On/Off
Fan Speed Regulator
IR Remote Control
Touchscreen Display
Smartphone App
Twitter Alerts
SMS Alerts
Mood Lighting
Log to Cloud Platforms
Smart Algorithms
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.
Here’s a helpful link to troubleshooting remote controlled ceiling fan. (http://leoricksimon.blogspot.in/2011/07/troubleshooting-remote-controlled.html)
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, I 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 DIY 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 1: 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 5v relays WS2812B LED strip IR receiver Active buzzer BTA136 triacs MOC3041 triacs 4N25 zero cross detectors Bridge rectifier DIP switches Resistors Capacitors Breakaway male and female headers PCBs Enclosures 220v AC to 5v DC 2 Amps power supply module Current transformer 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 2: Circuits

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.

Step 3: Circuit contd...

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.

Step 4: Circuits contd...

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.

Step 5: Wiring

Original 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

Step 6: New wiring

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 https://github.com/tangophi/iot_wall_socket The node-red code is available at https://github.com/tangophi/iot_house 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: Normal Operation SW1: 1100 SW2: 1100 SW3: 01 Uploading sketch to Arduino SW1: 0011 SW2: 0000 SW3: 01 Uploading firmware to ESP8266 SW1: 0000 SW2: 0011 SW3: 10

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.

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 athttp://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. Infrared remote 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. Smartphone app 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 logging 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. Twitter Alerts 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. SMS Alerts 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. https://thingspeak.com/channels/32932 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: http://logic.sysbiol.cam.ac.uk/?p=1473

http://logic.sysbiol.cam.ac.uk/teaching/Node-RED_...

Step 15: Smart algorhythms

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. Features 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.

Leave a Reply

Your email address will not be published. Required fields are marked *