The purpose of this project was to create a system that would allow users to monitor a locked device using a Bluetooth device. This Bluetooth device will show the last user that unlocked the device, and also allows the user to unlock the device using the Bluetooth device. This device can be physically unlocked using a capacitive touch keypad sensor.

Dependencies:   mbed Motor Servo

Fork of SerialPassthrough_LPC1768 by jim hamblen

main.cpp

Committer:
ewilliams61
Date:
2016-03-15
Revision:
8:39172c01c0f1
Parent:
7:79d0b30fedb4
Child:
9:c216d7b63f92

File content as of revision 8:39172c01c0f1:

#include "mbed.h"
#include <mpr121.h>
#include <string>
#include "Shiftbrite.h"
//Print output to pc via usb
RawSerial  pc(USBTX, USBRX);
//Print output to Bluetooth device
RawSerial  dev(p9,p10);
//LEDs
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

//Interrupt for Touchpad
InterruptIn interrupt(p26);
//I2C for Touchpad 
I2C i2c(p28, p27);
//Touchpad 
Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
//Shiftbrite
Shiftbrite myShiftbrite(p16, p15, p11, p12, p13);

//Code Combinations
int code1[] = {1,1,1,1};
int code2[] = {2,2,2,2};
int code3[] = {8,8,8,8};//Key 4 (labeled 3) on touchpad

volatile int input[4]; //stores input values
volatile int inNdx = 0;//to place combination in input
bool codeIn = false; //determine if code is being entered via bluetooth


//Interrupt method to read from touchpad
void fallInterrupt() {
       int key_code=0;
       int i = 0;
    int value=mpr121.read(0x00);
    value +=mpr121.read(0x01)<<8;
    //output value to onboard LEDs
    for (i=0; i<12; i++) {
        if (((value>>i)&0x01)==1) key_code=i+1;
        }
    led4=key_code & 0x01;
    led3=(key_code>>1) & 0x01;
    led2=(key_code>>2) & 0x01;
    led1=(key_code>>3) & 0x01;
    //ignore second character and input value into inputNdx
    if (value > 0){
    input[inNdx] = value;
    inNdx++;  
    }

  
    
}

//receive value from bluetooth device
void dev_recv()
{
    string temp ="";
    led1 = !led1;
    while(dev.readable()) {
        //pc.putc(dev.getc());
        if(!codeIn){
            if(dev.getc() == 'a'){
                dev.printf("Send code 4 digit code.\n"); 
                inNdx = 0;
                codeIn = true;
               // myShiftbrite.write(0,0,255);
            } 
            else {dev.printf("Command not recognized");}
        } else {
        //    pc.printf("BT entered: %c\n", dev.getc());
            input[inNdx] = dev.getc() - '0';
            if(input[inNdx] > -1){  
            dev.printf("Just entered: %d", input[inNdx]);  
            inNdx++;}
            //break;
        }
    }
    
}
//receive value from PC
void pc_recv()
{
    led4 = !led4;
    while(pc.readable()) {
        dev.putc(pc.getc());
    }
}

int main()
{
    //reset Shiftbrite
    myShiftbrite.write(0,0,0);
    pc.baud(9600);
    dev.baud(9600);
    //Set up interrupt
    interrupt.fall(&fallInterrupt);
    interrupt.mode(PullUp);
    //Set up read methods bluetooth and PC
    pc.attach(&pc_recv, Serial::RxIrq);
    dev.attach(&dev_recv, Serial::RxIrq);
    
    dev.printf("Monitoring Locked Device\n");
    dev.printf("To unlock the device remotely, send 'a'.\n");

    while(1) {
         wait(5);
        //If three characters have been entered, attempt to unlock
        if (inNdx > 3) {
            dev.printf("Attempting to Unlock\n");
            codeIn = false;
            bool c1 = true; bool c2 = true; bool c3 = true;
            //check entered code against saved codes
            for(int x = 0; x <= 3; x++){
                //if no code matches, end loop
                if(!(c1 | c2 | c3)) {
                    break;    
                } else {
                    //compare codes
                    if (code1[x] != input[x]){c1 = false;}
                    if (code2[x] != input[x]){c2 = false;}
                    if (code3[x] != input[x]){c3 = false;}
                    
                }   
            }
            if(c1){
                //Send welcome user 1 to bluetooth
                dev.printf("Welcome User 1\n");
                myShiftbrite.write(0,255,0);
            } else if(c2) {
                //Send welcome user 2 to bluetooth
                dev.printf("Welcome User 2\n");
                myShiftbrite.write(0,255,0);
            } else if(c3) {
                //Send welcome user 3 to bluetooth
                dev.printf("Welcome User 3\n");
                myShiftbrite.write(0,255,0);
            } else {
                //Send welcome Try Again message to bluetooth
                dev.printf("Try Again\n");
                myShiftbrite.write(255,0,0); 
 
            }
            //reset indexing variable when checking is complete
            inNdx = 0;
            
        }
    }
    
}