IoT for mbed1

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

Fork of IoT by Tal Landes

Committer:
landes
Date:
Sun Dec 06 20:18:37 2015 +0000
Revision:
2:922d5b43bee3
Parent:
1:7f873efe5b11
Child:
4:d8de964b3d2c
blbl

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