ESP8266 ESP-01 Wireless Switch With Deep Sleep Mode

by willemvl in Circuits > Sensors

5201 Views, 16 Favorites, 0 Comments

ESP8266 ESP-01 Wireless Switch With Deep Sleep Mode

SendingUnit_Schema.JPG
ReceivingUnit_Schema.JPG
Complete_System02.jpg

INTRODUCTION

This project is a wireless approach to sensing the open/closed position of my garage door and displaying the door state via an LED indicator in a different location of the house. I decided to make this project because there have been a few instances where I forgot I had left the garage door open when turning in for the night. Since there was no economical way to install new wiring in the house, the sensor had to be wireless and battery operated which in turn meant it had to use very little power. I decided to utilize the ESP8266 ESP-01 Wi-Fi module because of its low cost and its ability to go into a low power (battery saver) deep sleep mode when not in use. Since the garage door isn’t opened/closed that often, putting the module to sleep when not in use makes the circuit ideal for being powered by batteries.

During my research in developing a design approach I found that although there are a number of published designs that use the ESP-01 deep sleep mode, they (1) only provided a wake-up signal when the switch was opened or closed but not both, or (2) required additional active circuitry (i.e. transistors) to generate the wake-up signal and that additional circuitry continued to drain significant battery power even though the ESP-01 module was in deep sleep. The intent of this Instructable is to provide a design solution that solves this problem by using passive components that draw very little current when the switch is inactive regardless of its state.

Putting the ESP-01 module in its low power deep sleep mode is fairly simple and only requires a few lines of code in the software. However, a unique challenge was encountered having to do with waking the module up when the door position switch was opened or closed. To wake the module up from deep sleep mode required applying a (logic) high-to-low-to-high pulse to the RESET pin of the module. The solution involved using RC (Resistor, Capacitor) networks and a SPDT switch with both a Normally Open (N.O.) and Normally Closed (N.C.) set of contacts to create a reset pulse any time the switch changed state.

How the Reset Circuit Works: Refer to the sending unit wiring diagram. Let’s assume the switch S1 is in the Normally Closed (N.C.) position. In this position capacitor C1 is charged to a positive voltage through resistor R1 and that positive voltage is seen at the RESET pin of the ESP-01 Wi-Fi module as a logic 1 (high) signal. Resistor R2 across capacitor C1 is of such a high value (1 Meg-ohm) compared to R1 (10 K-ohm) that it has a negligible effect on the charge. With the switch in this position capacitor C2 is essentially an open circuit causing any charge in C2 to discharge to 0 (zero) volts through resistor R3. When switch S1 is toggled to the Normally Open (N.O.) position a positive current is applied to capacitor C2 through R1. As C2 begins to charge, the voltage across C2 slowly increases from 0 volts to 3 volts in about 0.5 seconds (RC time constant = 0.22 seconds). This 0 to 3 volt rise is seen at the RESET pin of the ESP-01 Wi-Fi module and is interpreted as a reset signal which causes the module to wake up. Meanwhile, since capacitor C1 is no longer connected to a current source it slowly discharges to zero volts through resistor R2. When switch S1 changes state again, the whole process is repeated by again sending a reset signal to the module.

Use OF A Field Effect Transistor (FET): Field effect transistor (FET) Q1 is used to reflect the voltage state of the switch’s Normally Open (N.O.) circuit which is fed to the Rx input pin of the ESP-01 module. The module’s software reads the Rx input to determine the position the switch. A field effect transistor was used because unlike a regular transistor, its input (gate) draws no current and doesn’t affect the RC circuit. Additionally the Source/Drain circuit tied to the module Rx pin will have no power applied when the module is in its deep sleep mode and thus doesn’t drain power from the battery. The Rx pin was used instead of GPIO0 or GPIO2 to read the switch state because both those pins have to be at a logic 1 state during the reset sequence so that the module initializes correctly.

Overall Operation Described: The ESP8266 ESP-01 Wi-Fi module used in the receiving unit was programmed to operate as an “Access Point” (i.e. server) using User Datagram Protocol (UDP) and the Wi-Fi module used in the sending unit was programmed to operate as a “Client” also using UDP. Note that this Wi-Fi system is standalone and does not require connection to the internet or access to your home computer network or router/server. When the door position switch changes state the sending unit (Client) wakes up, establishes communication with the receiving unit, and sends the state of the switch to the receiving unit. Upon receipt of the switch state the receiving unit will either turn on the red LED (door open) or the green LED (door closed) and send an acknowledge message back to the sending unit letting it know the message was received. Upon receipt of the acknowledge message the sending unit will remain awake for 1 more second and then go to sleep. If an acknowledge message wasn’t received in 1 second, the sending unit will retry sending the switch state again. If after 10 seconds an acknowledge message is still not received the sending unit will give up and go to sleep to conserve battery life.

Testing of the circuit found that when in the deep sleep mode, the entire circuit draws less than 18 micro-amps. Assuming we use batteries that have a 1200 milliamp-hour (mah) rating, we can expect the batteries to last well beyond 2 years.

The attached figure shows the complete system before the sending unit (left) was mounted at the garage door and the receiving unit (right) was packaged in a small enclosure for mounting in another room of the house.

The following sections provide the information and steps for building this project. Note that this project requires electrical component assembly and soldering skills. It is not recommended for those with little or no experience in these areas.

Supplies

Sending Unit Parts List - Refer to schematic diagram for reference designations

  1. ESP8266 ESP-01 Wi-Fi module
  2. J1 - 2-Pin 2.54mm 0.1" Male Header
  3. J2 - 1x3 3-Pin 2.54mm 0.1" Male Header
  4. J3 - 2x4 8-Pin 2.54mm 0.1" Female Pin Socket
  5. R1 & R4 - Resistor, 10K, 1/4 Watt
  6. R2 & R3 - Resistor, 1 Meg, 1/4 Watt
  7. C1 & C2 - Electrolytic Capacitor, 2.2uF, Radial
  8. Q1 - N-Channel Enhanced Mode Field Effect Transistor (MOSFET) 2N7000
  9. S1 - Magnetic Contact Reed Switch w/ N.O. & N.C contacts or Micro Limit Switch w/ N.O. & N.C contacts.
  10. B1 - 2X AAA DIY Battery Holder Case, 3 Volt
  11. AAA Battery, 1.5 Volt, Alkaline or better, Qty=2
  12. Circuit board, Custom Made by OSHPARK (https://OshPark.com) from author supplied file: ESP-01_Wireless_Switch.kicad_pcb
  13. Misc. Wire, mounting hardware

Receiver Unit Parts List - Refer to schematic diagram for reference designations

  1. ESP8266 ESP-01 Wi-Fi module
  2. J1 - 2-Pin 2.54mm 0.1" Male Header
  3. J2 - 2x4 8-Pin 2.54mm 0.1" Female Pin Socket
  4. D1 - LED, RGB Common Anode, 4-Wire
  5. R1 - Resistor, 4.7K Ohm, 1/4 Watt
  6. PS1 - AC Power Adapter, 3VDC, 100ma (minimum)
  7. Circuit board, Custom Made by OSHPARK (https://OshPark.com) from author supplied file: ESP-01_Wireless_Indicator.kicad_pcb
  8. Enclosure w/ mounting hardware

Tools Required - The following items and tools are required to build this project.

  1. Computer – Used for programming the ESP8266 ESP-01 modules
  2. ESP-01 Programmer Adapter UART (See Figure 6) - Used for programming the ESP8266 ESP-01 modules
  3. Software: Arduino Integrated Development Environment (IDE) - Free download from https://www.arduino.cc
  4. Fine Tip Soldering Iron
  5. Rosin Core 60/40 Solder
  6. Standard hand tools, wire cutter, wire strippers, needle nose pliers

The Circuit Boards

Circuit_Boards4.jpg

Although the circuits are fairly simple and could be assembled and wired using a prototyping circuit board, the low cost of having a professional circuit board manufactured was too good to pass up. I had them built by a company called OSHPARK (https://oshpark.com). At the time of this writing OSHPARK will manufacture the boards for only $5.00 per square inch (includes shipping). And since each board is fairly small, the price I paid for both boards was only $10.35. For this price, OSHPARK will provide you 3 boards of each type. To order the circuit boards from OSHPARK, perform the following:

  1. Browse over to the OSHPARK website https://oshpark.com and click on the LOGIN icon to create an account and login.
  2. Click on “SHARING” to bring up the Shared Projects page.
  3. In the search box enter the email address: billv923@outlook.com.
  4. Click on the Load Projects button. The following two projects should be displayed:
    ESP-01_Wireless_Indicator.kicad_pcb
    ESP-01_Wireless_Switch.kicad_pcb
  5. For each of the two boards mouse over the Actions button and select “Oder Board”.
  6. Follow the on screen directions to complete the process all the way through checkout and payment.
  7. OSHPARK will keep you apprised of the manufacture progress via email.

Populating the Circuit Boards

Circuit_Boards3.jpg

Once the circuit boards are received from the manufacturer install the components listed in the parts list using the provided figure as a guide. Ensure you install the correct value resistors in their designated locations. Also, capacitors C1 and C2 are polarized so ensure they are oriented correctly. If your experience with soldering circuit board components is limited, it is suggested you refer to one of the many YouTube videos such as "How to Solder on Circuit Boards" before you begin to solder. It is necessary that you use a fine tip soldering iron.

If using a prototyping circuit board arrange and install the components listed in the parts list using the figure and the schematic diagrams as a guide. Solder the components in place. Then using appropriate gauge wire, connect the components using the schematic diagrams as a guide.

Programming the ESP8266 ESP-01 Modules

Programmer4.jpg
ESP-01_USB_Programming_Adapter.JPG
Circuit_Boards2.jpg

Use the following instructions to install the Arduino Integrated Development Environment (IDE) software on your PC and then use it to program the ESP8266 ESP-01 modules.

Arduino Software Environment Installation: Download and install the free Arduino Integrated Development Environment (IDE) onto your PC. For complete step-by-step instructions on how to download and install the Arduino IDE, visit https://www.arduino.cc/ . Versions are available for Windows, Mac OS X, and Linux. The environment makes it easy to write code and upload it to the board. The WEB site also provides tutorials to help you every step of the way.

Once you have the Arduino IDE installed, you must also install the ESP8266 board package so that the Arduino IDE can recognize and program them. To do this go to the Instructable “Getting Started With ESP8266 ESP-01 With Arduino IDE | Installing ESP Boards in Arduino IDE and Programming ESP” and follow the instructions.

Now that the Arduino IDE and the ESP8266 board package are installed, perform the following to install
the two ESP8266 source code files onto your PC (We will upload them to the ESP8266 ESP-01 modules in the next section):

  1. Create two subfolders under the “Arduino” folder called ‘WirelessSwitch_Client’ and ‘WirelessSwitch_Server’. The “Arduino” folder was created during the install, usually under your Documents folder.
  2. Download the file ‘WirelessSwitch_Client.ino’ provided with this Instructable and paste it into the ‘WirelessSwitch_Client’ folder created in the previous step.
  3. Download the file ‘WirelessSwitch_Server.ino’ provided with this Instructable and paste it into the ‘WirelessSwitch_Server’ folder created in the previous step.

Uploading the Software to the ESP8266 ESP-01 modules

Perform the following steps to upload the WirelessSwitch_Client software to one of the ESP-01 modules. Refer to the figures for guidance.

  1. Ensure your PC is powered up and ready.
  2. Install one of the ESP8266 ESP-01 modules onto the ESP01 Programmer Adapter UART
  3. Plug the ESP01 Programmer Adapter module into your PC’s USB port.
  4. Double click on the ‘WirelessSwitch_Client.ino’ file to launch the file and bring up the Arduino integrated development environment (IDE). The Arduino IDE window is displayed with the WirelessSwitch_Client source code listing.
  5. Select the ‘Tools/Board’ pull-down menu to select the Generic ESP8266 Module.
  6. Select the ‘Tools/Port’ pull-down menu to select/verify the COM port selection (i.e. COM1, COM2, etc.).
  7. Select “→” (upload) to start the compile and upload process. The program will compile and automatically upload to the ESP-01 module. When the upload is complete the message “Done Uploading” will be displayed in the bottom left of the IDE window. The following text will be displayed in the bottom of the IDE message window: “Leaving…" & “Hard resetting via RTS pin…”
  8. Unplug the ESP8266 ESP-01 module from the ESP01 Programmer Adapter and identify it as the “Client”.

Perform the following steps to upload the WirelessSwitch_Server software to one of the ESP-01 modules. Refer to the figures for guidance.

  1. Ensure your PC is powered up and ready.
  2. Install the other ESP8266 ESP-01 module onto the ESP01 Programmer Adapter UART.
  3. Plug the ESP01 Programmer Adapter module into your PC’s USB port.
  4. Double click on the ‘WirelessSwitch_Server.ino’ file to launch the file and bring up the Arduino integrated development environment (IDE). The Arduino IDE window is displayed with the ESP8266_WirelessSwitch_Server source code listing.
  5. Select the ‘Tools/Board’ pull-down menu to select the Generic ESP8266 Module.
  6. Select the ‘Tools/Port’ pull-down menu to select/verify the COM port selection (i.e. COM1, COM2, etc.).
  7. Select “→” (upload) to start the compile and upload process. The program will compile and automatically upload to the ESP-01 module. When the upload is complete the message “Done Uploading” will be displayed in the bottom left of the IDE window. The following text will be displayed in the bottom of the IDE message window: “Leaving…" & “Hard resetting via RTS pin…”
  8. Unplug the ESP01 Programmer Adapter from your USB port, then remove the ESP8266 ESP-01 module from the ESP01 Programmer Adapter and identify it as the “Server”.

After the modules have been programmed, they are ready to be plugged into their respective mother boards for use.

Wrap Up

The attached video is a demonstration of the wireless switch as the magnetic contact reed switch is put in the open and closed positions.