Toolbox lock

Just a quickie to show what I have done with Blynk so far.

Everything is connected to the internet nowadays so I thought I’d add my toolbox to the list.

I replaced the original lock and mechanism with a couple of cheap rc type servo motors.

Knocked up a quick circuit and threw it in a box and Bob’s your uncle.

The lock and unlock angle can be tweaked using the adjusters at the bottom of the page.

Video on YouTube



/*************************************************************
 * 
 *      Toolbox lock project
 *      Author: B Crawford
 *      Date: 12/04/19
 *      Version: 1.0
 * 
 *************************************************************/

#define servoPin D8
#define relayPin D6

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>

char auth[] = "XXXX";
char ssid[] = "XXXX";
char pass[] = "XXXX";

Servo servo;
WidgetLCD lcd(V0);
BlynkTimer timer;

bool locked = false;
int unlockedPos = 165;
int lockedPos = 15;
bool transition = false;

BLYNK_WRITE(V1){
  if (param.asInt() == 1){
    lock();
  }
}

BLYNK_WRITE(V2){
  if (param.asInt() == 1){
    unlock();
  }
}

BLYNK_WRITE(V3)
{
  lockedPos = param.asInt();
}

BLYNK_WRITE(V4)
{
  unlockedPos = param.asInt();
}

void setup()
{
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);
  Blynk.begin(auth, ssid, pass);
  Blynk.syncAll();
  lcd.clear();
  timer.setTimeout(1000L, lock);
}

void loop()
{
  Blynk.run();
  timer.run();
}

void lock()
{
  if (locked == false && transition == false)
  {
    servoOn();
    timer.setTimeout(250L, moveServo, (void*)lockedPos);
  }
}

void unlock()
{
  if (locked == true && transition == false)
  {
    servoOn();
    timer.setTimeout(250L, moveServo, (void*)unlockedPos);
  }
}

void servoOn()
{
  servo.attach(servoPin);
  digitalWrite(relayPin, HIGH);
  transition = true;
}

void moveServo(void* pos)
{
  int sPos = (int)pos;
  servo.write(sPos);
  timer.setTimeout(1500L, servoOff);
}

void servoOff()
{
  int servoPos = servo.read();
  if (servoPos == unlockedPos)
  {
    locked = false;
  }
  else if (servoPos == lockedPos)
  {
    locked = true;
  }
  
  digitalWrite(relayPin, LOW);
  servo.detach();
  setLcdStatus();
}

void setLcdStatus()
{
  lcd.clear();
  switch (locked)
  {
    case false:
      lcd.print(0,0,"Status:");
      lcd.print(0,1,"Unlocked");
      break;
    case true:
      lcd.print(0,0,"Status:");
      lcd.print(0,1,"Locked");
    break;
  }
  transition = false;
}

nice, but you you just published your auth code, so that allows us to keep locking your toolbox.
I suggest you get a new code and delete it (and your wifi credentials) from your post!

2 Likes

Just my luck! haha - I even thought to myself about removing them before posting and completely forgot.

Edited now, yeah I think I’ll get a new code.

1 Like

Thanks for the pics… Curious what is the purpose of the relay in your control box?

1 Like

@daveblynk, it’s just to kill power to the servo’s when not in use.

  1. to save power
  2. to stop any jitter from the servos
1 Like

I use the Servo Detach command to do the same thing.

2 Likes

I use that too in my code but redundancy doesn’t hurt :slight_smile: Probably a waste though :crazy_face:

1 Like

I guess it comes down to the need to save power… the relay obviously draws extra current when active, but does the servo draw measurable current when NOT active?

To be fair I didn’t even measure the idle current draw after using servo detach().

Maybe I will one day? But yes the relay will draw around 100mA when in use, but it is only short term while the servos are moving and then will switch off again.

Oh and I have 2 servos connected in parallel to lift/lower each locking bar.

1 Like