Building an IoT key safe box

Justine-Molly Bocian

A project by our guest author Justine-Molly Bocian

Building an IoT key safe box

“The measuring instrument is in the drawer of my colleague, but he is ill – we can’t reach it today” (drawer locked) or “Then I have to bring you the key to the second room in the holiday home tomorrow” are two sentences that initiated the project presented here. In our daily world, simple keys, whether to a mailbox, a drawer container or a room door, are widely used. Even in the long term, such locks will not be replaced by high-quality electronic locking systems (whether for reasons of cost or effort). But what happens if the key is actually not available because a colleague is away at work? How can I give an additional area to a holiday home user if I am far away from the holiday home myself?

The project

A simple solution can be realized by building a key safe box with IoT capability. This can be operated by one person via the Internet, so that e.g. the colleagues at work can prove that a key is released via the Internet, or that the holiday home tenant can be given access to another room key upon request. The basis for this is a box into which both the keys and electronics fit. In the scenario presented here, a disused assortment box from the hardware store was used. In this box, electronics based on the D1 Mini system with integrated ESP8266 W-LAN interface are built. An innovative drive based on smart materials (shape memory alloys) is used as a box opener at the interface. Finally, the programming of a simple operation via internet is explained.

Basic Idea of an IoT key safe box
Image 1: Basic Idea of an IoT key safe box

Project profile

Suitable for: Advanced

Time required: approx. 2 hours

Costs: approx. £33

These materials from the Reichelt shopping cart are required.

Additionally, a lockable box, ideally with a hinge (e.g. assortment box).

Preparations

First, we will briefly familiarize ourselves with the components:

The small microcontroller is very generously equipped for its size: it has 11 digital ports (all with interrupts), an analog input (max. 3.3 V) and a built-in ESP8266 W-LAN module. The D1 mini board can be connected to the PC via USB cable. This requires the appropriate drivers and the Arduino IDE software.

I have found my drivers for Windows 10 as well as for MAC OSX here:
Driver Link: https://github.com/himalayanelixir/Arduino_USB_Drivers

You can get the Arduino IDE here for free:
Software Link: https://www.arduino.cc/en/main/software

The D1 Mini Board can be coupled with a variety of suitable expansion boards. In our example we take either a suitable D1 relay shield or a relay which can be switched with 3.3 V coil voltage. The relay is used to switch drives that have a different or higher current requirement than what the digital output pins of the D1 Mini Board provide.

The mechanical opening is done by a shape memory actuator (NiTinol drive). The small ONE-easy 5N can decouple even somewhat bulky mechanisms with an opening force of over 500 g. Thus, a self-made lock can be unlocked. The drive is only 6 mm thick, so you can secure relatively much space for the keys.

The ONE-Easy 5N actuator operated with a 9 V battery
Image 2: The ONE-Easy 5N actuator operated with a 9 V battery

Technological basics about the mode of operation or background I have found in German on the site of the manufacturer:

The implementation

The following figure shows the electronic circuit diagram:

Image 3: Circuit diagram of the electronics installed in the IoT key safe box.
Image 3: Circuit diagram of the electronics installed in the IoT key safe box.

Two batteries are used for the system – one 3.6 V battery for the electronics and thus also the W-LAN interface, and one battery for the respective activation of the unlocking actuator.

The voltage of the 3.6 V battery is measured by the D1 board and transmitted to the owner via Internet. One wants to keep an eye on the supply voltage, as it is continuously consumed by the electronics.

According to the manufacturer’s data sheet, the 9 V block provides 600 mAh and is therefore sufficient for about 200 releases of the shape memory actuator. Here you can add a counter in the future, which shows how often you have already used the 9 V battery.

The relay can be connected to digital port 1 of the D1 board. The relay board is designed so that the relay can be controlled via port 1. When the relay is activated (i.e. CM with NO) the circuit of the 9 V block is closed with the release actuator. The activation time is of importance here. The activation time determines the stroke of the actuator. In the example, a time of 1500 ms is sufficient.

If a power supply unit is to be connected, or if you only want to work with the 9 V battery, a special module for the supply voltage of the D1 mini can be installed.

The assembly

First, find a suitable position for the unlocking device in the key safe box. In this example, the plastic latch has been filed off so that it cannot engage in the latch via a wedge. Instead, a hole was drilled into the plastic snapper. On the dividers of the assortment box, space was filed off to mount the unlocking actuator. It has been inserted in such a way that it protrudes through the hole in the snap trap in the currentless state. The unlocking actuator must also be adjusted. On its pulling side there was originally an M4 thread, which was filed off to form a wedge. So when the plastic snap is put on, it pushes the locking slider aside first (see sketches). When the hole in the plastic snapper is flush with the linear axis of the unlocking actuator, the slider springs back and the housing is connected to the cover. The electronics are soldered together according to the circuit diagram and mounted in the compartments on the side.

Image 4: Mechanic design of the IoT key safe box
Image 4: Mechanic design of the IoT key safe box

The software

The description lists the code with the essential explanations.

To implement the software, the D1 mini board must be installed in the Arduino IDE. To do this, first add the following line in the Arduino IDE’s preferences under “additional board administrator URLs”: http://arduino.esp8266.com/stable/package_esp8266com_index.json

After that, you can go to Tools>Board>Board Manager with the keyword ESP8266 to automatically install the appropriate package for the ESP-enabled boards. After that the “LOLIN(WEMOS) D1 R2 & mini” is available in the selection of the board to be used. The following settings were used for the example discussed here:

Preferences for the D1 mini board
Image 5: Preferences for the D1 mini board

Now the actual program code:
First, the ESP8266 library has to be added. Furthermore, the W-LAN to be used must be defined together with the password. The same applies to the port used (80 by default).

#include <ESP8266WiFi.h>
const char* ssid = "NAME W-LAN-NETZ";
const char* password = "IHR W-LAN-NETZ PASSWORT";
WiFiServer server(80);

IPAddress ip(10, 0, 0, 99);
IPAddress gateway(10, 0, 0, 1);
IPAddress subnet(255, 255, 255, 0);

int Mess = 0;
int Status = 0;

You can also assign fixed IP addresses (if free): Here you can enter parameters known from network technology such as IP, gateway and subnet. The variable Value is used to measure the supply voltage of the electronics. The variable Status is used later for the web page to be programmed. Afterwards the following definitions are implemented in the void setup ():

void setup() {
 pinMode(D1, OUTPUT);
 pinMode(A0, INPUT);
 pinMode(LED_BUILTIN, OUTPUT);
 digitalWrite(D1, LOW);
 digitalWrite(LED_BUILTIN, LOW);

 Serial.begin(9600);
 delay(10);

 Serial.print("Aufbau der Verbindung zu : ");
 Serial.println(ssid);
 WiFi.begin(ssid, password);

 while (WiFi.status () != WL_CONNECTED)
 {
  delay(500);
  Serial.print(".");
 }

Serial.println(" ");
Serial.println("mit ");
Serial.print(ssid);
Serial.print(" verbunden");

server.begin();
Serial.println(" ");
Serial.println("Server gestartet");
Serial.println(" ");

Serial.print("Adresse: http://");
Serial.print(WiFi.localIP());
Serial.println("/");
for (int z=0; z<5; z++)
      {
      digitalWrite(LED_BUILTIN, LOW);
      delay(300);
      digitalWrite(LED_BUILTIN, HIGH);
      delay(300);
      }

}

D1 is defined to the digital output and A0 to the analog input. The LED built into the D1 mini is defined as output. At the same time the digital outputs are set to LOW at the beginning.

For debugging via the USB interface we activate the serial interface with 9600 Baud. With WiFi.begin our D1 mini logs into the W-LAN network and outputs the status via the serial interface (WiFi.status commands). When the login is done, we start the server operation of our D1 Mini with server.begin(). If everything went well, we let the built-in LED flash over the for-loop.

The loop void loop () is implemented as follows:

void loop() 
{
digitalWrite(LED_BUILTIN, LOW);  
digitalWrite(D1, LOW);
Status = 0;
WiFiClient client = server.available();
if (!client)
{
  return;
}

Serial.println("Neuer Client verbunden");
while (!client.available())
{
  delay(1);
}
delay(1);
String request = client.readStringUntil('r');
Serial.println(request);
client.flush();
if (request.indexOf("OFFNEN") != -1)
{
  for (int z=0; z<3; z++)
  {
   digitalWrite(D1, HIGH);
   digitalWrite(LED_BUILTIN, HIGH);
   delay(250);
   digitalWrite(LED_BUILTIN, LOW);
   delay(250);
   Status = 1;
  }
}

if (request.indexOf("MESSUNG") !=-1)
{
  digitalWrite(D1, LOW);
  Status = 2;
  Mess = analogRead(A0);
          
}
client.println ("HTTP/1.1 200 OK");
client.println ("Content-Type: text/html");
client.println ("");
client.println ("<! DOCTYPE HTML>");
client.println ("<html>");
client.print("Schluesselbox");
client.println("<br><br><br>");
client.print("STATUS: ");
if (Status == 1)
{
  client.print("OFFEN");
}

if (Status == 2)
{
  client.print("Batteriemessung");
  client.print("<br>");
  client.print(Mess);
  client.print("    1024 = Voll");
  
}
else
{
  client.print("GESCHLOSSEN");
}
client.println("<br>");
client.println ("<br><br><br>");
client.println ("<a href="/OFFNEN"> OEFFNEN DER SCHLUESSELBOX </a><br><br><br>");
client.println ("<a href="/MESSUNG"> SPANNUNG DER ELEKTRONIK MESSEN </a><br><br><br>");
client.println ("</html>");
delay(1);
Serial.println ("client disconnected");
Serial.println ("");
}

In the main program void loop () our server now checks whether a client, i.e. user, has logged in. If this is the case, a corresponding information is sent via the serial interface (‘New client connected’). Then we start the query what the client wants by defining a string ‘request’ as input via the internet.

In the following, logical cases about if loops are made:

If the request is “OPEN”, then digital port 1 (thus the relay with FG actuator) and the built-in LED is activated. The ‘Status’ variable is set to 1. To make it a bit nicer, the execution as for-loop can be done 3 times as in the code shown. Then you can switch on the actuator for 1.5 s and let the LED flash quickly over this time.

If the request “MEASUREMENT” is requested, the integer value (between 0 and 1023) is read at port A0. The current value of the battery charge is thus stored in the variable ‘Value’.

Now the internet page is programmed on our server. Essentially, all information is entered via the ‘Client.print’ command family, similar to the Serial.print command. Who perhaps knows HTML, has it easy at this point. In the short version you should do the following:

The definitions of HTML can be adopted; they are universal.

Then we start with the simple HTML page and write “Key safe box” as title. The character
corresponds to the line break in the HTML language.

First, we let the status of our system be shown to us via the Internet using two different if-clauses. If both clauses do not apply, the status = closed is displayed.

Further down on the web page two links are programmed. With ‘

The System Test

After everything is programmed and assembled, you can compile and upload the software via Arduino IDE with the USB interface connected. Afterwards you can watch the connection process to the W-LAN network in the serial monitor. If you did not decide to assign a fixed IP address, the serial monitor will display it when the connection is successful.

After that you can enter the IP address in the browser. The simple web page appears where you can read the status of the key safe box and the voltage of the 3.6 V battery. Here you can convert and display the integer value in volts or percent. If you click on the link to ‘Open the key safe box’, the relay is activated for 1.5 s and our unlocking actuator releases the connection. You can then lift the lid and get to the keys.

Image 6: Operation of the IoT key safe box
Image 6: Operation of the IoT key safe box

Leave a Reply

Your email address will not be published. Required fields are marked *