ESP8266-01 IoT Smart Timer for Home Automation

51K182142

Intro: ESP8266-01 IoT Smart Timer for Home Automation

UPDATES

30/09/2018: Firmware Updated to Ver 1.09. Now with Sonoff Basic Support

01/10/2018: Firmware Version 1.10 trial available for testing on ESP8266-01 with issues

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:

  • GPIO0
  • GPIO2
  • TX
  • RX

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.

Boot-up

To ensure correct boot-up mode of the ESP8266-01 during power up, the following pins are pulled high using 10K resistors:

  • GPIO0
  • GPIO2
  • RST
  • CH_PD

This ensures that the unit boots up correctly each time.

Output Relay

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.

MODE/SET Button

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

Indication LED

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.

220V AC
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:

5V Rail

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

3.3V Rail

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.

Noise Filtering

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:

Using Arduino Uno

DIY Programming Board

Libraries

You will need to install additional libraries to be able to compile the code. Again, refer to this Instructable:

Install and Use Arduino Libraries

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:

Status

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.

Programs

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.

Button Function

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.

Configuration

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.

NOTE

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.

Time Adjust

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.

Time Synchronisation

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.

Regards

Eric

96 Comments

GREAT write up. Unfortunately will not compile. Error message is "Error compiling for board generic esp8266 module". This seems to be a common problem but no solution yet. Would you please advise on which version Arduino IDE you used and which version of the esp8266 boards def you used. Any other suggestions appreciated. Also cannot find any flash setting "DIN". There is a DIO and DUO. Thanks much for all the fine work you put into this project and sharing it. Would really like to use. Best!
Hi MarcoN50.
I also want to get a module ready to use like you did without having to assemble everything.
there are so many modules on the net and I wouldn't want to buy the wrong one with "puya" eeprom chip.
would you be so kind as to tell me which PCB module should I buy?
is it possible to control two ESP8266-01 cards?. in which way ?.
instead of the cell phone it is possible to use something alternative as in the following links by installing an S.O. RTOS and browser?
https://it.aliexpress.com/item/1005003966169573.ht... % 22sku_id% 22% 3A% 2212000027589985184% 22% 7D & pdp_npi = 2% 40dis% 21EUR% 2138.48% 2133.86% 21% 21% 214.82% 21% 21% 402100bdec16629691895577862e4abnage% 2112001800275899

https://it.aliexpress.com/item/1005004502250619.ht... %7B%22sku_id%22%3A%2212000029387813500%22%7D&pdp_npi=2%40dis%21EUR%2111.93%2111.93%21%21%215.04%21%21%402100bdec16629693687511950e4aae%2112000029387813500%21sea&curPageLogUid=pK0iWwJkp9xj&ad_pvid=202209120056086572148798118750004528682_2
great project, I am try to replicate one of these using comercial esp01 relay shield and right out of the box the code is working as is should, but some how the output relay pin need to be inverted, since the relay is on while the webserver said to be off, any advice on how I can invert the output state?
Great design. I am having trouble uploading the software to the ESP12 module. Arduino cannot compile properly. Someone may have encountered this problem.
well done on this, have the same problem in ZA, this is a fantastic base for what I want to do, is there a reason why you saved the settings to eeprom and not the file system, I think it would make it easier to expand the timer settings. For the people battling with wifimanager, get the latest version, that solved my problems as described by some other users. For people having problem like HTML_HEAD, change it in the .h and .cpp to HTML_HEADER
Hats of to the creator, great work and share.
Only one of the ino got compiled, and that one is working.
just added this line in html code generation part so that webpage appears bigger on mobile phones
client.println("<meta name=viewport content='width=300'>") after head tag so that the webpage appears well on mobile device.

have already make relay switch to work with RX pin and was looking for a good code to work with it. thanks again for your work.
Great design! Worked great for a month until a power outage. My board is an identical replication of your design. Now it does not come back on line. Any suggestions?

Unfortunately this project, as we found in the comment above, doesn't work with latest esp01 module with "puya" eeprom chips. Actual arduino ide doesn't support correct writing of SPIFFS file system on puya eeprom.

In Eric's code the only thing using spiffs was wifi manager: I eliminated the function to set wifi password and hardcoded wifi settings in sketch. It's a little bit uncomfortable, I know, but at least in this way it works like a charm!
If Eric agrees I can post the modified sketch.

I also adapted the code for the use of this cheap pcb with integrated relay and power supply, see attached schematic: https://www.ebay.it/itm/ESP8266-5V-Wifi-Relay-Mod...

I'll test the system in this day: for the moment it's all ok.

Great project Eric, thanks!

Hi Marco. Would appreciate using your code. Can you please post or message to me? Thank you.
Thank you very much. I figured the problem with the compilation error on Eric's code. The timer script was not loading properly. Thanks again for sharing your work. Would please advise on the flash setings you used?

Thanks again.
I'm very sorry I didn't think to write down such settings and was a long time ago! I think I used the standard one for my module!

Hi, and thanks for your comments.

Yes, please feel free to modify and share your code with others.
Also, thanks for sharing the small relay board drawing. I still wanted to make use of mine, but have no more "old" ESP-01 modules left.

regards,

Eric.

I would like to add RTC with esp8266 12f.
How would you suggest I do? Thank you!
Hello Eric
This is the best web based timer for ESP8266 i have ever seen but there is only one thing is lacking that there is no Seconds in time because i want to active relay for 8 to 10sec when button is pressed then off in this project we can go to Max 1 minute minimum can you add code for seconds also and it would be much better if we can change password through AP mode using Web browser.Kindly reply soon. Thanks
Hello Dear Eric
I am Trying to compile code directly to nodemcu it give me error below mention

In file included from C:\Users\the_b\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3/tools/sdk/libc/xtensa-lx106-elf/include/sys/stdio.h:6:0,

from C:\Users\the_b\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3/tools/sdk/libc/xtensa-lx106-elf/include/stdio.h:63,

from C:\Users\the_b\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266/Arduino.h:32,

from sketch\IoT_Timer_Ver_1.11.ino.cpp:1:

C:\Users\the_b\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3/tools/sdk/libc/xtensa-lx106-elf/include/sys/pgmspace.h:25:130: error: 'const char HTTP_HEAD []' redeclared as different kind of symbol

#define PROGMEM __attribute__((section( "\".irom.text." __FILE__ "." __STRINGIZE(__LINE__) "." __STRINGIZE(__COUNTER__) "\"")))

^

C:\Users\the_b\Documents\Arduino\libraries\WiFiManager/WiFiManager.h:25:24: note: in expansion of macro 'PROGMEM'

const char HTTP_HEAD[] PROGMEM = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/><title>{v}</title>";

^

In file included from C:\Users\the_b\Desktop\IoT_Timer_Ver_1.11\IoT_Timer_Ver_1.11.ino:80:0:

C:\Users\the_b\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\libraries\ESP8266WebServer\src/ESP8266WebServer.h:33:39: error: previous declaration of 'HTTPMethod HTTP_HEAD'

enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_HEAD, HTTP_POST, HTTP_PUT, HTTP_PATCH, HTTP_DELETE, HTTP_OPTIONS };

^

exit status 1
Please use Sketch 'IoT_Timer_NodeMcu_01.ino' while using Node MCU device.
Hi all, I can't get to compile this sketch.
Using Arduino IDE v1.8.8, ESP8266 board libraries (tried various versions from 2.0.0 to latest). I installed the libraries from libraries.zip file correctly and the IDE is correctly configured for the ESP8266 (I can run the blynk example).
Compiling this sketch (I tried both the 1.09 and the1.11) gives me this output:

In file included from C:\Users\the_b\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3/tools/sdk/libc/xtensa-lx106-elf/include/sys/stdio.h:6:0,

from C:\Users\the_b\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3/tools/sdk/libc/xtensa-lx106-elf/include/stdio.h:63,

from C:\Users\the_b\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266/Arduino.h:32,

from sketch\IoT_Timer_Ver_1.11.ino.cpp:1:

C:\Users\the_b\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3/tools/sdk/libc/xtensa-lx106-elf/include/sys/pgmspace.h:25:130: error: 'const char HTTP_HEAD []' redeclared as different kind of symbol

#define PROGMEM __attribute__((section( "\".irom.text." __FILE__ "." __STRINGIZE(__LINE__) "." __STRINGIZE(__COUNTER__) "\"")))

^

C:\Users\the_b\Documents\Arduino\libraries\WiFiManager/WiFiManager.h:25:24: note: in expansion of macro 'PROGMEM'

const char HTTP_HEAD[] PROGMEM = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/><title>{v}</title>";

^

In file included from C:\Users\the_b\Desktop\IoT_Timer_Ver_1.11\IoT_Timer_Ver_1.11.ino:80:0:

C:\Users\the_b\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\libraries\ESP8266WebServer\src/ESP8266WebServer.h:33:39: error: previous declaration of 'HTTPMethod HTTP_HEAD'

enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_HEAD, HTTP_POST, HTTP_PUT, HTTP_PATCH, HTTP_DELETE, HTTP_OPTIONS };

^

exit status 1



Any advice? Thanks
hi there, you need to ground the reset pin on the arduino uno. or else it won't work.
More Comments