logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Arduino

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

Nora Gaspar
Published by Nora Gaspar at May 30, 2016
Categories
  • Arduino
Tags
  • Arduino
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
//Your ssid and password
const char* ssid = "....";
const char* password = ".....";
ESP8266WebServer server(80);
//This form will be sent to your browser, when the device is locked
String form_closed =
  "<html>"
   "<head>"
   "</head>"
   "<body>"
"<center>"
"<img width='200px' src='http://www.endlessicons.com/wp-content/uploads/2012/12/lock-icon-614x460.png'>"
"<form action='msg'> <p>Do you really need that chocolate?</p>"
"<select name='msg'>"
"<option value='open'>Yesssssss</option>"
"<option value='close'>No, I continue my diet...</option>"
"</select>"
"<input type='submit' value='Open'></form>"
"</center>"
"</body>"
"</html>";
//this form will be sent to your browser, when the device is open
//there is a redirect link in the html to the main page
String form_open =
"<html>"
   "<head>"
   "<meta http-equiv='refresh' content='3;url=/' />"
   "</head>"
   "<body>"
     "<center>"
     "<h1>Voillaaaa </h1>"
     "<img width='400px' src='http://www.clipartreview.com/_images_300/A_refrigerator_with_an_open_door_100429-171689-937009.jpg'>"
     "</center>"
    "</body>"
   "</html>";
  
void handleMessage(){
   String state= server.arg("msg");
   Serial.println(state);
   if(state=="close"){
     server.send(200, "text/html", form_closed);
   }    
   else{
       server.send(200, "text/html", form_open);
       //after 3 seconds closes back either way
       delay(3000);
       Serial.println("close");
   }
}
void setup(void){
//open Serial port
Serial.begin(115200);
//open Wifi connection
WiFi.begin(ssid, password);
Serial.println("Serial started...");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
}
Serial.println("");
Serial.print("Wifi connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//handling the form itself
server.on("/msg", handleMessage);
//handling the main site
server.on("/", [](){
   server.send(200, "text/html", form_closed);
   });
//handling wrong url
server.onNotFound([](){
   server.send(404, "text/plain", "File Not Foundnn");
   });
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}

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


The Arduino code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int lock=8;
void setup() {
pinMode(lock, OUTPUT);
digitalWrite(lock, HIGH);
Serial.begin(115200);
}
void loop() {
  while (!Serial.available()) {};
  //reading the answer to a string
  String a= Serial.readString();
  
  if (a=="openrn") {
    digitalWrite(lock, LOW);
  };
  if (a=="closern") {
    digitalWrite(lock, HIGH);
  };
}

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.

Nora Gaspar
Nora Gaspar
Nora is an electrical and biomedical engineer living a Nomadic life. She enjoys exploring the tech world by experimenting and building new cool gadgets.

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • ESP-WROOM-02 Wifi Setup Guide – AT CommandsESP-WROOM-02 Wifi Setup Guide – AT Commands
  • How to Read Your Arduino’s MindHow to Read Your Arduino’s Mind
  • Make Your Own Arduino RFID Door LockMake Your Own Arduino RFID Door Lock
  • ESP8266 Setup Tutorial using ArduinoESP8266 Setup Tutorial using Arduino
  • Make Your Own Arduino RFID Door Lock – Part 2: Unlock Using Your SmartphoneMake Your Own Arduino RFID Door Lock – Part 2: Unlock Using Your Smartphone
  • JPEG Decoding on Arduino TutorialJPEG Decoding on Arduino Tutorial
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort