Open/close sensor with SMS text alarm

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 about it so I thought of using one of the ESP8266 modules and add an email service in it, but ultimately send SMS messages to me and my wife.

It is a simple mechanism. I pulled a 1k resistor to 3.3 V on  the GPIO2 of the ESP8266 and a switch to ground. 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.

Screen Shot 2015-07-31 at 4.03.43 PM

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 code:

open_mess_sent = 0

function onAlarm()
value =
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(“”, “WARNING”, “Garage door is open”)
open_mess_sent = 1

function close_msg()
send_email(“”, “RESOLVED”, “Garage door is now closed”)

function init_STA()
gpio.trig(pin, ‘both’, onAlarm)

About hbouzas

Born in Buenos Aires, Argentina. Studied Physics at the University of Buenos Aires. Joined Schlumberger in February 1985 in Houston, Texas, and worked in several technical and managerial positions until 2000. From 2000 until 2008 held several management positions in Abingdon, UK; Calgary, Alberta and London, UK. Worked in the areas of Geophysical Exploration, Geological Modeling, Structural Modeling, Reservoir Modeling and Petroleum Economics and holds several patents. He is currently the Norway Technology Center Manager for Schlumberger Information Solutions and is based in Oslo and Stavanger. Main interest are software, technology, innovation, 3D visualization, design, human computer interaction, energy, environment.
This entry was posted in Uncategorized. Bookmark the permalink.

4 Responses to Open/close sensor with SMS text alarm

  1. John S. says:

    Where is the sendmail.lua code? It seems to be missing.

  2. Mike says:

    My esp8266 has an AT command set. Do you have a tutorial on how to make it work like yours and how do I send the code, paste via a terminal program?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s