Electronic Circuits and Tutorials
Home > Engineering Projects > WiFi Open/Close Detector with SMS alarm
Electronics Educational Practical
Engineering Projects

WiFi Open/Close Detector with SMS alarm

Author: Horacio Bouzas   Email: hbouzas@yahoo.com

Our garage faces the alley and it is not unusual to hear of people leaving the garage door open and get stuff stolen. The trade offs of leaving inside the loop in Houston as it is for any large city in the world (even Oslo). Now it has been 3 times that we forget the garage door open, and this last time we left it overnight! Lucky us, the garage was intact and this is unusual, more than 8 hours open and nothing taken, more than unusual I'll classify it as miracle. Anyway, I needed to do something Figure 1  about it so I thought of using the ESP8266 Wifi microcontroller unit (figure 1) and add an email service in it, and ultimately send SMS messages to me and my wife when the garage door remains open for a certain period of time.

Figure 1

It is a simple mechanism. But lets look at the ESP8266 first.

The ESP8266 is a very small WIFI enabled microcontroller. It can be programmed in C by flashing it with the manufacturer’s (Espressif) software development kit (SDK). But it can also be programmed in LUA using the NodeMCU SDK (open source, just Google it or go to GitHub). LUA is a scripting language used widely in the gaming industry and I picked it to program the ESP8266 because of being compact, very high level and allowed rapid prototyping. The ESP8266 acts as an access point and/or a WIFI station, so once it is configured it acquires an IP address and then you can communicate with it via a web browser or a TCP connection (phone, tablet, computer, etc).

The ESP8266-01 used in this project, has 2 GPIOs, GPIO0 and GPIO2, meaning that we can program the module so it can read and write stuff on these GPIOs. The ‘stuff’ is simply digital signals, a 0 or a 1, or in volts, a few millivolts or 3.3 volts. This is perfect to get the state of a switch which will indicate whether the door is open or closed.

The module features a serial interface so you can communicate with it to program it.

There is plenty of material out there to get anyone going with this little wonder. The key things to know is that you will need a USB to serial module to initially talk to the ESP8266, be familiar with serial communication and able to do some script programming. For USB to serial, any FTDI232 based module will pretty much work, but be careful as they are counterfeit FTDI232 that can render useless, make sure whatever you get is genuine. Then you need to choose a serial terminal to send commands to the serial module that will send commands to the ESP8266. Something like CoolTerm or SSCOM32 would do the job. I use CoolTerm mostly on the Mac. Also, when you start copying LUA code into the module, CoolTerm does a great job. With this brief introduction to the ESP8266, you can have a lot of fun prototyping all kinds of interesting WIFI projects.

Figure 2 shows the pins on the ESP8266:
VCC: 3.3 Volts

CH_PD: Module enable, connect to VCC
RST: Resets the module when pulled to GND
UTXD: Serial transmit
URXD: Serial receive
GPIO0: General purpose IO 0
GPIO2: General purpose IO 2
GND: Ground

I pulled a 1k resistor to 3.3 V on the GPIO2 of the ESP8266 and a switch to ground, shown in Figure 3. When the switch is open, there is voltage in the GPIO2 so its state is 1, when the switch closes, it sends the GIPO2 to ground, voltage is 0, state is 0.

I programmed the ESP8266 to monitor changes in GPIO2 as follows:

  • If switch closes, wait a number of minutes (typically lets say 5 minutes) before sending a text message.
  • If switch opens before the 5 minutes are passed, do nothing, the door has just been normally opened and closed.
  • If switch is closed and 5 minutes passed, send a text message.

Keep sending a message every 5 minutes until switch closes.
Thats it.

Here is the LUA code:

open_mess_sent = 0

function onAlarm()
    value = gpio.read(pin)
    if value == 0 then
      open_mess_sent = 0
      print("Sending open message")
      tmr.alarm(1,300000,1, open_msg)
    elseif value == 1 then
         if open_mess_sent == 1 then
            print("Sending close message")
            open_mess_sent = 0

function open_msg()
    send_email("5555555555@txt.att.net", "WARNING", "Garage door is open")
    open_mess_sent = 1

function close_msg()
    send_email("5555555555@txt.att.net", "RESOLVED", "Garage door is now closed")

function init_STA()
    gpio.trig(pin, 'both', onAlarm)

Figure 4

Note: To report broken links or to submit your projects, tutorials please email to Webmaster