![](/media/cache/group/DevelopConnectDevices.png.50x50_q85.png)
Demo for Mbed Connect Cloud board and an HTTP Python Server
Dependencies: C12832 mbed-http
View the tutorial for this demo here.
working/main_working.h@2:b91140c3c3f6, 2017-11-17 (annotated)
- Committer:
- Jenny Plunkett
- Date:
- Fri Nov 17 16:04:17 2017 -0600
- Revision:
- 2:b91140c3c3f6
- Parent:
- 0:c5b042cf8162
Fixed typo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jenny Plunkett |
0:c5b042cf8162 | 1 | //---------------------------------------------------------------------------- |
Jenny Plunkett |
0:c5b042cf8162 | 2 | // The confidential and proprietary information contained in this file may |
Jenny Plunkett |
0:c5b042cf8162 | 3 | // only be used by a person authorised under and to the extent permitted |
Jenny Plunkett |
0:c5b042cf8162 | 4 | // by a subsisting licensing agreement from ARM Limited or its affiliates. |
Jenny Plunkett |
0:c5b042cf8162 | 5 | // |
Jenny Plunkett |
0:c5b042cf8162 | 6 | // (C) COPYRIGHT 2016 ARM Limited or its affiliates. |
Jenny Plunkett |
0:c5b042cf8162 | 7 | // ALL RIGHTS RESERVED |
Jenny Plunkett |
0:c5b042cf8162 | 8 | // |
Jenny Plunkett |
0:c5b042cf8162 | 9 | // This entire notice must be reproduced on all copies of this file |
Jenny Plunkett |
0:c5b042cf8162 | 10 | // and copies of this file may only be made by a person if such person is |
Jenny Plunkett |
0:c5b042cf8162 | 11 | // permitted to do so under the terms of a subsisting license agreement |
Jenny Plunkett |
0:c5b042cf8162 | 12 | // from ARM Limited or its affiliates. |
Jenny Plunkett |
0:c5b042cf8162 | 13 | //---------------------------------------------------------------------------- |
Jenny Plunkett |
0:c5b042cf8162 | 14 | #include "mbed.h" |
Jenny Plunkett |
0:c5b042cf8162 | 15 | #include "C12832.h" |
Jenny Plunkett |
0:c5b042cf8162 | 16 | #include "OdinWiFiInterface.h" |
Jenny Plunkett |
0:c5b042cf8162 | 17 | #include "http_request.h" |
Jenny Plunkett |
0:c5b042cf8162 | 18 | |
Jenny Plunkett |
0:c5b042cf8162 | 19 | // GLOBAL VARIABLES HERE |
Jenny Plunkett |
0:c5b042cf8162 | 20 | |
Jenny Plunkett |
0:c5b042cf8162 | 21 | C12832 lcd(PE_14, PE_12, PD_12, PD_11, PE_9); |
Jenny Plunkett |
0:c5b042cf8162 | 22 | OdinWiFiInterface wifi; |
Jenny Plunkett |
0:c5b042cf8162 | 23 | InterruptIn post_button(PF_2); |
Jenny Plunkett |
0:c5b042cf8162 | 24 | InterruptIn get_put_button(PG_4); |
Jenny Plunkett |
0:c5b042cf8162 | 25 | volatile bool post_clicked = false; |
Jenny Plunkett |
0:c5b042cf8162 | 26 | volatile bool get_clicked = false; |
Jenny Plunkett |
0:c5b042cf8162 | 27 | volatile bool put_clicked = false; |
Jenny Plunkett |
0:c5b042cf8162 | 28 | |
Jenny Plunkett |
0:c5b042cf8162 | 29 | // FUNCTION DEFINTIONS HERE |
Jenny Plunkett |
0:c5b042cf8162 | 30 | |
Jenny Plunkett |
0:c5b042cf8162 | 31 | void lcd_print(const char* message) { |
Jenny Plunkett |
0:c5b042cf8162 | 32 | lcd.cls(); |
Jenny Plunkett |
0:c5b042cf8162 | 33 | lcd.locate(0, 3); |
Jenny Plunkett |
0:c5b042cf8162 | 34 | lcd.printf(message); |
Jenny Plunkett |
0:c5b042cf8162 | 35 | } |
Jenny Plunkett |
0:c5b042cf8162 | 36 | |
Jenny Plunkett |
0:c5b042cf8162 | 37 | void send_post() { |
Jenny Plunkett |
0:c5b042cf8162 | 38 | post_clicked = true; |
Jenny Plunkett |
0:c5b042cf8162 | 39 | } |
Jenny Plunkett |
0:c5b042cf8162 | 40 | |
Jenny Plunkett |
0:c5b042cf8162 | 41 | void send_get_put() { |
Jenny Plunkett |
0:c5b042cf8162 | 42 | get_clicked = true; |
Jenny Plunkett |
0:c5b042cf8162 | 43 | } |
Jenny Plunkett |
0:c5b042cf8162 | 44 | |
Jenny Plunkett |
0:c5b042cf8162 | 45 | int main() { |
Jenny Plunkett |
0:c5b042cf8162 | 46 | |
Jenny Plunkett |
0:c5b042cf8162 | 47 | // MAIN CODE HERE |
Jenny Plunkett |
0:c5b042cf8162 | 48 | |
Jenny Plunkett |
0:c5b042cf8162 | 49 | lcd_print("Connecting..."); |
Jenny Plunkett |
0:c5b042cf8162 | 50 | int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
Jenny Plunkett |
0:c5b042cf8162 | 51 | if (ret != 0) { |
Jenny Plunkett |
0:c5b042cf8162 | 52 | lcd_print("Connection error."); |
Jenny Plunkett |
0:c5b042cf8162 | 53 | return -1; |
Jenny Plunkett |
0:c5b042cf8162 | 54 | } |
Jenny Plunkett |
0:c5b042cf8162 | 55 | lcd_print("Successfully connected!"); |
Jenny Plunkett |
0:c5b042cf8162 | 56 | |
Jenny Plunkett |
0:c5b042cf8162 | 57 | post_button.rise(&send_post); |
Jenny Plunkett |
0:c5b042cf8162 | 58 | get_put_button.rise(&send_get_put); |
Jenny Plunkett |
0:c5b042cf8162 | 59 | |
Jenny Plunkett |
0:c5b042cf8162 | 60 | while (true) { |
Jenny Plunkett |
0:c5b042cf8162 | 61 | |
Jenny Plunkett |
0:c5b042cf8162 | 62 | // WHILE LOOP CODE HERE |
Jenny Plunkett |
0:c5b042cf8162 | 63 | |
Jenny Plunkett |
0:c5b042cf8162 | 64 | if (post_clicked) { |
Jenny Plunkett |
0:c5b042cf8162 | 65 | post_clicked = false; |
Jenny Plunkett |
0:c5b042cf8162 | 66 | NetworkInterface* net = &wifi; |
Jenny Plunkett |
0:c5b042cf8162 | 67 | HttpRequest* request = new HttpRequest(net, HTTP_POST, "http://IP_ADDRESS_HERE:8080"); |
Jenny Plunkett |
0:c5b042cf8162 | 68 | request->set_header("Content-Type", "application/json"); |
Jenny Plunkett |
0:c5b042cf8162 | 69 | const char body[] = "{\"post\":\"request\"}"; |
Jenny Plunkett |
0:c5b042cf8162 | 70 | HttpResponse* response = request->send(body, strlen(body)); |
Jenny Plunkett |
0:c5b042cf8162 | 71 | lcd_print(response->get_body_as_string().c_str()); |
Jenny Plunkett |
0:c5b042cf8162 | 72 | delete request; |
Jenny Plunkett |
0:c5b042cf8162 | 73 | } |
Jenny Plunkett |
0:c5b042cf8162 | 74 | |
Jenny Plunkett |
0:c5b042cf8162 | 75 | if (get_clicked) { |
Jenny Plunkett |
0:c5b042cf8162 | 76 | get_clicked = false; |
Jenny Plunkett |
0:c5b042cf8162 | 77 | put_clicked = true; |
Jenny Plunkett |
0:c5b042cf8162 | 78 | NetworkInterface* net = &wifi; |
Jenny Plunkett |
0:c5b042cf8162 | 79 | HttpRequest* request = new HttpRequest(net, HTTP_GET, "http://IP_ADDRESS_HERE:8080"); |
Jenny Plunkett |
0:c5b042cf8162 | 80 | request->set_header("Content-Type", "application/json"); |
Jenny Plunkett |
0:c5b042cf8162 | 81 | const char body[] = "{\"get\":\"request\"}"; |
Jenny Plunkett |
0:c5b042cf8162 | 82 | HttpResponse* response = request->send(body, strlen(body)); |
Jenny Plunkett |
0:c5b042cf8162 | 83 | lcd_print(response->get_body_as_string().c_str()); |
Jenny Plunkett |
0:c5b042cf8162 | 84 | delete request; |
Jenny Plunkett |
0:c5b042cf8162 | 85 | } |
Jenny Plunkett |
0:c5b042cf8162 | 86 | |
Jenny Plunkett |
0:c5b042cf8162 | 87 | wait_ms(2000); |
Jenny Plunkett |
0:c5b042cf8162 | 88 | |
Jenny Plunkett |
0:c5b042cf8162 | 89 | if (put_clicked) { |
Jenny Plunkett |
0:c5b042cf8162 | 90 | put_clicked = false; |
Jenny Plunkett |
0:c5b042cf8162 | 91 | NetworkInterface* net = &wifi; |
Jenny Plunkett |
0:c5b042cf8162 | 92 | HttpRequest* request = new HttpRequest(net, HTTP_PUT, "http://IP_ADDRESS_HERE:8080"); |
Jenny Plunkett |
0:c5b042cf8162 | 93 | request->set_header("Content-Type", "application/json"); |
Jenny Plunkett |
0:c5b042cf8162 | 94 | const char body[] = "{\"put\":\"request\"}"; |
Jenny Plunkett |
0:c5b042cf8162 | 95 | HttpResponse* response = request->send(body, strlen(body)); |
Jenny Plunkett |
0:c5b042cf8162 | 96 | lcd_print(response->get_body_as_string().c_str()); |
Jenny Plunkett |
0:c5b042cf8162 | 97 | delete request; |
Jenny Plunkett |
0:c5b042cf8162 | 98 | } |
Jenny Plunkett |
0:c5b042cf8162 | 99 | |
Jenny Plunkett |
0:c5b042cf8162 | 100 | } |
Jenny Plunkett |
0:c5b042cf8162 | 101 | |
Jenny Plunkett |
0:c5b042cf8162 | 102 | } |