IoT for mbed1

Dependencies:   4DGL-uLCD-SE IoTsecuritySys PinDetect mbed-rtos mbed

Fork of IoT by Tal Landes

Committer:
jsmith352
Date:
Tue Nov 24 20:15:53 2015 +0000
Revision:
0:04dcbfb4388c
Child:
1:7f873efe5b11
Touch pad door lock with two phase authentication

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jsmith352 0:04dcbfb4388c 1 #include <mbed.h>
jsmith352 0:04dcbfb4388c 2 #include <mpr121.h>
jsmith352 0:04dcbfb4388c 3 #include <stdlib.h>
jsmith352 0:04dcbfb4388c 4 #include "PinDetect.h"
jsmith352 0:04dcbfb4388c 5
jsmith352 0:04dcbfb4388c 6 /* CODE_LENGTH needs to be double the amount of numbers you
jsmith352 0:04dcbfb4388c 7 want in your authenticator/passcode because of the way interrupt.fall(&fallInterrupt)
jsmith352 0:04dcbfb4388c 8 works. It is called twice every time an interrupt is detected.
jsmith352 0:04dcbfb4388c 9 The extra numbers in the array will just be filled with zeros and ignored in checking
jsmith352 0:04dcbfb4388c 10 code sequences, so they will not matter either way */
jsmith352 0:04dcbfb4388c 11 /* i.e, you want a code with 7 numbers, CODE_LENGTH needs to be 14 */
jsmith352 0:04dcbfb4388c 12 #define CODE_LENGTH 14
jsmith352 0:04dcbfb4388c 13
jsmith352 0:04dcbfb4388c 14 DigitalOut led1(LED1);
jsmith352 0:04dcbfb4388c 15 DigitalOut led2(LED2);
jsmith352 0:04dcbfb4388c 16 DigitalOut led3(LED3);
jsmith352 0:04dcbfb4388c 17 DigitalOut led4(LED4);
jsmith352 0:04dcbfb4388c 18
jsmith352 0:04dcbfb4388c 19 DigitalOut doorlock(p29);
jsmith352 0:04dcbfb4388c 20
jsmith352 0:04dcbfb4388c 21 // Create the interrupt receiver object on pin 26
jsmith352 0:04dcbfb4388c 22 InterruptIn interrupt(p30);
jsmith352 0:04dcbfb4388c 23
jsmith352 0:04dcbfb4388c 24 // Setup the i2c bus on pins 9 and 10
jsmith352 0:04dcbfb4388c 25 I2C i2c(p9, p10);
jsmith352 0:04dcbfb4388c 26
jsmith352 0:04dcbfb4388c 27 // Setup the Mpr121:
jsmith352 0:04dcbfb4388c 28 // constructor(i2c object, i2c address of the mpr121)
jsmith352 0:04dcbfb4388c 29 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
jsmith352 0:04dcbfb4388c 30
jsmith352 0:04dcbfb4388c 31 // pc serial communication for testing
jsmith352 0:04dcbfb4388c 32 Serial pc(USBTX, USBRX);
jsmith352 0:04dcbfb4388c 33
jsmith352 0:04dcbfb4388c 34 // ***** GLOBALS ***** //
jsmith352 0:04dcbfb4388c 35 // Timer is to seed rand
jsmith352 0:04dcbfb4388c 36 Timer t1;
jsmith352 0:04dcbfb4388c 37 // code counter is the next position in inputCode array
jsmith352 0:04dcbfb4388c 38 int codeCounter;
jsmith352 0:04dcbfb4388c 39 // inputCode array is the sequence of numbers the user will enter
jsmith352 0:04dcbfb4388c 40 int inputCode[CODE_LENGTH];
jsmith352 0:04dcbfb4388c 41 bool code_enabled;
jsmith352 0:04dcbfb4388c 42
jsmith352 0:04dcbfb4388c 43 // Key hit/release interrupt routine
jsmith352 0:04dcbfb4388c 44 void fallInterrupt() {
jsmith352 0:04dcbfb4388c 45 int key_code=0;
jsmith352 0:04dcbfb4388c 46 int i=0;
jsmith352 0:04dcbfb4388c 47 int value=mpr121.read(0x00);
jsmith352 0:04dcbfb4388c 48 value +=mpr121.read(0x01)<<8;
jsmith352 0:04dcbfb4388c 49 // LED demo mod
jsmith352 0:04dcbfb4388c 50 i=0;
jsmith352 0:04dcbfb4388c 51 // puts key number out to LEDs for demo
jsmith352 0:04dcbfb4388c 52 for (i=0; i<12; i++) {
jsmith352 0:04dcbfb4388c 53 if (((value>>i)&0x01)==1) key_code=i+1;
jsmith352 0:04dcbfb4388c 54 }
jsmith352 0:04dcbfb4388c 55 led4=key_code & 0x01;
jsmith352 0:04dcbfb4388c 56 led3=(key_code>>1) & 0x01;
jsmith352 0:04dcbfb4388c 57 led2=(key_code>>2) & 0x01;
jsmith352 0:04dcbfb4388c 58 led1=(key_code>>3) & 0x01;
jsmith352 0:04dcbfb4388c 59
jsmith352 0:04dcbfb4388c 60 // save the keypress to inputCode array
jsmith352 0:04dcbfb4388c 61 if(codeCounter < CODE_LENGTH){
jsmith352 0:04dcbfb4388c 62 // ignore odd numbers
jsmith352 0:04dcbfb4388c 63 if(codeCounter % 2 != 0){
jsmith352 0:04dcbfb4388c 64 inputCode[codeCounter] = 0;
jsmith352 0:04dcbfb4388c 65 }
jsmith352 0:04dcbfb4388c 66 // only save the even numbers (see lines 6-10)
jsmith352 0:04dcbfb4388c 67 else{
jsmith352 0:04dcbfb4388c 68 inputCode[codeCounter] = key_code - 1;
jsmith352 0:04dcbfb4388c 69 pc.printf("codeCounter: %d -- code: %d\n\r", codeCounter, key_code - 1);
jsmith352 0:04dcbfb4388c 70 }
jsmith352 0:04dcbfb4388c 71 codeCounter++;
jsmith352 0:04dcbfb4388c 72 }
jsmith352 0:04dcbfb4388c 73 }
jsmith352 0:04dcbfb4388c 74
jsmith352 0:04dcbfb4388c 75 // generate randomized code
jsmith352 0:04dcbfb4388c 76 void generate_random_code(int (&codeArray)[CODE_LENGTH]){
jsmith352 0:04dcbfb4388c 77 int i = 0;
jsmith352 0:04dcbfb4388c 78 // only care about the even numbers (see lines 6-10)
jsmith352 0:04dcbfb4388c 79 pc.printf("NEW CODE: ");
jsmith352 0:04dcbfb4388c 80 for(i = 0; i < CODE_LENGTH; i+=2){
jsmith352 0:04dcbfb4388c 81 srand(t1.read_us());
jsmith352 0:04dcbfb4388c 82 codeArray[i] = rand() % 12;
jsmith352 0:04dcbfb4388c 83 pc.printf("%d, ", codeArray[i]);
jsmith352 0:04dcbfb4388c 84 }
jsmith352 0:04dcbfb4388c 85 pc.printf("\n\r");
jsmith352 0:04dcbfb4388c 86 }
jsmith352 0:04dcbfb4388c 87
jsmith352 0:04dcbfb4388c 88 // check if the code entered is the correct code
jsmith352 0:04dcbfb4388c 89 bool check_code_sequence(int (&codeArray)[CODE_LENGTH]){
jsmith352 0:04dcbfb4388c 90 int i = 0;
jsmith352 0:04dcbfb4388c 91 int j = 0;
jsmith352 0:04dcbfb4388c 92 // only care about the even numbers (see lines 6-10)
jsmith352 0:04dcbfb4388c 93 for(i = 0; i < CODE_LENGTH; i+=2){
jsmith352 0:04dcbfb4388c 94 if(inputCode[i] == codeArray[i])
jsmith352 0:04dcbfb4388c 95 j++; // count the number of right numbers
jsmith352 0:04dcbfb4388c 96 }
jsmith352 0:04dcbfb4388c 97 if(j == CODE_LENGTH/2)
jsmith352 0:04dcbfb4388c 98 return(true);
jsmith352 0:04dcbfb4388c 99 else
jsmith352 0:04dcbfb4388c 100 return(false);
jsmith352 0:04dcbfb4388c 101 }
jsmith352 0:04dcbfb4388c 102
jsmith352 0:04dcbfb4388c 103 int main() {
jsmith352 0:04dcbfb4388c 104 interrupt.fall(&fallInterrupt);
jsmith352 0:04dcbfb4388c 105 interrupt.mode(PullUp);
jsmith352 0:04dcbfb4388c 106
jsmith352 0:04dcbfb4388c 107 // authenticator is the randomly generated sequence of numbers by the machine
jsmith352 0:04dcbfb4388c 108 // the user has to match this sequence to gain access, used for phase 2
jsmith352 0:04dcbfb4388c 109 int authenticator[CODE_LENGTH];
jsmith352 0:04dcbfb4388c 110 // passcode is the user's personal passcode, used for phase 1
jsmith352 0:04dcbfb4388c 111 int passcode[CODE_LENGTH] = {0,0,1,0,2,0,3,0,4,0,5,0,6,0};
jsmith352 0:04dcbfb4388c 112 codeCounter = 0;
jsmith352 0:04dcbfb4388c 113 bool pass = false;
jsmith352 0:04dcbfb4388c 114
jsmith352 0:04dcbfb4388c 115 // these 2 variables tell the machine when to generate a new random authentication code
jsmith352 0:04dcbfb4388c 116 int new_code_timer = 0;
jsmith352 0:04dcbfb4388c 117 int new_code_counter = 0;
jsmith352 0:04dcbfb4388c 118 // this tells the state machine with phase of authentication we are in
jsmith352 0:04dcbfb4388c 119 code_enabled = false;
jsmith352 0:04dcbfb4388c 120
jsmith352 0:04dcbfb4388c 121 for(int i = 0; i < CODE_LENGTH; i++){
jsmith352 0:04dcbfb4388c 122 authenticator[i] = 0;
jsmith352 0:04dcbfb4388c 123 inputCode[i] = 0;
jsmith352 0:04dcbfb4388c 124 }
jsmith352 0:04dcbfb4388c 125
jsmith352 0:04dcbfb4388c 126 // go ahead and start the timer so that when a random code is generated,
jsmith352 0:04dcbfb4388c 127 // the seed will always be random, unlike the predecessor version
jsmith352 0:04dcbfb4388c 128 t1.start();
jsmith352 0:04dcbfb4388c 129
jsmith352 0:04dcbfb4388c 130 // while loop constantly checks if the entered code sequence is right or wrong,
jsmith352 0:04dcbfb4388c 131 // given that the correct amount of numbers were entered
jsmith352 0:04dcbfb4388c 132 pc.printf("Please Enter Your Personal Security Code\n\r");
jsmith352 0:04dcbfb4388c 133 while (1){
jsmith352 0:04dcbfb4388c 134 switch(code_enabled){
jsmith352 0:04dcbfb4388c 135 case false:
jsmith352 0:04dcbfb4388c 136 if(codeCounter >= CODE_LENGTH){
jsmith352 0:04dcbfb4388c 137 pass = check_code_sequence(passcode);
jsmith352 0:04dcbfb4388c 138 if(pass == true){
jsmith352 0:04dcbfb4388c 139 pc.printf("SENDING AUTHENTICATION CODE...\n\r");
jsmith352 0:04dcbfb4388c 140 generate_random_code(authenticator);
jsmith352 0:04dcbfb4388c 141 t1.stop(); // reset the time
jsmith352 0:04dcbfb4388c 142 t1.reset(); // so that it is an even
jsmith352 0:04dcbfb4388c 143 t1.start(); // 30 seconds before 1st new code is generated
jsmith352 0:04dcbfb4388c 144 codeCounter = 0;
jsmith352 0:04dcbfb4388c 145 code_enabled = true;
jsmith352 0:04dcbfb4388c 146 }
jsmith352 0:04dcbfb4388c 147 else{
jsmith352 0:04dcbfb4388c 148 pc.printf("WRONG passcode\n\r");
jsmith352 0:04dcbfb4388c 149 codeCounter = 0;
jsmith352 0:04dcbfb4388c 150 }
jsmith352 0:04dcbfb4388c 151 }
jsmith352 0:04dcbfb4388c 152 break;
jsmith352 0:04dcbfb4388c 153 case true:
jsmith352 0:04dcbfb4388c 154 if(codeCounter >= CODE_LENGTH){
jsmith352 0:04dcbfb4388c 155 pass = check_code_sequence(authenticator);
jsmith352 0:04dcbfb4388c 156 if(pass == true){
jsmith352 0:04dcbfb4388c 157 pc.printf("ACCESS GRANTED\n\r");
jsmith352 0:04dcbfb4388c 158 doorlock = 1;
jsmith352 0:04dcbfb4388c 159 wait(5);
jsmith352 0:04dcbfb4388c 160 doorlock = 0;
jsmith352 0:04dcbfb4388c 161 pc.printf("Resetting....\n\r");
jsmith352 0:04dcbfb4388c 162 pc.printf("\n\n\rPlease Enter Your Personal Security Code\n\r");
jsmith352 0:04dcbfb4388c 163 codeCounter = 0;
jsmith352 0:04dcbfb4388c 164 code_enabled = false;
jsmith352 0:04dcbfb4388c 165 }
jsmith352 0:04dcbfb4388c 166 else{
jsmith352 0:04dcbfb4388c 167 pc.printf("ACCESS DENIED\n\r");
jsmith352 0:04dcbfb4388c 168 codeCounter = 0;
jsmith352 0:04dcbfb4388c 169 }
jsmith352 0:04dcbfb4388c 170 }
jsmith352 0:04dcbfb4388c 171 // this code generates a new authentication code every 30 seconds (30000 ms)
jsmith352 0:04dcbfb4388c 172 new_code_timer = (int)(t1.read_ms()/30000);
jsmith352 0:04dcbfb4388c 173 if(new_code_timer > new_code_counter){
jsmith352 0:04dcbfb4388c 174 new_code_counter++;
jsmith352 0:04dcbfb4388c 175 generate_random_code(authenticator);
jsmith352 0:04dcbfb4388c 176 codeCounter = 0;
jsmith352 0:04dcbfb4388c 177 }
jsmith352 0:04dcbfb4388c 178 break;
jsmith352 0:04dcbfb4388c 179 }
jsmith352 0:04dcbfb4388c 180 // reset the timer when the number gets too high, should last about 35 minutes
jsmith352 0:04dcbfb4388c 181 // We do this because int can only hold a number up to 2^32 - 1, preventing errors
jsmith352 0:04dcbfb4388c 182 if(t1.read_us() > 2147400000){
jsmith352 0:04dcbfb4388c 183 t1.stop();
jsmith352 0:04dcbfb4388c 184 t1.reset();
jsmith352 0:04dcbfb4388c 185 new_code_timer = 0;
jsmith352 0:04dcbfb4388c 186 new_code_counter = 0;
jsmith352 0:04dcbfb4388c 187 t1.start();
jsmith352 0:04dcbfb4388c 188 }
jsmith352 0:04dcbfb4388c 189 }
jsmith352 0:04dcbfb4388c 190 }