IoT for mbed1
Dependencies: 4DGL-uLCD-SE IoTsecuritySys PinDetect mbed-rtos mbed
Fork of IoT by
main.cpp@8:429fa05b4952, 2015-12-08 (annotated)
- Committer:
- landes
- Date:
- Tue Dec 08 21:32:24 2015 +0000
- Revision:
- 8:429fa05b4952
- Parent:
- 7:758d4e6fc145
- Child:
- 9:413c094dd1e2
123
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jsmith352 | 0:04dcbfb4388c | 1 | #include <mbed.h> |
landes | 2:922d5b43bee3 | 2 | #include <string> |
landes | 5:ebc70efa2e86 | 3 | #include <iostream> |
landes | 1:7f873efe5b11 | 4 | #include "rtos.h" |
jsmith352 | 0:04dcbfb4388c | 5 | #include <mpr121.h> |
jsmith352 | 0:04dcbfb4388c | 6 | #include <stdlib.h> |
jsmith352 | 0:04dcbfb4388c | 7 | #include "PinDetect.h" |
landes | 1:7f873efe5b11 | 8 | #include "uLCD_4DGL.h" |
landes | 1:7f873efe5b11 | 9 | #include "SongPlayer.h" |
landes | 1:7f873efe5b11 | 10 | #include "Speaker.h" |
landes | 1:7f873efe5b11 | 11 | #include "EthernetInterface.h" |
jsmith352 | 0:04dcbfb4388c | 12 | /* CODE_LENGTH needs to be double the amount of numbers you |
jsmith352 | 0:04dcbfb4388c | 13 | want in your authenticator/passcode because of the way interrupt.fall(&fallInterrupt) |
jsmith352 | 0:04dcbfb4388c | 14 | works. It is called twice every time an interrupt is detected. |
jsmith352 | 0:04dcbfb4388c | 15 | The extra numbers in the array will just be filled with zeros and ignored in checking |
jsmith352 | 0:04dcbfb4388c | 16 | code sequences, so they will not matter either way */ |
jsmith352 | 0:04dcbfb4388c | 17 | /* i.e, you want a code with 7 numbers, CODE_LENGTH needs to be 14 */ |
landes | 1:7f873efe5b11 | 18 | #define CODE_LENGTH 8 |
jsmith352 | 0:04dcbfb4388c | 19 | |
jsmith352 | 0:04dcbfb4388c | 20 | DigitalOut led1(LED1); |
jsmith352 | 0:04dcbfb4388c | 21 | DigitalOut led2(LED2); |
jsmith352 | 0:04dcbfb4388c | 22 | DigitalOut led3(LED3); |
jsmith352 | 0:04dcbfb4388c | 23 | DigitalOut led4(LED4); |
jsmith352 | 0:04dcbfb4388c | 24 | |
landes | 1:7f873efe5b11 | 25 | DigitalOut doorlock(p21); |
landes | 1:7f873efe5b11 | 26 | |
landes | 1:7f873efe5b11 | 27 | |
landes | 1:7f873efe5b11 | 28 | //uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
landes | 1:7f873efe5b11 | 29 | uLCD_4DGL uLCD(p28, p27, p29); |
jsmith352 | 0:04dcbfb4388c | 30 | |
jsmith352 | 0:04dcbfb4388c | 31 | // Create the interrupt receiver object on pin 26 |
jsmith352 | 0:04dcbfb4388c | 32 | InterruptIn interrupt(p30); |
jsmith352 | 0:04dcbfb4388c | 33 | |
jsmith352 | 0:04dcbfb4388c | 34 | // Setup the i2c bus on pins 9 and 10 |
jsmith352 | 0:04dcbfb4388c | 35 | I2C i2c(p9, p10); |
jsmith352 | 0:04dcbfb4388c | 36 | |
jsmith352 | 0:04dcbfb4388c | 37 | // Setup the Mpr121: |
jsmith352 | 0:04dcbfb4388c | 38 | // constructor(i2c object, i2c address of the mpr121) |
jsmith352 | 0:04dcbfb4388c | 39 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
jsmith352 | 0:04dcbfb4388c | 40 | |
jsmith352 | 0:04dcbfb4388c | 41 | // pc serial communication for testing |
jsmith352 | 0:04dcbfb4388c | 42 | Serial pc(USBTX, USBRX); |
jsmith352 | 0:04dcbfb4388c | 43 | |
landes | 1:7f873efe5b11 | 44 | //Set up IR sensor |
landes | 1:7f873efe5b11 | 45 | AnalogIn IrSensor(p20); |
landes | 1:7f873efe5b11 | 46 | |
landes | 1:7f873efe5b11 | 47 | //Shiftbright |
landes | 1:7f873efe5b11 | 48 | DigitalOut latch(p15); |
landes | 1:7f873efe5b11 | 49 | DigitalOut enable(p16); |
landes | 1:7f873efe5b11 | 50 | //AnalogOut DACout(p18); |
landes | 1:7f873efe5b11 | 51 | //Cycles through different colors on RGB LED |
landes | 1:7f873efe5b11 | 52 | SPI spi(p11, p12, p13); |
landes | 1:7f873efe5b11 | 53 | |
landes | 1:7f873efe5b11 | 54 | |
landes | 1:7f873efe5b11 | 55 | SongPlayer mySpeaker(p26); |
landes | 1:7f873efe5b11 | 56 | Speaker NotePlayer(p26); |
landes | 1:7f873efe5b11 | 57 | |
landes | 1:7f873efe5b11 | 58 | // ethernet setup |
landes | 1:7f873efe5b11 | 59 | EthernetInterface eth; |
landes | 1:7f873efe5b11 | 60 | |
landes | 1:7f873efe5b11 | 61 | //Lock timeout |
landes | 4:d8de964b3d2c | 62 | Timeout flipper; |
landes | 1:7f873efe5b11 | 63 | |
jsmith352 | 0:04dcbfb4388c | 64 | // ***** GLOBALS ***** // |
jsmith352 | 0:04dcbfb4388c | 65 | // Timer is to seed rand |
landes | 4:d8de964b3d2c | 66 | string ID = "1"; |
landes | 5:ebc70efa2e86 | 67 | volatile int NumTry = 0; |
jsmith352 | 0:04dcbfb4388c | 68 | Timer t1; |
landes | 6:33185f926189 | 69 | Timer t2; |
jsmith352 | 0:04dcbfb4388c | 70 | // code counter is the next position in inputCode array |
landes | 1:7f873efe5b11 | 71 | volatile int codeCounter; |
jsmith352 | 0:04dcbfb4388c | 72 | // inputCode array is the sequence of numbers the user will enter |
landes | 1:7f873efe5b11 | 73 | volatile int inputCode[CODE_LENGTH]; |
landes | 8:429fa05b4952 | 74 | //volatile bool code_enabled; |
landes | 1:7f873efe5b11 | 75 | volatile float IrVoltage = 0.0; |
landes | 5:ebc70efa2e86 | 76 | int FindStrLocation(string sntsc, string word, string ptr); |
landes | 4:d8de964b3d2c | 77 | volatile enum Statetype { Armed = 0, IR_sensed = 1, Second_Step = 2, Cleared = 3, Alarm_ON = 4}; |
landes | 1:7f873efe5b11 | 78 | Statetype state = Armed; |
landes | 2:922d5b43bee3 | 79 | char charCode[5]; |
landes | 1:7f873efe5b11 | 80 | |
landes | 1:7f873efe5b11 | 81 | float note[18]= {1568.0,1396.9}; |
landes | 1:7f873efe5b11 | 82 | float duration[18]= {0.48,0.24}; |
landes | 1:7f873efe5b11 | 83 | |
landes | 1:7f873efe5b11 | 84 | Mutex LCD_Access; |
landes | 5:ebc70efa2e86 | 85 | Mutex PC_Access; |
landes | 8:429fa05b4952 | 86 | Mutex Eth_Lock; |
landes | 1:7f873efe5b11 | 87 | Semaphore Consul_Access(5); |
landes | 1:7f873efe5b11 | 88 | |
landes | 1:7f873efe5b11 | 89 | void Shiftbright_thread(void const *args); |
landes | 1:7f873efe5b11 | 90 | void IR_thread(void const *args); |
landes | 1:7f873efe5b11 | 91 | void LCD_Code_Enter_Thread(void const *args); |
landes | 1:7f873efe5b11 | 92 | void uLCD_thread(void const *args); |
landes | 1:7f873efe5b11 | 93 | void RGB_LED(int red, int green, int blue); |
landes | 1:7f873efe5b11 | 94 | void Speaker_thread(void const *args); |
landes | 1:7f873efe5b11 | 95 | void Ethernet_thread(void const *args); |
landes | 2:922d5b43bee3 | 96 | bool Ethernet_massage_Send(string args); |
landes | 1:7f873efe5b11 | 97 | void Activate_Lock(); |
landes | 1:7f873efe5b11 | 98 | void init_LCD(); |
jsmith352 | 0:04dcbfb4388c | 99 | // Key hit/release interrupt routine |
jsmith352 | 0:04dcbfb4388c | 100 | void fallInterrupt() { |
jsmith352 | 0:04dcbfb4388c | 101 | int key_code=0; |
jsmith352 | 0:04dcbfb4388c | 102 | int i=0; |
jsmith352 | 0:04dcbfb4388c | 103 | int value=mpr121.read(0x00); |
jsmith352 | 0:04dcbfb4388c | 104 | value +=mpr121.read(0x01)<<8; |
jsmith352 | 0:04dcbfb4388c | 105 | // LED demo mod |
jsmith352 | 0:04dcbfb4388c | 106 | i=0; |
jsmith352 | 0:04dcbfb4388c | 107 | // puts key number out to LEDs for demo |
jsmith352 | 0:04dcbfb4388c | 108 | for (i=0; i<12; i++) { |
jsmith352 | 0:04dcbfb4388c | 109 | if (((value>>i)&0x01)==1) key_code=i+1; |
jsmith352 | 0:04dcbfb4388c | 110 | } |
jsmith352 | 0:04dcbfb4388c | 111 | led4=key_code & 0x01; |
jsmith352 | 0:04dcbfb4388c | 112 | led3=(key_code>>1) & 0x01; |
jsmith352 | 0:04dcbfb4388c | 113 | led2=(key_code>>2) & 0x01; |
jsmith352 | 0:04dcbfb4388c | 114 | led1=(key_code>>3) & 0x01; |
jsmith352 | 0:04dcbfb4388c | 115 | |
jsmith352 | 0:04dcbfb4388c | 116 | // save the keypress to inputCode array |
landes | 2:922d5b43bee3 | 117 | switch (state) { |
landes | 2:922d5b43bee3 | 118 | case Armed: |
landes | 2:922d5b43bee3 | 119 | break; |
landes | 2:922d5b43bee3 | 120 | case IR_sensed: |
landes | 2:922d5b43bee3 | 121 | case Second_Step: |
landes | 2:922d5b43bee3 | 122 | if(codeCounter < CODE_LENGTH){ |
landes | 2:922d5b43bee3 | 123 | // ignore odd numbers |
landes | 2:922d5b43bee3 | 124 | if(codeCounter % 2 != 0){ |
landes | 2:922d5b43bee3 | 125 | inputCode[codeCounter] = 0; |
landes | 2:922d5b43bee3 | 126 | } |
landes | 2:922d5b43bee3 | 127 | // only save the even numbers (see lines 6-10) |
landes | 2:922d5b43bee3 | 128 | else{ |
landes | 2:922d5b43bee3 | 129 | inputCode[codeCounter] = key_code - 1; |
landes | 2:922d5b43bee3 | 130 | //pc.printf("codeCounter: %d -- code: %d\n\r", codeCounter, key_code - 1); |
landes | 2:922d5b43bee3 | 131 | } |
landes | 2:922d5b43bee3 | 132 | codeCounter++; |
jsmith352 | 0:04dcbfb4388c | 133 | } |
landes | 2:922d5b43bee3 | 134 | break; |
landes | 2:922d5b43bee3 | 135 | case Alarm_ON: |
landes | 8:429fa05b4952 | 136 | if(key_code == 12 ){ |
landes | 8:429fa05b4952 | 137 | state = Armed; |
landes | 8:429fa05b4952 | 138 | Ethernet_massage_Send("UpdateStatus"); |
landes | 8:429fa05b4952 | 139 | } |
landes | 2:922d5b43bee3 | 140 | break; |
landes | 2:922d5b43bee3 | 141 | case Cleared: |
landes | 5:ebc70efa2e86 | 142 | if(key_code == 12 ){ |
landes | 2:922d5b43bee3 | 143 | state = Armed; |
landes | 8:429fa05b4952 | 144 | Ethernet_massage_Send("UpdateStatus"); |
landes | 5:ebc70efa2e86 | 145 | } |
landes | 5:ebc70efa2e86 | 146 | else if (key_code == 11 ){ |
landes | 5:ebc70efa2e86 | 147 | doorlock = 1; |
landes | 5:ebc70efa2e86 | 148 | flipper.attach(&Activate_Lock, 5.0); |
landes | 5:ebc70efa2e86 | 149 | } |
landes | 2:922d5b43bee3 | 150 | break; |
landes | 2:922d5b43bee3 | 151 | } |
landes | 2:922d5b43bee3 | 152 | |
jsmith352 | 0:04dcbfb4388c | 153 | } |
jsmith352 | 0:04dcbfb4388c | 154 | |
jsmith352 | 0:04dcbfb4388c | 155 | // generate randomized code |
jsmith352 | 0:04dcbfb4388c | 156 | void generate_random_code(int (&codeArray)[CODE_LENGTH]){ |
landes | 1:7f873efe5b11 | 157 | //int i = 0; |
jsmith352 | 0:04dcbfb4388c | 158 | // only care about the even numbers (see lines 6-10) |
landes | 8:429fa05b4952 | 159 | //PC_Access.lock(); |
landes | 5:ebc70efa2e86 | 160 | //pc.printf("NEW CODE: "); |
landes | 5:ebc70efa2e86 | 161 | //PC_Access.unlock(); |
landes | 1:7f873efe5b11 | 162 | for(int i = 0; i < CODE_LENGTH; i+=2){ |
jsmith352 | 0:04dcbfb4388c | 163 | srand(t1.read_us()); |
landes | 1:7f873efe5b11 | 164 | codeArray[i] = (rand() % 9)+1; //nake code only 1-9 |
landes | 8:429fa05b4952 | 165 | PC_Access.lock(); |
jsmith352 | 0:04dcbfb4388c | 166 | pc.printf("%d, ", codeArray[i]); |
landes | 8:429fa05b4952 | 167 | PC_Access.unlock(); |
jsmith352 | 0:04dcbfb4388c | 168 | } |
landes | 5:ebc70efa2e86 | 169 | |
landes | 2:922d5b43bee3 | 170 | snprintf(charCode, sizeof(charCode), "%i%i%i%i ", codeArray[0], codeArray[2], codeArray[4], codeArray[6]); |
landes | 8:429fa05b4952 | 171 | //PC_Access.lock(); |
landes | 5:ebc70efa2e86 | 172 | //pc.printf("\n\r"); |
landes | 5:ebc70efa2e86 | 173 | //pc.printf("%s\n\r",charCode,codeArray[6]); |
landes | 5:ebc70efa2e86 | 174 | //PC_Access.unlock(); |
landes | 5:ebc70efa2e86 | 175 | //Ethernet_massage_Send("TextCode"); |
jsmith352 | 0:04dcbfb4388c | 176 | } |
jsmith352 | 0:04dcbfb4388c | 177 | |
jsmith352 | 0:04dcbfb4388c | 178 | // check if the code entered is the correct code |
jsmith352 | 0:04dcbfb4388c | 179 | bool check_code_sequence(int (&codeArray)[CODE_LENGTH]){ |
landes | 5:ebc70efa2e86 | 180 | //int i = 0; |
jsmith352 | 0:04dcbfb4388c | 181 | int j = 0; |
jsmith352 | 0:04dcbfb4388c | 182 | // only care about the even numbers (see lines 6-10) |
landes | 4:d8de964b3d2c | 183 | |
landes | 5:ebc70efa2e86 | 184 | if (NumTry < 3) { |
landes | 5:ebc70efa2e86 | 185 | NumTry++; |
landes | 5:ebc70efa2e86 | 186 | for(int i = 0; i < CODE_LENGTH; i+=2){ |
landes | 5:ebc70efa2e86 | 187 | |
landes | 5:ebc70efa2e86 | 188 | if(inputCode[i] == codeArray[i]){ |
jsmith352 | 0:04dcbfb4388c | 189 | j++; // count the number of right numbers |
landes | 5:ebc70efa2e86 | 190 | } |
landes | 5:ebc70efa2e86 | 191 | |
jsmith352 | 0:04dcbfb4388c | 192 | } |
landes | 5:ebc70efa2e86 | 193 | if(j == CODE_LENGTH/2){ |
landes | 8:429fa05b4952 | 194 | /*for(int i = 0; i < CODE_LENGTH; i+=2){ |
landes | 8:429fa05b4952 | 195 | //inputCode[i] =0; |
landes | 8:429fa05b4952 | 196 | }*/ |
jsmith352 | 0:04dcbfb4388c | 197 | return(true); |
landes | 5:ebc70efa2e86 | 198 | } |
landes | 2:922d5b43bee3 | 199 | else if (Ethernet_massage_Send("GetTempCode")){ |
landes | 8:429fa05b4952 | 200 | /*for(int i = 0; i < CODE_LENGTH; i+=2){ |
landes | 8:429fa05b4952 | 201 | //inputCode[i] =0; |
landes | 8:429fa05b4952 | 202 | }*/ |
landes | 5:ebc70efa2e86 | 203 | //pc.printf("return true"); |
landes | 2:922d5b43bee3 | 204 | return(true); |
landes | 2:922d5b43bee3 | 205 | } |
landes | 2:922d5b43bee3 | 206 | else { |
landes | 8:429fa05b4952 | 207 | /*for(int i = 0; i < CODE_LENGTH; i+=2){ |
landes | 8:429fa05b4952 | 208 | //inputCode[i] =0; |
landes | 8:429fa05b4952 | 209 | }*/ |
landes | 2:922d5b43bee3 | 210 | return(false); |
landes | 2:922d5b43bee3 | 211 | } |
landes | 5:ebc70efa2e86 | 212 | } |
landes | 5:ebc70efa2e86 | 213 | else { |
landes | 8:429fa05b4952 | 214 | //PC_Access.lock(); |
landes | 8:429fa05b4952 | 215 | //pc.printf("3 times "); |
landes | 8:429fa05b4952 | 216 | //PC_Access.unlock(); |
landes | 5:ebc70efa2e86 | 217 | state = Alarm_ON; |
landes | 8:429fa05b4952 | 218 | Ethernet_massage_Send("UpdateStatus"); |
landes | 5:ebc70efa2e86 | 219 | return(false); |
landes | 5:ebc70efa2e86 | 220 | } |
jsmith352 | 0:04dcbfb4388c | 221 | } |
jsmith352 | 0:04dcbfb4388c | 222 | |
jsmith352 | 0:04dcbfb4388c | 223 | int main() { |
jsmith352 | 0:04dcbfb4388c | 224 | interrupt.fall(&fallInterrupt); |
jsmith352 | 0:04dcbfb4388c | 225 | interrupt.mode(PullUp); |
landes | 1:7f873efe5b11 | 226 | pc.baud(921600); |
landes | 1:7f873efe5b11 | 227 | |
jsmith352 | 0:04dcbfb4388c | 228 | |
jsmith352 | 0:04dcbfb4388c | 229 | // authenticator is the randomly generated sequence of numbers by the machine |
jsmith352 | 0:04dcbfb4388c | 230 | // the user has to match this sequence to gain access, used for phase 2 |
jsmith352 | 0:04dcbfb4388c | 231 | int authenticator[CODE_LENGTH]; |
jsmith352 | 0:04dcbfb4388c | 232 | // passcode is the user's personal passcode, used for phase 1 |
landes | 1:7f873efe5b11 | 233 | int passcode[CODE_LENGTH] = {1,0,2,0,3,0,4,0};//,4,0,5,0,6,0}; |
jsmith352 | 0:04dcbfb4388c | 234 | codeCounter = 0; |
jsmith352 | 0:04dcbfb4388c | 235 | bool pass = false; |
jsmith352 | 0:04dcbfb4388c | 236 | |
jsmith352 | 0:04dcbfb4388c | 237 | // these 2 variables tell the machine when to generate a new random authentication code |
jsmith352 | 0:04dcbfb4388c | 238 | int new_code_timer = 0; |
jsmith352 | 0:04dcbfb4388c | 239 | int new_code_counter = 0; |
jsmith352 | 0:04dcbfb4388c | 240 | // this tells the state machine with phase of authentication we are in |
landes | 8:429fa05b4952 | 241 | //code_enabled = false; |
jsmith352 | 0:04dcbfb4388c | 242 | |
jsmith352 | 0:04dcbfb4388c | 243 | for(int i = 0; i < CODE_LENGTH; i++){ |
jsmith352 | 0:04dcbfb4388c | 244 | authenticator[i] = 0; |
jsmith352 | 0:04dcbfb4388c | 245 | inputCode[i] = 0; |
jsmith352 | 0:04dcbfb4388c | 246 | } |
jsmith352 | 0:04dcbfb4388c | 247 | |
jsmith352 | 0:04dcbfb4388c | 248 | // go ahead and start the timer so that when a random code is generated, |
jsmith352 | 0:04dcbfb4388c | 249 | // the seed will always be random, unlike the predecessor version |
jsmith352 | 0:04dcbfb4388c | 250 | t1.start(); |
landes | 1:7f873efe5b11 | 251 | init_LCD(); |
landes | 8:429fa05b4952 | 252 | |
landes | 8:429fa05b4952 | 253 | Timer t; |
landes | 8:429fa05b4952 | 254 | t.start(); |
landes | 1:7f873efe5b11 | 255 | //start threads: |
landes | 8:429fa05b4952 | 256 | PC_Access.lock(); |
landes | 1:7f873efe5b11 | 257 | pc.printf("\n\n\nSetting up Ethernet\n\r"); |
landes | 8:429fa05b4952 | 258 | PC_Access.unlock(); |
landes | 2:922d5b43bee3 | 259 | Thread Ethernetthread(Ethernet_thread); |
landes | 2:922d5b43bee3 | 260 | wait(5); //Give the Ethernet connection some time to set up |
landes | 8:429fa05b4952 | 261 | //Thread IRthread(IR_thread); |
landes | 1:7f873efe5b11 | 262 | Thread Shiftbright(Shiftbright_thread); |
landes | 1:7f873efe5b11 | 263 | Thread LCDthread(uLCD_thread); |
landes | 5:ebc70efa2e86 | 264 | //Thread LCD_CodeEnterThread(LCD_Code_Enter_Thread); |
landes | 5:ebc70efa2e86 | 265 | //Thread Speakerthread(Speaker_thread); |
landes | 5:ebc70efa2e86 | 266 | |
jsmith352 | 0:04dcbfb4388c | 267 | // while loop constantly checks if the entered code sequence is right or wrong, |
jsmith352 | 0:04dcbfb4388c | 268 | // given that the correct amount of numbers were entered |
landes | 8:429fa05b4952 | 269 | PC_Access.lock(); |
landes | 5:ebc70efa2e86 | 270 | pc.printf("Ready\n\r"); |
landes | 8:429fa05b4952 | 271 | PC_Access.unlock(); |
landes | 1:7f873efe5b11 | 272 | doorlock = 0; // make sure locked |
jsmith352 | 0:04dcbfb4388c | 273 | while (1){ |
landes | 1:7f873efe5b11 | 274 | switch(state){ |
landes | 8:429fa05b4952 | 275 | case Armed: |
landes | 8:429fa05b4952 | 276 | |
landes | 8:429fa05b4952 | 277 | |
landes | 8:429fa05b4952 | 278 | |
landes | 8:429fa05b4952 | 279 | |
landes | 8:429fa05b4952 | 280 | |
landes | 8:429fa05b4952 | 281 | |
landes | 8:429fa05b4952 | 282 | |
landes | 8:429fa05b4952 | 283 | |
landes | 8:429fa05b4952 | 284 | |
landes | 8:429fa05b4952 | 285 | |
landes | 8:429fa05b4952 | 286 | |
landes | 8:429fa05b4952 | 287 | // while(1) { |
landes | 8:429fa05b4952 | 288 | |
landes | 8:429fa05b4952 | 289 | if (state == Armed) { |
landes | 8:429fa05b4952 | 290 | IrVoltage=IrSensor.read(); |
landes | 8:429fa05b4952 | 291 | if (IrVoltage <= 0.1) { //if value just nois reset timer |
landes | 8:429fa05b4952 | 292 | t.reset(); |
landes | 8:429fa05b4952 | 293 | state = Armed; |
landes | 8:429fa05b4952 | 294 | //Ethernet_massage_Send("UpdateStatus"); |
landes | 8:429fa05b4952 | 295 | } |
landes | 8:429fa05b4952 | 296 | if (t.read() >= 3) { //wait 5 seconds to make sure that sense someone |
landes | 8:429fa05b4952 | 297 | state = IR_sensed; |
landes | 8:429fa05b4952 | 298 | Ethernet_massage_Send("UpdateStatus"); |
landes | 8:429fa05b4952 | 299 | } |
landes | 8:429fa05b4952 | 300 | // Thread::wait(2000); |
landes | 8:429fa05b4952 | 301 | } |
landes | 8:429fa05b4952 | 302 | else { |
landes | 8:429fa05b4952 | 303 | //nothing to do for this thread make space for others |
landes | 8:429fa05b4952 | 304 | // Thread::wait(2000); |
landes | 8:429fa05b4952 | 305 | } |
landes | 8:429fa05b4952 | 306 | //} |
landes | 8:429fa05b4952 | 307 | |
landes | 8:429fa05b4952 | 308 | |
landes | 8:429fa05b4952 | 309 | |
landes | 8:429fa05b4952 | 310 | |
landes | 8:429fa05b4952 | 311 | |
landes | 8:429fa05b4952 | 312 | |
landes | 8:429fa05b4952 | 313 | |
landes | 8:429fa05b4952 | 314 | |
landes | 8:429fa05b4952 | 315 | |
landes | 8:429fa05b4952 | 316 | |
landes | 8:429fa05b4952 | 317 | |
landes | 8:429fa05b4952 | 318 | |
landes | 8:429fa05b4952 | 319 | |
landes | 8:429fa05b4952 | 320 | break; |
landes | 1:7f873efe5b11 | 321 | case Cleared: |
landes | 1:7f873efe5b11 | 322 | break; |
landes | 1:7f873efe5b11 | 323 | case IR_sensed: |
jsmith352 | 0:04dcbfb4388c | 324 | if(codeCounter >= CODE_LENGTH){ |
jsmith352 | 0:04dcbfb4388c | 325 | pass = check_code_sequence(passcode); |
jsmith352 | 0:04dcbfb4388c | 326 | if(pass == true){ |
landes | 8:429fa05b4952 | 327 | //PC_Access.lock(); |
landes | 5:ebc70efa2e86 | 328 | //pc.printf("SENDING AUTHENTICATION CODE...\n\r"); |
landes | 8:429fa05b4952 | 329 | //PC_Access.unlock(); |
jsmith352 | 0:04dcbfb4388c | 330 | generate_random_code(authenticator); |
jsmith352 | 0:04dcbfb4388c | 331 | t1.stop(); // reset the time |
jsmith352 | 0:04dcbfb4388c | 332 | t1.reset(); // so that it is an even |
jsmith352 | 0:04dcbfb4388c | 333 | t1.start(); // 30 seconds before 1st new code is generated |
jsmith352 | 0:04dcbfb4388c | 334 | codeCounter = 0; |
landes | 1:7f873efe5b11 | 335 | //code_enabled = true; |
landes | 1:7f873efe5b11 | 336 | state = Second_Step; |
landes | 7:758d4e6fc145 | 337 | Ethernet_massage_Send("UpdateStatus"); |
landes | 8:429fa05b4952 | 338 | Ethernet_massage_Send("TextCode"); |
jsmith352 | 0:04dcbfb4388c | 339 | } |
jsmith352 | 0:04dcbfb4388c | 340 | else{ |
landes | 8:429fa05b4952 | 341 | //PC_Access.lock(); |
landes | 5:ebc70efa2e86 | 342 | //pc.printf("WRONG passcode\n\r"); |
landes | 8:429fa05b4952 | 343 | //PC_Access.unlock(); |
jsmith352 | 0:04dcbfb4388c | 344 | codeCounter = 0; |
jsmith352 | 0:04dcbfb4388c | 345 | } |
jsmith352 | 0:04dcbfb4388c | 346 | } |
jsmith352 | 0:04dcbfb4388c | 347 | break; |
landes | 1:7f873efe5b11 | 348 | case Second_Step: |
jsmith352 | 0:04dcbfb4388c | 349 | if(codeCounter >= CODE_LENGTH){ |
landes | 5:ebc70efa2e86 | 350 | NumTry = 0; |
jsmith352 | 0:04dcbfb4388c | 351 | pass = check_code_sequence(authenticator); |
jsmith352 | 0:04dcbfb4388c | 352 | if(pass == true){ |
landes | 8:429fa05b4952 | 353 | //PC_Access.lock(); |
landes | 5:ebc70efa2e86 | 354 | //pc.printf("ACCESS GRANTED\n\r"); |
landes | 8:429fa05b4952 | 355 | //PC_Access.unlock(); |
landes | 8:429fa05b4952 | 356 | |
landes | 1:7f873efe5b11 | 357 | //wait(5); |
landes | 1:7f873efe5b11 | 358 | //doorlock = 0;*/ |
landes | 5:ebc70efa2e86 | 359 | //pc.printf("Resetting....\n\r"); |
landes | 5:ebc70efa2e86 | 360 | //pc.printf("\n\n\rPlease Enter Your Personal Security Code\n\r"); |
jsmith352 | 0:04dcbfb4388c | 361 | codeCounter = 0; |
landes | 8:429fa05b4952 | 362 | //code_enabled = false; |
landes | 1:7f873efe5b11 | 363 | state = Cleared; |
landes | 5:ebc70efa2e86 | 364 | Ethernet_massage_Send("TextCode"); |
landes | 7:758d4e6fc145 | 365 | Ethernet_massage_Send("UpdateStatus"); |
landes | 8:429fa05b4952 | 366 | doorlock = 1; |
landes | 8:429fa05b4952 | 367 | flipper.attach(&Activate_Lock, 5.0); |
jsmith352 | 0:04dcbfb4388c | 368 | } |
jsmith352 | 0:04dcbfb4388c | 369 | else{ |
landes | 8:429fa05b4952 | 370 | //PC_Access.lock(); |
landes | 5:ebc70efa2e86 | 371 | //pc.printf("ACCESS DENIED\n\r"); |
landes | 8:429fa05b4952 | 372 | //PC_Access.unlock(); |
jsmith352 | 0:04dcbfb4388c | 373 | codeCounter = 0; |
jsmith352 | 0:04dcbfb4388c | 374 | } |
jsmith352 | 0:04dcbfb4388c | 375 | } |
jsmith352 | 0:04dcbfb4388c | 376 | // this code generates a new authentication code every 30 seconds (30000 ms) |
landes | 2:922d5b43bee3 | 377 | new_code_timer = (int)(t1.read_ms()/300000000); |
jsmith352 | 0:04dcbfb4388c | 378 | if(new_code_timer > new_code_counter){ |
jsmith352 | 0:04dcbfb4388c | 379 | new_code_counter++; |
jsmith352 | 0:04dcbfb4388c | 380 | generate_random_code(authenticator); |
jsmith352 | 0:04dcbfb4388c | 381 | codeCounter = 0; |
jsmith352 | 0:04dcbfb4388c | 382 | } |
jsmith352 | 0:04dcbfb4388c | 383 | break; |
jsmith352 | 0:04dcbfb4388c | 384 | } |
jsmith352 | 0:04dcbfb4388c | 385 | // reset the timer when the number gets too high, should last about 35 minutes |
jsmith352 | 0:04dcbfb4388c | 386 | // We do this because int can only hold a number up to 2^32 - 1, preventing errors |
jsmith352 | 0:04dcbfb4388c | 387 | if(t1.read_us() > 2147400000){ |
jsmith352 | 0:04dcbfb4388c | 388 | t1.stop(); |
jsmith352 | 0:04dcbfb4388c | 389 | t1.reset(); |
jsmith352 | 0:04dcbfb4388c | 390 | new_code_timer = 0; |
jsmith352 | 0:04dcbfb4388c | 391 | new_code_counter = 0; |
jsmith352 | 0:04dcbfb4388c | 392 | t1.start(); |
jsmith352 | 0:04dcbfb4388c | 393 | } |
jsmith352 | 0:04dcbfb4388c | 394 | } |
landes | 1:7f873efe5b11 | 395 | } |
landes | 1:7f873efe5b11 | 396 | |
landes | 1:7f873efe5b11 | 397 | |
landes | 1:7f873efe5b11 | 398 | void Shiftbright_thread(void const *args){ |
landes | 1:7f873efe5b11 | 399 | spi.format(16,0); |
landes | 1:7f873efe5b11 | 400 | spi.frequency(500000); |
landes | 1:7f873efe5b11 | 401 | enable=0; |
landes | 1:7f873efe5b11 | 402 | latch=0; |
landes | 1:7f873efe5b11 | 403 | |
landes | 1:7f873efe5b11 | 404 | while(1) { |
landes | 1:7f873efe5b11 | 405 | switch (state) { |
landes | 1:7f873efe5b11 | 406 | case Armed: |
landes | 8:429fa05b4952 | 407 | |
landes | 1:7f873efe5b11 | 408 | for (int i = 0; i <= 50; i++) { |
landes | 1:7f873efe5b11 | 409 | RGB_LED( i, 0, 0); |
landes | 1:7f873efe5b11 | 410 | Thread::wait(10); |
landes | 1:7f873efe5b11 | 411 | } |
landes | 1:7f873efe5b11 | 412 | for (int i = 50; i >= 0; i--) { |
landes | 1:7f873efe5b11 | 413 | RGB_LED( i, 0, 0); |
landes | 1:7f873efe5b11 | 414 | Thread::wait(10); |
landes | 1:7f873efe5b11 | 415 | } |
landes | 1:7f873efe5b11 | 416 | break; |
landes | 1:7f873efe5b11 | 417 | case IR_sensed: |
landes | 1:7f873efe5b11 | 418 | RGB_LED( 100, 0, 0); |
landes | 1:7f873efe5b11 | 419 | Thread::wait(500); |
landes | 1:7f873efe5b11 | 420 | RGB_LED( 0, 0, 0); |
landes | 1:7f873efe5b11 | 421 | break; |
landes | 1:7f873efe5b11 | 422 | case Alarm_ON: |
landes | 8:429fa05b4952 | 423 | mySpeaker.PlaySong(note,duration); |
landes | 8:429fa05b4952 | 424 | //Thread::wait(2000); |
landes | 1:7f873efe5b11 | 425 | for (int i = 0; i <= 100; i++) { |
landes | 1:7f873efe5b11 | 426 | RGB_LED( i, i/2, 0); |
landes | 1:7f873efe5b11 | 427 | Thread::wait(10); |
landes | 1:7f873efe5b11 | 428 | } |
landes | 1:7f873efe5b11 | 429 | for (int i = 100; i >= 0; i--) { |
landes | 1:7f873efe5b11 | 430 | RGB_LED( i, i/3, 0); |
landes | 1:7f873efe5b11 | 431 | Thread::wait(10); |
landes | 1:7f873efe5b11 | 432 | } |
landes | 1:7f873efe5b11 | 433 | break; |
landes | 1:7f873efe5b11 | 434 | case Cleared: |
landes | 1:7f873efe5b11 | 435 | RGB_LED( 0, 100, 0); |
landes | 1:7f873efe5b11 | 436 | break; |
landes | 1:7f873efe5b11 | 437 | } |
landes | 1:7f873efe5b11 | 438 | Thread::wait(1000); |
landes | 1:7f873efe5b11 | 439 | } |
landes | 1:7f873efe5b11 | 440 | } |
landes | 1:7f873efe5b11 | 441 | |
landes | 1:7f873efe5b11 | 442 | |
landes | 1:7f873efe5b11 | 443 | void IR_thread(void const *args) { |
landes | 1:7f873efe5b11 | 444 | |
landes | 1:7f873efe5b11 | 445 | Timer t; |
landes | 1:7f873efe5b11 | 446 | t.start(); |
landes | 1:7f873efe5b11 | 447 | |
landes | 1:7f873efe5b11 | 448 | while(1) { |
landes | 1:7f873efe5b11 | 449 | |
landes | 1:7f873efe5b11 | 450 | if (state == Armed) { |
landes | 1:7f873efe5b11 | 451 | IrVoltage=IrSensor.read(); |
landes | 1:7f873efe5b11 | 452 | if (IrVoltage <= 0.1) { //if value just nois reset timer |
landes | 1:7f873efe5b11 | 453 | t.reset(); |
landes | 1:7f873efe5b11 | 454 | state = Armed; |
landes | 7:758d4e6fc145 | 455 | Ethernet_massage_Send("UpdateStatus"); |
landes | 1:7f873efe5b11 | 456 | } |
landes | 8:429fa05b4952 | 457 | if (t.read() >= 3) { //wait 5 seconds to make sure that sense someone |
landes | 1:7f873efe5b11 | 458 | state = IR_sensed; |
landes | 7:758d4e6fc145 | 459 | Ethernet_massage_Send("UpdateStatus"); |
landes | 1:7f873efe5b11 | 460 | } |
landes | 8:429fa05b4952 | 461 | Thread::wait(2000); |
landes | 1:7f873efe5b11 | 462 | } |
landes | 1:7f873efe5b11 | 463 | else { |
landes | 1:7f873efe5b11 | 464 | //nothing to do for this thread make space for others |
landes | 8:429fa05b4952 | 465 | Thread::wait(2000); |
landes | 1:7f873efe5b11 | 466 | } |
landes | 1:7f873efe5b11 | 467 | } |
landes | 1:7f873efe5b11 | 468 | } |
landes | 1:7f873efe5b11 | 469 | |
landes | 1:7f873efe5b11 | 470 | void RGB_LED(int red, int green, int blue) { |
landes | 1:7f873efe5b11 | 471 | |
landes | 1:7f873efe5b11 | 472 | unsigned int low_color=0; |
landes | 1:7f873efe5b11 | 473 | unsigned int high_color=0; |
landes | 1:7f873efe5b11 | 474 | high_color=(blue<<4)|((red&0x3C0)>>6); |
landes | 1:7f873efe5b11 | 475 | low_color=(((red&0x3F)<<10)|(green)); |
landes | 1:7f873efe5b11 | 476 | spi.write(high_color); |
landes | 1:7f873efe5b11 | 477 | spi.write(low_color); |
landes | 1:7f873efe5b11 | 478 | latch=1; |
landes | 1:7f873efe5b11 | 479 | latch=0; |
landes | 1:7f873efe5b11 | 480 | } |
landes | 1:7f873efe5b11 | 481 | |
landes | 1:7f873efe5b11 | 482 | void init_LCD() { |
landes | 1:7f873efe5b11 | 483 | uLCD.baudrate(3000000); |
landes | 1:7f873efe5b11 | 484 | uLCD.background_color(BLACK); |
landes | 1:7f873efe5b11 | 485 | |
landes | 1:7f873efe5b11 | 486 | } |
landes | 1:7f873efe5b11 | 487 | |
landes | 1:7f873efe5b11 | 488 | |
landes | 1:7f873efe5b11 | 489 | void uLCD_thread(void const *args) { |
landes | 1:7f873efe5b11 | 490 | int Change = 99; |
landes | 2:922d5b43bee3 | 491 | string temp; |
landes | 2:922d5b43bee3 | 492 | temp = "abc"; |
landes | 1:7f873efe5b11 | 493 | while(1) { |
landes | 1:7f873efe5b11 | 494 | |
landes | 1:7f873efe5b11 | 495 | if (Change != state) { |
landes | 1:7f873efe5b11 | 496 | Change = state; |
landes | 1:7f873efe5b11 | 497 | switch (state) { |
landes | 1:7f873efe5b11 | 498 | case Armed: |
landes | 1:7f873efe5b11 | 499 | LCD_Access.lock(); |
landes | 1:7f873efe5b11 | 500 | uLCD.cls(); |
landes | 1:7f873efe5b11 | 501 | uLCD.color(WHITE); |
landes | 1:7f873efe5b11 | 502 | uLCD.text_width(2); |
landes | 1:7f873efe5b11 | 503 | uLCD.text_height(2); |
landes | 1:7f873efe5b11 | 504 | uLCD.printf(" ARMED\r\n"); |
landes | 1:7f873efe5b11 | 505 | uLCD.text_width(1); |
landes | 1:7f873efe5b11 | 506 | uLCD.text_height(1); |
landes | 1:7f873efe5b11 | 507 | |
landes | 1:7f873efe5b11 | 508 | if (eth.getIPAddress() == "\0") { |
landes | 1:7f873efe5b11 | 509 | uLCD.printf("\n\n No Internet connection"); |
landes | 1:7f873efe5b11 | 510 | } |
landes | 1:7f873efe5b11 | 511 | else { |
landes | 1:7f873efe5b11 | 512 | uLCD.printf("\n\nConnected to the Internet\n"); |
landes | 1:7f873efe5b11 | 513 | uLCD.printf("IP Address: \n%s ", eth.getIPAddress()); |
landes | 1:7f873efe5b11 | 514 | } |
landes | 1:7f873efe5b11 | 515 | LCD_Access.unlock(); |
landes | 1:7f873efe5b11 | 516 | break; |
landes | 1:7f873efe5b11 | 517 | case IR_sensed: |
landes | 1:7f873efe5b11 | 518 | LCD_Access.lock(); |
landes | 1:7f873efe5b11 | 519 | uLCD.cls(); |
landes | 1:7f873efe5b11 | 520 | uLCD.printf("\nSensor triggred \n"); |
landes | 1:7f873efe5b11 | 521 | uLCD.printf("\n Enter the code ..."); |
landes | 1:7f873efe5b11 | 522 | LCD_Access.unlock(); |
landes | 1:7f873efe5b11 | 523 | for (int i=30; i>=0; --i) { |
landes | 1:7f873efe5b11 | 524 | if (state == IR_sensed) { |
landes | 1:7f873efe5b11 | 525 | LCD_Access.lock(); |
landes | 1:7f873efe5b11 | 526 | uLCD.text_width(4); |
landes | 1:7f873efe5b11 | 527 | uLCD.text_height(4); |
landes | 8:429fa05b4952 | 528 | LCD_Access.unlock(); |
landes | 8:429fa05b4952 | 529 | LCD_Access.lock(); |
landes | 1:7f873efe5b11 | 530 | uLCD.color(RED); |
landes | 1:7f873efe5b11 | 531 | uLCD.locate(1,2); |
landes | 1:7f873efe5b11 | 532 | uLCD.printf("%2D",i); |
landes | 1:7f873efe5b11 | 533 | LCD_Access.unlock(); |
landes | 1:7f873efe5b11 | 534 | Thread::wait(1000); |
landes | 1:7f873efe5b11 | 535 | } |
landes | 1:7f873efe5b11 | 536 | } |
landes | 1:7f873efe5b11 | 537 | if (state == IR_sensed) { |
landes | 8:429fa05b4952 | 538 | LCD_Access.lock(); |
landes | 8:429fa05b4952 | 539 | uLCD.cls(); |
landes | 8:429fa05b4952 | 540 | LCD_Access.unlock(); |
landes | 1:7f873efe5b11 | 541 | state = Alarm_ON; |
landes | 7:758d4e6fc145 | 542 | Ethernet_massage_Send("UpdateStatus"); |
landes | 2:922d5b43bee3 | 543 | Ethernet_massage_Send("TextAlarm"); |
landes | 1:7f873efe5b11 | 544 | } |
landes | 1:7f873efe5b11 | 545 | |
landes | 1:7f873efe5b11 | 546 | break; |
landes | 1:7f873efe5b11 | 547 | case Second_Step: |
landes | 1:7f873efe5b11 | 548 | LCD_Access.lock(); |
landes | 1:7f873efe5b11 | 549 | uLCD.cls(); |
landes | 1:7f873efe5b11 | 550 | uLCD.color(BLUE); |
landes | 1:7f873efe5b11 | 551 | uLCD.printf("\nPleas enter code from text massage \n"); |
landes | 1:7f873efe5b11 | 552 | LCD_Access.unlock(); |
landes | 1:7f873efe5b11 | 553 | break; |
landes | 1:7f873efe5b11 | 554 | case Alarm_ON: |
landes | 1:7f873efe5b11 | 555 | LCD_Access.lock(); |
landes | 1:7f873efe5b11 | 556 | uLCD.cls(); |
landes | 1:7f873efe5b11 | 557 | uLCD.color(RED); |
landes | 1:7f873efe5b11 | 558 | uLCD.text_width(1.5); //4X size text |
landes | 1:7f873efe5b11 | 559 | uLCD.text_height(1.5); |
landes | 1:7f873efe5b11 | 560 | uLCD.printf("\nALARM IS ON \nText message sent \n"); |
landes | 1:7f873efe5b11 | 561 | LCD_Access.unlock(); |
landes | 1:7f873efe5b11 | 562 | break; |
landes | 1:7f873efe5b11 | 563 | case Cleared: |
landes | 1:7f873efe5b11 | 564 | LCD_Access.lock(); |
landes | 1:7f873efe5b11 | 565 | uLCD.cls(); |
landes | 1:7f873efe5b11 | 566 | uLCD.color(GREEN); |
landes | 1:7f873efe5b11 | 567 | uLCD.printf("\n\nAccess Granted. \n\n"); |
landes | 1:7f873efe5b11 | 568 | LCD_Access.unlock(); |
landes | 1:7f873efe5b11 | 569 | break; |
landes | 1:7f873efe5b11 | 570 | } |
landes | 1:7f873efe5b11 | 571 | } |
landes | 1:7f873efe5b11 | 572 | Thread::wait(500); |
landes | 1:7f873efe5b11 | 573 | } |
landes | 1:7f873efe5b11 | 574 | } |
landes | 1:7f873efe5b11 | 575 | |
landes | 1:7f873efe5b11 | 576 | void LCD_Code_Enter_Thread(void const *args) { |
landes | 1:7f873efe5b11 | 577 | int LineHight = 120; |
landes | 1:7f873efe5b11 | 578 | int LineWidth = 10; |
landes | 1:7f873efe5b11 | 579 | int SpaceWidth = 5; |
landes | 1:7f873efe5b11 | 580 | int MidPoint = 127/2; |
landes | 1:7f873efe5b11 | 581 | while(1) { |
landes | 1:7f873efe5b11 | 582 | switch (state) { |
landes | 1:7f873efe5b11 | 583 | case Armed: |
landes | 1:7f873efe5b11 | 584 | break; |
landes | 1:7f873efe5b11 | 585 | case IR_sensed: |
landes | 1:7f873efe5b11 | 586 | case Cleared: |
landes | 1:7f873efe5b11 | 587 | case Second_Step: |
landes | 1:7f873efe5b11 | 588 | |
landes | 1:7f873efe5b11 | 589 | Thread::wait(500); |
landes | 1:7f873efe5b11 | 590 | while((state == IR_sensed)||(state == Cleared)||(state == Second_Step)) { |
landes | 1:7f873efe5b11 | 591 | LCD_Access.lock(); |
landes | 1:7f873efe5b11 | 592 | //dusplay four lines |
landes | 1:7f873efe5b11 | 593 | uLCD.line(MidPoint-2*(LineWidth+SpaceWidth), LineHight, MidPoint- 2*SpaceWidth-LineWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color) |
landes | 1:7f873efe5b11 | 594 | uLCD.line(MidPoint-LineWidth-SpaceWidth, LineHight, MidPoint-SpaceWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color) |
landes | 1:7f873efe5b11 | 595 | uLCD.line(MidPoint+SpaceWidth, LineHight, MidPoint+SpaceWidth+LineWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color) |
landes | 1:7f873efe5b11 | 596 | uLCD.line(MidPoint+2*SpaceWidth+LineWidth, LineHight, MidPoint+2*(SpaceWidth+LineWidth), LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color) |
landes | 1:7f873efe5b11 | 597 | uLCD.locate(5,14); |
landes | 1:7f873efe5b11 | 598 | uLCD.text_width(1); //4X size text |
landes | 1:7f873efe5b11 | 599 | uLCD.text_height(1); |
landes | 5:ebc70efa2e86 | 600 | |
landes | 1:7f873efe5b11 | 601 | // add black numbers |
landes | 5:ebc70efa2e86 | 602 | /*if (inputCode[0] == inputCode[2] ==inputCode[4] ==inputCode[6] == 0) { |
landes | 5:ebc70efa2e86 | 603 | uLCD.color(BLACK); |
landes | 5:ebc70efa2e86 | 604 | } |
landes | 5:ebc70efa2e86 | 605 | else {*/ |
landes | 5:ebc70efa2e86 | 606 | //uLCD.color(WHITE); |
landes | 1:7f873efe5b11 | 607 | uLCD.printf("%d %d %d %d",inputCode[0],inputCode[2],inputCode[4],inputCode[6]); |
landes | 5:ebc70efa2e86 | 608 | //} |
landes | 1:7f873efe5b11 | 609 | LCD_Access.unlock(); |
landes | 1:7f873efe5b11 | 610 | } |
landes | 1:7f873efe5b11 | 611 | |
landes | 1:7f873efe5b11 | 612 | case Alarm_ON: |
landes | 1:7f873efe5b11 | 613 | break; |
landes | 1:7f873efe5b11 | 614 | } |
landes | 1:7f873efe5b11 | 615 | } |
landes | 1:7f873efe5b11 | 616 | } |
landes | 1:7f873efe5b11 | 617 | |
landes | 1:7f873efe5b11 | 618 | void Speaker_thread(void const *args) { |
landes | 1:7f873efe5b11 | 619 | while (1) { |
landes | 1:7f873efe5b11 | 620 | if (state == Alarm_ON) { |
landes | 1:7f873efe5b11 | 621 | mySpeaker.PlaySong(note,duration); |
landes | 8:429fa05b4952 | 622 | Thread::wait(2000); |
landes | 1:7f873efe5b11 | 623 | } |
landes | 1:7f873efe5b11 | 624 | } |
landes | 1:7f873efe5b11 | 625 | } |
landes | 1:7f873efe5b11 | 626 | |
landes | 1:7f873efe5b11 | 627 | void Ethernet_thread(void const *args) { |
landes | 4:d8de964b3d2c | 628 | |
landes | 2:922d5b43bee3 | 629 | char buffer[300]; |
landes | 5:ebc70efa2e86 | 630 | |
landes | 5:ebc70efa2e86 | 631 | int ret,found; |
landes | 1:7f873efe5b11 | 632 | eth.init(); //Use DHCP |
landes | 1:7f873efe5b11 | 633 | eth.connect(); |
landes | 5:ebc70efa2e86 | 634 | //pc.printf("IP Address is: %s\n\r", eth.getIPAddress()); |
landes | 5:ebc70efa2e86 | 635 | while (1) { |
landes | 7:758d4e6fc145 | 636 | Thread::wait(3000); |
landes | 6:33185f926189 | 637 | int timeout; |
landes | 6:33185f926189 | 638 | |
landes | 6:33185f926189 | 639 | //t2.start(); |
landes | 6:33185f926189 | 640 | TCPSocketConnection sock; |
landes | 6:33185f926189 | 641 | sock.connect("dreamphysix.com", 80); |
landes | 6:33185f926189 | 642 | char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/readstatus.php?mbedID=0 HTTP/1.0\n\n"; |
landes | 8:429fa05b4952 | 643 | //PC_Access.lock(); |
landes | 5:ebc70efa2e86 | 644 | //pc.printf("%s",http_cmd); |
landes | 8:429fa05b4952 | 645 | //PC_Access.unlock(); |
landes | 8:429fa05b4952 | 646 | Eth_Lock.lock(); |
landes | 4:d8de964b3d2c | 647 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
landes | 8:429fa05b4952 | 648 | Eth_Lock.unlock(); |
landes | 4:d8de964b3d2c | 649 | while (true) { |
landes | 8:429fa05b4952 | 650 | Eth_Lock.lock(); |
landes | 1:7f873efe5b11 | 651 | ret = sock.receive(buffer, sizeof(buffer)-1); |
landes | 8:429fa05b4952 | 652 | Eth_Lock.unlock(); |
landes | 1:7f873efe5b11 | 653 | if (ret <= 0) |
landes | 1:7f873efe5b11 | 654 | break; |
landes | 1:7f873efe5b11 | 655 | buffer[ret] = '\0'; |
landes | 1:7f873efe5b11 | 656 | Consul_Access.wait(); |
landes | 8:429fa05b4952 | 657 | //PC_Access.lock(); |
landes | 7:758d4e6fc145 | 658 | //pc.printf("Received %d chars from server:\n%s\n", ret, buffer); |
landes | 8:429fa05b4952 | 659 | //PC_Access.unlock(); |
landes | 1:7f873efe5b11 | 660 | Consul_Access.release(); |
landes | 1:7f873efe5b11 | 661 | } |
landes | 1:7f873efe5b11 | 662 | sock.close(); |
landes | 6:33185f926189 | 663 | //t2.stop(); |
landes | 6:33185f926189 | 664 | string str(buffer); |
landes | 6:33185f926189 | 665 | found = str.find("Status="); |
landes | 5:ebc70efa2e86 | 666 | //pc.printf("location: %d string: %s" , found, str); |
landes | 5:ebc70efa2e86 | 667 | //pc.printf("\n\rhttp_cmd: %c\n\r",buffer[found+7]); |
landes | 5:ebc70efa2e86 | 668 | //pc.printf("\n\state: %i\n\r",state); |
landes | 6:33185f926189 | 669 | int dummy = (buffer[found+7])-48; |
landes | 5:ebc70efa2e86 | 670 | //int dummy = ((int)buffer[found+7]); |
landes | 5:ebc70efa2e86 | 671 | //dummy = atoi(dummy); |
landes | 6:33185f926189 | 672 | state = (Statetype)dummy; |
landes | 5:ebc70efa2e86 | 673 | //state = (Statetype)buffer[found+7]; |
landes | 8:429fa05b4952 | 674 | PC_Access.lock(); |
landes | 6:33185f926189 | 675 | pc.printf("\n\state: %i\n\r",dummy); |
landes | 8:429fa05b4952 | 676 | PC_Access.unlock(); |
landes | 5:ebc70efa2e86 | 677 | //Thread::wait(3000); |
landes | 6:33185f926189 | 678 | |
landes | 6:33185f926189 | 679 | |
landes | 5:ebc70efa2e86 | 680 | } |
landes | 2:922d5b43bee3 | 681 | } |
landes | 2:922d5b43bee3 | 682 | |
landes | 2:922d5b43bee3 | 683 | bool Ethernet_massage_Send(string buff) { |
landes | 4:d8de964b3d2c | 684 | |
landes | 2:922d5b43bee3 | 685 | char buffer[300]; |
landes | 2:922d5b43bee3 | 686 | int ret; |
landes | 5:ebc70efa2e86 | 687 | //pc.printf("IP Address is: %s\n\r", eth.getIPAddress()); |
landes | 2:922d5b43bee3 | 688 | |
landes | 2:922d5b43bee3 | 689 | TCPSocketConnection sock; |
landes | 2:922d5b43bee3 | 690 | sock.connect("dreamphysix.com", 80); |
landes | 2:922d5b43bee3 | 691 | |
landes | 2:922d5b43bee3 | 692 | if (buff == "TextCode") { |
landes | 2:922d5b43bee3 | 693 | char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/sendcode.php?authcode=0e9cae34a0&randomcode="; |
landes | 2:922d5b43bee3 | 694 | strcat(http_cmd, charCode); |
landes | 2:922d5b43bee3 | 695 | strcat(http_cmd, " HTTP/1.0\n\n"); |
landes | 8:429fa05b4952 | 696 | PC_Access.lock(); |
landes | 8:429fa05b4952 | 697 | pc.printf("%s",http_cmd); |
landes | 8:429fa05b4952 | 698 | PC_Access.unlock(); |
landes | 8:429fa05b4952 | 699 | Eth_Lock.lock(); |
landes | 2:922d5b43bee3 | 700 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
landes | 8:429fa05b4952 | 701 | Eth_Lock.unlock(); |
landes | 2:922d5b43bee3 | 702 | } |
landes | 2:922d5b43bee3 | 703 | else if (buff == "TextAlarm") { |
landes | 2:922d5b43bee3 | 704 | char http_cmd[] = "GET http://dreamphysix.com/alarm/sendalert.php?authcode=0e9cae34a0 HTTP/1.0\n\n"; |
landes | 8:429fa05b4952 | 705 | Eth_Lock.lock(); |
landes | 2:922d5b43bee3 | 706 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
landes | 8:429fa05b4952 | 707 | Eth_Lock.unlock(); |
landes | 8:429fa05b4952 | 708 | PC_Access.lock(); |
landes | 8:429fa05b4952 | 709 | pc.printf("%s",http_cmd); |
landes | 8:429fa05b4952 | 710 | PC_Access.unlock(); |
landes | 2:922d5b43bee3 | 711 | } |
landes | 2:922d5b43bee3 | 712 | else if (buff == "GetTempCode") { |
landes | 4:d8de964b3d2c | 713 | snprintf(charCode, sizeof(charCode), "%i%i%i%i ", inputCode[0], inputCode[2], inputCode[4], inputCode[6]); |
landes | 2:922d5b43bee3 | 714 | char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/validatecode.php?code="; |
landes | 2:922d5b43bee3 | 715 | strcat(http_cmd, charCode); |
landes | 2:922d5b43bee3 | 716 | strcat(http_cmd, " HTTP/1.0\n\n"); |
landes | 8:429fa05b4952 | 717 | PC_Access.lock(); |
landes | 8:429fa05b4952 | 718 | pc.printf("%s",http_cmd); |
landes | 8:429fa05b4952 | 719 | PC_Access.unlock(); |
landes | 8:429fa05b4952 | 720 | Eth_Lock.lock(); |
landes | 2:922d5b43bee3 | 721 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
landes | 8:429fa05b4952 | 722 | Eth_Lock.unlock(); |
landes | 2:922d5b43bee3 | 723 | } |
landes | 2:922d5b43bee3 | 724 | else if (buff == "UpdateStatus") { |
landes | 4:d8de964b3d2c | 725 | char tempStatus[2]; |
landes | 4:d8de964b3d2c | 726 | snprintf(tempStatus, sizeof(tempStatus), "%i", state); |
landes | 4:d8de964b3d2c | 727 | char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/updatestatus.php?mbedID=1"; |
landes | 4:d8de964b3d2c | 728 | //strcat(http_cmd, ID); |
landes | 4:d8de964b3d2c | 729 | strcat(http_cmd, "&status="); |
landes | 4:d8de964b3d2c | 730 | strcat(http_cmd, tempStatus); |
landes | 4:d8de964b3d2c | 731 | strcat(http_cmd, " HTTP/1.0\n\n"); |
landes | 8:429fa05b4952 | 732 | PC_Access.lock(); |
landes | 8:429fa05b4952 | 733 | pc.printf("%s",http_cmd); |
landes | 8:429fa05b4952 | 734 | PC_Access.unlock(); |
landes | 8:429fa05b4952 | 735 | Eth_Lock.lock(); |
landes | 4:d8de964b3d2c | 736 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
landes | 8:429fa05b4952 | 737 | Eth_Lock.unlock(); |
landes | 2:922d5b43bee3 | 738 | } |
landes | 2:922d5b43bee3 | 739 | else { |
landes | 2:922d5b43bee3 | 740 | |
landes | 2:922d5b43bee3 | 741 | } |
landes | 2:922d5b43bee3 | 742 | |
landes | 2:922d5b43bee3 | 743 | |
landes | 2:922d5b43bee3 | 744 | while (true) { |
landes | 8:429fa05b4952 | 745 | Eth_Lock.lock(); |
landes | 2:922d5b43bee3 | 746 | ret = sock.receive(buffer, sizeof(buffer)-1); |
landes | 8:429fa05b4952 | 747 | Eth_Lock.unlock(); |
landes | 2:922d5b43bee3 | 748 | if (ret <= 0) |
landes | 2:922d5b43bee3 | 749 | break; |
landes | 2:922d5b43bee3 | 750 | buffer[ret] = '\0'; |
landes | 2:922d5b43bee3 | 751 | Consul_Access.wait(); |
landes | 8:429fa05b4952 | 752 | //PC_Access.lock(); |
landes | 7:758d4e6fc145 | 753 | //pc.printf("Received %d chars from server:\n%s\n", ret, buffer); |
landes | 8:429fa05b4952 | 754 | //PC_Access.unlock(); |
landes | 2:922d5b43bee3 | 755 | Consul_Access.release(); |
landes | 2:922d5b43bee3 | 756 | } |
landes | 2:922d5b43bee3 | 757 | sock.close(); |
landes | 4:d8de964b3d2c | 758 | |
landes | 5:ebc70efa2e86 | 759 | snprintf(buffer, ret, "%c",buffer); |
landes | 5:ebc70efa2e86 | 760 | //pc.printf("buffer: %s", buffer); |
landes | 4:d8de964b3d2c | 761 | if (strstr(buffer,"True") != NULL) { |
landes | 5:ebc70efa2e86 | 762 | //pc.printf("true fron eth check"); |
landes | 5:ebc70efa2e86 | 763 | return true; |
landes | 5:ebc70efa2e86 | 764 | } |
landes | 5:ebc70efa2e86 | 765 | else if (strstr(buffer,"False") != NULL) { |
landes | 5:ebc70efa2e86 | 766 | //pc.printf("false fron eth check"); |
landes | 4:d8de964b3d2c | 767 | return false; |
landes | 2:922d5b43bee3 | 768 | } |
landes | 5:ebc70efa2e86 | 769 | else { |
landes | 5:ebc70efa2e86 | 770 | //pc.printf("default "); |
landes | 8:429fa05b4952 | 771 | return true; |
landes | 4:d8de964b3d2c | 772 | } |
landes | 8:429fa05b4952 | 773 | Thread::wait(1000); |
landes | 1:7f873efe5b11 | 774 | } |
landes | 1:7f873efe5b11 | 775 | |
landes | 1:7f873efe5b11 | 776 | void Activate_Lock(){ |
landes | 1:7f873efe5b11 | 777 | doorlock =! doorlock; |
landes | 1:7f873efe5b11 | 778 | } |