IoT Intro: SMS me when I leave my garage door open
Want to build a connected garage door in a few hours for about $30 which notifies you by SMS when you’ve left it open and forgot? - skip down to Parts.
Edit: This tutorial used to contain instructions to dispatch SMS messages via the Twilio service. However, due to an issue with the Arduino software for the esp8266 module (which has now been resolved), I re-wrote the article to use the service ifttt for SMS message sending instead. You can see the original version of this page at the Internet Archive if you’re interested.
In this post I’m going to detail how you can easily build a prototype that demonstrates a simple garage door monitor. The idea is that you’ve left your garage door open and gone back inside or out back and you really would prefer it wasn’t open, inviting your whole street to help themselves to your stuff. Well, after 10 minutes of being open, you get an SMS on your phone warning you that you’ve forgotten it and you go and remedy the situation. If you’re like me, you forget stuff like this more often than legitimately needing the garage open for ages so this is actually useful. If not, it’s at least a good excuse to build your first IoT project.
There is a plethora of options to choose from when it comes to microcontrollers and embedded connected (IoT) electronics. Arduino seems to be the popular mainstream platform of choice these days so I wanted to find something that was “Arduino compatible” given the assumption that there would be a lot of online resources.
A requirement for IoT is that your project be “connected”, meaning it can reach the internet. Arduino has a number of “shields” (add-on boards) that have wired ethernet, and others with wifi but they’re all relatively expensive. I discovered the esp8266 (henceforth ‘esp’) some months ago and it has been receiving a lot of attention lately. You can buy the wifi MCU module for as low as $2.50 on eBay and it’s all-in-one: an Arduino-compatible controller with built in wifi - IoT magic!
This post covers building a prototype which will work and send a real sms. However it won’t attach well to the ceiling of your garage and so it’s meant as a demonstration project which lives on your desk. You don’t need a soldering iron or electronics knowledge. Stay tuned for a follow up post which shows how to install this to your garage.
The inline links are the cheapest eBay prices I could find. You can put this together for less than $30:
esp8266 module - this is the brain where your program goes and the part that can connect to your wifi. I’d order two or more since they’re only a few bucks each. You can fry/brick them easily so better to have a backup I think.
FTDI / serial / rs232 programmer - you need this to send your program over the USB to the esp
Solderless prototyping board “breadboard” kit with 3.3v power supply - this is where you build your circuit
Some push buttons - these are the type of buttons that you push with your finger
Some 10k (or similar size) resistors
You will need an ESP-01 adapter like this or this but be sure you ask when checking out for the “male headers to be soldered” unless you want to give soldering a try and build your own. I have not tried the adapter but given how easy this makes things I think it makes sense.
Mini USB cable (not micro). But look in a box of cables first - you’ll probably have one.
A multimeter - this is not essential but helps a lot!
With your parts assembled, it’s about time to get going.
If you’ve never used a breadboard before, have a look over this quick primer. Basically, you want to lay things out with enough room in between so that things are not too tight-
Push in your power supply on the right-hand side. This way the positive and negative terminals (red and blue lines) on the breadboard will be connected correctly. Make sure the two sets of jumpers are selecting the “3.3″ option and not the “5v” one. esp needs 3.3v and 5v can kill it!
Straddle your esp adapter over the center track.
Push in some of your push buttons - you will need one each for program and reset. You will also need a third for test which will simulate the button that would be connected to your garage door assembly. These usually have 4 pins and are arranged in pairs. You can use your meter (multimeter) if you have one to see which pairs are shorted together and which are ‘normally open’. You want the ‘normally open’ ones on different tracks. This is a good illustration of how the pins of the push button should connect to the tracks on the breadboard.
Find a spot for the USB/serial adapter and make sure the little jumper is shorting the 3.3v and not the 5v option.
Next you want to wire up the power rails so you can use the whole length of the board:
Connect a pair of jumper wires over the vertical break between the two breadboard halves. When these breadboards are new, components and jumper wires can take quite a bit of force to insert. Sometimes a small pair of pliers can help shoving them in - don’t worry about breaking jumper wires or resistors since you have spares.
You can see that when powered, only the rails on the side of the power supply unit are energised. By bridging the gap, you can use the rails along the whole breadboard.
Now we can start wiring up the basic configuration to operate and program the esp. I’m inserting my esp with the antenna (gold ‘s’ shape track) pointing upwards.
This leaves me with the two rows of pins from my adapter having these assignments (from left to right, top row first):
GND: ground or “negative rail” (top left pin)
GPIO 2: “general purpose Input/Output” pin #2
TX: serial transmit (bottom left pin)
VCC: +3.3v or “positive 3.3v rail”
There are various references I used to establish a minimum wiring but two of the best are Getting Started with ESP8266 and Minimal Hardware Setup for Bootloading and Usage.
Note: Your breadboard wiring does not need to look exactly the same as mine. If you’re using a different esp adapter than mine then the pins may be in different places. The instructions below are still useful. The following images show how I did it using the adapter I have.
So with that, let’s get to wiring it up (I’m taking the esp out first):
GND: short this up to the -ve / “negative” rail (blue line on the breadboard)
(I’m using the black jumper wire for this connection in the image above)
GPIO 2: needs to be “pulled up” or “pulled high” for our test button (garage open/closed). Simplistically, when I/O are described as requiring to be “pulled high” or “pulled low”, it just means you take a resistor (1k, 10k, or similar) and use it to short that pin to the VCC “pull high” or ground “pull low” so that the MCU can determine it’s logical state. So grab a resistor and connect it between GPIO 2 and VCC.
GPIO 2 also needs to be connected to our switch which when closed will short the pin to ground (pull it ‘down’). This gives us the logical state for this pin as HIGH normally and LOW when the button is pressed.
GPIO 0: should be “pulled to ground” or “pulled low” with the program button.
(note the new blue wire in the image above from GPIO 0 to the ‘prog’ button)
RX: connect to the TX pin of the USB/serial adapter
TX: connect to RX of the USB/serial adapter.
(red wire: TX of esp to RX of USB/serial module)
CH_PD: should be “pulled up” to the VCC/+ve/3.3v rail with a resistor
RST: also needs to be “pulled up” and wired through the reset button to ground.
(CH_PD and RST pulled up)
(showing the two new pull up resistors and the wiring of RST to ground via the reset button)
VCC: connect to the +3.3v rail
And your basic esp wiring is done.
An important step now is to connect the power supply from your breadboard kit to the USB/serial adapter. The USB/serial module also can power some devices as hinted earlier: it has a 5v and a 3.3v option. Generally these modules don’t deliver enough current to power esp. It’s for this reason we need the breadboard supply. I have found however that leaving them isolated creates problems. We need to join them together.
So take two jumper wires and connect the GND and VCC from the USB/serial adapter to the GND/-ve and VCC/+ve rails of our breadboard respectively.
(GND is black and VCC is blue in the above image)
It’s at this point we can pop the esp back into the socket (antenna trace facing up of course), plug in our power and turn everything on (push button next to the power socket and green LED). You should see the tiny red power LED light up on esp.
You’ll also notice that we have the red power LED from the USB/serial adapter lighting up too since we bridged the power rails.
You should see a tiny blue LED on esp flicker for a brief moment when it powers. You can see this again by pushing the reset button you’ve now wired up. The blue LED is on the ‘transmit’ or TX line and so this is actually esp sending some data immediately as the MCU starts up.
The first thing you should know about the esp8266 is that you can talk to it in a number of ways. The Arduino bootloader enables you to write Arduino code (which is basically c++) using the Arduino IDE - we’re going to do it this way. But it’s also good to know that you don’t have to. You can write programs in Lua, Python and even Basic if you like. The base instruction set is what is known as AT commands. And while we haven’t loaded anything else onto our esp, it’s a good time to try a few just to see how it works.
So you will need to install the Arduino IDE from their website. Once that’s done, start up the Arduino application. Hopefully it will start a new sketch (Arduino’s name for a "program") for you and open it up ready for editing.
Next you will want to connect your USB cable but before you do, power up your breadboard power supply. You should see the LEDs come on again. Now connect the USB cable from your computer to the FTDI adapter.
Depending on your operating system, you may not need to install the FTDI drivers. But if you do, SparkFun has a great tutorial which should help you out. You’ll know you’ve done it right when after connecting it again from the computer to the adapter, the next step works without a error message appearing.
So next, hit the Serial Monitor button:
Now you should see something like this:
Various esp8266 modules may be set to different baud rates by default so ensure that the option Both NL & CR is selected as shown in the image. But given my experience with the ESP-01 module, which is the one in all my images here and the one I suggested in the eBay link above, you are fine to start with 115200 baud.
Now that it’s powered and the Serial Monitor is connected, reset the esp with your reset push button. If everything is working properly you should see some garbage text in the Serial Monitor window and if you’re lucky the words “ready” or otherwise perhaps “invalid”.
From Googling and my tinkering experience it doesn’t matter if you get this “invalid” message. You should still be ok. The real test is the next part.
Enter the command AT and hit the ENTER key (or click Send).
The response should be “OK”.
Now enter AT+GMR followed by ENTER
If you’re getting responses similar to this then everything is awesome!
Create an ifttt account so you can send sms
ifttt is pretty sweet. It allows you to connect all your internet thingies together to perform awesome automation. Today we’re just going to use it to listen for the web request made by our esp, and in return it will send an sms: simple right?
If you don’t have an ifttt account, go and sign up at https://ifttt.com
The next thing you’ll want to do is Connect the SMS channel which will require you to activate your mobile number with a verification pin. Remember to enter your number with the leading zeroes instead of +. For example: in AU I’d start with 00614 rather than +614 which seems more obvious.
Next, go and grab my public recipe for this project and Add it to your account. You will need to activate the Maker channel here. Just follow the on screen instructions; it’s pretty straightforward.
Enter esp_message into the first field.
Leave the Message field as it is with the Value1 placeholder there.
Hit Add. And then Done on the next page.
Now we need to go to the https://ifttt.com/maker page and click the link How to Trigger Events. This will then show a link with a placeholder {event} which you need to replace with esp_message. Once you’ve done that you can select the whole line and copy it to the clipboard. Keep this page open for the next section.
Setting up the iforgottocloseit sketch
Now that the esp appears to be alive and we have the ability to send an SMS from the internet, we need to set up the program/sketch.
Set up the esp board in the IDE by going to File > Preferences and pasting “http://arduino.esp8266.com/staging/package_esp8266com_index.json” into the Additional Boards Manager URLs, then hit OK
Select the esp board by Tools > Board > Generic ESP8266 Module
Add the SimpleTimer library by downloading the zip and then Sketch > Include Library > Add .ZIP Library... and then selecting the zip file you just downloaded
Open up https://raw.githubusercontent.com/cottsak/opensesameseed/e1e04af4dd90b6f1ad764b2014d39aa0cd9fbdda/iforgottocloseit/iforgottocloseit.ino in your browser and copy the entire text from the page
Now in your Arduino IDE, open a new sketch (File > New) and replace the whole file with the code you copied from the github link above (select all, and then paste).
Now you will want to create the keys.h file for your wifi credentials and ifttt key.
Click the arrow underneath the Serial Monitor button and select New Tab
Name it keys.h and hit OK
Now paste the this sample code into the new keys.h file
Update the wifiCreds with your wifi access point ssid and password
Update the iftttMakerUrl with the full url from the ifttt Maker page in the previous section. The link should be similar in form to https://maker.ifttt.com/trigger/esp_message/with/key/d7piVH30EgTjwftpJs9lED only the last part d7piVH30EgTjwftpJs9lED will be different for your account so make sure you don’t copy this link.
You should end up with something like this:
Switch back to the main file in your Arduino application and click the Verify button:
Hopefully if you can see “Done compiling” and something like this image, then you’re winning.
If there is an error then perhaps you have made a typo in one of the files. Start again from the Setting up the iforgottocloseit sketch section.
Uploading the sketch to the esp8266
Now it’s time to send the program to the esp. You want to do a couple of things first before clicking the Upload button:
So testing out the program doesn’t take >10 minutes, change the value of the variable openForTooLongInMins from 10 minutes to something like 0.5 minutes. This way when the program starts you should get the sms fairly quickly.
Increase the baud rate for uploads. We changed the baud rate for the Serial Monitor earlier but the one for uploading your sketch is a different setting. With your sketch selected hit Tools > Upload Speed > 115200
So we can seen the output from the program in the Serial Monitor, hit Tools > Debug Port > Serial (when you upload the sketch without debugging enabled, the blue LED flashes to indicate door open state)
Power on your breadboard power supply with the USB disconnected. Watch for LEDs coming on.
Connect your USB to the FTDI module.
Close and then re-open the Serial Monitor (Tools > Serial Monitor)
Set the esp into “programming mode” by holding the program button and then hitting the reset button. Release reset first then release program. You should see some garbage text in the monitor.
Now that we’re sure everything is working:
The Serial Monitor should clear as the sketch builds and then sends the program over the USB to the esp. It might take about a minute. When it’s done, the program should start and you’ll start to see action in the Serial Monitor; similar to this:
Looks like the HTTP message to ifttt was received. You can also confirm that by heading over to the recipe page in ifttt and clicking the View Recipe logs link. Now just wait for the SMS to arrive on your phone.
If you don’t see this message, the response was something other than Congratulations! You've fired the esp_message event or the esp crashes (you’ll know - it will dump a lot of text to the monitor) then simply reset the esp with your reset button and let it start again. Sometimes crashes can occur immediately after programming mode. Resetting the esp puts it back into ‘run mode’ manually which is always a good isolation test when things get hairy.
Now is the time to go back and tweak the program. Change openForTooLongInMins back to 10 minutes or another duration that suits you. Or perhaps edit the string containing the message that is sent as a SMS to your phone. I’ll let you figure out that part.
Uploading your changes is easy. Just make sure you power the board first; then connect the USB; activate the serial monitor; and lastly put the esp into ‘programming mode’ before hitting the Upload button. Take another look over the section Uploading the sketch to the esp8266 above.
A little about how it works
So there you have it - working hopefully. The GPIO 2 button should normally be pressed up against your garage when it’s closed. To test the rig you want to hold down the button to indicate the garage is closed. Watch the monitor to see the debugging messages: “door is closed”. Then notice as you release your finger it says “door is open”. Now the counter starts to count to your openForTooLongInMins threshold.
I will be adding a follow up post that includes more details on the operation and the design of this simple “proof of concept” program.
Additionally, for those who want to take the breadboard prototype and actually fit it to their garage door like in my video, I will have a follow up post showing how to do that too.
Huge thanks to @iaintdoingit, @mattwillis and Gian for really useful draft feedback.
Also, if you’ve made it this far and you liked this post, feel free to vote for it over on esp8266.com.