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

Committer:
ewilliams61
Date:
Tue Mar 15 19:36:01 2016 +0000
Revision:
8:39172c01c0f1
Parent:
7:79d0b30fedb4
Child:
9:c216d7b63f92
Works :)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:59bec1fd956e 1 #include "mbed.h"
ewilliams61 7:79d0b30fedb4 2 #include <mpr121.h>
ewilliams61 7:79d0b30fedb4 3 #include <string>
ewilliams61 7:79d0b30fedb4 4 #include "Shiftbrite.h"
ewilliams61 8:39172c01c0f1 5 //Print output to pc via usb
sam_grove 5:96cb82af9996 6 RawSerial pc(USBTX, USBRX);
ewilliams61 8:39172c01c0f1 7 //Print output to Bluetooth device
ewilliams61 7:79d0b30fedb4 8 RawSerial dev(p9,p10);
ewilliams61 8:39172c01c0f1 9 //LEDs
sam_grove 5:96cb82af9996 10 DigitalOut led1(LED1);
ewilliams61 7:79d0b30fedb4 11 DigitalOut led2(LED2);
ewilliams61 7:79d0b30fedb4 12 DigitalOut led3(LED3);
ewilliams61 8:39172c01c0f1 13 DigitalOut led4(LED4);
ewilliams61 7:79d0b30fedb4 14
ewilliams61 8:39172c01c0f1 15 //Interrupt for Touchpad
ewilliams61 8:39172c01c0f1 16 InterruptIn interrupt(p26);
ewilliams61 8:39172c01c0f1 17 //I2C for Touchpad
ewilliams61 8:39172c01c0f1 18 I2C i2c(p28, p27);
ewilliams61 8:39172c01c0f1 19 //Touchpad
ewilliams61 8:39172c01c0f1 20 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
ewilliams61 8:39172c01c0f1 21 //Shiftbrite
ewilliams61 8:39172c01c0f1 22 Shiftbrite myShiftbrite(p16, p15, p11, p12, p13);
ewilliams61 8:39172c01c0f1 23
ewilliams61 8:39172c01c0f1 24 //Code Combinations
ewilliams61 7:79d0b30fedb4 25 int code1[] = {1,1,1,1};
ewilliams61 7:79d0b30fedb4 26 int code2[] = {2,2,2,2};
ewilliams61 8:39172c01c0f1 27 int code3[] = {8,8,8,8};//Key 4 (labeled 3) on touchpad
ewilliams61 7:79d0b30fedb4 28
ewilliams61 8:39172c01c0f1 29 volatile int input[4]; //stores input values
ewilliams61 8:39172c01c0f1 30 volatile int inNdx = 0;//to place combination in input
ewilliams61 8:39172c01c0f1 31 bool codeIn = false; //determine if code is being entered via bluetooth
ewilliams61 7:79d0b30fedb4 32
ewilliams61 8:39172c01c0f1 33
ewilliams61 8:39172c01c0f1 34 //Interrupt method to read from touchpad
ewilliams61 7:79d0b30fedb4 35 void fallInterrupt() {
ewilliams61 7:79d0b30fedb4 36 int key_code=0;
ewilliams61 7:79d0b30fedb4 37 int i = 0;
ewilliams61 7:79d0b30fedb4 38 int value=mpr121.read(0x00);
ewilliams61 7:79d0b30fedb4 39 value +=mpr121.read(0x01)<<8;
ewilliams61 8:39172c01c0f1 40 //output value to onboard LEDs
ewilliams61 7:79d0b30fedb4 41 for (i=0; i<12; i++) {
ewilliams61 7:79d0b30fedb4 42 if (((value>>i)&0x01)==1) key_code=i+1;
ewilliams61 7:79d0b30fedb4 43 }
ewilliams61 7:79d0b30fedb4 44 led4=key_code & 0x01;
ewilliams61 7:79d0b30fedb4 45 led3=(key_code>>1) & 0x01;
ewilliams61 7:79d0b30fedb4 46 led2=(key_code>>2) & 0x01;
ewilliams61 7:79d0b30fedb4 47 led1=(key_code>>3) & 0x01;
ewilliams61 8:39172c01c0f1 48 //ignore second character and input value into inputNdx
ewilliams61 7:79d0b30fedb4 49 if (value > 0){
ewilliams61 7:79d0b30fedb4 50 input[inNdx] = value;
ewilliams61 7:79d0b30fedb4 51 inNdx++;
ewilliams61 7:79d0b30fedb4 52 }
ewilliams61 7:79d0b30fedb4 53
ewilliams61 7:79d0b30fedb4 54
ewilliams61 7:79d0b30fedb4 55
ewilliams61 7:79d0b30fedb4 56 }
mbedAustin 2:a8dcb07a1d00 57
ewilliams61 8:39172c01c0f1 58 //receive value from bluetooth device
sam_grove 5:96cb82af9996 59 void dev_recv()
mbedAustin 2:a8dcb07a1d00 60 {
ewilliams61 7:79d0b30fedb4 61 string temp ="";
sam_grove 5:96cb82af9996 62 led1 = !led1;
sam_grove 5:96cb82af9996 63 while(dev.readable()) {
ewilliams61 7:79d0b30fedb4 64 //pc.putc(dev.getc());
ewilliams61 7:79d0b30fedb4 65 if(!codeIn){
ewilliams61 7:79d0b30fedb4 66 if(dev.getc() == 'a'){
ewilliams61 7:79d0b30fedb4 67 dev.printf("Send code 4 digit code.\n");
ewilliams61 7:79d0b30fedb4 68 inNdx = 0;
ewilliams61 7:79d0b30fedb4 69 codeIn = true;
ewilliams61 7:79d0b30fedb4 70 // myShiftbrite.write(0,0,255);
ewilliams61 7:79d0b30fedb4 71 }
ewilliams61 7:79d0b30fedb4 72 else {dev.printf("Command not recognized");}
ewilliams61 7:79d0b30fedb4 73 } else {
ewilliams61 7:79d0b30fedb4 74 // pc.printf("BT entered: %c\n", dev.getc());
ewilliams61 7:79d0b30fedb4 75 input[inNdx] = dev.getc() - '0';
ewilliams61 7:79d0b30fedb4 76 if(input[inNdx] > -1){
ewilliams61 7:79d0b30fedb4 77 dev.printf("Just entered: %d", input[inNdx]);
ewilliams61 7:79d0b30fedb4 78 inNdx++;}
ewilliams61 7:79d0b30fedb4 79 //break;
ewilliams61 7:79d0b30fedb4 80 }
sam_grove 5:96cb82af9996 81 }
ewilliams61 7:79d0b30fedb4 82
sam_grove 5:96cb82af9996 83 }
ewilliams61 8:39172c01c0f1 84 //receive value from PC
sam_grove 5:96cb82af9996 85 void pc_recv()
sam_grove 5:96cb82af9996 86 {
sam_grove 5:96cb82af9996 87 led4 = !led4;
sam_grove 5:96cb82af9996 88 while(pc.readable()) {
mbedAustin 4:ba9100d52e48 89 dev.putc(pc.getc());
mbedAustin 0:59bec1fd956e 90 }
mbedAustin 0:59bec1fd956e 91 }
mbedAustin 4:ba9100d52e48 92
mbedAustin 4:ba9100d52e48 93 int main()
mbedAustin 4:ba9100d52e48 94 {
ewilliams61 8:39172c01c0f1 95 //reset Shiftbrite
ewilliams61 7:79d0b30fedb4 96 myShiftbrite.write(0,0,0);
mbedAustin 4:ba9100d52e48 97 pc.baud(9600);
mbedAustin 4:ba9100d52e48 98 dev.baud(9600);
ewilliams61 8:39172c01c0f1 99 //Set up interrupt
ewilliams61 7:79d0b30fedb4 100 interrupt.fall(&fallInterrupt);
ewilliams61 7:79d0b30fedb4 101 interrupt.mode(PullUp);
ewilliams61 8:39172c01c0f1 102 //Set up read methods bluetooth and PC
sam_grove 5:96cb82af9996 103 pc.attach(&pc_recv, Serial::RxIrq);
sam_grove 5:96cb82af9996 104 dev.attach(&dev_recv, Serial::RxIrq);
ewilliams61 8:39172c01c0f1 105
ewilliams61 7:79d0b30fedb4 106 dev.printf("Monitoring Locked Device\n");
ewilliams61 7:79d0b30fedb4 107 dev.printf("To unlock the device remotely, send 'a'.\n");
sam_grove 5:96cb82af9996 108
mbedAustin 4:ba9100d52e48 109 while(1) {
ewilliams61 7:79d0b30fedb4 110 wait(5);
ewilliams61 8:39172c01c0f1 111 //If three characters have been entered, attempt to unlock
ewilliams61 7:79d0b30fedb4 112 if (inNdx > 3) {
ewilliams61 7:79d0b30fedb4 113 dev.printf("Attempting to Unlock\n");
ewilliams61 7:79d0b30fedb4 114 codeIn = false;
ewilliams61 7:79d0b30fedb4 115 bool c1 = true; bool c2 = true; bool c3 = true;
ewilliams61 8:39172c01c0f1 116 //check entered code against saved codes
ewilliams61 7:79d0b30fedb4 117 for(int x = 0; x <= 3; x++){
ewilliams61 8:39172c01c0f1 118 //if no code matches, end loop
ewilliams61 7:79d0b30fedb4 119 if(!(c1 | c2 | c3)) {
ewilliams61 7:79d0b30fedb4 120 break;
ewilliams61 7:79d0b30fedb4 121 } else {
ewilliams61 8:39172c01c0f1 122 //compare codes
ewilliams61 7:79d0b30fedb4 123 if (code1[x] != input[x]){c1 = false;}
ewilliams61 7:79d0b30fedb4 124 if (code2[x] != input[x]){c2 = false;}
ewilliams61 7:79d0b30fedb4 125 if (code3[x] != input[x]){c3 = false;}
ewilliams61 7:79d0b30fedb4 126
ewilliams61 7:79d0b30fedb4 127 }
ewilliams61 7:79d0b30fedb4 128 }
ewilliams61 7:79d0b30fedb4 129 if(c1){
ewilliams61 8:39172c01c0f1 130 //Send welcome user 1 to bluetooth
ewilliams61 7:79d0b30fedb4 131 dev.printf("Welcome User 1\n");
ewilliams61 7:79d0b30fedb4 132 myShiftbrite.write(0,255,0);
ewilliams61 7:79d0b30fedb4 133 } else if(c2) {
ewilliams61 8:39172c01c0f1 134 //Send welcome user 2 to bluetooth
ewilliams61 7:79d0b30fedb4 135 dev.printf("Welcome User 2\n");
ewilliams61 7:79d0b30fedb4 136 myShiftbrite.write(0,255,0);
ewilliams61 7:79d0b30fedb4 137 } else if(c3) {
ewilliams61 8:39172c01c0f1 138 //Send welcome user 3 to bluetooth
ewilliams61 7:79d0b30fedb4 139 dev.printf("Welcome User 3\n");
ewilliams61 7:79d0b30fedb4 140 myShiftbrite.write(0,255,0);
ewilliams61 7:79d0b30fedb4 141 } else {
ewilliams61 8:39172c01c0f1 142 //Send welcome Try Again message to bluetooth
ewilliams61 7:79d0b30fedb4 143 dev.printf("Try Again\n");
ewilliams61 7:79d0b30fedb4 144 myShiftbrite.write(255,0,0);
ewilliams61 7:79d0b30fedb4 145
ewilliams61 7:79d0b30fedb4 146 }
ewilliams61 8:39172c01c0f1 147 //reset indexing variable when checking is complete
ewilliams61 7:79d0b30fedb4 148 inNdx = 0;
ewilliams61 7:79d0b30fedb4 149
ewilliams61 7:79d0b30fedb4 150 }
mbedAustin 4:ba9100d52e48 151 }
ewilliams61 7:79d0b30fedb4 152
mbedAustin 4:ba9100d52e48 153 }