How To Read Your Arduino’s Mind: Building A Childproof Lock

childproof lock

IoT childproof lock with the ESP82

childproof lock

IoT childproof lock with the ESP82

If you are familiar with the tech world, you surely heard the term IoT (Internet of Things) already. The concept is to connect each and every device to the internet enabling them to communicate between each other, and enabling you to communicate with them. One of the groundbreaking tools for this is the ESP8266 serial to Wi-Fi chip. Today we are going to build a childproof lock with this chip by reading an Arduino’s mind. I call it a childproof lock, but you can use it for anything else that you want to lock, and only open for certain occasions. For example to keep your roommates (or yourself) away from those tempting cookies in the fridge.

Today’s recipe:

  • Arduino UNO board
  • ESP8266-12 module with built-in level shifters, and switches
  • LM 7805 5V voltage regulator: https://www.fairchildsemi.com/pf/Lm/LM7805.html
  • 2 capacitors (0.1 mF and 0.33 mF)
  • USB to serial converter (Prolific PL-2303 used here, but a newer version is recommended)
  • A small solenoid magnet
  • 9V external power supply

This code is available on https://github.com/gasparnori/iot_lock

Wiring the ESP8266 for programming

This chip has a lot of advantages and disadvantages. It is low-cost, relatively simple, and extremely versatile. However, there can be many pitfalls when using this chip. This module with on-board level shifters and switches, might help you to avoid many of these problems, but keep in mind a few more things:

  • The ESP needs almost 200 mA current. The power output pin of the Arduino might not be able to supply it, so you should use an external power source and a regulator. For this project you can use a 5V regulator (such as LM7805), or a 3.3V regulator as well.
  • When programming or using the module, the “UART—Programming” switch needs to be adjusted. Don’t forget this, otherwise you will receive weird memory errors.
  • When you connect the USB to the serial converter, be sure to leave the Vcc pin floating, but connect the grounds together.
childproof lock programming

Figure 1: Block diagram of the setup for programming

Communicating with the ESP

So, now that you are connected, let’s see what your chip has to say. There are several Arduino serial communication tools available, but I’ve found ESPlorer to be the best. Once you download it, just simply run the ESPlorer.jar file. Set your port, and the communication speed (baud rate). Usually it’s 115200, but maybe some devices come with different speed, so if 115200 doesn’t work, just keep trying with different values. First, in order to check if the communication is working, press the reset button. If you see a welcome message, you are good to go. Try a few commands in the top left corner, like a simple AT, or connect to your local Wi-Fi network. Now we are ready to program the module.

ESPlorer childproof lock

Figure 2: ESPlorer

Programming the ESP8266

There are two ways to control the chip. It can be controlled from the Arduino board with simple AT commands via the Serial port (this is what you’ve just tried), or an even simpler way: Uploading the code directly to the chip. In this tutorial, we are going to use the second option. Open the Arduino IDE, and go to Tools–>Board–>Board Manager. Try to find the ESP board, and install it.

childproof lock board manager

Figure 3: Board Manager

Open a new sketch, go to Tools–>Board and choose your ESP module. Also, make sure to choose the right port in Tools–>Port.

childproof lock port

Figure 4: Choosing the port and the board

Now, copy the code below into your sketch, turn your chip into programming mode, press a reset, and start programming. After the code is uploaded, turn it back to “uart mode,” press another reset, open the ESPlorer again, and see what the module sends back. If everything worked fine, you should see two messages saying which Wi-Fi you are connected to, and what your IP address is. Open the given IP address in a browser and you should be seeing this:

childproof lock IP

Figure 5: Try clicking on the open button



Explanation of the code

When you open the website, you see a main html page. If you want to open the childproof lock, the website redirects you to the “/msg?msg=open” or to the “/msg?msg=close” URL. On this URL you see another html page for 3 seconds and then it redirects back to the first one. Luckily, most of the redirection is being done in the html code, the server only need to associate each possible URL’s with the html sites to return, which are the form_closed and form_open strings. In order to associate the html with the URL’s, we use the server.on(url, handler). The most important communication happens in the handleMessage function. The server reads the msg argument from the URL, and sends it out on the serial port as a command to open or close the childproof lock.

If everything worked fine, we are ready to connect it to a real Arduino device.

Arduino setup

From the Arduino’s side we are using a very simple setup. While the solenoid could work on up to 12 Volts, right now for this application the 5V, and the current provided by the Arduino’s output pin is enough to keep the magnet working. (However, if you want a stronger magnet, you can easily improve this solution with the help of a transistor or a relay). So, remove the USB converter, and connect the serial ports and the ground together. Again, supply the Vin from the regulator to both devices.

Important to keep in mind:

  • Use the hardware serial port of the Arduino (pin 0 and 1) as software serial ports might not be able to perform on such high speed. However don’t forget to unplug these pins when you program the Arduino in order to avoid communication error.
  • Unplug the Vin pin when you program the Arduino. You really don’t want to accidentally fry your device.
childproof lock arduino

Figure 6: Arduino setup

childproof lock diagram

Figure 7: Completed Circuit diagram



Let’s put it together:

childproof lock

childproof lock brain

Figure 9: A finished lock

As for the lock, I’ve used a plastic rod to stabilize the solenoid and a simple coin to have something magnetic to hold the childproof lock when it’s on. If you use it on your refrigerator or another metal door, that might not be necessary.

Summary

So, here we created a working IoT childproof lock using a small solenoid and the ESP8266 module. The ESP module runs as a server, providing access to lock through a local IP address. The childproof lock can be turned on and off through clicking on a button in a website that is available to any device connected to the same Wi-Fi as the ESP connects to.