Digital Safe version 2

Using an ESP8266 to control a digital safe

Previously I modified a cheap electric safe to work with an Arduino, because the original controller board had failed.

But because my build skills aren’t the best, I kept getting serial port issues, and more than once needed to get to the emergency key to open the safe.

At the same time someone asked me if the same design would work with one of the real cheap ($30) safes on Amazon.

So I wondered if I could do better. This time using an ESP8266 board, which provides WiFi access. No serial ports, and the board can host all the software. A quick test of putting one inside the safe (powered by a battery pack) showed only a small attenuation in the WiFi signal; it was still strong enough to talk to the network.

I had also been asked if some of the cheaper safes might be convertable.


The MOSFET, diode and resistor aren’t size sensitive. I just picked these ones ‘cos they felt right. The important part is the MOSFET needs to trigger at 2V and be of N-channel type (to switch the -ve side of the circuit).

The Safe

This is the safe I got. It’s about $30 from Amazon. The model I bought was ‘cos it was $2 cheaper, but there’s a lot of identical models on Amazon.

Original Safe

This is the inside of the safe. The red button on the left is what you would press to reset the combination. But we’re interested in the two screw holes. A standard phillips head will let you remove these two screws.

Inside Safe

You also need to remove this screw. At this point the cover can be removed. It’s a bit tight because of the rubber plug that’s there to stop the door rattling, but a bit of brute force will get it out.

Door Screw

And this is what the internals look like. The magic is the blue solenoid. In the “down” state it lets the locking gate slide sideways. And that’s how this type of safe works; the key (as shown in this picture) pushes down on the solenoid. The electronic part activates the solenoid and that lets the gate slide.


This is what it looks like in the locked position.

Solenoid Locked

The only cable we care about is the two wired to the solenoid.

Original Wiring

Disconnect those from the board. FWIW the other 4 wires handle the power (red/black) and the reset button (white). We don’t care about these, though; we only want the two wires to the solenoid. So carefully unplug that cable. That’s it!

At this point we could remove the circuit board, but let’s leave it in place. We can always revert the changes and switch the safe back to normal operation just by plugging the cable back in again.

NOTE: This solenoid is especially easy to “bounce” by banging on the safe; the spring is very weak. That means that the right timing of banging on the top and turning the handle might open the safe. This isn’t high security! (The key lock also isn’t high quality). Increasing the security of the safe isn’t part of this instruction!

Annoyingly it’s not clear what the solenoid is. The blue wrapper blocks all data. So how much power does it need? We know it can work on 6V (‘cos of the 4 batteries) but how much current does it need? I measured the resistance (15ohms). At 6V that’d be around 400mA which made me wonder if the Vin line of the NodeMCU would be sufficient. And… it wasn’t. It was enough to hold the lock open but it wasn’t enough to cause the solenoid to activate. So we’re gonna need to be more innovative about powering the system.

Designing the replacement board

So let’s look at designing the replacement.

For power, let’s use a MOSFET as a relay. I used a RFP30N06LE because this can be triggered at 2V and so the 3.3V output from the NodeMCU should work.

To make sure we have the order of the pins on the MOSFET right, looking at the front (with the writing facing you and the pins downwards) the 3 pins are:

    Gate   Drain   Source

Source will be connected to ground, drain will connect to the solenoid, and gate will connect to the NodeMCU pin we use to trigger this. The other pin of the solenoid will connect to the power. Now when the gate line goes high the solenoid will trigger.

Finally there’s a 4k7 resistor between gate and ground. This is a pull-down resistor just in case the ESP-12E pin floats. It’s possibly not needed, because the software drives the pin high or low and if the software crashes then the watchdog timer will reboot it pretty quickly, but I put it there just in case.

That all sounds complicated! Now to work out the best placement for a veroboard so I can solder it all together. Which appears to be:

Circuit Diagram

That doesn’t look too complicated. One diode, one resistor, one MOSFET, a few cables, and some pins.


So I prototyped this on protoboard. This is powered by a 9V battery, which won’t be the final solution! I removed the solenoid from the safe to make it easier to test.

Proof Of Concept

(Note: there’s no diode here; I added it later! And if you’re looking carefully the resistor is in the wrong place. Apparently I got lucky and didn’t destroy the MOSFET… Also shush spot the cable from D7 when the diagram says D6. You didn’t see that! How this prototype ever worked… I dunno!)

In this setup I can do

    curl http://safe.local/safe/?open=1

and the solenoid will open for 5 seconds. And… it worked!

In this case I used a NodeMCU ESP-12E even though it’s overkill and oversized. You could use a WeMos D1 Mini or clone and it would work just a well. However the D1 Mini will only take 4->6V input, whereas the NodeMCU ESP-12E can take up to 10V (maybe even 12V). That gives us more flexibility in the power supply we use. I did my testing with a 9V battery, which wouldn’t have been possible with the D1 Mini.

Note that D6 on the ESP-12E “conveniently” lines up with the Gate on the MOSFET. Not like I planned that or anything, honest! (Confession I had planned on D7 being the pin but I counted wrong and D6 it is. Which is why the proof of concept used the wrong pin. Oops!).

Now I wanted to use a “socket” like approach for the ESP-12E so I could remove it for software upgrades. This just required 3 pins (GND, Vin, D6) but I used 4 (the second GND) to add a bit of stability. This turned out to be one of the harder parts because I couldn’t find any single-pin sockets that are big enough to handle the standard pins. I ended up cutting up a 40pin strip, and that’s not very tidy… but it worked.

(In the second of these boards that I made I used a couple of 8 pin strips and cut the pins off and covered the gaps with insulation tape; that also worked and was easier to handle).

I also put a couple of normal pins on the board to make it easy to connect to the solenoid via normal male->female connector cables, and I soldered relatively thick power cables that can be connected to the power supply.

The Board

(and yes the resistor is still in the wrong place! but at least the diode is there, now)

And this is how it looks with the solenoid connected and the ESP-12E inserted.

Board with ESP

My soldering skills still aren’t the best… but, at least it works!

Bad Soldering


Unfortunately there’s not enough space inside the safe cover on this small safe (the old large safe had plenty of free space) so now we get to the mounting part.

To run the solenoid cables from inside the safe cover to outside it I cut a small hole near the top of the battery door. Just big enough to slide the two wires through. This is the only real change to the safe.

I connected two “dupont compatible” male-female cables to this connector.

Hole for Solenoid Cable

Mounting of the whole thing was… done by duct tape. Yeah not very professional but it seems to work.

Importantly I taped down the power wire so that it didn’t flex whenever the door was opened/closed. This means the wires shouldn’t snap off the board. Whenever movement happens it can stress solder joints, so we want to prevent that as much as possible. Let the tape take the strain!

Duct Tape

And the ESP-12E just slots in, nicely. It’s easy to remove if software updates are needed. Just be careful to align the pins correctly when you plug it in.

Mounted Result

Now the power cables can feed out of the back of the case. I was worried that the relatively thin metal could rub and end up damaging the cables so I protected them with some more tape. And I taped the wire to the top edge of the box. I didn’t do a very good job, really. Not pretty… but it works!

Power Cable Inside

And then out of the back I added more tape to try and keep it in place.

Back Of Safe

Powering this safe can be done pretty much from any DC power supply from 5V to 10V (maybe up to 12V). I had a really old Blackberry power supply (I never had a Blackberry, so not sure where I got it? Maybe from a “junk” bin at work). This is 5V 700mA. Plenty. I cut the tip off and connected the wires up.

It’s very important to ensure you get the +ve side connected to the +ve input to the board, because the ESP-12E may not like reverse voltages. This power supply didn’t make it obvious which wire was +ve so I had to use a multi-meter. But once I knew which was which it was simple to connect.

Power Supply

And to protect this also from movement I stuck it to the back. Lots of duct tape used in this build!

More Duct Tape

And that’s it! The safe is built.


Now we come to the software

This software can be built with the standard Arduino tool set with the esp8266 libraries loaded. To make it simpler I embed the HTML inside the code (as an include file) rather than using SPIFFS. That way there’s just one image to flash and not two. The code is small enough that it still fits.

The safe is modeled with some simple states:

  • Unlocked (no password set; safe can be opened any number of times)
  • Unlocked once (safe can be opened once, then goes back to being locked)
  • Locked (password is needed).

The solenoid lock is always kept in the locked position unless the “open” function is selected. Open can not be selected if the safe is Locked.

When the safe software is first loaded it will create a WiFi Access Point called something like “Safe-123456” (the exact name will depend on the MAC address of your board). You should connect your phone or laptop to that access point.

This will let you go to or (if you’re lucky) http://safe.local and that will display the WiFi configuration screen.

Change AP

Enter your WiFi SSID and password. In 5 seconds the software will reboot and the safe will connect to your local network. It will attempt to get an address by DHCP.

If it is unable to connect to the network after 60 seconds then it will give up and start up the Safe-123456 access point again and allow you to change the network details and try again.

Once the safe is on your network it will broadcast the name “safe.local” via mDNS (aka zeroconf, Bonjour, Rendezvous). If your machine supports this then you should be able to go to http://safe.local otherwise you may need to check your router to find the IP address that was assigned to it.

Opening the web page in a browser should give you a menu on the left and a larger status box on the right.

Main Menu

The first thing you should do is go to the “Change Safe Authentication Details” link at the bottom of the menu. Here you can set a username and password for your safe. It’s recommended you do this so that only authorised people on your network can reach this interface. After setting the password you should be prompted to login before doing any more work.

Set Authentication

On the main menu you will see various options:

  • Status” will tell you if the safe is unlocked, locked, or one-time unlocked.

  • Open safe” lets you open the solenoid so the door can be opened. This will not work if the safe is locked.

  • Unlock pswd” has 3 options. If you get the password wrong for any of these then there is a 2 second delay before the safe will respond again.

    • “Test” - this will let you test the password but leave the safe locked. This may be useful to use after setting a password so you can be sure you can unlock it again, but before closing the door.
    • “Unlock Once” - this will let you select the “Open Safe” function once. After that the safe will go back to locked mode and you’ll need to re-enter the password to unlock it again. This is the normal function to emulate the original safe.
    • “Unlock Permanent” - this will remove the password totally and let you use the “Open Safe” function as many times as you like. This is similar to pressing the “red button” inside the safe that lets you change the combination.
  • Finally the “Lock Safe” function lets you set a new password for the safe. This can only be done if the safe is unlocked. If the safe is locked or “unlock once” then it will be rejected.

Note I’ve been saying “Password” and not combination. That’s because we’re not limited to numbers and we’re not limited to 8 digits.

Passwords can be up to 100 characters long. Although I’ve tested with a few special characters (eg. % and &) you should verify the password with the “Test password” function before closing the safe door!

Talking to the safe programmatically

Although the software wasn’t designed to provide an API, all the access is via HTTP requests and so can be performed from the command line (eg curl).

The format is


The /? are important.

Of course you need to provide the username and password.

So, for example, to get the status:

    curl -u user:pass http://safe.local/safe/?status=1
    Safe is unlocked

Commands you might use:


Note that “lock” needs the password sent twice ‘cos it checks the two match.

Now you might think “if I can send unlock_all requests in a program then I can write a loop to test them all”.

Yes… but, good luck! An 8 digit numeric password has 100,000,000 combinations. Let’s assume you get it right half way through. 50,000,000 attempts. Each attempt takes 2 seconds ‘cos the safe code enforces that. That’s around 3 years. And the password can be digits and letters and more. You might get lucky! But don’t bet on it…


And that’s it!

These cheap safes can be converted in pretty much the same way as the larger one. I’m not sure what use these safes are, though, because they’re not very secure.

I think I’m gonna replace the Arduino/Relay board in my other safe with another one of these ESP8266 based boards, though. Should be a lot more reliable than the old design, has less cables and isn’t chained to the computer by a serial port!