Door Lock Project

Hey everyone,

So I am super excited that my spark core came in the mail. I already had a project in mind that I wanted to implement: a wi-fi door lock/unlock. I made some assumptions that turned out to be incorrect:

I assumed that the Tinker app was the direct interface to the core (which it technically is), but not strictly for prototyping.

I assumed that you could write some code, upload it to the Spark core, and then still control the pins using the Tinker app in conjunction with the code that was loaded on to the core.

So, as I have come to learn, this is not how the Spark core works. I wanted to be able to trigger a servo, one that is connected to a basic sliding door lock, to be either in a lock or unlock position based on a high or low pin signal. I thought that I could control this pin signal using the Tinker app, with my servoLock code already loaded on to it.

My question is, how do you send signals to the spark core over wifi if you can’t use the Tinker app?

Here is my code if you want to see what I was trying to do:

#define LOCKPIN 6
#define KEYPIN 7

Servo doorLock;
const int locked = 180;
const int unlocked = 0;
bool unlockDoor = false;

void checkKeyStatus();
void doorHandle(bool unlockDoor);

void setup() {
    doorLock.attach(A7);
    pinMode(LOCKPIN, INPUT);
    pinMode(KEYPIN, OUTPUT);
}

void loop() {
    
    checkKeyStatus();
    doorHandle(unlockDoor);
    delay(500);
    
}

void checkKeyStatus(){
    int key = digitalRead(KEYPIN);
    
    if (key){
        unlockDoor = true;
    }
    else {
        unlockDoor = false;
    }
}

void doorHandle(bool unlockDoor){
    if (unlockDoor){
        doorLock.write(unlocked);
    }
    else {
        doorLock.write(locked);
    }
    
}

KEYPIN is the pin that I wanted to set high or low using Tinker, and LOCKPIN was the pin that was going to read the value on KEYPIN by shorting the two.

Thanks for your help/input!

Hi @Mohrad,

Sounds like a fun project! The stock Tinker app is just another firmware you can load on your core. If you want to add your own custom functionality to it, you can just start with tinker and start adding functions. Just grab the source from here and hack away! – If you leave in the digitalwrite / etc, you can keep controlling your core from your phone.

https://github.com/spark/core-firmware/blob/master/src/application.cpp

A bunch of community members have also made great example apps for controlling custom projects, for example:

I think in this case you just want to hook your functions up to the API as well:

//in setup
Spark.function("checkKeyStatus", checkKeyStatus);
Spark.function("doorHandle", doorHandle);

//elsewhere
int checkKeyStatus(String args) {
	//
}

int doorHandle(String args) {
	//
}

Hope this helps!
David

1 Like

THANK YOU!!

This is exactly what I needed! I’ll post some pics once I finish the project.

1 Like