CO528 - Assessment 3 - Guard Room Project

Dependencies:   C12832 MMA7660 mbed-http

Fork of http-example by sandbox

Committer:
fpaf2
Date:
Wed May 03 12:06:22 2017 +0000
Revision:
15:8965fc128e7b
CO528 - Assessment 3 - Guard Room project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fpaf2 15:8965fc128e7b 1 #include "Core.h"
fpaf2 15:8965fc128e7b 2
fpaf2 15:8965fc128e7b 3 // Speaker
fpaf2 15:8965fc128e7b 4 PwmOut spkr(D6);
fpaf2 15:8965fc128e7b 5
fpaf2 15:8965fc128e7b 6 // Using Arduino pin notation
fpaf2 15:8965fc128e7b 7 C12832 lcd(D11, D13, D12, D7, D10);
fpaf2 15:8965fc128e7b 8
fpaf2 15:8965fc128e7b 9 // Orientation
fpaf2 15:8965fc128e7b 10 MMA7660 MMA(D14,D15);
fpaf2 15:8965fc128e7b 11
fpaf2 15:8965fc128e7b 12 // Led color
fpaf2 15:8965fc128e7b 13 DigitalOut r(D5);
fpaf2 15:8965fc128e7b 14 DigitalOut b(D8);
fpaf2 15:8965fc128e7b 15 DigitalOut g(D9);
fpaf2 15:8965fc128e7b 16
fpaf2 15:8965fc128e7b 17 // Used for USB connection
fpaf2 15:8965fc128e7b 18 Serial host(USBTX, USBRX);
fpaf2 15:8965fc128e7b 19
fpaf2 15:8965fc128e7b 20 Core::Core() {
fpaf2 15:8965fc128e7b 21 r = 1.0;
fpaf2 15:8965fc128e7b 22 b = 1.0;
fpaf2 15:8965fc128e7b 23 g = 1.0;
fpaf2 15:8965fc128e7b 24 _message = "Searching..";
fpaf2 15:8965fc128e7b 25 _quiet = true;
fpaf2 15:8965fc128e7b 26 host.baud(38400);
fpaf2 15:8965fc128e7b 27
fpaf2 15:8965fc128e7b 28 _network = easy_connect(true);
fpaf2 15:8965fc128e7b 29 if (!_network) {
fpaf2 15:8965fc128e7b 30 printf("Cannot connect to the network, see serial output");
fpaf2 15:8965fc128e7b 31 return;
fpaf2 15:8965fc128e7b 32 }
fpaf2 15:8965fc128e7b 33 }
fpaf2 15:8965fc128e7b 34
fpaf2 15:8965fc128e7b 35 bool Core::doorIsOpening() {
fpaf2 15:8965fc128e7b 36 if (MMA.x() > 0.2 || MMA.x() < -0.2) {
fpaf2 15:8965fc128e7b 37 return true;
fpaf2 15:8965fc128e7b 38 }
fpaf2 15:8965fc128e7b 39 return false;
fpaf2 15:8965fc128e7b 40 }
fpaf2 15:8965fc128e7b 41
fpaf2 15:8965fc128e7b 42 void Core::displayMessageOnScreen() {
fpaf2 15:8965fc128e7b 43 lcd.cls();
fpaf2 15:8965fc128e7b 44 lcd.locate(0,0);
fpaf2 15:8965fc128e7b 45 lcd.printf("%s", _message); // Around 60max
fpaf2 15:8965fc128e7b 46 printf("%s\n", _message);
fpaf2 15:8965fc128e7b 47 }
fpaf2 15:8965fc128e7b 48
fpaf2 15:8965fc128e7b 49 void Core::triggerAlarm() {
fpaf2 15:8965fc128e7b 50 static bool state = true;
fpaf2 15:8965fc128e7b 51
fpaf2 15:8965fc128e7b 52 if (state) {
fpaf2 15:8965fc128e7b 53 r = 0;
fpaf2 15:8965fc128e7b 54 g = 1;
fpaf2 15:8965fc128e7b 55 b = 1;
fpaf2 15:8965fc128e7b 56 spkr.period(1.0/1000);
fpaf2 15:8965fc128e7b 57 spkr=0.5;
fpaf2 15:8965fc128e7b 58
fpaf2 15:8965fc128e7b 59 } else {
fpaf2 15:8965fc128e7b 60 r = 1;
fpaf2 15:8965fc128e7b 61 g = 1;
fpaf2 15:8965fc128e7b 62 b = 0;
fpaf2 15:8965fc128e7b 63 spkr.period(1.0/500);
fpaf2 15:8965fc128e7b 64 spkr=0.5;
fpaf2 15:8965fc128e7b 65 }
fpaf2 15:8965fc128e7b 66 state = !state;
fpaf2 15:8965fc128e7b 67 }
fpaf2 15:8965fc128e7b 68
fpaf2 15:8965fc128e7b 69 void Core::dumpResponseGet(HttpResponse* res) {
fpaf2 15:8965fc128e7b 70 char *pch;
fpaf2 15:8965fc128e7b 71 char *cstr = new char[res->get_body_as_string().length() + 1];
fpaf2 15:8965fc128e7b 72
fpaf2 15:8965fc128e7b 73 strcpy(cstr, res->get_body_as_string().c_str());
fpaf2 15:8965fc128e7b 74 pch = strtok (cstr,"{}:,");
fpaf2 15:8965fc128e7b 75 int i = 0;
fpaf2 15:8965fc128e7b 76 while (pch != NULL) {
fpaf2 15:8965fc128e7b 77 pch = strtok (NULL, "{}:,");
fpaf2 15:8965fc128e7b 78 if (i == 2) {
fpaf2 15:8965fc128e7b 79 _message = pch;
fpaf2 15:8965fc128e7b 80 } else if (i == 4) {
fpaf2 15:8965fc128e7b 81 if (strcmp(pch, "false") == 0) {
fpaf2 15:8965fc128e7b 82 _quiet = false;
fpaf2 15:8965fc128e7b 83 } else {
fpaf2 15:8965fc128e7b 84 _quiet = true;
fpaf2 15:8965fc128e7b 85 }
fpaf2 15:8965fc128e7b 86
fpaf2 15:8965fc128e7b 87 }
fpaf2 15:8965fc128e7b 88 i++;
fpaf2 15:8965fc128e7b 89 }
fpaf2 15:8965fc128e7b 90 }
fpaf2 15:8965fc128e7b 91
fpaf2 15:8965fc128e7b 92 void Core::getRequest() {
fpaf2 15:8965fc128e7b 93 HttpRequest* get_req = new HttpRequest(_network, HTTP_GET, "http://10.0.0.91:8080/api/alarms/room1");
fpaf2 15:8965fc128e7b 94 HttpResponse* get_res = get_req->send();
fpaf2 15:8965fc128e7b 95
fpaf2 15:8965fc128e7b 96 if (!get_res) {
fpaf2 15:8965fc128e7b 97 printf("HttpRequest get failed (error code %d)\n", get_req->get_error());
fpaf2 15:8965fc128e7b 98 return;
fpaf2 15:8965fc128e7b 99 }
fpaf2 15:8965fc128e7b 100
fpaf2 15:8965fc128e7b 101 dumpResponseGet(get_res);
fpaf2 15:8965fc128e7b 102
fpaf2 15:8965fc128e7b 103 printf("\n end response get\n");
fpaf2 15:8965fc128e7b 104 delete get_req;
fpaf2 15:8965fc128e7b 105 }
fpaf2 15:8965fc128e7b 106
fpaf2 15:8965fc128e7b 107 void Core::putRequest() {
fpaf2 15:8965fc128e7b 108 HttpRequest* put_req = new HttpRequest(_network, HTTP_PUT, "http://10.0.0.91:8080/api/alarms/room1");
fpaf2 15:8965fc128e7b 109 put_req->set_header("Content-Type", "application/json");
fpaf2 15:8965fc128e7b 110
fpaf2 15:8965fc128e7b 111 const char body[] = "{\"message\":\"Warning - Intruder !\", \"quiet\":\"false\"}";
fpaf2 15:8965fc128e7b 112
fpaf2 15:8965fc128e7b 113 HttpResponse* put_res = put_req->send(body, strlen(body));
fpaf2 15:8965fc128e7b 114 if (!put_res) {
fpaf2 15:8965fc128e7b 115 printf("HttpRequest put failed (error code %d)\n", put_req->get_error());
fpaf2 15:8965fc128e7b 116 return;
fpaf2 15:8965fc128e7b 117 }
fpaf2 15:8965fc128e7b 118 delete put_req;
fpaf2 15:8965fc128e7b 119 }
fpaf2 15:8965fc128e7b 120
fpaf2 15:8965fc128e7b 121 void Core::shutDownAlarm(void) {
fpaf2 15:8965fc128e7b 122 r = 1;
fpaf2 15:8965fc128e7b 123 g = 1;
fpaf2 15:8965fc128e7b 124 b = 1;
fpaf2 15:8965fc128e7b 125 spkr=0;
fpaf2 15:8965fc128e7b 126 }
fpaf2 15:8965fc128e7b 127
fpaf2 15:8965fc128e7b 128 void Core::guard(void) {
fpaf2 15:8965fc128e7b 129 // Inifinte loop
fpaf2 15:8965fc128e7b 130 while (1) {
fpaf2 15:8965fc128e7b 131 if (doorIsOpening()) {
fpaf2 15:8965fc128e7b 132 putRequest(); // Update infos from local to API
fpaf2 15:8965fc128e7b 133 }
fpaf2 15:8965fc128e7b 134 getRequest(); // Update infos from API to local
fpaf2 15:8965fc128e7b 135 if (_quiet == false) {
fpaf2 15:8965fc128e7b 136 triggerAlarm();
fpaf2 15:8965fc128e7b 137 } else {
fpaf2 15:8965fc128e7b 138 shutDownAlarm();
fpaf2 15:8965fc128e7b 139 }
fpaf2 15:8965fc128e7b 140 displayMessageOnScreen();
fpaf2 15:8965fc128e7b 141 wait(0.5);
fpaf2 15:8965fc128e7b 142 }
fpaf2 15:8965fc128e7b 143 }
fpaf2 15:8965fc128e7b 144
fpaf2 15:8965fc128e7b 145 int main()
fpaf2 15:8965fc128e7b 146 {
fpaf2 15:8965fc128e7b 147 Core *core = new Core();
fpaf2 15:8965fc128e7b 148
fpaf2 15:8965fc128e7b 149 core->guard();
fpaf2 15:8965fc128e7b 150 delete core;
fpaf2 15:8965fc128e7b 151 }