Introduction: ESP8266-01 IoT Smart Timer for Home Automation
23/01/2018 Firmware Updated
With the new buzzwords being Internet Of Things (IoT) and Home Automation, I decided to look at the current items in and around my home that is controlled via some sort of device. The items that stood out, are the following:
- Swimming pool pump
- Swimming pool water filler
- Swimming pool and surrounding lights
- TV/Entertainment system cabinet lights
The common item used to control these devices, are standard wall plug timers. Each device is fitted with it's own timer, and are all located in different locations. So why did I choose these items to begin with Internet of Things or Home Automation projects, you might ask?
Well, living in South Africa, means power failures are a regular occurrence. With statistics of my home, I had 35 power failures in the past year, totaling 40 hours. This is normally not an issue, as all the currently installed timers are fitted with a back-up battery for time keeping during power failures. But there are some issues:
- These back-up batteries only last a year or two, then the timer has to be replaced. The timers are constructed such that the timer needs to be destroyed to gain access to the internal Ni-Cad battery.
- Each time the power fails, the timers with faulty batteries needs to be reprogrammed, and the time set.
- The physical location of the timer, when plugged into the wall socket, makes it almost impossible to read the lcd displays viewing the timer from the top. This means that the timer needs to be unplugged, or I have to lay down on the floor to set or adjust the timers after a power failure.
Due to the above reasons, I decided to test the possibility to replace the timers with an IoT Smart Timer, connected to my local home network.
The idea was to design a stand-alone timer, which can:
- Automatically adjust the current time using the internet (IoT)
- Operated without any user actions (Smart)
- Turn an output On/Off according to set times (Timer)
- Programmable and controllable via network (Home Automation)
Step 1: The ESP8266-01 Design
The design was done using an ESP8266-01 WiFi module, as this is what I had available. In it's simplest form, the ESP8266-01 has four I/O pins:
ESP8266-01 Power -up Modes
The logic state of the I/O pins are used to determine in which mode the ESP8266-01 will boot up. The first step was to determine which of the I/O pins can be used to drive an output relay.
- For normal power up, GPIO0 and GPIO2 must be set to logic HIGH. Thus it is clear that these two pins can not be used as an digital output.
- The Tx pin is set as an output on power up, and the output set high. This Tx pin also transmit some serial data during power up. Thus, this pin can also not be used as an output.
The only remaining pin is the Rx pin. This pin is set as an input at power up, and does not have to be pulled high during power up. This pin is thus the most suitable to be used as an output pin.
To ensure correct boot-up mode of the ESP8266-01 during power up, the following pins are pulled high using 10K resistors:
This ensures that the unit boots up correctly each time.
The RX is the only pin suitable to be used as an output. This pin is thus used to drive the output relay via a NPN transistor. The standard flywheel diode and transistor base resistors were added.
The button is connected to GPIO2, and with the button released, a 10K resistor will pull GPIO2 high. Whith the button pressed, GPIO2 is pulled to 0V.
This button is used for two functions:
- Initial set up to connect the unit to a local WiFi network
- To manually control the output during normal operations
The LED is connected to GPIO0, and indicates the following:
- On initial power up, flashes FAST to indicate WiFi Setup mode
- Flashes slow when the time of the unit is not set
- indicates the output relay On/Off status
Step 2: The Power Supply
I will be using the IoT Smart Timer on different voltage levels, therefore there are two power supply options available:
12 - 24V DC
The DC-DC converter used is suitable for supplies up to 28V DC. The output of the converter is adjustable, and is set to 5V. This needs to be done before the ESP8266 module is connected.
A diode was added to to protect against reverse polarity on the supply input.
For this option, I was able to obtain a small 220V/5V switch mode power supply on eBay.
Irrespective of the input voltage, the IoT Smart Timer needs two power supplies:
With both options, the 5V DC is obtained from a switched mode power supply, and not a linear regulator. This means that there is minimal heat generated by the power supply. The 5V is used to drive the output relay
The 3.3V for the ESP8266-01 is obtained from an ASM1117 3.3 regulator. The ASM1117 3.3 is a linear regulator, and can handle up to 500mA. However, the heat generated will be determined by the input voltage to the ASM1117. To reduce heat, the ASM1117 is powered from the 5V rail.
To reduce voltage ripple to the ESP8266-01, the 3.3V rail is fitted with a 100 - 1000uf capacitor. Both 5V and 3.3V rails are also protected from high frequency interference by 0.1uf capacitors.
Step 3: Assembling the PC Board
The PC Board was designed using the freeware version of Eagle. It is a single sided board, which can be easily made at home using the toner transfer method.
Once the PC Board is made, assemble the PC Board in the following order:
- Solder the ASM1117 regulator and three 0.1uf SMD components to the solder side of the board
- Add the single jumper to the component side of the board
- Solder the resistors and diodes in place
- Add the headers for the ESP8266-01 module
- Add the header pins for the LED and button
- Add the screw terminals
- Using header pins, connect the DC/DC convertor to the board.
- Solder the relay into place
- Complete the board by soldering the transistor and 100uf capacitor.
Once all components are soldered to the board, verify all solder points, and ensure no short circuits between pads.
! ! ! IMPORTANT NOTE ! ! !
To ensure that the PC Board can handle large currents on the output contacts, apply a decent amount of solder to the tracks between the relay contacts and screw terminals.
Step 4: Testing of the PC Board
! ! ! Before applying power ! ! !
Remove the ESP8266-01 module from the unit. This is to prevent overheating of the ASM1117 regulator before the 5V supply is adjusted.
There is not a lot of tests that can be done after assembly. The most important step is to ensure the correct voltage levels.
- Apply 12 - 24V DC to the unit.
- Measure the output voltage of the DC/DC converter
- Adjust the output of the converter to between 5.0 and 5.5V.
- Next, measure the 3.3V supply.
- If supplies are OK, remove power from the unit
You can now insert the ESP8266-01 module into the headers provided.
! ! ! Note ! ! !
Once you have tested the IoT Timer and it is working, use clear lacquer to cover the solder side of the PC Board. This will prevent oxidization of the tracks, and provide extra insulation between the relay contacts and the rest of the circuit.
Step 5: The Enclosure
The enclosure is not that important, as long as the PC board and all wiring fits neatly and safe into it.
To make construction easier, I have made up a cable with the LED and MODE/SETUP button connected to it. This gave me more flexibility in mounting the LED and button to the enclosure. This cable is then plugged into the header on the PC Board.
The photos shows one of the 12V units used for LED lights.
Step 6: Programming the ESP8266-01/NodeMCU
To program the ESP8266-01, you need to set up the Arduino IDE first. I am not going into this details, as there are plenty of great Instructables available on this topic. I have chosen the following links on Instructables for reference, without any specific order to the authors. Thanks for their individual Instructables.
Follow this ESP8266 and Arduino IDE to set up the Arduino IDE for the ESP8266 module..
Next, you will need a programmer to program the ESP8266. Here are two links:
You will need to install additional libraries to be able to compile the code. Again, refer to this Instructable:
I can not remember which libraries I had to install, but I do know WiFiManager must be downloaded separately.. I I have included these in the Libraries.zip file.
Step 7: First Time Setup
When used for the first time, the IoT Smart Timer needs to be connected to a WiFi network. This task is done using the WiFiManager library, so no SSID or passwords needs to be typed into the code.
Follow these few steps:
- Power up the unit
- The LED will start to flash rapidly
- Press the MODE/SETUP button
- When the LED turns off, release the button
- Wait a few seconds, then open your smartphone or device WiFi connections
- A new WiFi netword called IoT Timer will be visible
- Select this access point
- Log onto the IoT Timer (no password is required)
- Wait until your device is connected to the IoT Timer network
- Open up any internet browser
- In the address bar, type in the following IP address - 192.168.4.1
- WiFiManager console will open
- Select Configure WiFi
- A list with available WiFi networks points will be displayed
- Select the required WiFi network, and type in the password
- Next, enter the IP address you want to use to connect to the IoT Timer
- Enter the Default Gateway IP address, followed by the mask
- Once all the settings are done, click on the Save button
- A new window will open to confirm the new credentials were saved
- Close your browser
Once saved, the IoT Timer network will shut down, and the unit will try to connect to your WiFi network.
- Connect your Smartphone or device to the same WiFi network as used for the IoT Timer.
- Open your browser
- In the address bar, type in the IP address of your IoT Timer
- The configuration page of the IoT Timer will open
Your IoT Timer is now ready for use
Step 8: Setup of IoT Timer
The build-in web page of the IoT Timer consist of five sections:
This shows the device name, as well as the current time and output status of the timer
Additionally, the operating mode of the timer is set in this section. There are three modes:
- Auto - Output will be controlled by the different timer programs
- On - Output is forced ON, and will stay on until the mode is changed
- Off - Output is forced OFF, and will stay off until the mode is changed.
This section contains the On and OFF times of the timer. There are seven programs available, and each program can be set individually.
Before changing the the next program, press the SAVE button to save any changes made to the current program.
The MODE/SETUP button can be used to control the output relay during normal operation. Here, select what the button must do when pressed.
Tick the "Update Button Function" box before pressing the Save button to save the new settings.
Here, you can change the name of the IoT Timer. This makes it easy to identify between multiple timers.
Time on the unit is obtained from the internet via a NTP time server. To display the correct time, please update the Time Zone to your region.
Should you want to make use of a different NTP time server, enter the new IP address in the space provided.
Tick the "Update Configuration" box before pressing the Save button to save the new settings.
When changing the Time Zone, the new time will only be set correct during the next time query. The unit is set to update the time every 5 minutes.
Sometimes, it happens that the NTP time server does not respond on every time query. Should it take too long for the time to be set via the NTP server, you can enter the time and date manually.
Tick the "Update Time" box before pressing the Save button to save the new time and date.
The last part of the page indicates the time and date when last the time was synchronised via the NTP time server.
Step 9: Final Words
With the help of several Instructables by different authors, the WiFiManager and it's included examples, and a lot of hours testing and debugging, the current design caters for different errors or faults that the IoT Timer might encounter. To name but a few:
After a power failure, the IoT Timers will boot up faster than the WiFi Router they are connected to. This is handled in code by a 120 second timer that will keep on trying to connecting to the WiFi network.
During normal operations, the WiFi router was turned off for 30 minutes, then restarted. The IoT Timers were all able to automatically reconnect to the WiFi network.
So far, I have three IoT Timers and one Instructable Hit Counter connected to my network, all using the ESP8266-01 WiFi modules. After several days in operation, no additional bugs were detected.
I have created an IoT folder on my smartphone, and added links to each device. This makes accessing the timers quick and simple.
I hope you enjoyed this Instructable, and find it's content useful.
2 People Made This Project!
We have a be nice policy.
Please be positive and constructive.
Thanks Eric, nice comprehensive project that does something really useful. Only real issue I have had is with the NTP time. It does not seem to update under any circumstances. I have been careful to build with the libraries you supplied with the project. I have tried a few other small projects that read and display NTP time and they all work. I have tried to use the ports they used with no improvement. I am probably missing something, as the rest of the software is great. Maybe one of the initialisation variables. Manually set time is fine. Any suggestions? Gerard (ZA).
Hi again GerryGit.
We noticed that the NTP server used as default was down from 13 April to 17 April. Please check if your device is working now.
I am currently using 5 of the devices in my home, and do not have a problem with the NTP time synch.
I am using the NTP IP of 184.108.40.206 on all my devices without any problems.
After complete power failure on all devices, time is normally restored within 15 - 265 minutes on devices.
Please note that NTP servers are not very reliable.... When first powered up, the device will try to connect to the NTP server every 5 minute (might be 10, I can't remember). Thereafter, the time is verified with the NTP server only once every hour. It might be that the time is not updated every hour, but only after a couple of hours.
In my installation, I can say with sertainty that the time is updated at least once every day.
I am not sure if this IP address is allowed/blocked via your internet service provider. During my development, I have tested the devices on the following networks:
- Telkom ASDL
- Vodacom cellular (normal cellular data with phone set up as access point)
- Vodacom APN (Vodacom 3G router via my company's APN network)
- TelkomSA cellular
This is really impressive. I've been trying to modify the code so that the wifi ssid and password is hard coded into the code instead of having to configure the network. I'm having troubles getting this to work though. Any idea on how to get that to work?
I have actually spent several days programming and testing the unit using WiFiManager, to set the SSID and password via the network.
The main aim was to create a device that will be usable to all users, and does not require hard coding of the Access Point details.
I have build in the region of 160 units, imagine if I had to obtain the WiFi SSID and password of each user I have sold a unit to. Imagine if these users had more than one WiFi access point on their premises...
Security wise, the WiFiManager setup is relatively secured, as the IoT Timer is only accessible from within the user's home network. Yes, if the user enables port forwarding on their main router/DHCP server, the device can be accessed from the internet.
Another security feature on most home internet connections, is that the IP address of the home router changes periodically, and withuot a sort of DDNS service, access to the IoT Timers are further prevented.
Personally, I believe that the use of hard-coded SSID and passwords is not recommended.