Demo for Mbed Connect Cloud board and an IFTTT integration with Google Sheets

Dependencies:   mbed-http C12832

View the tutorial for this demo here.

working/main_working.h

Committer:
Jenny Plunkett
Date:
2017-11-10
Revision:
0:e7e395d6f1fc
Child:
2:ba90e563a0d0

File content as of revision 0:e7e395d6f1fc:

//----------------------------------------------------------------------------
// 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 "OdinWiFiInterface.h"
#include "TCPSocket.h"
#include "http_request.h"

// GLOBAL VARIABLES HERE

OdinWiFiInterface wifi;
InterruptIn button(PF_2);
volatile int count = 0;
volatile bool clicked = false;

// FUNCTION DEFINTIONS HERE

void button_clicked() {
    clicked = true;
    count += 1;
    printf("Button Clicked %d\n\r", count);
}

int main() {

    // MAIN CODE HERE

    printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
    int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
    if (ret != 0) {
        printf("\nConnection error\n");
        return -1;
    }
    printf("Success\n\n");
    printf("MAC: %s\n", wifi.get_mac_address());
    printf("IP: %s\n", wifi.get_ip_address());
    printf("Netmask: %s\n", wifi.get_netmask());
    printf("Gateway: %s\n", wifi.get_gateway());
    printf("RSSI: %d\n\n", wifi.get_rssi());

    button.rise(&button_clicked);

    while (true) {
        if (clicked) {
            clicked = false;
            char body[140];
            char event_name[] = "Button Clicked";
            NetworkInterface* net = &wifi;
            HttpRequest* request = new HttpRequest(net, HTTP_POST, "http://maker.ifttt.com/trigger/mbed_connect/with/key/SECRETKEY");
            request->set_header("Content-Type", "application/json");
            sprintf(body, "{\"value1\":\"%s\", \"value2\":\"%d\"}", event_name, count);
            HttpResponse* response = request->send(body, strlen(body));
            printf("%s\n\r", response->get_body_as_string().c_str());
            delete request;
        }
    }

}