Demo for Mbed Connect Cloud board and an HTTP Python Server

Dependencies:   C12832 mbed-http

Fork of HTTP-Python-Demo by Cambridge Hackathon

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main_working.h Source File

main_working.h

00001 //----------------------------------------------------------------------------
00002 // The confidential and proprietary information contained in this file may
00003 // only be used by a person authorised under and to the extent permitted
00004 // by a subsisting licensing agreement from ARM Limited or its affiliates.
00005 //
00006 // (C) COPYRIGHT 2016 ARM Limited or its affiliates.
00007 // ALL RIGHTS RESERVED
00008 //
00009 // This entire notice must be reproduced on all copies of this file
00010 // and copies of this file may only be made by a person if such person is
00011 // permitted to do so under the terms of a subsisting license agreement
00012 // from ARM Limited or its affiliates.
00013 //----------------------------------------------------------------------------
00014 #include "mbed.h"
00015 #include "C12832.h"
00016 #include "OdinWiFiInterface.h"
00017 #include "http_request.h"
00018 
00019 // GLOBAL VARIABLES HERE
00020 
00021 C12832  lcd(PE_14, PE_12, PD_12, PD_11, PE_9);
00022 OdinWiFiInterface wifi;
00023 InterruptIn post_button(PF_2);
00024 InterruptIn get_put_button(PG_4);
00025 volatile bool post_clicked = false;
00026 volatile bool get_clicked = false;
00027 volatile bool put_clicked = false;
00028 
00029 // FUNCTION DEFINTIONS HERE
00030 
00031 void lcd_print(const char* message) {
00032     lcd.cls();
00033     lcd.locate(0, 3);
00034     lcd.printf(message);
00035 }
00036 
00037 void send_post() {
00038     post_clicked = true;
00039 }
00040 
00041 void send_get_put() {
00042     get_clicked = true;
00043 }
00044 
00045 int main() {
00046 
00047     // MAIN CODE HERE
00048 
00049     lcd_print("Connecting...");
00050     int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00051     if (ret != 0) {
00052         lcd_print("Connection error.");
00053         return -1;
00054     }
00055     lcd_print("Successfully connected!");
00056 
00057     post_button.rise(&send_post);
00058     get_put_button.rise(&send_get_put);
00059 
00060     while (true) {
00061 
00062         // WHILE LOOP CODE HERE
00063 
00064         if (post_clicked) {
00065             post_clicked = false;
00066             NetworkInterface* net = &wifi;
00067             HttpRequest* request = new HttpRequest(net, HTTP_POST, "http://IP_ADDRESS_HERE:8080");
00068             request->set_header("Content-Type", "application/json");
00069             const char body[] = "{\"post\":\"request\"}";
00070             HttpResponse* response = request->send(body, strlen(body));
00071             lcd_print(response->get_body_as_string().c_str());
00072             delete request;
00073         }
00074 
00075         if (get_clicked) {
00076             get_clicked = false;
00077             put_clicked = true;
00078             NetworkInterface* net = &wifi;
00079             HttpRequest* request = new HttpRequest(net, HTTP_GET, "http://IP_ADDRESS_HERE:8080");
00080             request->set_header("Content-Type", "application/json");
00081             const char body[] = "{\"get\":\"request\"}";
00082             HttpResponse* response = request->send(body, strlen(body));
00083             lcd_print(response->get_body_as_string().c_str());
00084             delete request;
00085         }
00086 
00087         wait_ms(2000);
00088 
00089         if (put_clicked) {
00090             put_clicked = false;
00091             NetworkInterface* net = &wifi;
00092             HttpRequest* request = new HttpRequest(net, HTTP_PUT, "http://IP_ADDRESS_HERE:8080");
00093             request->set_header("Content-Type", "application/json");
00094             const char body[] = "{\"put\":\"request\"}";
00095             HttpResponse* response = request->send(body, strlen(body));
00096             lcd_print(response->get_body_as_string().c_str());
00097             delete request;
00098         }
00099 
00100     }
00101 
00102 }