n/a

Dependencies:   C12832 mbed-http

Fork of HTTP-Python-Demo by Cambridge Hackathon

main.cpp

Committer:
BKasza
Date:
2017-11-26
Revision:
3:8135a6313648
Parent:
2:b91140c3c3f6
Child:
4:14c774cf7ac2

File content as of revision 3:8135a6313648:

//----------------------------------------------------------------------------
// The confidential and proprietary information contained in this file may
// only be used by a person authorised under and to the extent permitted
// by a subsisting licensing agreement from ARM Limited or its affiliates.
//
// (C) COPYRIGHT 2016 ARM Limited or its affiliates.
// ALL RIGHTS RESERVED
//
// This entire notice must be reproduced on all copies of this file
// and copies of this file may only be made by a person if such person is
// permitted to do so under the terms of a subsisting license agreement
// from ARM Limited or its affiliates.
//----------------------------------------------------------------------------
#include "mbed.h"
#include "config.h"
#include "C12832.h"
#include "OdinWiFiInterface.h"
#include "http_request.h"

C12832  lcd(PE_14, PE_12, PD_12, PD_11, PE_9);
OdinWiFiInterface wifiIF;
InterruptIn post_button(PF_2);
InterruptIn get_put_button(PG_4);
volatile bool post_clicked = false;
volatile bool get_clicked = false;
volatile bool put_clicked = false;

void lcd_print(const char* message) {
    lcd.cls();
    lcd.locate(0, 3);
    lcd.printf(message);
}

void send_post() {
    post_clicked = true;
}
 
void send_get_put() {
    get_clicked = true;
}

int main() {

    int ret;
    SocketAddress serverAddr;
    
    lcd_print("Connecting...");
    ret = wifiIF.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
    if (ret != 0) {
        lcd_print("Connection error.");
        return -1;
    }
    lcd_print("Successfully connected!");
    lcd_print(wifiIF.get_mac_address());
    wifiIF.gethostbyname(SERVERNAME, &serverAddr);

    /* Enable interrupts */
    post_button.rise(&send_post);
    get_put_button.rise(&send_get_put);
    
    /* Main loop */
    while (1) {
        /* Post data */
        if (post_clicked) {
            lcd_print("post");
            post_clicked = false;
            NetworkInterface* netIF = &wifiIF;
            HttpRequest* request = new HttpRequest(netIF, HTTP_POST, "http://10.25.2.152:8080");
            request->set_header("Content-Type", "application/json");
            const char body[] = "HelloWorld";
            HttpResponse* response = request->send(body, strlen(body));
            lcd_print(response->get_body_as_string().c_str());
            delete request;
        }
        
        /* Get data */
        if (get_clicked) {
            get_clicked = false;
            put_clicked = true;
            NetworkInterface* netIF = &wifiIF;
            HttpRequest* request = new HttpRequest(netIF, HTTP_GET, "http://10.25.2.152:8080");
            request->set_header("Content-Type", "application/json");
            const char body[] = "{\"get\":\"request\"}";
            HttpResponse* response = request->send(body, strlen(body));
            lcd_print(response->get_body_as_string().c_str());
            delete request;
        }

        wait_ms(2000);
        
        /* Put */
        if (put_clicked) {
            put_clicked = false;
            lcd_print("put");
            NetworkInterface* net = &wifiIF;
            HttpRequest* request = new HttpRequest(net, HTTP_PUT, "http://10.25.2.152:8080");
            request->set_header("Content-Type", "application/json");
            const char body[] = "{\"put\":\"request\"}";
            HttpResponse* response = request->send(body, strlen(body));
            lcd_print(response->get_body_as_string().c_str());
            delete request;
        }
   
    }

}