CO528 - Assessment 3 - Guard Room Project
Dependencies: C12832 MMA7660 mbed-http
Fork of http-example by
Revision 15:8965fc128e7b, committed 2017-05-03
- Comitter:
- fpaf2
- Date:
- Wed May 03 12:06:22 2017 +0000
- Parent:
- 14:3c173847e681
- Commit message:
- CO528 - Assessment 3 - Guard Room project
Changed in this revision
diff -r 3c173847e681 -r 8965fc128e7b C12832.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832.lib Wed May 03 12:06:22 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/askksa12543/code/C12832/#990d5eec2ef6
diff -r 3c173847e681 -r 8965fc128e7b MMA7660.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA7660.lib Wed May 03 12:06:22 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/Sissors/code/MMA7660/#36a163511e34
diff -r 3c173847e681 -r 8965fc128e7b mbedtls_entropy_config.h --- a/mbedtls_entropy_config.h Thu Mar 30 15:15:47 2017 +0200 +++ b/mbedtls_entropy_config.h Wed May 03 12:06:22 2017 +0000 @@ -17,8 +17,6 @@ * This file is part of mbed TLS (https://tls.mbed.org) */ -#include "select-demo.h" - /* Enable entropy for K64F and K22F. This means entropy is disabled for all other targets. */ /* Do **NOT** deploy this code in production on other targets! */ /* See https://tls.mbed.org/kb/how-to/add-entropy-sources-to-entropy-pool */
diff -r 3c173847e681 -r 8965fc128e7b source/Core.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/Core.cpp Wed May 03 12:06:22 2017 +0000 @@ -0,0 +1,151 @@ +#include "Core.h" + +// Speaker +PwmOut spkr(D6); + +// Using Arduino pin notation +C12832 lcd(D11, D13, D12, D7, D10); + +// Orientation +MMA7660 MMA(D14,D15); + +// Led color +DigitalOut r(D5); +DigitalOut b(D8); +DigitalOut g(D9); + +// Used for USB connection +Serial host(USBTX, USBRX); + +Core::Core() { + r = 1.0; + b = 1.0; + g = 1.0; + _message = "Searching.."; + _quiet = true; + host.baud(38400); + + _network = easy_connect(true); + if (!_network) { + printf("Cannot connect to the network, see serial output"); + return; + } +} + +bool Core::doorIsOpening() { + if (MMA.x() > 0.2 || MMA.x() < -0.2) { + return true; + } + return false; +} + +void Core::displayMessageOnScreen() { + lcd.cls(); + lcd.locate(0,0); + lcd.printf("%s", _message); // Around 60max + printf("%s\n", _message); +} + +void Core::triggerAlarm() { + static bool state = true; + + if (state) { + r = 0; + g = 1; + b = 1; + spkr.period(1.0/1000); + spkr=0.5; + + } else { + r = 1; + g = 1; + b = 0; + spkr.period(1.0/500); + spkr=0.5; + } + state = !state; +} + +void Core::dumpResponseGet(HttpResponse* res) { + char *pch; + char *cstr = new char[res->get_body_as_string().length() + 1]; + + strcpy(cstr, res->get_body_as_string().c_str()); + pch = strtok (cstr,"{}:,"); + int i = 0; + while (pch != NULL) { + pch = strtok (NULL, "{}:,"); + if (i == 2) { + _message = pch; + } else if (i == 4) { + if (strcmp(pch, "false") == 0) { + _quiet = false; + } else { + _quiet = true; + } + + } + i++; + } +} + +void Core::getRequest() { + HttpRequest* get_req = new HttpRequest(_network, HTTP_GET, "http://10.0.0.91:8080/api/alarms/room1"); + HttpResponse* get_res = get_req->send(); + + if (!get_res) { + printf("HttpRequest get failed (error code %d)\n", get_req->get_error()); + return; + } + + dumpResponseGet(get_res); + + printf("\n end response get\n"); + delete get_req; +} + +void Core::putRequest() { + HttpRequest* put_req = new HttpRequest(_network, HTTP_PUT, "http://10.0.0.91:8080/api/alarms/room1"); + put_req->set_header("Content-Type", "application/json"); + + const char body[] = "{\"message\":\"Warning - Intruder !\", \"quiet\":\"false\"}"; + + HttpResponse* put_res = put_req->send(body, strlen(body)); + if (!put_res) { + printf("HttpRequest put failed (error code %d)\n", put_req->get_error()); + return; + } + delete put_req; +} + +void Core::shutDownAlarm(void) { + r = 1; + g = 1; + b = 1; + spkr=0; +} + +void Core::guard(void) { + // Inifinte loop + while (1) { + if (doorIsOpening()) { + putRequest(); // Update infos from local to API + } + getRequest(); // Update infos from API to local + if (_quiet == false) { + triggerAlarm(); + } else { + shutDownAlarm(); + } + displayMessageOnScreen(); + wait(0.5); + } +} + +int main() +{ + Core *core = new Core(); + + core->guard(); + delete core; +} \ No newline at end of file
diff -r 3c173847e681 -r 8965fc128e7b source/Core.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/Core.h Wed May 03 12:06:22 2017 +0000 @@ -0,0 +1,63 @@ +/* mbed library for the mbed Lab Board 128*32 pixel LCD + * use C12832 controller + * Copyright (c) 2012 Peter Drescher - DC2PD + * Released under the MIT License: http://mbed.org/license/mit + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef CORE_H +#define CORE_H + +#include <string> +#include "MMA7660.h" +#include "C12832.h" +#include "easy-connect.h" +#include "http_request.h" +//#include "select-demo.h" + +/** This classe is being used to control the temperature program + * + */ + +class Core { +public: + /** Create a Core object and initialise all the values + * + */ + Core(); + + /** Plays the program, this is the heart of this class + * + */ + void guard(void); + +private: + bool doorIsOpening(void); + void displayMessageOnScreen(void); + void triggerAlarm(void); + void shutDownAlarm(void); + void updateInfoWithAPI(void); + void dumpResponsePut(HttpResponse *); + void dumpResponseGet(HttpResponse *); + void putRequest(void); + void getRequest(void); + + /** These are the variables used in the program + * + */ + bool _quiet; + std::string _message; + NetworkInterface* _network; +}; + + + + +#endif
diff -r 3c173847e681 -r 8965fc128e7b source/main-http-socket-reuse.cpp --- a/source/main-http-socket-reuse.cpp Thu Mar 30 15:15:47 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,90 +0,0 @@ -#include "select-demo.h" - -#if DEMO == DEMO_HTTP_SOCKET_REUSE - -#include "mbed.h" -#include "easy-connect.h" -#include "http_request.h" - -Serial pc(USBTX, USBRX); - -void dump_response(HttpResponse* res) { - printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); - - printf("Headers:\n"); - for (size_t ix = 0; ix < res->get_headers_length(); ix++) { - printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); - } - printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); -} - -int main() { - pc.baud(115200); - // Connect to the network (see mbed_app.json for the connectivity method used) - NetworkInterface *network = easy_connect(true); - if (!network) { - printf("Cannot connect to the network, see serial output"); - return 1; - } - - // Create a TCP socket - printf("\n----- Setting up TCP connection -----\n"); - - TCPSocket* socket = new TCPSocket(); - nsapi_error_t open_result = socket->open(network); - if (open_result != 0) { - printf("Opening TCPSocket failed... %d\n", open_result); - return 1; - } - - nsapi_error_t connect_result = socket->connect("httpbin.org", 80); - if (connect_result != 0) { - printf("Connecting over TCPSocket failed... %d\n", connect_result); - return 1; - } - - printf("Connected over TCP to httpbin.org:80\n"); - - // Do a GET request to httpbin.org - { - HttpRequest* get_req = new HttpRequest(socket, HTTP_GET, "http://httpbin.org/status/418"); - - // By default the body is automatically parsed and stored in a string, this is memory heavy. - // To receive chunked response, pass in a callback as third parameter to 'send'. - HttpResponse* get_res = get_req->send(); - if (!get_res) { - printf("HttpRequest failed (error code %d)\n", get_req->get_error()); - return 1; - } - - printf("\n----- HTTP GET response -----\n"); - dump_response(get_res); - - delete get_req; - } - - // POST request to httpbin.org - { - HttpRequest* post_req = new HttpRequest(socket, HTTP_POST, "http://httpbin.org/post"); - post_req->set_header("Content-Type", "application/json"); - - const char body[] = "{\"hello\":\"world\"}"; - - HttpResponse* post_res = post_req->send(body, strlen(body)); - if (!post_res) { - printf("HttpRequest failed (error code %d)\n", post_req->get_error()); - return 1; - } - - printf("\n----- HTTP POST response -----\n"); - dump_response(post_res); - - delete post_req; - } - - delete socket; - - Thread::wait(osWaitForever); -} - -#endif
diff -r 3c173847e681 -r 8965fc128e7b source/main-http.cpp --- a/source/main-http.cpp Thu Mar 30 15:15:47 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -#include "select-demo.h" - -#if DEMO == DEMO_HTTP - -#include "mbed.h" -#include "easy-connect.h" -#include "http_request.h" - -Serial pc(USBTX, USBRX); - -void dump_response(HttpResponse* res) { - printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); - - printf("Headers:\n"); - for (size_t ix = 0; ix < res->get_headers_length(); ix++) { - printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); - } - printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); -} - -int main() { - pc.baud(115200); - // Connect to the network (see mbed_app.json for the connectivity method used) - NetworkInterface *network = easy_connect(true); - if (!network) { - printf("Cannot connect to the network, see serial output"); - return 1; - } - - // Do a GET request to httpbin.org - { - // By default the body is automatically parsed and stored in a buffer, this is memory heavy. - // To receive chunked response, pass in a callback as last parameter to the constructor. - HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418"); - - HttpResponse* get_res = get_req->send(); - if (!get_res) { - printf("HttpRequest failed (error code %d)\n", get_req->get_error()); - return 1; - } - - printf("\n----- HTTP GET response -----\n"); - dump_response(get_res); - - delete get_req; - } - - // POST request to httpbin.org - { - HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post"); - post_req->set_header("Content-Type", "application/json"); - - const char body[] = "{\"hello\":\"world\"}"; - - HttpResponse* post_res = post_req->send(body, strlen(body)); - if (!post_res) { - printf("HttpRequest failed (error code %d)\n", post_req->get_error()); - return 1; - } - - printf("\n----- HTTP POST response -----\n"); - dump_response(post_res); - - delete post_req; - } - - Thread::wait(osWaitForever); -} - -#endif
diff -r 3c173847e681 -r 8965fc128e7b source/main-https-socket-reuse.cpp --- a/source/main-https-socket-reuse.cpp Thu Mar 30 15:15:47 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,117 +0,0 @@ -#include "select-demo.h" - -/** - * This demo shows how to re-use sockets, so the TLS handshake only has to happen once - */ - -#if DEMO == DEMO_HTTPS_SOCKET_REUSE - -#include "mbed.h" -#include "easy-connect.h" -#include "https_request.h" - -Serial pc(USBTX, USBRX); - -/* List of trusted root CA certificates - * currently one: Let's Encrypt, the CA for httpbin.org - * - * To add more root certificates, just concatenate them. - */ -const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" - "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" - "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" - "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" - "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" - "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" - "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" - "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" - "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" - "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT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TH2H3eDJAkSnh6/DNFu0Qg==\n" - "-----END CERTIFICATE-----\n"; - -void dump_response(HttpResponse* res) { - mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); - - mbedtls_printf("Headers:\n"); - for (size_t ix = 0; ix < res->get_headers_length(); ix++) { - mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); - } - mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); -} - -int main() { - pc.baud(115200); - - NetworkInterface* network = easy_connect(true); - if (!network) { - return 1; - } - - // Create a TLS socket (which holds a TCPSocket) - printf("\n----- Setting up TLS connection -----\n"); - - TLSSocket* socket = new TLSSocket(network, "httpbin.org", 443, SSL_CA_PEM); - socket->set_debug(true); - if (socket->connect() != 0) { - printf("TLS Connect failed %d\n", socket->error()); - return 1; - } - - // GET request to httpbin.org - { - HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418"); - get_req->set_debug(true); - - HttpResponse* get_res = get_req->send(); - if (!get_res) { - printf("HttpRequest failed (error code %d)\n", get_req->get_error()); - return 1; - } - printf("\n----- HTTPS GET response -----\n"); - dump_response(get_res); - - delete get_req; - } - - // POST request to httpbin.org - { - HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "https://httpbin.org/post"); - post_req->set_debug(true); - post_req->set_header("Content-Type", "application/json"); - - const char body[] = "{\"hello\":\"world\"}"; - - HttpResponse* post_res = post_req->send(body, strlen(body)); - if (!post_res) { - printf("HttpRequest failed (error code %d)\n", post_req->get_error()); - return 1; - } - - printf("\n----- HTTPS POST response -----\n"); - dump_response(post_res); - - delete post_req; - } - - delete socket; - - Thread::wait(osWaitForever); -} - -#endif
diff -r 3c173847e681 -r 8965fc128e7b source/main-https.cpp --- a/source/main-https.cpp Thu Mar 30 15:15:47 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,126 +0,0 @@ -#include "select-demo.h" - -#if DEMO == DEMO_HTTPS - -#include "mbed.h" -#include "easy-connect.h" -#include "https_request.h" - -Serial pc(USBTX, USBRX); - -/* List of trusted root CA certificates - * currently two: GlobalSign, the CA for developer.mbed.org and Let's Encrypt, the CA for httpbin.org - * - * To add more root certificates, just concatenate them. - */ -const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" - "MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG\n" - "A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv\n" - "b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw\n" - "MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i\n" - "YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT\n" - "aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ\n" - "jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp\n" - "xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp\n" - "1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG\n" - "snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ\n" - "U26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrXgzT/LCrBbBlDSgeF59N8\n" - "9iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8E\n" - "BTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0B\n" - "AQUFAAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOz\n" - "yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE\n" - "38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP\n" - "AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad\n" - "DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME\n" - "HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\n" - "-----END CERTIFICATE-----\n" - "-----BEGIN CERTIFICATE-----\n" - "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" - "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" - "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" - "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" - "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" - "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" - "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" - "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" - "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brv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nh6/DNFu0Qg==\n" - "-----END CERTIFICATE-----\n"; - -void dump_response(HttpResponse* res) { - mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); - - mbedtls_printf("Headers:\n"); - for (size_t ix = 0; ix < res->get_headers_length(); ix++) { - mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); - } - mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); -} - -int main() { - pc.baud(115200); - - NetworkInterface* network = easy_connect(true); - if (!network) { - return 1; - } - - // GET request to developer.mbed.org - { - printf("\n----- HTTPS GET request -----\n"); - - HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://developer.mbed.org/media/uploads/mbed_official/hello.txt"); - get_req->set_debug(true); - - HttpResponse* get_res = get_req->send(); - if (!get_res) { - printf("HttpRequest failed (error code %d)\n", get_req->get_error()); - return 1; - } - printf("\n----- HTTPS GET response -----\n"); - dump_response(get_res); - - delete get_req; - } - - // POST request to httpbin.org - { - printf("\n----- HTTPS POST request -----\n"); - - HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post"); - post_req->set_debug(true); - post_req->set_header("Content-Type", "application/json"); - - const char body[] = "{\"hello\":\"world\"}"; - - HttpResponse* post_res = post_req->send(body, strlen(body)); - if (!post_res) { - printf("HttpRequest failed (error code %d)\n", post_req->get_error()); - return 1; - } - - printf("\n----- HTTPS POST response -----\n"); - dump_response(post_res); - - delete post_req; - } - - Thread::wait(osWaitForever); -} - -#endif