Server and Interrupts
Dependencies: mbed mbed-rtos C12832_lcd EthernetInterface
main.cpp@0:fe9e5099378a, 2020-11-01 (annotated)
- Committer:
- lamondt
- Date:
- Sun Nov 01 13:46:36 2020 +0000
- Revision:
- 0:fe9e5099378a
CW2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lamondt | 0:fe9e5099378a | 1 | #include "mbed.h" // Incluide the main Mbed library |
lamondt | 0:fe9e5099378a | 2 | #include "C12832_lcd.h" // Include the LCD library |
lamondt | 0:fe9e5099378a | 3 | #include <EthernetInterface.h> // Include the Ethernet Library |
lamondt | 0:fe9e5099378a | 4 | #include "mbed_debug.h" // Include the Mbed debug library |
lamondt | 0:fe9e5099378a | 5 | |
lamondt | 0:fe9e5099378a | 6 | DigitalOut RedLed (p23); |
lamondt | 0:fe9e5099378a | 7 | DigitalOut BlueLed (p25); // Declare digital outs for the RGB located on the application board |
lamondt | 0:fe9e5099378a | 8 | DigitalOut GreenLed (p24); |
lamondt | 0:fe9e5099378a | 9 | |
lamondt | 0:fe9e5099378a | 10 | C12832_LCD lcd; // Declare object called LCD from the C12832_LCD Class |
lamondt | 0:fe9e5099378a | 11 | |
lamondt | 0:fe9e5099378a | 12 | Ticker StrobeRoutine; // Declare ticker object called StrobeRoutine |
lamondt | 0:fe9e5099378a | 13 | |
lamondt | 0:fe9e5099378a | 14 | DigitalOut SecurityLightFront (LED1); // Declare object and define pin as a digital output |
lamondt | 0:fe9e5099378a | 15 | DigitalOut SecurityLightBack (LED2); // Declare object and define pin as a digital output |
lamondt | 0:fe9e5099378a | 16 | DigitalOut IndoorStrobe (LED4); // Declare object and define pin as a digital output |
lamondt | 0:fe9e5099378a | 17 | |
lamondt | 0:fe9e5099378a | 18 | InterruptIn Button (p13); // Declare button to send data |
lamondt | 0:fe9e5099378a | 19 | InterruptIn Reset (p14); // Declare object and define pin as an input with interrupt functionality |
lamondt | 0:fe9e5099378a | 20 | InterruptIn FrontDoorPIR(p21); // Declare object and define pin as an input with interrupt functionality |
lamondt | 0:fe9e5099378a | 21 | InterruptIn BackDoorPIR(p22); // Declare object and define pin as an input with interrupt functionality |
lamondt | 0:fe9e5099378a | 22 | |
lamondt | 0:fe9e5099378a | 23 | #define Success 0 |
lamondt | 0:fe9e5099378a | 24 | #define Failure -1 // Defining terms to logical values in order to |
lamondt | 0:fe9e5099378a | 25 | #define On Success // make programming more intuitive later on |
lamondt | 0:fe9e5099378a | 26 | #define Off 1 |
lamondt | 0:fe9e5099378a | 27 | |
lamondt | 0:fe9e5099378a | 28 | |
lamondt | 0:fe9e5099378a | 29 | int FrontDoorCounter = 0; // Creating counter variables to store the |
lamondt | 0:fe9e5099378a | 30 | int BackDoorCounter = 0; // number of times the sensor has been activated |
lamondt | 0:fe9e5099378a | 31 | |
lamondt | 0:fe9e5099378a | 32 | bool StartProcessing = 0; |
lamondt | 0:fe9e5099378a | 33 | bool Setup = 1; |
lamondt | 0:fe9e5099378a | 34 | bool FrontDoorStatus =0; // Declaring various varibales and setting their starting values |
lamondt | 0:fe9e5099378a | 35 | bool BackDoorStatus = 0; |
lamondt | 0:fe9e5099378a | 36 | bool ResetButtonStatus = 0; |
lamondt | 0:fe9e5099378a | 37 | |
lamondt | 0:fe9e5099378a | 38 | |
lamondt | 0:fe9e5099378a | 39 | void Flash() // Declare function called flash |
lamondt | 0:fe9e5099378a | 40 | { |
lamondt | 0:fe9e5099378a | 41 | IndoorStrobe=!IndoorStrobe; // Alternate the indoor strobe state |
lamondt | 0:fe9e5099378a | 42 | } |
lamondt | 0:fe9e5099378a | 43 | |
lamondt | 0:fe9e5099378a | 44 | void ISR_BackDoorPIR() // Interrupt service routine (ISR) to |
lamondt | 0:fe9e5099378a | 45 | { // run when back door PIR is activated |
lamondt | 0:fe9e5099378a | 46 | BackDoorStatus = 1; // ISR creates and sets variable to logic 1 |
lamondt | 0:fe9e5099378a | 47 | } |
lamondt | 0:fe9e5099378a | 48 | |
lamondt | 0:fe9e5099378a | 49 | void ISR_FrontDoorPIR() // ISR to run when front door PIR is activated |
lamondt | 0:fe9e5099378a | 50 | { |
lamondt | 0:fe9e5099378a | 51 | FrontDoorStatus = 1; // ISR creates and sets variable to logic 1 |
lamondt | 0:fe9e5099378a | 52 | } |
lamondt | 0:fe9e5099378a | 53 | |
lamondt | 0:fe9e5099378a | 54 | void ISR_Reset() // ISR to run when reset button is activated |
lamondt | 0:fe9e5099378a | 55 | { |
lamondt | 0:fe9e5099378a | 56 | ResetButtonStatus = 1; // ISR creates and sets variable to logic 1 |
lamondt | 0:fe9e5099378a | 57 | } |
lamondt | 0:fe9e5099378a | 58 | |
lamondt | 0:fe9e5099378a | 59 | void ISR_Data (void) // ISR to run when data transfer is required |
lamondt | 0:fe9e5099378a | 60 | { |
lamondt | 0:fe9e5099378a | 61 | StartProcessing = 1; // ISR creates and sets variable to logic 1 |
lamondt | 0:fe9e5099378a | 62 | } |
lamondt | 0:fe9e5099378a | 63 | |
lamondt | 0:fe9e5099378a | 64 | |
lamondt | 0:fe9e5099378a | 65 | |
lamondt | 0:fe9e5099378a | 66 | int main() |
lamondt | 0:fe9e5099378a | 67 | { |
lamondt | 0:fe9e5099378a | 68 | RedLed = BlueLed = GreenLed = 1; // Using inverse logic on RBG LED sets to off |
lamondt | 0:fe9e5099378a | 69 | lcd.cls (); |
lamondt | 0:fe9e5099378a | 70 | lcd.locate (0,0); // Print text |
lamondt | 0:fe9e5099378a | 71 | lcd.printf("Starting"); |
lamondt | 0:fe9e5099378a | 72 | |
lamondt | 0:fe9e5099378a | 73 | int ConnectionStatus = 0; // Creating variable to hold connection status values when returned |
lamondt | 0:fe9e5099378a | 74 | |
lamondt | 0:fe9e5099378a | 75 | EthernetInterface myEthernetInterface; // Creating objects from the ethernet and TCP classes |
lamondt | 0:fe9e5099378a | 76 | TCPSocketConnection myTCPSocketConnection; |
lamondt | 0:fe9e5099378a | 77 | |
lamondt | 0:fe9e5099378a | 78 | ConnectionStatus = myEthernetInterface.init(); // Initialise the interface and stores return value in ConnectionStatus |
lamondt | 0:fe9e5099378a | 79 | ConnectionStatus = myEthernetInterface.connect(10000); // Bring up the interface and store return value in ConnectionStatus |
lamondt | 0:fe9e5099378a | 80 | |
lamondt | 0:fe9e5099378a | 81 | lcd.cls (); |
lamondt | 0:fe9e5099378a | 82 | lcd.locate (0,10); |
lamondt | 0:fe9e5099378a | 83 | |
lamondt | 0:fe9e5099378a | 84 | if (ConnectionStatus == Failure){ |
lamondt | 0:fe9e5099378a | 85 | lcd.printf("Connection Error"); // Displaying the connection status of the ethernetInterface |
lamondt | 0:fe9e5099378a | 86 | wait(2); // if a failure value is returned for the ".init" or ".connect" functions |
lamondt | 0:fe9e5099378a | 87 | } // then connecton error is displayed on the LCD else Connected is displayed. |
lamondt | 0:fe9e5099378a | 88 | else { |
lamondt | 0:fe9e5099378a | 89 | lcd.printf("Connected"); // If Connection is successful connected is displayed |
lamondt | 0:fe9e5099378a | 90 | wait(2); // Wait to make message visible to user |
lamondt | 0:fe9e5099378a | 91 | } |
lamondt | 0:fe9e5099378a | 92 | |
lamondt | 0:fe9e5099378a | 93 | int TramissionStatus; // Variable created to store socket connection status |
lamondt | 0:fe9e5099378a | 94 | |
lamondt | 0:fe9e5099378a | 95 | const char* GroupName = "Group 7"; |
lamondt | 0:fe9e5099378a | 96 | const char* ClientIPAddress = myEthernetInterface.getIPAddress (); // Data required for trasnsmission is defined |
lamondt | 0:fe9e5099378a | 97 | const char* ClientMacAddress = myEthernetInterface.getMACAddress (); |
lamondt | 0:fe9e5099378a | 98 | const char* ServerIPAddress = "192.168.0.21"; |
lamondt | 0:fe9e5099378a | 99 | const int PortNumber = 29; |
lamondt | 0:fe9e5099378a | 100 | |
lamondt | 0:fe9e5099378a | 101 | |
lamondt | 0:fe9e5099378a | 102 | TramissionStatus = myTCPSocketConnection.connect (ServerIPAddress,PortNumber); // Connect the TCP socket to the server and store result in variable |
lamondt | 0:fe9e5099378a | 103 | lcd.locate (0,10); |
lamondt | 0:fe9e5099378a | 104 | lcd.printf("Transmission: %d",TramissionStatus); // Result of socket conection printed to LCD |
lamondt | 0:fe9e5099378a | 105 | |
lamondt | 0:fe9e5099378a | 106 | if (TramissionStatus == Success) // Green LED activated if connection is successful |
lamondt | 0:fe9e5099378a | 107 | { |
lamondt | 0:fe9e5099378a | 108 | GreenLed = On; |
lamondt | 0:fe9e5099378a | 109 | } |
lamondt | 0:fe9e5099378a | 110 | |
lamondt | 0:fe9e5099378a | 111 | Button.rise (&ISR_Data); // Interrupt for data transfe to server |
lamondt | 0:fe9e5099378a | 112 | Button.enable_irq (); |
lamondt | 0:fe9e5099378a | 113 | FrontDoorPIR.rise(&ISR_FrontDoorPIR); // Interrupt for Front Door PIR detection on the rise of the signal and calls the ISR |
lamondt | 0:fe9e5099378a | 114 | BackDoorPIR.rise(&ISR_BackDoorPIR); // Interrupt for Back Door PIR detection on the rise of the signal and calls the ISR |
lamondt | 0:fe9e5099378a | 115 | Reset.rise(&ISR_Reset); // Interrupt for Reset button activation on the rise of the signal and calls the ISR |
lamondt | 0:fe9e5099378a | 116 | |
lamondt | 0:fe9e5099378a | 117 | |
lamondt | 0:fe9e5099378a | 118 | while(1) |
lamondt | 0:fe9e5099378a | 119 | { |
lamondt | 0:fe9e5099378a | 120 | if (Setup == 1) // Statement activated only on start up or reset |
lamondt | 0:fe9e5099378a | 121 | { |
lamondt | 0:fe9e5099378a | 122 | IndoorStrobe = 0; |
lamondt | 0:fe9e5099378a | 123 | SecurityLightFront = 0; // Set lights initial state to off |
lamondt | 0:fe9e5099378a | 124 | SecurityLightBack = 0; |
lamondt | 0:fe9e5099378a | 125 | lcd.cls(); // Clear LCD |
lamondt | 0:fe9e5099378a | 126 | lcd.locate(0,0); // Set text position |
lamondt | 0:fe9e5099378a | 127 | lcd.printf ("System Active"); // Print text to LCD |
lamondt | 0:fe9e5099378a | 128 | Setup = 0; // Clear Setup variable so statement does not run again |
lamondt | 0:fe9e5099378a | 129 | goto Start; // Exits the if statement |
lamondt | 0:fe9e5099378a | 130 | } |
lamondt | 0:fe9e5099378a | 131 | |
lamondt | 0:fe9e5099378a | 132 | Start: |
lamondt | 0:fe9e5099378a | 133 | |
lamondt | 0:fe9e5099378a | 134 | if (FrontDoorStatus ==1) // If statement acitvated by Front Door PIR activation |
lamondt | 0:fe9e5099378a | 135 | { |
lamondt | 0:fe9e5099378a | 136 | FrontDoorCounter ++; // Incease counter value by 1 to keep record of total activations |
lamondt | 0:fe9e5099378a | 137 | lcd.locate (0,0); |
lamondt | 0:fe9e5099378a | 138 | lcd.printf(" "); // Clear the the screen except for the status of the other PIR activation |
lamondt | 0:fe9e5099378a | 139 | lcd.locate (0,20); |
lamondt | 0:fe9e5099378a | 140 | lcd.printf(" "); |
lamondt | 0:fe9e5099378a | 141 | lcd.locate(0,20); // Set text position |
lamondt | 0:fe9e5099378a | 142 | lcd.printf("Intruder at front door!!"); // Print text to LCD |
lamondt | 0:fe9e5099378a | 143 | StrobeRoutine.attach(&Flash,1); // Attach the StrobeRoutine object to the flash function to flash the LED at a set frequency |
lamondt | 0:fe9e5099378a | 144 | SecurityLightBack = 1; // Turn on security light |
lamondt | 0:fe9e5099378a | 145 | SecurityLightFront = 1; // Turn on security light |
lamondt | 0:fe9e5099378a | 146 | FrontDoorStatus = 0; // Reset variable so the if statement doesnt run repeatedly |
lamondt | 0:fe9e5099378a | 147 | } |
lamondt | 0:fe9e5099378a | 148 | |
lamondt | 0:fe9e5099378a | 149 | if (BackDoorStatus == 1) // If statement acitvated by Front Door PIR activation |
lamondt | 0:fe9e5099378a | 150 | { |
lamondt | 0:fe9e5099378a | 151 | BackDoorCounter ++; // Incease counter value by 1 to keep record of total activations |
lamondt | 0:fe9e5099378a | 152 | lcd.locate (0,0); |
lamondt | 0:fe9e5099378a | 153 | lcd.printf(" "); // Clear the the screen except for the status of the other PIR activation |
lamondt | 0:fe9e5099378a | 154 | lcd.locate(0,10); |
lamondt | 0:fe9e5099378a | 155 | lcd.printf(" "); |
lamondt | 0:fe9e5099378a | 156 | lcd.locate(0,10); // Set text position |
lamondt | 0:fe9e5099378a | 157 | lcd.printf("Intruder at Back door!! "); // Print text to LCD |
lamondt | 0:fe9e5099378a | 158 | StrobeRoutine.attach(&Flash,1); // Attach the stroberoutine object to the flash function to flash the LED at a set frequency |
lamondt | 0:fe9e5099378a | 159 | SecurityLightBack = 1; // Turn on security light |
lamondt | 0:fe9e5099378a | 160 | SecurityLightFront = 1; // Turn on security light |
lamondt | 0:fe9e5099378a | 161 | BackDoorStatus = 0; |
lamondt | 0:fe9e5099378a | 162 | } |
lamondt | 0:fe9e5099378a | 163 | |
lamondt | 0:fe9e5099378a | 164 | if (ResetButtonStatus ==1) |
lamondt | 0:fe9e5099378a | 165 | { |
lamondt | 0:fe9e5099378a | 166 | StrobeRoutine.detach(); // Turn off the indoor StrobeRoutine |
lamondt | 0:fe9e5099378a | 167 | IndoorStrobe = 0; // Set the Indoor LED to off(Inverse logic) |
lamondt | 0:fe9e5099378a | 168 | SecurityLightBack = 0; // Set the Outdoor security lights to off |
lamondt | 0:fe9e5099378a | 169 | SecurityLightFront = 0; // Set the Outdoor security lights to off |
lamondt | 0:fe9e5099378a | 170 | lcd.cls(); // Clear LCD display |
lamondt | 0:fe9e5099378a | 171 | lcd.locate(0,0); // Set text position |
lamondt | 0:fe9e5099378a | 172 | lcd.printf("System Reset"); // Print text to LCD |
lamondt | 0:fe9e5099378a | 173 | wait (3); // Wait to allow system stability |
lamondt | 0:fe9e5099378a | 174 | ResetButtonStatus = 0; |
lamondt | 0:fe9e5099378a | 175 | Setup = 1; // Run the setup function and return system to active status |
lamondt | 0:fe9e5099378a | 176 | |
lamondt | 0:fe9e5099378a | 177 | } |
lamondt | 0:fe9e5099378a | 178 | |
lamondt | 0:fe9e5099378a | 179 | if (StartProcessing) |
lamondt | 0:fe9e5099378a | 180 | { |
lamondt | 0:fe9e5099378a | 181 | BlueLed = On; |
lamondt | 0:fe9e5099378a | 182 | wait (0.5); |
lamondt | 0:fe9e5099378a | 183 | char *ReceiveBuffer = new char [256]; |
lamondt | 0:fe9e5099378a | 184 | char *SendBuffer = new char [256]; |
lamondt | 0:fe9e5099378a | 185 | int FD = FrontDoorCounter; |
lamondt | 0:fe9e5099378a | 186 | int BD = BackDoorCounter; // variables declared including type and size |
lamondt | 0:fe9e5099378a | 187 | int WrittenByteCount = 0; |
lamondt | 0:fe9e5099378a | 188 | int ReadByteCount = 0; |
lamondt | 0:fe9e5099378a | 189 | char ResultBuffer [10]; |
lamondt | 0:fe9e5099378a | 190 | char *Information = new char [100]; |
lamondt | 0:fe9e5099378a | 191 | |
lamondt | 0:fe9e5099378a | 192 | strcat (Information, "Front Door: "); |
lamondt | 0:fe9e5099378a | 193 | sprintf(ResultBuffer, "%d ", FD); |
lamondt | 0:fe9e5099378a | 194 | strcat (Information, ResultBuffer); // Values to be sent to the database are converted to string format |
lamondt | 0:fe9e5099378a | 195 | strcat (Information, "Back Door: "); |
lamondt | 0:fe9e5099378a | 196 | sprintf(ResultBuffer, "%d", BD); |
lamondt | 0:fe9e5099378a | 197 | strcat (Information, ResultBuffer); |
lamondt | 0:fe9e5099378a | 198 | |
lamondt | 0:fe9e5099378a | 199 | strcat (SendBuffer, GroupName); |
lamondt | 0:fe9e5099378a | 200 | strcat (SendBuffer, "|"); |
lamondt | 0:fe9e5099378a | 201 | strcat (SendBuffer, ClientMacAddress); |
lamondt | 0:fe9e5099378a | 202 | strcat (SendBuffer, "|"); // Format and sequence of information to be sent |
lamondt | 0:fe9e5099378a | 203 | strcat (SendBuffer, ClientIPAddress); |
lamondt | 0:fe9e5099378a | 204 | strcat (SendBuffer, "|"); |
lamondt | 0:fe9e5099378a | 205 | strcat (SendBuffer, Information); |
lamondt | 0:fe9e5099378a | 206 | strcat (SendBuffer, "\r\n"); |
lamondt | 0:fe9e5099378a | 207 | |
lamondt | 0:fe9e5099378a | 208 | debug (SendBuffer); |
lamondt | 0:fe9e5099378a | 209 | |
lamondt | 0:fe9e5099378a | 210 | WrittenByteCount = myTCPSocketConnection.send_all (SendBuffer, strlen (SendBuffer)); // Send data |
lamondt | 0:fe9e5099378a | 211 | |
lamondt | 0:fe9e5099378a | 212 | lcd.cls(); |
lamondt | 0:fe9e5099378a | 213 | lcd.locate (10,10); // Print byte value of data sent to LCD |
lamondt | 0:fe9e5099378a | 214 | lcd.printf("Write: %d",WrittenByteCount); |
lamondt | 0:fe9e5099378a | 215 | |
lamondt | 0:fe9e5099378a | 216 | if (WrittenByteCount == Failure) |
lamondt | 0:fe9e5099378a | 217 | { // LED to red if data sending failure |
lamondt | 0:fe9e5099378a | 218 | RedLed = On; // doesnt reset StartProcessing so the loop repeats. |
lamondt | 0:fe9e5099378a | 219 | } // ie it attempts to transfer the data again |
lamondt | 0:fe9e5099378a | 220 | |
lamondt | 0:fe9e5099378a | 221 | else |
lamondt | 0:fe9e5099378a | 222 | { |
lamondt | 0:fe9e5099378a | 223 | ReadByteCount = myTCPSocketConnection.receive_all (ReceiveBuffer, strlen (ReceiveBuffer)); |
lamondt | 0:fe9e5099378a | 224 | |
lamondt | 0:fe9e5099378a | 225 | lcd.locate (20,0); |
lamondt | 0:fe9e5099378a | 226 | lcd.printf("Read: %d",ReadByteCount); // Print the read return byte value from the server |
lamondt | 0:fe9e5099378a | 227 | if (ReadByteCount == Failure) |
lamondt | 0:fe9e5099378a | 228 | { |
lamondt | 0:fe9e5099378a | 229 | RedLed = On; |
lamondt | 0:fe9e5099378a | 230 | } |
lamondt | 0:fe9e5099378a | 231 | else |
lamondt | 0:fe9e5099378a | 232 | { |
lamondt | 0:fe9e5099378a | 233 | StartProcessing = false; // If data transfer is successful reset variable to false |
lamondt | 0:fe9e5099378a | 234 | } // so data isnt sent again |
lamondt | 0:fe9e5099378a | 235 | } |
lamondt | 0:fe9e5099378a | 236 | BlueLed = Off; |
lamondt | 0:fe9e5099378a | 237 | |
lamondt | 0:fe9e5099378a | 238 | delete[] ReceiveBuffer; |
lamondt | 0:fe9e5099378a | 239 | delete[] SendBuffer; |
lamondt | 0:fe9e5099378a | 240 | wait(2); |
lamondt | 0:fe9e5099378a | 241 | lcd.cls(); |
lamondt | 0:fe9e5099378a | 242 | lcd.locate(0,0); |
lamondt | 0:fe9e5099378a | 243 | lcd.printf ("System Active"); // Print text to LCD |
lamondt | 0:fe9e5099378a | 244 | } // End of If (StartProcessing) |
lamondt | 0:fe9e5099378a | 245 | } // End of While |
lamondt | 0:fe9e5099378a | 246 | } // End of Main |
lamondt | 0:fe9e5099378a | 247 |