DIY WiFi Garage Door Opener Project
The IoT world is just starting to explode - every technology company around the globe is trying to figure out how they will fit into this new world. It's just that big of an opportunity! So for this instructable, in keeping with this IoT theme, we'll go over how you can make your very own IoT demonstrator that's actually kind of useful ;)
- setup a reliable workflow for coding the ESP32 WiFi module
- flash it
- assemble it up on your breadboard
- download our application code and unzip it
- add your WiFi credentials & static IP
- connect it to your WiFi network
- edit thresholds and mount it in your garage
- wire it up to your garage door opener contacts
- and click away!
- NO SOLDERING REQUIRED (except for the pins to breakout boards if required)
- tinyLiDAR time of flight distance sensor module
- Wipy3.0 or similar ESP32 based WiFi board
- Optically isolated solid state relay (Omron G3VM-201AY1) to control the garage door opener
- 470ohm resistor (5% 1/8watt or larger is okay)
- Momentary push button switch for the BOOT (GPIO0) pin to upgrade the firmware on the ESP32 board
- USB to Serial dongle to upload code and interact with REPL on the ESP32 (use the 3.3v I/O version)
- Breadboard + wires
- Power supply: 3.3V to 5V at 500mA or greater. You can use a microUSB cell phone charger for the power supply & microUSB breakout board to plug into your breadboard.
Step 1: IoT What?
Undoubtedly you've heard of the term IoT by now in all the media, but what does it mean?
Loosely speaking it means getting all sorts of sensors and controllable things connected to the Internet. These days, the Internet is synonymous with wireless and thus we have everything electronic all of sudden becoming wireless connected over some sort of wireless link like WiFi/BT/LoRa/SigFox etc. Once connected to the Internet, we can sense and/or control these things from our favorite mobile controller like our cellphone or automate them via some app running in a server somewhere (i.e. the cloud).
Although the larger companies have been marketing more voice control, AI and cloud connectivity lately; the basics of making this all happen are still the same. You need to connect your "thing" to a wireless link before any of these concepts are possible. So let's start with the basics and learn how to connect up the tinyLiDAR time of flight distance sensor to a low cost WiFi module and then show to send data back and forth across the network. By the end of this instructable you will have your own working WiFi enabled garage door remote control with a real time monitor to check if the door is open or closed.
Technically speaking, as shown in the block diagram above, this project implements a micropython webserver running on an ESP32 WiFi module using the 'websockets' communication protocol to pass data back and forth from any mobile web browser. Added to this, we have the tinyLiDAR time of flight distance sensor taking measurements on demand so you can check if the garage door was left open.
Step 2: Try It - No Really, Try It Out Now
This is all a relatively new field in electronics so there will be a lot of experimentation required to get things to work just right. Our hope is that you will be able to build on this code base and make some more interesting IoT projects of your own.
All of the code used in this article worked well at the time of this writing. However, as the rate of innovation in the IoT space is increasing, things may have changed by the time you read this. In any case, working through problems and adapting it for your own use will at least get your head into this exciting new space and start thinking like an IoT Engineer!
Ready? Let's start with the first step of setting up your own stable development environment.
Step 3: Micropython & ESP32
The ESP32 WiFi modules were created by Espressif and they have improved a lot since their first generation ESP8266 modules from just a few years ago. These new versions have lot more memory, stronger processor and more features than the original modules and are still low cost. The diagram above gives you sense of just how much they were able to pack into this little ESP32 chip. The ESP32 IC itself is a dual core microcontroller with an 802.11b/g/n WiFi radio and also a Bluetooth 4.2 radio integrated. The ESP32 based modules will typically add an antenna, extra FLASH memory and power regulators.
Note that when we say ESP32 module in this instructable, we are referring to the Pycom Wipy3.0boards which are based on the ESP32 chip/module. In our experience, the Pycom boards seem be of higher build quality than the typical low cost ESP32 modules available. When developing, it is always helpful to reduce as many variables as possible so we went for the Pycom boards instead of low cost generics.
For OEM applications, the ESP32 coding is typically done in C language but thankfully there are also a lot of options for us to choose from so you won't have to get down to this low level if you don't want to. We chose to use micropython for all of our coding in this instructable.
Micropython as you may have surmised is a subset of the full Python programming language that powers some lesser known search engines and websites like Google, YouTube and Instagram ;)
Micropython started as a kickstarter project originally for the STM32 processor but has become very popular for a lot of different microcontrollers now. We are using the latest official Pycom ESP32 port of micropython here.
Step 4: The Faster Way
The micropython code has a simple front end GUI that's called REPL which stands for "Read–Eval–Print Loop".
ESP32's REPL normally runs at 115.2Kbaud since its accessed through the serial port. The picture above shows this REPL prompt signified by its three arrows waiting for direct commands. It's an easy way to try our simple commands and most coders use it for developing their software but we found it to be a painfully slow way to go. Hence we decided to do it a different way for this instructable...
As the ESP32 modules have fast WiFi connectivity, we just need to access the module over WiFi via an FTP server that's already embedded inside of the standard micropython code. This will then enable us to use FTP clients like FileZilla to simply drag and drop our code onto the ESP32.
So to do this we need to get the ESP32 module onto your WiFi network first. The Wipy3.0 modules run a small access point by default at power up so you can connect directly to them from a laptop at 192.168.4.1. Check out more details here if you like this method.
We work on desktops in our lab so we wanted the ESP32 modules to connect up to our network instead. To do this, we just need to give the module a static IP address and our password information to log onto our WiFi network.
Step 5: Download Now
Download the application code now and unzip the files to a temporary folder on your computer. Then start editing mywifi.txt and boot.py script files with your own WiFi network credentials.
Btw - our favorite text editor is still SublimeText. It can be downloaded here.
You will have to setup FileZilla as shown in the pictures above. Also in the site manager you need to "add new site" for the ESP32 login using the static IP address you chose like shown above. User is "micro" and Password is "python". Its important to use passive FTP and limit it to single connections only. We found limiting the upload speed helped as well to prevent upload hangs. Although not shown in the pictures, it would be helpful to associate the SublimeText program for the file types so that you can edit the code by double clicking on the left side of the FTP screen. To do this, just go to the Settings menu and in the File editing/Filetype associations enter the location of your SublimeText exe file for each association. For example ours was:
js "C:\Sublime Text Build 3065 x64\sublime_text.exe" . "C:\Sublime Text Build 3065 x64\sublime_text.exe" htm "C:\Sublime Text Build 3065 x64\sublime_text.exe" html "C:\Sublime Text Build 3065 x64\sublime_text.exe" py "C:\Sublime Text Build 3065 x64\sublime_text.exe" css "C:\Sublime Text Build 3065 x64\sublime_text.exe"
Copy the extracted application files for this instructable to a new folder called "FTP" on your computer like we did. It will be easier to drag from here inside of FileZilla later.
Its normally a good idea to have the very latest firmware running on the ESP32. Upgrading the Pycom modules to use the latest micropython is very simple and can be done in about 3 minutes with their firmware update tool.
Just be sure to set the COM port for your USB to Serial dongle and de-select the high speed mode as shown in the "Communication" picture above. Ours was COM port 2. Note that to get the ESP32 modules into this upgrade mode, you will have to press the GPIO0/Boot button (on P2 pin) while pressing and releasing the Reset button.
Step 6: Hardware Time
Now would be a good time to wire up the hardware on a breadboard as shown in the pictorial schematic diagram above.
After this is all completed. Start up the terminal software with the proper COM port for your USB to Serial dongle set it to 115.2Kbaud.
At power up, the module should show the familiar REPL prompt which gives three arrows ">>>".
Now go to your edited mywifi.txt file and copy all of the contents (CTRL+C). Then go to the REPL terminal screen and hit CTRL+E to get into cut and paste mode. You then right click to paste the contents into the REPL screen and then press the CTRL+D keys to execute what you pasted.
It should start a count down right away to say it's trying to connect to your WiFi network. The screen shot above shows a successful connection message.
Once connected, you can use FileZilla to connect to the FTP server in the modules at the static IP address you chose already in your mywifi.txt and boot.py files.
Step 7: Still With Us?
If made it okay so far then good for you! The hard work is done :) Now it'll be smooth sailing - just a bunch of cut and paste and you'll be up and running so you can then mount it in your garage.
To edit any of the code, you can double click on the left side of the FTP window in FileZilla and it will launch SublimeText. Save your changes and and then drag it across to the right side which is the ESP32 window.
For now, just drag the files from the left side to the right side of FileZilla to upload each file separately to the ESP32 module. This takes only a few short seconds instead of minutes like the normal REPL method does. Please note that all the files should be under the root directory called "flash" inside of the Pycom board. You can make a bookmark in FileZilla to make it easier to come back here for next time.
If you ever get a problem where FileZilla hangs and times out for the upload, you will notice a file in the ESP32 side which has 0 bytes. Trying to write over it can drive you crazy as it never finishes no matter what you try! It's a very strange state and happens very in-frequently. The best solution for this is to delete the 0 byte file and power cycle the module. Then get a FRESH copy of the source file to upload again to the ESP32 module. Note that a fresh copy is the key here. Somehow the source file just won't upload properly if it hangs like this even a single time.
We found it helps to drag each file individually across to the ESP32 module starting with boot.py. This first file is responsible for getting your module onto the network so you won't need to do the cut and paste in REPL anymore. You can however grab the www folder and drag it over in one shot. This has always worked for us in our development. All of these files are stored in the on-board non-volatile flash storage in the ESP32 module so they will be there after power is removed. Just fyi - the main.py will be executed after boot.py each time the module is powered up.
Step 8: Hacking Tips
Have a look at all the code and try to Google for keywords you don't recognize. Once everything is up and running you can try to change whatever you feel like to see what it does.
If anything goes wrong, you can always clear out the code and/or re-flash the module in about 3 minutes like you have already done earlier.
To reformat the flash and clear out all of your code in one shot, you can type the following in REPL:
Then do a power cycle or press the reset button on the Wipy board.
Note there is also another way of bypassing the boot.py & main.py if things go mental on you. Just temporarily connect pin P12 to the 3.3V output pin and press the Reset button as shown above. It will bypass all your code and go straight to REPL one time so you can figure things out without deleting all of your code from flash.
Once done uploading all of the files, just hit the Reset button on the ESP32 module to reboot it.
You will see the familiar countdown on the REPL terminal screen as it logs onto your WiFi network again. Difference is that this code is now running from the boot.py file this time.
Step 9: Webpages
The microwebserver should be up and running now on the ESP32 so try it out using your desktop browser or your mobile device.
Just go to your static IP address and you should see a screen similar the one above.
There are two web pages being served out from our microwebserver which is running on the ESP32.
The first one is the default index.html page which gives you a simple OPEN/CLOSE button to simulate the clicker type of garage door opener you have. As you press it on your web browser, you will see a large blue gear icon showing up. This is a confirmation that the websocket connection was made successfully and you have received an acknowledgement from the server that your "press" command was received correctly. You should also see a bright green LED light up on the Pycom board when you press this button. The websockets connection is transmitting the states of the button by sending simple text messages of "press" when you press it and "pressup" when you release it. For acknowledgement, the microwebserver is sending back this text but adding "_OK" to it to say it received it correctly.
Once you have connected the optically isolated solid state relay (SSR) terminals to your garage door opener (refer to the pictorial schematic diagram) then pressing the button will also physically open/close the door.
Give it a few seconds and try again if you don't see the blue gear icon showing up as it maybe rebooting or something. Note that the websocket will close automatically in about 20 seconds if you are not using it to prevent lock ups. Also note that websockets are connection oriented, so you need to stop the websocket to change pages or else you may not be able to connect back up again until you hit the reset on the ESP32 module.
For our example code, we have a few ways of stopping the websocket: tap the status text, spinning dots or the hyperlink to go to the next page.
The second webpage is for reading distance measurements from the tinyLiDAR time of flight distance sensor. Just press the button once and it will start streaming the distance readings to your mobile device for about 20 seconds. As you press down, it will light up a red LED on the Pycom board so you can tell it is receiving the button press command from this page.
Both pages give an indication of the door being open or closed by reading distance from tinyLiDAR. The doorThreshold variable needs to be set in both of the html files in the script section as shown here:
//-------------------------- //**** Adjust as needed **** var doorThreshold = 100; // distance in cm var ws_timeout = 20000; // max time in ms to allow for door to open/close default is 20sec //-------------------------- //--------------------------
You will have to edit this threshold for your garage setup so that it can detect when the garage door is rolled up and therefore OPEN or rolled down and therefore CLOSED. After you have made the edits for your threshold in both of the html files, upload these html files again and reboot it to verify everything is still working okay.
If all is good, you can now go ahead and mount the board upside down in your garage as shown in the picture above. Wire up pins 3 and 4 of the SSR to your garage door opener as well. Polarity is not important since we are using a MOSFET version of the SSR - it only has to short out the contacts to simulate a button click on your garage door base unit.
Step 10: And That's It!
Congratulations! Opening your garage door is now as easy as tapping on your phone and you can check if it was left open or not by taking real time measurements with tinyLiDAR :)
You can also now use ESP32 with websockets over WiFi for just about anything you want. Read more about "websockets" if you are not familiar with them - they are really quite fast and easy to use.
Implementing tinyLiDAR with the ESP32 was super easy as well even though the sensor was originally designed to run on an Arduino UNO. We have a more elaborate beta release of the Terminal GUI that runs most of the tinyLiDAR commands in micropython on the ESP32 - see picture above. It's available at our downloads section along with the ref manual, etc.
Have a look through all of our code to understand how everything comes together and try to change things around so you can build on it to do whatever you want.
Please keep in mind that there was no mention of security here. Security is a huge area in IoT and should be taken seriously. If you want to use this project in your garage you should keep your WiFi network passwords strong and secure. There is a lot information on the web about security so be sure to read up on the latest and stay on top of it.
Thanks for reading and happy hacking! Cheers.