Page 1 of 3

Need help with idea - Arduino controlled safe

Posted: 02 Aug 2017, 22:47
by Philbert
I have an idea that I want to start tinkering with. I have one of those cheap digital lock safes where you can program in up to an 8 digit code. This is great as 8 digits are hard to memorize but it's also a bit like the "Don't think about an Elephant" situation - trying to actively not remember the combination you just typed in makes you remember it more. I've worked around this a bit by programming in the code days in advance of a session but sometimes I don't always remember to do so.

I want to get an Arduino that could program the safe with a random code and generate the JPG image of that combination. This way it is completely random and I have no idea what the code is. But you shouldn't be able to just turn off and reboot to have it generate a new code - it would have to know it is in an active session and not reprogram the combination and send out a new image. Possible solution I just thought of - save the combination locally and when it is entered correctly on the keypad, the safe unlocks, waits 10 seconds, and then resets to a default combination of 00000.

Now I don't have an Arduino yet but I am getting close to making the leap. I think I can cobble together the code (with a bit of a learning curve) but I'm not sure on the hardware side. I'm going to have to connect the Arduino to the keypad of the safe so it can do two things - 1. Read the inputs of what keys are being pressed. 2. Send output signals to the keypad (and programming switch) so it can program a new lock code. Would anyone have any suggestions on how to go about wiring it up, reading the values of each key press, and then mimicking the same keys from the Arduino?

Re: Need help with idea - Arduino controlled safe

Posted: 03 Aug 2017, 02:56
by mechwilliam
:hi:

Without the exact safe model at hand, I don't think it'd be possible to help you remotely. The problem you're describing is a little bit advanced and I'm not sure if "vanilla" Arduino is enough to tackle it. I think you might need to plan out a circuit design utilising transistors, diodes, amplifiers and other electronics stuff to parse or understand the responses of the safe.

I would suggest you commission this stuff to an electronic geek around your neighborhood and layout the details:
1) The Arduino must be housed inside
2) It must use an SDCard and write current state (active/inactive)
3) It must reset the lock back to 00000000 once the time is finished

Problems you might run into:
1) SDCards are not very reliable, they fail often.
2) The guy who did it messed up somehow or missed an edge case.
3) Arduino needs power and if relying on batteries...well batteries run out.

Other "shooting a bug with a cannon" solution:
Implementing a robot Arduino with arms that clicks the combinations and stores them encrypted into a text file and decrypts them once the time is up.
The components will be very expensive and this is very error-prone unless carefully tested.


If I were to take a wild guess at actually answering your question:

You need to find out the cables which pump out data, but since this is a commercial safe that is mass produced, everything will be on a printed PCB.
So you'll need to test stuff (pcb in-circuit tester??!) on the PCB until you find the data ones and then solder a cable to Arduino analogue in or digital in (should be digital in, not sure).

Re: Need help with idea - Arduino controlled safe

Posted: 03 Aug 2017, 18:03
by tiemeupalso
people love to make things complicated.
why don't you just record your finger putting in the new code on video.with your eyes closed.
then put the video in a "lockit type program where you cant see it.
you didn't see the code being punched in so there is nothing to remember,it is totally random,and only needs your safe, a webcam,and your computer to operate

Re: Need help with idea - Arduino controlled safe

Posted: 04 Aug 2017, 04:08
by Philbert
The locking of the safe is only one part. I'm going to use APIs to program the session, add / remove time, etc.

I'll pick up an Arduino Wifi and mess around with it. It will probably take me 6 months before I figure anything concrete out, but it's fun to experiment.

Re: Need help with idea - Arduino controlled safe

Posted: 04 Aug 2017, 14:58
by occorics
If you solder wires to the battery-contacts, you can control the power of the safe's electronics with the arduino. That way you can use the factory-default code, but still can't open the safe until the power is switched on.

Just make sure to have a spare key somewhere in case the arduino crashes (or know how to open the safe in another way. it's very easy with those cheap safes if you know how the mechanics work... :wink: )

Regarding wifi... I'm not sure if that will work inside a metal box, unless you can connect an external antenna...

Re: Need help with idea - Arduino controlled safe

Posted: 04 Aug 2017, 17:31
by Sir Cumference
What do you really want?

Is it to keep the safe locked, until a given time has passed, and then the code is released, and you can open it?


It is normally prudent to first identify the task, then find possible solutions, the select a doable solution from the possible solutions.

Re: Need help with idea - Arduino controlled safe

Posted: 06 Aug 2017, 03:37
by Philbert
Right now I'm mostly using Emlalock; I use a random code generator to give me 10 different codes, make a single image with all of those codes, and use one of them and program the safe. It helps with the randomization so I won't remember the combination. For the session, I normally start out with short-ish amounts of time but have php code that calls the API that randomly adds time until release. It just makes it more fun as the timer ticks down and then a little bit gets added back on.

I do have a backup set of keys in another locking method to be on the safe side. 1 - this is much safer. 2 - I have modified the spring mechanism in the cheap-o safe so you can't hit the top of the safe to pop the latch anymore.

I had thought about the problem with the wifi inside the safe; my solution would be to run an external antenna to make sure I had a good enough signal.


My initial thought was to have the Arduino call Emlalock for the locking duration management portion, but I could skip that step and have it all self contained. It would require more programming, but it also removes the possibility of an internet outage.

Re: Need help with idea - Arduino controlled safe

Posted: 10 Aug 2017, 01:33
by sweh
So I've been thinking about exactly this problem, on and off, for a while now. One day I'll get around to implementing it.

All it requires is the Arduino USB port to be exposed to the outside (run a cable through one of the mounting holes in the back). This will power and let you control the Arduino. We need the Arduino to expose a serial port on the USB. We'll get to the software running, here, in a moment.

The keypad on the front is disconnected (not needed any more).

I doubt the Arduino can send enough current to activate the solonoid, so we might also need a relay switch. To be determined. Also whether the USB port can send enough power, or if batteries are still needed for the solonoid.

Software on the PC (or Linux machine) can generate a 15 (or longer) character password. Doesn't have to be digits; includes Alpha characters.

This software will send the string to the Arduino. You will ask to verify the safe is locked (lock it in open state). The software will then send the same string to the Arduino. You will verify the safe is unlocked. (Basically this checks the software is working properly and communications are good). You now put the key in the safe close. The software again sends the string; this locks the safe.

The software also makes an image out of this, which is the image you use to create your Emlalock session. Don't look at this image; delete it after uploading. The safe is locked and you can't send the unlock string until you get the image back, read the string and type it in. Good luck guessing 15 character passwords!

Now the Arduino software. This is pretty simple. It works because most Arduino's have some Non-Volatile (EEPROM) memory that the program can read/write. It's normally pretty small (eg 1K in size) but we don't need much. https://www.arduino.cc/en/Tutorial/Memory

So in "unlock state" the software will read a string, write it to EEPROM, and then disable the relay so the solonoid has no power.
In the "lock" state the software will read a string, compare it to the EEPROM. If they match then power the relay which turns on the solonoid which lets you unlock the safe.

This is simple enough it'd work with any of the time keyholder services (Emla, Carli, ...).

If you want APIs, etc, this can all be implemented on the PC; the only thing that needs to be sent to the arduino is the unlock password and that's held in the keyholder site, not locally.

Re: Need help with idea - Arduino controlled safe

Posted: 10 Aug 2017, 12:46
by Keyless
sweh has beaten me to it. I was going to post a very similar idea. It may be that the file you upload to Elmlalock does not have to be a JPG, in which case the program can write the password directly to a text file and that can be uploaded without even looking at it.

An alternative might be to store some details of the session on the Ardunio, under the protection of a password hard coded into the software; release time, whether the outstanding time is visible, punishment for asking to be released early etc. This data could be read and altered by the computer. Of course it should not give you the option to reduce the time but there are all sorts of other possibilities. For example, you could be required to turn on the computer every day and have time added or subtracted according to the temperature in the safe. Overdo the heating and you pay a penalty! You might be required to turn on every morning and type in a short sequence of random characters. Get up late and you pay a penalty!

So that you can't just cheat by altering the computer clock, you would need to get time of day from a web server.

So you do not know the password, create it by a random number generator, cut and paste it into the source code for both the computer and the Arduino, compile, then destroy that copy of the source. If you write the program in C++ it will be very difficult to recover the password – at least I don’t know how to do it.

Note that, with all these schemes, both the computer and the Ardunio can be turned off between operations. You certainly do not want the Arduino to go straight back to looking for a new password if it sees a wrong one. That way you could write a program to keep trying passwords and hope to be lucky. Even a computer would take a while to get 15 or 20 characters right, but if it were me, I would want to know it was not even a possibility.

If you don't want to modify the safe, I think Occorics method of applying power to the key safe electronics and using the default code should work.

Anyone have any more diabolical ideas as to what you could be made to do?

Re: Need help with idea - Arduino controlled safe

Posted: 11 Aug 2017, 00:31
by sweh
Keyless wrote:It may be that the file you upload to Elmlalock does not have to be a JPG, in which case the program can write the password directly to a text file and that can be uploaded without even looking at it.
It's easy enough to create JPG files; eg on Linux:

Code: Select all

echo hello | pbmtext -builtin fixed | /usr/bin/ppmtojpeg > hello.jpg
So that would be the file you upload without looking at it.

Re: Need help with idea - Arduino controlled safe

Posted: 11 Aug 2017, 11:39
by Keyless
sweh wrote: It's easy enough to create JPG files; eg on Linux:

Code: Select all

echo hello | pbmtext -builtin fixed | /usr/bin/ppmtojpeg > hello.jpg
So that would be the file you upload without looking at it.
That's clever. I did wonder if it could be done. If you can do it with a command I think you could get the program to do it automatically. I discovered that you can just rename the file .JPG as well. The file would be a lot smaller than a picture. I suppose it's possible that might upset Elmlalock. Try it and see with a dummy run I suppose.

PS. Even simpler. I think you should be able to write the password into the file <filename>.JPG directly from the program.

Re: Need help with idea - Arduino controlled safe

Posted: 12 Aug 2017, 00:07
by sweh
Keyless wrote:PS. Even simpler. I think you should be able to write the password into the file <filename>.JPG directly from the program.
Right. I simple shell script could work something like:

Code: Select all

#!/bin/bash

# The tty entry the Arduino shows up in
ARD=/dev/ttyUSB0

# Generate a 20 character passcode
code=$(tr -dc '[A-Za-z0-9]' < /dev/urandom | dd bs=1c count=20 2>/dev/null)

# Save it as a JPG file
echo $code | pbmtext -builtin fixed | /usr/bin/ppmtojpeg > unlock_string.jpg

# Send this to the Arduinio
echo $code > $ARD

# Verify it worked OK
echo "Try to lock the safe in an open position; press RETURN if OK or ^C to abort"
read x

# Try and unlock the safe
echo $code > $ARD
echo "Try to open the safe; press RETURN if OK or ^C to abort"
read x

# Final lock
echo $code > $ARD
echo "Lock your keys in the safe and upload unlock_string.jpg to Emlalock"
Of course this is just a simple example, but it shows how simple it is.

Re: Need help with idea - Arduino controlled safe

Posted: 13 Aug 2017, 13:57
by Keyless
I never really got the hang of shell scripts, but it looks as though it's quite simple if you know what you are doing. I had in mind a bit of Java. That should run on the OS that most people seem to like.

Re: Need help with idea - Arduino controlled safe

Posted: 20 Aug 2017, 17:25
by sweh
So I started down this path and wrote a "proof of concept" code the arduino. The Solonoid appears (through googling) to be 6V 350mA, which is a lot more than the 40mA that an Arduino can supply.

But we can use the Arduino to drive a relay, and the 5V supply line appears to be adequate. So with the right wiring (5V+Gnd taken from the supply lines on an Arduino Uno, with 5V connected via a relay) then if I do "writeDigital(pin,LOW)" then the relay clicks, and the solenoid clunks open. So far so good!

But I realised there's a big issue... with the USB port it'd be perfectly possible to send a new sketch to the Arduino that opens the safe. That's not too good! We'd need to use a board that can only be put into programming mode via a jumper and bootloader mode, or have some way to simulate this :-(

Re: Need help with idea - Arduino controlled safe

Posted: 20 Aug 2017, 19:00
by sweh
FWIW, though, here's the basic Arduino code that should(!!) implement the code logic I was thinking of.

A typical session may look something like:

Code: Select all

status::
OK Safe is unlocked
lock:123:
OK Safe locked
status::
OK Safe is locked
open::
ERROR State is locked
unlock:987:
ERROR Wrong password
unlock:123:
OK Safe unlocked
open::
OK opening safe for 5 seconds

I'm not an expert at Arduino, but it seems to work :-)

Code: Select all

// Simple sketch to act as a safe controller
// Safe can be in 2 modes; locked or unlocked
// Commands should be : separated and terminated with a :
//    STATUS::     -- returns the locked/unlocked state
//    LOCK:pswd:   -- sets the lock password, sets the state to locked
//    UNLOCK:pswd: -- Validates teh password, sets the state to unlocked
//    OPEN:x:      -- Opens the safe for x seconds (default to 5 if not sent)
// The password is stored in EEPROM so it is retained over a reboot.  On restart it is read back in.  We have a 
// magic string so random data isn't mis-read as a password.

#include <Arduino.h>
#include <EEPROM.h>

// This is pin D7 on most boards; this is the pin that needs to be connected to the relay
#define pin 7

// Passwords long than this aren't allowed
#define maxpwlen 100

void setup();
void loop();

enum safestate {
  UNLOCKED,
  LOCKED
};

int state=UNLOCKED;
String pswd;

String get_eeprom()
{
  char d[maxpwlen];
  for (int i=0; i<maxpwlen; i++)
  {
    d[i]=EEPROM.read(i);
  }
  return String(d);
}

void set_eeprom(String s)
{
  for(int i=0; i < s.length(); i++)
  {
    EEPROM.write(i,s[i]);
  }
  EEPROM.write(s.length(),0);
}

void setup()
{
  Serial.begin(9600);
  pinMode(pin,OUTPUT);
  digitalWrite(pin,HIGH);

  // Try reading a string from the EEPROM
  pswd=get_eeprom();
  if (pswd.startsWith("PSWD:"))
  {
    state=LOCKED;
    pswd=pswd.substring(5);
  }
  else
  {
    set_eeprom("");
    pswd="";
  }
}

void opensafe(String d)
{
  if (state == LOCKED)
  {
    Serial.println("ERROR State is locked");
  }
  else
  {
    int del=d.toInt();
    if (del==0) { del=5; }
    Serial.println("OK opening safe for " + String(del) + " seconds");
    digitalWrite(pin,LOW);
    delay(del*1000);
    digitalWrite(pin,HIGH);
  }
}

void status()
{
  Serial.print("OK Safe is ");
  if (state==UNLOCKED) { Serial.print("un"); }
  Serial.println("locked");
}

void lock(String val)
{
  if (val.length()>maxpwlen)
  {
    Serial.println("ERROR Password too long");
  }
  else if (state==UNLOCKED)
  {
    set_eeprom("PSWD:"+val);
    pswd=val;
    state=LOCKED;
    Serial.println("OK Safe locked");
  }
  else
  {
    Serial.println("ERROR Safe already locked");
  }
}

void unlock(String val)
{
  if (state==UNLOCKED)
  {
    Serial.println("ERROR Safe already unlocked");
  }
  else if (val != pswd)
  {
    Serial.println("ERROR Wrong password");
  }
  else
  {
    set_eeprom("");
    pswd="";
    state=UNLOCKED;
    Serial.println("OK Safe unlocked");
  }
}

void loop()
{
  String inp;

  while(1)
  {
    String cmd="";
    while (cmd=="")
    {
      cmd=Serial.readStringUntil(':');
      cmd.toUpperCase();
    }
    String val=Serial.readStringUntil(':');
    
    if (cmd=="STATUS") { status(); }
    else if (cmd=="OPEN") { opensafe(val); }
    else if (cmd=="LOCK") { lock(val); }
    else if (cmd=="UNLOCK") { unlock(val); }
    else { Serial.println("ERROR Unknown command: "+cmd); }
  }
}