IoT for mbed1

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

Fork of IoT by Tal Landes

Committer:
landes
Date:
Sun Dec 06 23:15:34 2015 +0000
Revision:
4:d8de964b3d2c
Parent:
2:922d5b43bee3
Child:
5:ebc70efa2e86
Sunday before eating pizza;

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 1:7f873efe5b11 3 #include "rtos.h"
jsmith352 0:04dcbfb4388c 4 #include <mpr121.h>
jsmith352 0:04dcbfb4388c 5 #include <stdlib.h>
jsmith352 0:04dcbfb4388c 6 #include "PinDetect.h"
landes 1:7f873efe5b11 7 #include "uLCD_4DGL.h"
landes 1:7f873efe5b11 8 #include "SongPlayer.h"
landes 1:7f873efe5b11 9 #include "PinDetect.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";
jsmith352 0:04dcbfb4388c 67 Timer t1;
jsmith352 0:04dcbfb4388c 68 // code counter is the next position in inputCode array
landes 1:7f873efe5b11 69 volatile int codeCounter;
jsmith352 0:04dcbfb4388c 70 // inputCode array is the sequence of numbers the user will enter
landes 1:7f873efe5b11 71 volatile int inputCode[CODE_LENGTH];
landes 1:7f873efe5b11 72 volatile bool code_enabled;
landes 1:7f873efe5b11 73 volatile float IrVoltage = 0.0;
landes 4:d8de964b3d2c 74 volatile enum Statetype { Armed = 0, IR_sensed = 1, Second_Step = 2, Cleared = 3, Alarm_ON = 4};
landes 1:7f873efe5b11 75 Statetype state = Armed;
landes 2:922d5b43bee3 76 char charCode[5];
landes 1:7f873efe5b11 77
landes 1:7f873efe5b11 78 float note[18]= {1568.0,1396.9};
landes 1:7f873efe5b11 79 float duration[18]= {0.48,0.24};
landes 1:7f873efe5b11 80
landes 1:7f873efe5b11 81 Mutex LCD_Access;
landes 1:7f873efe5b11 82 Semaphore Consul_Access(5);
landes 1:7f873efe5b11 83
landes 1:7f873efe5b11 84 void Shiftbright_thread(void const *args);
landes 1:7f873efe5b11 85 void IR_thread(void const *args);
landes 1:7f873efe5b11 86 void LCD_Code_Enter_Thread(void const *args);
landes 1:7f873efe5b11 87 void uLCD_thread(void const *args);
landes 1:7f873efe5b11 88 void RGB_LED(int red, int green, int blue);
landes 1:7f873efe5b11 89 void Speaker_thread(void const *args);
landes 1:7f873efe5b11 90 void Ethernet_thread(void const *args);
landes 2:922d5b43bee3 91 bool Ethernet_massage_Send(string args);
landes 1:7f873efe5b11 92 void Activate_Lock();
landes 1:7f873efe5b11 93 void init_LCD();
jsmith352 0:04dcbfb4388c 94 // Key hit/release interrupt routine
jsmith352 0:04dcbfb4388c 95 void fallInterrupt() {
jsmith352 0:04dcbfb4388c 96 int key_code=0;
jsmith352 0:04dcbfb4388c 97 int i=0;
jsmith352 0:04dcbfb4388c 98 int value=mpr121.read(0x00);
jsmith352 0:04dcbfb4388c 99 value +=mpr121.read(0x01)<<8;
jsmith352 0:04dcbfb4388c 100 // LED demo mod
jsmith352 0:04dcbfb4388c 101 i=0;
jsmith352 0:04dcbfb4388c 102 // puts key number out to LEDs for demo
jsmith352 0:04dcbfb4388c 103 for (i=0; i<12; i++) {
jsmith352 0:04dcbfb4388c 104 if (((value>>i)&0x01)==1) key_code=i+1;
jsmith352 0:04dcbfb4388c 105 }
jsmith352 0:04dcbfb4388c 106 led4=key_code & 0x01;
jsmith352 0:04dcbfb4388c 107 led3=(key_code>>1) & 0x01;
jsmith352 0:04dcbfb4388c 108 led2=(key_code>>2) & 0x01;
jsmith352 0:04dcbfb4388c 109 led1=(key_code>>3) & 0x01;
jsmith352 0:04dcbfb4388c 110
jsmith352 0:04dcbfb4388c 111 // save the keypress to inputCode array
landes 2:922d5b43bee3 112 switch (state) {
landes 2:922d5b43bee3 113 case Armed:
landes 2:922d5b43bee3 114 break;
landes 2:922d5b43bee3 115 case IR_sensed:
landes 2:922d5b43bee3 116 case Second_Step:
landes 2:922d5b43bee3 117 if(codeCounter < CODE_LENGTH){
landes 2:922d5b43bee3 118 // ignore odd numbers
landes 2:922d5b43bee3 119 if(codeCounter % 2 != 0){
landes 2:922d5b43bee3 120 inputCode[codeCounter] = 0;
landes 2:922d5b43bee3 121 }
landes 2:922d5b43bee3 122 // only save the even numbers (see lines 6-10)
landes 2:922d5b43bee3 123 else{
landes 2:922d5b43bee3 124 inputCode[codeCounter] = key_code - 1;
landes 2:922d5b43bee3 125 //pc.printf("codeCounter: %d -- code: %d\n\r", codeCounter, key_code - 1);
landes 2:922d5b43bee3 126 }
landes 2:922d5b43bee3 127 codeCounter++;
jsmith352 0:04dcbfb4388c 128 }
landes 2:922d5b43bee3 129 break;
landes 2:922d5b43bee3 130 case Alarm_ON:
landes 2:922d5b43bee3 131 break;
landes 2:922d5b43bee3 132 case Cleared:
landes 2:922d5b43bee3 133 if(codeCounter % 2 == 11)
landes 2:922d5b43bee3 134 state = Armed;
landes 4:d8de964b3d2c 135 //Ethernet_massage_Send("UpdateStatus");
landes 2:922d5b43bee3 136 break;
landes 2:922d5b43bee3 137 }
landes 2:922d5b43bee3 138
jsmith352 0:04dcbfb4388c 139 }
jsmith352 0:04dcbfb4388c 140
jsmith352 0:04dcbfb4388c 141 // generate randomized code
jsmith352 0:04dcbfb4388c 142 void generate_random_code(int (&codeArray)[CODE_LENGTH]){
landes 1:7f873efe5b11 143 //int i = 0;
jsmith352 0:04dcbfb4388c 144 // only care about the even numbers (see lines 6-10)
jsmith352 0:04dcbfb4388c 145 pc.printf("NEW CODE: ");
landes 1:7f873efe5b11 146 for(int i = 0; i < CODE_LENGTH; i+=2){
jsmith352 0:04dcbfb4388c 147 srand(t1.read_us());
landes 1:7f873efe5b11 148 codeArray[i] = (rand() % 9)+1; //nake code only 1-9
jsmith352 0:04dcbfb4388c 149 pc.printf("%d, ", codeArray[i]);
jsmith352 0:04dcbfb4388c 150 }
jsmith352 0:04dcbfb4388c 151 pc.printf("\n\r");
landes 2:922d5b43bee3 152 snprintf(charCode, sizeof(charCode), "%i%i%i%i ", codeArray[0], codeArray[2], codeArray[4], codeArray[6]);
landes 2:922d5b43bee3 153 pc.printf("%s\n\r",charCode,codeArray[6]);
landes 2:922d5b43bee3 154 Ethernet_massage_Send("TextCode");
jsmith352 0:04dcbfb4388c 155 }
jsmith352 0:04dcbfb4388c 156
jsmith352 0:04dcbfb4388c 157 // check if the code entered is the correct code
jsmith352 0:04dcbfb4388c 158 bool check_code_sequence(int (&codeArray)[CODE_LENGTH]){
jsmith352 0:04dcbfb4388c 159 int i = 0;
jsmith352 0:04dcbfb4388c 160 int j = 0;
jsmith352 0:04dcbfb4388c 161 // only care about the even numbers (see lines 6-10)
landes 4:d8de964b3d2c 162
jsmith352 0:04dcbfb4388c 163 for(i = 0; i < CODE_LENGTH; i+=2){
jsmith352 0:04dcbfb4388c 164 if(inputCode[i] == codeArray[i])
jsmith352 0:04dcbfb4388c 165 j++; // count the number of right numbers
jsmith352 0:04dcbfb4388c 166 }
jsmith352 0:04dcbfb4388c 167 if(j == CODE_LENGTH/2)
jsmith352 0:04dcbfb4388c 168 return(true);
landes 2:922d5b43bee3 169 else if (Ethernet_massage_Send("GetTempCode")){
landes 2:922d5b43bee3 170 return(true);
landes 2:922d5b43bee3 171 }
landes 2:922d5b43bee3 172 else {
landes 2:922d5b43bee3 173 return(false);
landes 2:922d5b43bee3 174 }
jsmith352 0:04dcbfb4388c 175 }
jsmith352 0:04dcbfb4388c 176
jsmith352 0:04dcbfb4388c 177 int main() {
jsmith352 0:04dcbfb4388c 178 interrupt.fall(&fallInterrupt);
jsmith352 0:04dcbfb4388c 179 interrupt.mode(PullUp);
landes 1:7f873efe5b11 180 pc.baud(921600);
landes 1:7f873efe5b11 181
jsmith352 0:04dcbfb4388c 182
jsmith352 0:04dcbfb4388c 183 // authenticator is the randomly generated sequence of numbers by the machine
jsmith352 0:04dcbfb4388c 184 // the user has to match this sequence to gain access, used for phase 2
jsmith352 0:04dcbfb4388c 185 int authenticator[CODE_LENGTH];
jsmith352 0:04dcbfb4388c 186 // passcode is the user's personal passcode, used for phase 1
landes 1:7f873efe5b11 187 int passcode[CODE_LENGTH] = {1,0,2,0,3,0,4,0};//,4,0,5,0,6,0};
jsmith352 0:04dcbfb4388c 188 codeCounter = 0;
jsmith352 0:04dcbfb4388c 189 bool pass = false;
jsmith352 0:04dcbfb4388c 190
jsmith352 0:04dcbfb4388c 191 // these 2 variables tell the machine when to generate a new random authentication code
jsmith352 0:04dcbfb4388c 192 int new_code_timer = 0;
jsmith352 0:04dcbfb4388c 193 int new_code_counter = 0;
jsmith352 0:04dcbfb4388c 194 // this tells the state machine with phase of authentication we are in
jsmith352 0:04dcbfb4388c 195 code_enabled = false;
jsmith352 0:04dcbfb4388c 196
jsmith352 0:04dcbfb4388c 197 for(int i = 0; i < CODE_LENGTH; i++){
jsmith352 0:04dcbfb4388c 198 authenticator[i] = 0;
jsmith352 0:04dcbfb4388c 199 inputCode[i] = 0;
jsmith352 0:04dcbfb4388c 200 }
jsmith352 0:04dcbfb4388c 201
jsmith352 0:04dcbfb4388c 202 // go ahead and start the timer so that when a random code is generated,
jsmith352 0:04dcbfb4388c 203 // the seed will always be random, unlike the predecessor version
jsmith352 0:04dcbfb4388c 204 t1.start();
landes 1:7f873efe5b11 205 init_LCD();
landes 1:7f873efe5b11 206 //start threads:
landes 1:7f873efe5b11 207 pc.printf("\n\n\nSetting up Ethernet\n\r");
landes 2:922d5b43bee3 208 Thread Ethernetthread(Ethernet_thread);
landes 2:922d5b43bee3 209 wait(5); //Give the Ethernet connection some time to set up
landes 1:7f873efe5b11 210 Thread IRthread(IR_thread);
landes 1:7f873efe5b11 211 Thread Shiftbright(Shiftbright_thread);
landes 1:7f873efe5b11 212 Thread LCDthread(uLCD_thread);
landes 1:7f873efe5b11 213 Thread LCD_CodeEnterThread(LCD_Code_Enter_Thread);
landes 1:7f873efe5b11 214 Thread Speakerthread(Speaker_thread);
jsmith352 0:04dcbfb4388c 215 // while loop constantly checks if the entered code sequence is right or wrong,
jsmith352 0:04dcbfb4388c 216 // given that the correct amount of numbers were entered
jsmith352 0:04dcbfb4388c 217 pc.printf("Please Enter Your Personal Security Code\n\r");
landes 1:7f873efe5b11 218 doorlock = 0; // make sure locked
jsmith352 0:04dcbfb4388c 219 while (1){
landes 1:7f873efe5b11 220 switch(state){
landes 1:7f873efe5b11 221 case Cleared:
landes 1:7f873efe5b11 222 break;
landes 1:7f873efe5b11 223 case IR_sensed:
jsmith352 0:04dcbfb4388c 224 if(codeCounter >= CODE_LENGTH){
jsmith352 0:04dcbfb4388c 225 pass = check_code_sequence(passcode);
jsmith352 0:04dcbfb4388c 226 if(pass == true){
jsmith352 0:04dcbfb4388c 227 pc.printf("SENDING AUTHENTICATION CODE...\n\r");
jsmith352 0:04dcbfb4388c 228 generate_random_code(authenticator);
jsmith352 0:04dcbfb4388c 229 t1.stop(); // reset the time
jsmith352 0:04dcbfb4388c 230 t1.reset(); // so that it is an even
jsmith352 0:04dcbfb4388c 231 t1.start(); // 30 seconds before 1st new code is generated
jsmith352 0:04dcbfb4388c 232 codeCounter = 0;
landes 1:7f873efe5b11 233 //code_enabled = true;
landes 1:7f873efe5b11 234 state = Second_Step;
landes 4:d8de964b3d2c 235 //Ethernet_massage_Send("UpdateStatus");
jsmith352 0:04dcbfb4388c 236 }
jsmith352 0:04dcbfb4388c 237 else{
jsmith352 0:04dcbfb4388c 238 pc.printf("WRONG passcode\n\r");
jsmith352 0:04dcbfb4388c 239 codeCounter = 0;
jsmith352 0:04dcbfb4388c 240 }
jsmith352 0:04dcbfb4388c 241 }
jsmith352 0:04dcbfb4388c 242 break;
landes 1:7f873efe5b11 243 case Second_Step:
jsmith352 0:04dcbfb4388c 244 if(codeCounter >= CODE_LENGTH){
jsmith352 0:04dcbfb4388c 245 pass = check_code_sequence(authenticator);
jsmith352 0:04dcbfb4388c 246 if(pass == true){
jsmith352 0:04dcbfb4388c 247 pc.printf("ACCESS GRANTED\n\r");
landes 1:7f873efe5b11 248 doorlock = 1;
landes 1:7f873efe5b11 249 flipper.attach(&Activate_Lock, 5.0);
landes 1:7f873efe5b11 250 //wait(5);
landes 1:7f873efe5b11 251 //doorlock = 0;*/
jsmith352 0:04dcbfb4388c 252 pc.printf("Resetting....\n\r");
jsmith352 0:04dcbfb4388c 253 pc.printf("\n\n\rPlease Enter Your Personal Security Code\n\r");
jsmith352 0:04dcbfb4388c 254 codeCounter = 0;
jsmith352 0:04dcbfb4388c 255 code_enabled = false;
landes 1:7f873efe5b11 256 state = Cleared;
landes 4:d8de964b3d2c 257 Ethernet_massage_Send("UpdateStatus");
jsmith352 0:04dcbfb4388c 258 }
jsmith352 0:04dcbfb4388c 259 else{
jsmith352 0:04dcbfb4388c 260 pc.printf("ACCESS DENIED\n\r");
jsmith352 0:04dcbfb4388c 261 codeCounter = 0;
jsmith352 0:04dcbfb4388c 262 }
jsmith352 0:04dcbfb4388c 263 }
jsmith352 0:04dcbfb4388c 264 // this code generates a new authentication code every 30 seconds (30000 ms)
landes 2:922d5b43bee3 265 new_code_timer = (int)(t1.read_ms()/300000000);
jsmith352 0:04dcbfb4388c 266 if(new_code_timer > new_code_counter){
jsmith352 0:04dcbfb4388c 267 new_code_counter++;
jsmith352 0:04dcbfb4388c 268 generate_random_code(authenticator);
jsmith352 0:04dcbfb4388c 269 codeCounter = 0;
jsmith352 0:04dcbfb4388c 270 }
jsmith352 0:04dcbfb4388c 271 break;
jsmith352 0:04dcbfb4388c 272 }
jsmith352 0:04dcbfb4388c 273 // reset the timer when the number gets too high, should last about 35 minutes
jsmith352 0:04dcbfb4388c 274 // We do this because int can only hold a number up to 2^32 - 1, preventing errors
jsmith352 0:04dcbfb4388c 275 if(t1.read_us() > 2147400000){
jsmith352 0:04dcbfb4388c 276 t1.stop();
jsmith352 0:04dcbfb4388c 277 t1.reset();
jsmith352 0:04dcbfb4388c 278 new_code_timer = 0;
jsmith352 0:04dcbfb4388c 279 new_code_counter = 0;
jsmith352 0:04dcbfb4388c 280 t1.start();
jsmith352 0:04dcbfb4388c 281 }
jsmith352 0:04dcbfb4388c 282 }
landes 1:7f873efe5b11 283 }
landes 1:7f873efe5b11 284
landes 1:7f873efe5b11 285
landes 1:7f873efe5b11 286 void Shiftbright_thread(void const *args){
landes 1:7f873efe5b11 287 spi.format(16,0);
landes 1:7f873efe5b11 288 spi.frequency(500000);
landes 1:7f873efe5b11 289 enable=0;
landes 1:7f873efe5b11 290 latch=0;
landes 1:7f873efe5b11 291
landes 1:7f873efe5b11 292 while(1) {
landes 1:7f873efe5b11 293 switch (state) {
landes 1:7f873efe5b11 294 case Armed:
landes 1:7f873efe5b11 295 for (int i = 0; i <= 50; i++) {
landes 1:7f873efe5b11 296 RGB_LED( i, 0, 0);
landes 1:7f873efe5b11 297 Thread::wait(10);
landes 1:7f873efe5b11 298 }
landes 1:7f873efe5b11 299 for (int i = 50; i >= 0; i--) {
landes 1:7f873efe5b11 300 RGB_LED( i, 0, 0);
landes 1:7f873efe5b11 301 Thread::wait(10);
landes 1:7f873efe5b11 302 }
landes 1:7f873efe5b11 303 break;
landes 1:7f873efe5b11 304 case IR_sensed:
landes 1:7f873efe5b11 305 RGB_LED( 100, 0, 0);
landes 1:7f873efe5b11 306 Thread::wait(500);
landes 1:7f873efe5b11 307 RGB_LED( 0, 0, 0);
landes 1:7f873efe5b11 308 break;
landes 1:7f873efe5b11 309 case Alarm_ON:
landes 1:7f873efe5b11 310 for (int i = 0; i <= 100; i++) {
landes 1:7f873efe5b11 311 RGB_LED( i, i/2, 0);
landes 1:7f873efe5b11 312 Thread::wait(10);
landes 1:7f873efe5b11 313 }
landes 1:7f873efe5b11 314 for (int i = 100; i >= 0; i--) {
landes 1:7f873efe5b11 315 RGB_LED( i, i/3, 0);
landes 1:7f873efe5b11 316 Thread::wait(10);
landes 1:7f873efe5b11 317 }
landes 1:7f873efe5b11 318 break;
landes 1:7f873efe5b11 319 case Cleared:
landes 1:7f873efe5b11 320 RGB_LED( 0, 100, 0);
landes 1:7f873efe5b11 321 break;
landes 1:7f873efe5b11 322 }
landes 1:7f873efe5b11 323 Thread::wait(1000);
landes 1:7f873efe5b11 324 }
landes 1:7f873efe5b11 325 }
landes 1:7f873efe5b11 326
landes 1:7f873efe5b11 327
landes 1:7f873efe5b11 328 void IR_thread(void const *args) {
landes 1:7f873efe5b11 329
landes 1:7f873efe5b11 330 Timer t;
landes 1:7f873efe5b11 331 t.start();
landes 1:7f873efe5b11 332
landes 1:7f873efe5b11 333 while(1) {
landes 1:7f873efe5b11 334
landes 1:7f873efe5b11 335 if (state == Armed) {
landes 1:7f873efe5b11 336 IrVoltage=IrSensor.read();
landes 1:7f873efe5b11 337 if (IrVoltage <= 0.1) { //if value just nois reset timer
landes 1:7f873efe5b11 338 t.reset();
landes 1:7f873efe5b11 339 state = Armed;
landes 4:d8de964b3d2c 340 //Ethernet_massage_Send("UpdateStatus");
landes 1:7f873efe5b11 341 }
landes 1:7f873efe5b11 342 if (t.read() >= 5) { //wait 5 seconds to make sure that sense someone
landes 1:7f873efe5b11 343 state = IR_sensed;
landes 4:d8de964b3d2c 344 //Ethernet_massage_Send("UpdateStatus");
landes 1:7f873efe5b11 345 }
landes 1:7f873efe5b11 346 Thread::wait(1000);
landes 1:7f873efe5b11 347 }
landes 1:7f873efe5b11 348 else {
landes 1:7f873efe5b11 349 //nothing to do for this thread make space for others
landes 1:7f873efe5b11 350 Thread::wait(1000);
landes 1:7f873efe5b11 351 }
landes 1:7f873efe5b11 352 }
landes 1:7f873efe5b11 353 }
landes 1:7f873efe5b11 354
landes 1:7f873efe5b11 355 void RGB_LED(int red, int green, int blue) {
landes 1:7f873efe5b11 356
landes 1:7f873efe5b11 357 unsigned int low_color=0;
landes 1:7f873efe5b11 358 unsigned int high_color=0;
landes 1:7f873efe5b11 359 high_color=(blue<<4)|((red&0x3C0)>>6);
landes 1:7f873efe5b11 360 low_color=(((red&0x3F)<<10)|(green));
landes 1:7f873efe5b11 361 spi.write(high_color);
landes 1:7f873efe5b11 362 spi.write(low_color);
landes 1:7f873efe5b11 363 latch=1;
landes 1:7f873efe5b11 364 latch=0;
landes 1:7f873efe5b11 365 }
landes 1:7f873efe5b11 366
landes 1:7f873efe5b11 367 void init_LCD() {
landes 1:7f873efe5b11 368 uLCD.baudrate(3000000);
landes 1:7f873efe5b11 369 uLCD.background_color(BLACK);
landes 1:7f873efe5b11 370
landes 1:7f873efe5b11 371 }
landes 1:7f873efe5b11 372
landes 1:7f873efe5b11 373
landes 1:7f873efe5b11 374 void uLCD_thread(void const *args) {
landes 1:7f873efe5b11 375 int Change = 99;
landes 2:922d5b43bee3 376 string temp;
landes 2:922d5b43bee3 377 temp = "abc";
landes 1:7f873efe5b11 378 while(1) {
landes 1:7f873efe5b11 379
landes 1:7f873efe5b11 380 if (Change != state) {
landes 1:7f873efe5b11 381 Change = state;
landes 1:7f873efe5b11 382 switch (state) {
landes 1:7f873efe5b11 383 case Armed:
landes 1:7f873efe5b11 384 LCD_Access.lock();
landes 1:7f873efe5b11 385 uLCD.cls();
landes 1:7f873efe5b11 386 uLCD.color(WHITE);
landes 1:7f873efe5b11 387 uLCD.text_width(2);
landes 1:7f873efe5b11 388 uLCD.text_height(2);
landes 1:7f873efe5b11 389 uLCD.printf(" ARMED\r\n");
landes 1:7f873efe5b11 390 uLCD.text_width(1);
landes 1:7f873efe5b11 391 uLCD.text_height(1);
landes 1:7f873efe5b11 392
landes 1:7f873efe5b11 393 if (eth.getIPAddress() == "\0") {
landes 1:7f873efe5b11 394 uLCD.printf("\n\n No Internet connection");
landes 1:7f873efe5b11 395 }
landes 1:7f873efe5b11 396 else {
landes 1:7f873efe5b11 397 uLCD.printf("\n\nConnected to the Internet\n");
landes 1:7f873efe5b11 398 uLCD.printf("IP Address: \n%s ", eth.getIPAddress());
landes 1:7f873efe5b11 399 }
landes 1:7f873efe5b11 400 LCD_Access.unlock();
landes 1:7f873efe5b11 401 break;
landes 1:7f873efe5b11 402 case IR_sensed:
landes 1:7f873efe5b11 403 LCD_Access.lock();
landes 1:7f873efe5b11 404 uLCD.cls();
landes 1:7f873efe5b11 405 uLCD.printf("\nSensor triggred \n");
landes 1:7f873efe5b11 406 uLCD.printf("\n Enter the code ...");
landes 1:7f873efe5b11 407 LCD_Access.unlock();
landes 1:7f873efe5b11 408 for (int i=30; i>=0; --i) {
landes 1:7f873efe5b11 409 if (state == IR_sensed) {
landes 1:7f873efe5b11 410 LCD_Access.lock();
landes 1:7f873efe5b11 411 uLCD.text_width(4);
landes 1:7f873efe5b11 412 uLCD.text_height(4);
landes 1:7f873efe5b11 413 uLCD.color(RED);
landes 1:7f873efe5b11 414 uLCD.locate(1,2);
landes 1:7f873efe5b11 415 uLCD.printf("%2D",i);
landes 1:7f873efe5b11 416 LCD_Access.unlock();
landes 1:7f873efe5b11 417 Thread::wait(1000);
landes 1:7f873efe5b11 418 }
landes 1:7f873efe5b11 419 }
landes 1:7f873efe5b11 420 if (state == IR_sensed) {
landes 1:7f873efe5b11 421 state = Alarm_ON;
landes 4:d8de964b3d2c 422 //Ethernet_massage_Send("UpdateStatus");
landes 2:922d5b43bee3 423 Ethernet_massage_Send("TextAlarm");
landes 1:7f873efe5b11 424 }
landes 1:7f873efe5b11 425
landes 1:7f873efe5b11 426 break;
landes 1:7f873efe5b11 427 case Second_Step:
landes 1:7f873efe5b11 428 LCD_Access.lock();
landes 1:7f873efe5b11 429 uLCD.cls();
landes 1:7f873efe5b11 430 uLCD.color(BLUE);
landes 1:7f873efe5b11 431 uLCD.printf("\nPleas enter code from text massage \n");
landes 1:7f873efe5b11 432 LCD_Access.unlock();
landes 1:7f873efe5b11 433 break;
landes 1:7f873efe5b11 434 case Alarm_ON:
landes 1:7f873efe5b11 435 LCD_Access.lock();
landes 1:7f873efe5b11 436 uLCD.cls();
landes 1:7f873efe5b11 437 uLCD.color(RED);
landes 1:7f873efe5b11 438 uLCD.text_width(1.5); //4X size text
landes 1:7f873efe5b11 439 uLCD.text_height(1.5);
landes 1:7f873efe5b11 440 uLCD.printf("\nALARM IS ON \nText message sent \n");
landes 1:7f873efe5b11 441 LCD_Access.unlock();
landes 1:7f873efe5b11 442 break;
landes 1:7f873efe5b11 443 case Cleared:
landes 1:7f873efe5b11 444 LCD_Access.lock();
landes 1:7f873efe5b11 445 uLCD.cls();
landes 1:7f873efe5b11 446 uLCD.color(GREEN);
landes 1:7f873efe5b11 447 uLCD.printf("\n\nAccess Granted. \n\n");
landes 1:7f873efe5b11 448 LCD_Access.unlock();
landes 1:7f873efe5b11 449 break;
landes 1:7f873efe5b11 450 }
landes 1:7f873efe5b11 451 }
landes 1:7f873efe5b11 452 Thread::wait(500);
landes 1:7f873efe5b11 453 }
landes 1:7f873efe5b11 454 }
landes 1:7f873efe5b11 455
landes 1:7f873efe5b11 456 void LCD_Code_Enter_Thread(void const *args) {
landes 1:7f873efe5b11 457 int LineHight = 120;
landes 1:7f873efe5b11 458 int LineWidth = 10;
landes 1:7f873efe5b11 459 int SpaceWidth = 5;
landes 1:7f873efe5b11 460 int MidPoint = 127/2;
landes 1:7f873efe5b11 461 while(1) {
landes 1:7f873efe5b11 462 switch (state) {
landes 1:7f873efe5b11 463 case Armed:
landes 1:7f873efe5b11 464 break;
landes 1:7f873efe5b11 465 case IR_sensed:
landes 1:7f873efe5b11 466 case Cleared:
landes 1:7f873efe5b11 467 case Second_Step:
landes 1:7f873efe5b11 468
landes 1:7f873efe5b11 469 Thread::wait(500);
landes 1:7f873efe5b11 470 while((state == IR_sensed)||(state == Cleared)||(state == Second_Step)) {
landes 1:7f873efe5b11 471 LCD_Access.lock();
landes 1:7f873efe5b11 472 //dusplay four lines
landes 1:7f873efe5b11 473 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 474 uLCD.line(MidPoint-LineWidth-SpaceWidth, LineHight, MidPoint-SpaceWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
landes 1:7f873efe5b11 475 uLCD.line(MidPoint+SpaceWidth, LineHight, MidPoint+SpaceWidth+LineWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
landes 1:7f873efe5b11 476 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 477 uLCD.locate(5,14);
landes 1:7f873efe5b11 478 uLCD.text_width(1); //4X size text
landes 1:7f873efe5b11 479 uLCD.text_height(1);
landes 1:7f873efe5b11 480 uLCD.color(WHITE);
landes 1:7f873efe5b11 481 // add black numbers
landes 1:7f873efe5b11 482 uLCD.printf("%d %d %d %d",inputCode[0],inputCode[2],inputCode[4],inputCode[6]);
landes 1:7f873efe5b11 483 LCD_Access.unlock();
landes 1:7f873efe5b11 484 }
landes 1:7f873efe5b11 485
landes 1:7f873efe5b11 486 case Alarm_ON:
landes 1:7f873efe5b11 487 break;
landes 1:7f873efe5b11 488 }
landes 1:7f873efe5b11 489 }
landes 1:7f873efe5b11 490 }
landes 1:7f873efe5b11 491
landes 1:7f873efe5b11 492 void Speaker_thread(void const *args) {
landes 1:7f873efe5b11 493 while (1) {
landes 1:7f873efe5b11 494 if (state == Alarm_ON) {
landes 1:7f873efe5b11 495 mySpeaker.PlaySong(note,duration);
landes 1:7f873efe5b11 496 Thread::wait(1000);
landes 1:7f873efe5b11 497 }
landes 1:7f873efe5b11 498 }
landes 1:7f873efe5b11 499 }
landes 1:7f873efe5b11 500
landes 1:7f873efe5b11 501 void Ethernet_thread(void const *args) {
landes 4:d8de964b3d2c 502
landes 2:922d5b43bee3 503 char buffer[300];
landes 2:922d5b43bee3 504 int ret;
landes 1:7f873efe5b11 505 eth.init(); //Use DHCP
landes 1:7f873efe5b11 506 eth.connect();
landes 4:d8de964b3d2c 507 pc.printf("IP Address is: %s\n\r", eth.getIPAddress());
landes 4:d8de964b3d2c 508 /* while (1) {
landes 4:d8de964b3d2c 509 TCPSocketConnection sock;
landes 4:d8de964b3d2c 510 sock.connect("dreamphysix.com", 80);
landes 4:d8de964b3d2c 511 char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/readstatus.php?mbedID=0 HTTP/1.0\n\n";
landes 4:d8de964b3d2c 512 pc.printf("%s",http_cmd);
landes 4:d8de964b3d2c 513 sock.send_all(http_cmd, sizeof(http_cmd)-1);
landes 1:7f873efe5b11 514
landes 4:d8de964b3d2c 515 while (true) {
landes 1:7f873efe5b11 516 ret = sock.receive(buffer, sizeof(buffer)-1);
landes 1:7f873efe5b11 517 if (ret <= 0)
landes 1:7f873efe5b11 518 break;
landes 1:7f873efe5b11 519 buffer[ret] = '\0';
landes 1:7f873efe5b11 520 Consul_Access.wait();
landes 1:7f873efe5b11 521 pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
landes 1:7f873efe5b11 522 Consul_Access.release();
landes 1:7f873efe5b11 523 }
landes 1:7f873efe5b11 524 sock.close();
landes 4:d8de964b3d2c 525 find(buffer, size_t pos = 0);
landes 4:d8de964b3d2c 526 //size_t found = buffer.find("Status=");
landes 4:d8de964b3d2c 527 pc.printf("location: %i string: %s",found, buffer[found]);
landes 4:d8de964b3d2c 528
landes 4:d8de964b3d2c 529 Thread::wait(2000);
landes 4:d8de964b3d2c 530 }*/
landes 4:d8de964b3d2c 531
landes 2:922d5b43bee3 532 }
landes 2:922d5b43bee3 533
landes 2:922d5b43bee3 534 bool Ethernet_massage_Send(string buff) {
landes 4:d8de964b3d2c 535
landes 2:922d5b43bee3 536 char buffer[300];
landes 2:922d5b43bee3 537 int ret;
landes 2:922d5b43bee3 538 pc.printf("IP Address is: %s\n\r", eth.getIPAddress());
landes 2:922d5b43bee3 539
landes 2:922d5b43bee3 540 TCPSocketConnection sock;
landes 2:922d5b43bee3 541 sock.connect("dreamphysix.com", 80);
landes 2:922d5b43bee3 542
landes 2:922d5b43bee3 543 if (buff == "TextCode") {
landes 2:922d5b43bee3 544 char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/sendcode.php?authcode=0e9cae34a0&randomcode=";
landes 2:922d5b43bee3 545 strcat(http_cmd, charCode);
landes 2:922d5b43bee3 546 strcat(http_cmd, " HTTP/1.0\n\n");
landes 2:922d5b43bee3 547 pc.printf("%s",http_cmd);
landes 2:922d5b43bee3 548 sock.send_all(http_cmd, sizeof(http_cmd)-1);
landes 2:922d5b43bee3 549 }
landes 2:922d5b43bee3 550 else if (buff == "TextAlarm") {
landes 2:922d5b43bee3 551 char http_cmd[] = "GET http://dreamphysix.com/alarm/sendalert.php?authcode=0e9cae34a0 HTTP/1.0\n\n";
landes 2:922d5b43bee3 552 sock.send_all(http_cmd, sizeof(http_cmd)-1);
landes 2:922d5b43bee3 553 }
landes 2:922d5b43bee3 554 else if (buff == "GetTempCode") {
landes 4:d8de964b3d2c 555 snprintf(charCode, sizeof(charCode), "%i%i%i%i ", inputCode[0], inputCode[2], inputCode[4], inputCode[6]);
landes 2:922d5b43bee3 556 char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/validatecode.php?code=";
landes 2:922d5b43bee3 557 strcat(http_cmd, charCode);
landes 2:922d5b43bee3 558 strcat(http_cmd, " HTTP/1.0\n\n");
landes 2:922d5b43bee3 559 pc.printf("%s",http_cmd);
landes 2:922d5b43bee3 560 sock.send_all(http_cmd, sizeof(http_cmd)-1);
landes 2:922d5b43bee3 561 }
landes 2:922d5b43bee3 562 else if (buff == "UpdateStatus") {
landes 4:d8de964b3d2c 563 char tempStatus[2];
landes 4:d8de964b3d2c 564 snprintf(tempStatus, sizeof(tempStatus), "%i", state);
landes 4:d8de964b3d2c 565 char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/updatestatus.php?mbedID=1";
landes 4:d8de964b3d2c 566 //strcat(http_cmd, ID);
landes 4:d8de964b3d2c 567 strcat(http_cmd, "&status=");
landes 4:d8de964b3d2c 568 strcat(http_cmd, tempStatus);
landes 4:d8de964b3d2c 569 strcat(http_cmd, " HTTP/1.0\n\n");
landes 4:d8de964b3d2c 570 pc.printf("%s",http_cmd);
landes 4:d8de964b3d2c 571 sock.send_all(http_cmd, sizeof(http_cmd)-1);
landes 2:922d5b43bee3 572 }
landes 2:922d5b43bee3 573 else {
landes 2:922d5b43bee3 574
landes 2:922d5b43bee3 575 }
landes 2:922d5b43bee3 576
landes 2:922d5b43bee3 577
landes 2:922d5b43bee3 578 while (true) {
landes 2:922d5b43bee3 579 ret = sock.receive(buffer, sizeof(buffer)-1);
landes 2:922d5b43bee3 580 if (ret <= 0)
landes 2:922d5b43bee3 581 break;
landes 2:922d5b43bee3 582 buffer[ret] = '\0';
landes 2:922d5b43bee3 583 Consul_Access.wait();
landes 2:922d5b43bee3 584 pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
landes 2:922d5b43bee3 585 Consul_Access.release();
landes 2:922d5b43bee3 586 }
landes 2:922d5b43bee3 587 sock.close();
landes 4:d8de964b3d2c 588
landes 4:d8de964b3d2c 589 snprintf(buffer, sizeof(buffer), "%i",ret);
landes 4:d8de964b3d2c 590 //pc.printf("buffer: %s"
landes 4:d8de964b3d2c 591 if (strstr(buffer,"True") != NULL) {
landes 4:d8de964b3d2c 592 return false;
landes 2:922d5b43bee3 593 }
landes 4:d8de964b3d2c 594 else if (strstr(buffer,"False") != NULL) {
landes 4:d8de964b3d2c 595 return false;
landes 4:d8de964b3d2c 596 }
landes 4:d8de964b3d2c 597 else if (strstr(buffer,"Invalid") != NULL) {
landes 4:d8de964b3d2c 598 return false;
landes 4:d8de964b3d2c 599 }
landes 1:7f873efe5b11 600 }
landes 1:7f873efe5b11 601
landes 1:7f873efe5b11 602 void Activate_Lock(){
landes 1:7f873efe5b11 603 doorlock =! doorlock;
landes 1:7f873efe5b11 604 }