IoT Garage Door Opener/Monitor Using ESP8266 & SQL

Skills used:
  • Programmed ESP8266 microcontroller
  • SQL, PHP, HTML to make website and database
  • soldering
  • circuitry with diode, transistor, resistor, and relay 
    • magnetic contact sensor to monitor garage door
  • problem solving & troubleshooting

 

In June 2021 I a created a garage door opener/monitor using an ESP8266 microcontroller, relay, contact sensor, website, and database for basically free. After browsing the internet, I could not find a simple IoT garage door project that did not rely on third party services. Many articles referred to using an app called Blynk or setting up a local server. Blynk would not work for my application since control is limited to a set number of users, and future expansion is limited by the free plan. A local server was also not an option, since I wanted to monitor the garage door status from anywhere in the world. I ended up learning about databases and found a library called "MySQL_MariaDB_Generic" that would allow the ESP to communicate with the database. Below is a very brief overview of the steps I took to create the final product.

1. Create a free website using 000webhost

Reason for using 000webhost is because the free plan offers all the features we need, and originally I was going to use the included MySQL database. However, without paying remote connections to the database are not allowed. Therefore, we cannot connect the ESP directly to the database. I resolved this by using another service as seen in step 2. Make sure to password protect the website to prevent accidental opening. 

1.1 (Optional) Get a better domain name from NameCheap 

1.2 (Optional) Add HTTPS using Cloudflare 

This is not necessary since adding Cloudflare to our custom domain will not provide much security anyways due to limitations with the free 000webhost plan. 


2. Create a free MySQL database using Heroku

I referred to this YouTube video: https://www.youtube.com/watch?v=aEm0BN493sU and learnt some basic PHP commands from YouTube as well. 


3. Setup Circuit

You will need the following:

  • ESP8266 microcontroller 
  • relay
  • magnetic contact sensor 
  • wiring (22 AWG worked for me) 


4. Coding ESP8266

First, I added the MySQL_MariaDB_Generic library to the Arduino IDE. Then, I began to learn how the code worked by looking at sample code provided. By modifying and combining sample code, I was able to connect the ESP to the Heroku database successfully. Basically, need the code to update the database when the garage door state changes, and need to constantly check for the opening command. I did this by using 2 different integer entries in the database. For the door state, 1 correlated with the door being open and 0 correlated with the door being closed. The integer 1 also correlated with the command to open the garage door (actuate the relay). Make sure to reset the command integer to zero after actuating the relay or else the door will constantly try to open and close. 

5. Coding Website

Created a simple button and text in HTML/PHP and after the button is clicked, the PHP code updates the database with the integer 1 for the door command variable. Also, the PHP code checks for the database for the garage door status. Below is a screenshot of what it looks like. 

(ignore the light control - that's for a future project)

6. Done

This can be a tricky project due to all the hoops to jump through to avoid paying subscription fees, but it is well worth it since many more devices can be easily added in the future. As you can see in the website screenshot, I plan on adding controls for lighting and maybe an electric kettle as well. I would include a video demonstrating the garage door opener, however I did not take one in the summer when I made it.



Comments