The code for the home base station.

Dependencies:   mbed

Fork of HC05_send by Luke Pell

Committer:
natschwa
Date:
Mon Mar 19 17:24:25 2018 +0000
Revision:
1:f475551471a5
Parent:
0:c3b88e0b90bf
Finished

Who changed what in which revision?

UserRevisionLine numberNew contents of line
natschwa 1:f475551471a5 1 //Home_System
natschwa 1:f475551471a5 2
lmpell 0:c3b88e0b90bf 3 #include "mbed.h"
lmpell 0:c3b88e0b90bf 4
natschwa 1:f475551471a5 5 #define RX_ PTC14
natschwa 1:f475551471a5 6 #define TX_ PTC15
natschwa 1:f475551471a5 7
natschwa 1:f475551471a5 8 Timeout response; // Timer
lmpell 0:c3b88e0b90bf 9
natschwa 1:f475551471a5 10 //Delay declared in seconds
natschwa 1:f475551471a5 11 /*GPIO declaration*/
natschwa 1:f475551471a5 12 DigitalOut Green(LED_GREEN);
natschwa 1:f475551471a5 13 DigitalOut Red(LED_RED);
natschwa 1:f475551471a5 14 DigitalOut Blue(LED_BLUE);
natschwa 1:f475551471a5 15
lmpell 0:c3b88e0b90bf 16 DigitalIn sw2(SW2);
lmpell 0:c3b88e0b90bf 17 DigitalIn motion(D7);
lmpell 0:c3b88e0b90bf 18
natschwa 1:f475551471a5 19 Serial BlueTooth(TX_, RX_); // bluetooth serial port
natschwa 1:f475551471a5 20 Serial pc(USBTX, USBRX ); // USB serial port
lmpell 0:c3b88e0b90bf 21
natschwa 1:f475551471a5 22 void Alert() { // Timer call back function
lmpell 0:c3b88e0b90bf 23 printf("TimeOut\n\r");
natschwa 1:f475551471a5 24 Blue = 1;
natschwa 1:f475551471a5 25 Green = 1;
natschwa 1:f475551471a5 26 Red = 0;
natschwa 1:f475551471a5 27 int run = 5000;
lmpell 0:c3b88e0b90bf 28 while(run--){
natschwa 1:f475551471a5 29 BlueTooth.putc('F'); // send alert to slave
lmpell 0:c3b88e0b90bf 30 }
lmpell 0:c3b88e0b90bf 31 printf("Alert Sent\n\r");
natschwa 1:f475551471a5 32
lmpell 0:c3b88e0b90bf 33 run = 100;
lmpell 0:c3b88e0b90bf 34 while(run--){
natschwa 1:f475551471a5 35 BlueTooth.putc('G'); // send alarm timeout to slave
lmpell 0:c3b88e0b90bf 36 }
natschwa 1:f475551471a5 37 printf("Alert Timeout\n\r");
natschwa 1:f475551471a5 38
lmpell 0:c3b88e0b90bf 39 }
lmpell 0:c3b88e0b90bf 40
lmpell 0:c3b88e0b90bf 41
lmpell 0:c3b88e0b90bf 42 int main()
lmpell 0:c3b88e0b90bf 43 {
lmpell 0:c3b88e0b90bf 44 char send = 'Z';
lmpell 0:c3b88e0b90bf 45 char hold = 'a';
lmpell 0:c3b88e0b90bf 46 char correct = 'C';
lmpell 0:c3b88e0b90bf 47
natschwa 1:f475551471a5 48 /*LedsOFF (active low)*/
natschwa 1:f475551471a5 49 Red = 1;
natschwa 1:f475551471a5 50 Green = 1;
natschwa 1:f475551471a5 51 Blue = 1;
natschwa 1:f475551471a5 52
lmpell 0:c3b88e0b90bf 53 pc.baud(9600);
natschwa 1:f475551471a5 54 BlueTooth.baud(9600);
lmpell 0:c3b88e0b90bf 55 printf("Master Connecting to Slave:\n\r");
lmpell 0:c3b88e0b90bf 56
lmpell 0:c3b88e0b90bf 57 while (send!='R') {
lmpell 0:c3b88e0b90bf 58
natschwa 1:f475551471a5 59 BlueTooth.putc(send);
lmpell 0:c3b88e0b90bf 60 wait(0.5f); // wait a small period of time
lmpell 0:c3b88e0b90bf 61
natschwa 1:f475551471a5 62 if(BlueTooth.readable()) // read data from the bluetooth serial port
lmpell 0:c3b88e0b90bf 63 {
lmpell 0:c3b88e0b90bf 64 while(hold!='X'){
natschwa 1:f475551471a5 65 hold = BlueTooth.getc();
lmpell 0:c3b88e0b90bf 66 }
lmpell 0:c3b88e0b90bf 67 if(hold=='X')
lmpell 0:c3b88e0b90bf 68 {
lmpell 0:c3b88e0b90bf 69 printf("Ack Recieved from Slave: Sytem Ready!\n\r");
natschwa 1:f475551471a5 70 Blue = 0;
natschwa 1:f475551471a5 71 Red = 1;
natschwa 1:f475551471a5 72 Green = 1;
lmpell 0:c3b88e0b90bf 73 send = 'R'; //System Ready
lmpell 0:c3b88e0b90bf 74 }
lmpell 0:c3b88e0b90bf 75 }
lmpell 0:c3b88e0b90bf 76 wait(0.5f); // wait a small period of time
lmpell 0:c3b88e0b90bf 77 }
lmpell 0:c3b88e0b90bf 78
natschwa 1:f475551471a5 79 // -----------------------------------------------------------------------------------------
lmpell 0:c3b88e0b90bf 80 while(1)
lmpell 0:c3b88e0b90bf 81 {
natschwa 1:f475551471a5 82 while(motion) // detecting motion
natschwa 1:f475551471a5 83 { Blue = 1;
lmpell 0:c3b88e0b90bf 84 printf("Motion Detected\n\r");
lmpell 0:c3b88e0b90bf 85
natschwa 1:f475551471a5 86 if(sw2==1){ // use has 2 second window to enter password after motion dection
natschwa 1:f475551471a5 87 response.attach(&Alert, 2.0); // set timer for 2 seconds
natschwa 1:f475551471a5 88 BlueTooth.putc('A'); // send alert to slave
natschwa 1:f475551471a5 89 // LED yellow
natschwa 1:f475551471a5 90 Green=0;
natschwa 1:f475551471a5 91 Red = 0;
lmpell 0:c3b88e0b90bf 92 }
lmpell 0:c3b88e0b90bf 93 else{
lmpell 0:c3b88e0b90bf 94 printf("Sw2 pressed\n\r");
natschwa 1:f475551471a5 95 response.detach(); // disable timer
natschwa 1:f475551471a5 96 printf("Correct Password\n\r");
natschwa 1:f475551471a5 97 int run = 1000;
lmpell 0:c3b88e0b90bf 98 while(run--){
natschwa 1:f475551471a5 99 BlueTooth.putc(correct); // send correct password msg to slave
natschwa 1:f475551471a5 100 Red = 1;
natschwa 1:f475551471a5 101 Green = 0;
lmpell 0:c3b88e0b90bf 102 }
lmpell 0:c3b88e0b90bf 103 wait(3);
lmpell 0:c3b88e0b90bf 104 }
lmpell 0:c3b88e0b90bf 105 }
lmpell 0:c3b88e0b90bf 106 }
natschwa 1:f475551471a5 107 }//end Main