IoT for mbed1

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

Fork of IoT by Tal Landes

Committer:
landes
Date:
Mon Dec 07 19:43:53 2015 +0000
Revision:
5:ebc70efa2e86
Parent:
4:d8de964b3d2c
Child:
6:33185f926189
12/7/2015

Who changed what in which revision?

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