CO528 - Assessment 3 - Guard Room Project
Dependencies: C12832 MMA7660 mbed-http
Fork of http-example by
Core.cpp
00001 #include "Core.h" 00002 00003 // Speaker 00004 PwmOut spkr(D6); 00005 00006 // Using Arduino pin notation 00007 C12832 lcd(D11, D13, D12, D7, D10); 00008 00009 // Orientation 00010 MMA7660 MMA(D14,D15); 00011 00012 // Led color 00013 DigitalOut r(D5); 00014 DigitalOut b(D8); 00015 DigitalOut g(D9); 00016 00017 // Used for USB connection 00018 Serial host(USBTX, USBRX); 00019 00020 Core::Core() { 00021 r = 1.0; 00022 b = 1.0; 00023 g = 1.0; 00024 _message = "Searching.."; 00025 _quiet = true; 00026 host.baud(38400); 00027 00028 _network = easy_connect(true); 00029 if (!_network) { 00030 printf("Cannot connect to the network, see serial output"); 00031 return; 00032 } 00033 } 00034 00035 bool Core::doorIsOpening() { 00036 if (MMA.x() > 0.2 || MMA.x() < -0.2) { 00037 return true; 00038 } 00039 return false; 00040 } 00041 00042 void Core::displayMessageOnScreen() { 00043 lcd.cls(); 00044 lcd.locate(0,0); 00045 lcd.printf("%s", _message); // Around 60max 00046 printf("%s\n", _message); 00047 } 00048 00049 void Core::triggerAlarm() { 00050 static bool state = true; 00051 00052 if (state) { 00053 r = 0; 00054 g = 1; 00055 b = 1; 00056 spkr.period(1.0/1000); 00057 spkr=0.5; 00058 00059 } else { 00060 r = 1; 00061 g = 1; 00062 b = 0; 00063 spkr.period(1.0/500); 00064 spkr=0.5; 00065 } 00066 state = !state; 00067 } 00068 00069 void Core::dumpResponseGet(HttpResponse* res) { 00070 char *pch; 00071 char *cstr = new char[res->get_body_as_string().length() + 1]; 00072 00073 strcpy(cstr, res->get_body_as_string().c_str()); 00074 pch = strtok (cstr,"{}:,"); 00075 int i = 0; 00076 while (pch != NULL) { 00077 pch = strtok (NULL, "{}:,"); 00078 if (i == 2) { 00079 _message = pch; 00080 } else if (i == 4) { 00081 if (strcmp(pch, "false") == 0) { 00082 _quiet = false; 00083 } else { 00084 _quiet = true; 00085 } 00086 00087 } 00088 i++; 00089 } 00090 } 00091 00092 void Core::getRequest() { 00093 HttpRequest* get_req = new HttpRequest(_network, HTTP_GET, "http://10.0.0.91:8080/api/alarms/room1"); 00094 HttpResponse* get_res = get_req->send(); 00095 00096 if (!get_res) { 00097 printf("HttpRequest get failed (error code %d)\n", get_req->get_error()); 00098 return; 00099 } 00100 00101 dumpResponseGet(get_res); 00102 00103 printf("\n end response get\n"); 00104 delete get_req; 00105 } 00106 00107 void Core::putRequest() { 00108 HttpRequest* put_req = new HttpRequest(_network, HTTP_PUT, "http://10.0.0.91:8080/api/alarms/room1"); 00109 put_req->set_header("Content-Type", "application/json"); 00110 00111 const char body[] = "{\"message\":\"Warning - Intruder !\", \"quiet\":\"false\"}"; 00112 00113 HttpResponse* put_res = put_req->send(body, strlen(body)); 00114 if (!put_res) { 00115 printf("HttpRequest put failed (error code %d)\n", put_req->get_error()); 00116 return; 00117 } 00118 delete put_req; 00119 } 00120 00121 void Core::shutDownAlarm(void) { 00122 r = 1; 00123 g = 1; 00124 b = 1; 00125 spkr=0; 00126 } 00127 00128 void Core::guard(void) { 00129 // Inifinte loop 00130 while (1) { 00131 if (doorIsOpening()) { 00132 putRequest(); // Update infos from local to API 00133 } 00134 getRequest(); // Update infos from API to local 00135 if (_quiet == false) { 00136 triggerAlarm(); 00137 } else { 00138 shutDownAlarm(); 00139 } 00140 displayMessageOnScreen(); 00141 wait(0.5); 00142 } 00143 } 00144 00145 int main() 00146 { 00147 Core *core = new Core(); 00148 00149 core->guard(); 00150 delete core; 00151 }
Generated on Thu Jul 14 2022 10:16:14 by 1.7.2