Control a lock with the ESP8266

Software

If you don’t have the Arduino IDE installed already you can download it from: https://www.arduino.cc/en/Main/Software.

Then get the WiFiEsp library from:

https://github.com/bportaluri/WiFiEsp/archive/master.zip

This can be obtained as a zip file from the green Clone or download link on the GitHub page. Inside the zip file is a directory called WiFiEsp-master which needs to be placed inside your Arduino library. On Linux this will be in the Arduino/libraries directory inside your Home directory and on Windows this will be inside Documents\Arduino\libraries.

Basic lock test

Turn on power to the breadboard power supply and then connect the Arduino Nano via USB cable to your computer. This is done in order to ensure that the Breadboard powers the ESP8266. Given the power requirements of the ESP8266 the USB is not able to supply all the juice it requires.

Now start the Arduino IDE. Under the Tools menu, ensure the board type is set to “Arduino Nano” and the correct serial/USB device is chosen under the Port submenu.
With this done we can upload a simple test program to verify the Arduino can control the lock solenoid succesfully. From the File menu choose "New" and cut’n’paste the following code in, replacing any code in the window already there:

/*
 
Door Locker - test
 
A simple program which pulses the relay connected to the LOCK_RELAY pin of the Arduino
*/

#define LOCK_RELAY8 // Pin to control the relay which controls the lock

void setup()
{

  pinMode(LOCK_RELAY, OUTPUT);
}

void loop() {
  digitalWrite
(LOCK_RELAY, LOW);
  delay
(2000);
  digitalWrite
(LOCK_RELAY, HIGH);
  delay
(2000);
}

Upload this code into the Arduino Nano with Sketch -> Upload menu or use Control-R on the keyboard.

This code will cause the relay to close the contacts for two seconds which will unlock the lock, then two seconds later the relay contacts will open causing the lock to lock, and it will do this indefinitely.

If you get no response from the relay or lock solenoid ensure that your wiring is the same as in the diagram and that connections are firm with no loose wires. If your relay has a status LED and that appears to be turning on and off but the lock solenoid doesn’t operate, ensure you have applied power correctly to the circuit.

Testing with the ESP8266

With the connection from the Arduino and the lock solenoid verified, the next step is to see if we can get the ESP8266 to create a WiFi network which we web browse to, to view a test web page. For this test, go to File -> Examples -> WiFiEsp -> WebServerAP in the Arduino IDE.

Near the top of the sketch are where the SSID and password of the ESP8266 is set. You can leave these as the defaults of TwimEsp and 12345678 respectively if you like or change them to something else.

Upload this code into the Arduino as before and once done, open the Serial Monitor by choosing Tools -> Serial Monitor. Once initialisation has completed you should see something like this in the serial monitor:

[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.1.0
Attempting to start AP TwimEsp
[WiFiEsp] Access point started TwimEsp
Access point started
IP Address: 192.168.4.1

To see this page in action, connect to TwimEsp and open a browser to http://192.168.4.1

[WiFiEsp] Server started on port 80
Server started

With your smart phone, tablet or laptop, join the WiFi network TwimEsp and web browse to the address http://192.168.4.1. The serial monitor output may specify a different IP address and that’s OK. Once you’ve connected to this IP address in your web browser you’ll see Hello World! and the serial monitor will log evidence of your device connecting. The web page will refresh every 20 seconds showing a count of the requests received. Don’t worry about the number showing next to the analog input – we haven’t got anything connected to it.

Now we know we can create a WiFi access point, let’s connect to an already existing one. From the Arduino IDE, choose File -> Examples -> WiFiEsp -> WebServer. You’ll need to change the ssid and pass variables a couple of lines down to match what you use for your WiFi network. Here’s an example we’ll use:

char ssid[] = "DoorLock";           // your network SSID (name)
char
pass[] = "passw0rd";         // your network password


Upload this code in the Arduino and open the Serial Monitor. You should see output similar to this:


[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.1.0
Attempting to connect to WPA SSID: DoorLock
[WiFiEsp] Connected to DoorLock
You're connected to the network
SSID: DoorLock
IP Address: 192.168.1.4

To see this page in action, open a browser to http://192.168.1.4

[WiFiEsp] Server started on port 80


This output will let you know that we’ve successfully connected to the network called DoorLock and the IP address the ESP8266 is using, as determined by your WiFi router. When you web browse to the IP address quoted above you should see the same output in the web browser as before and the Serial Monitor will show evidence of your browser connecting to the ESP8266 and some basic information about your web browser and the operating system you are using.

With all these tests working we’re ready to move onto the final step.

<< Introduction and set up                      Getting the door lock going >>

Zircon - This is a contributing Drupal Theme
WeebPal