IFTTT: helloworld

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of IFTTT_Done by TaChing Yu

main.cpp

Committer:
yu10078999
Date:
2016-09-11
Revision:
2:678f949dccd7
Parent:
1:3010b44f07ff
Child:
3:a0efe12a75cf

File content as of revision 2:678f949dccd7:

#include "mbed.h"
#include "EthernetInterface.h"
#include "TCPSocketConnection.h"

EthernetInterface eth;
RawSerial pc(USBTX, USBRX); // tx, rx

int main()
{
    pc.baud(9600);
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s \n\r", eth.getIPAddress());
    TCPSocketConnection socket;
    
    socket.connect("maker.ifttt.com", 80);
    /*
    char str[]="";
    char *post = "POST /trigger/OWJMAN/with/key/bkJhw8soKcjx28zrqAgeW2 HTTP/1.1\r\n";
    char *host = "Host: maker.ifttt.com\r\n";
    char *type = "Content-Type: application/json\r\n\r\n";
    char contentLen[50] = {0};
    char *data = "{\"value1\":\"this is awesome\",\"value2\":\"test-ing\",\"value3\":\"data!!!\"}\r\n";
    sprintf(contentLen,"Content-Length: %d\r\n",strlen(data));
    sprintf(str,"%s%s%s%s%s",post,host,contentLen,type,data);
    socket.send_all(str, strlen(str));*/
    
    char *post = "POST /trigger/OWJMAN/with/key/bkJhw8soKcjx28zrqAgeW2 HTTP/1.1\r\n";
    char *host = "Host: maker.ifttt.com\r\n";
    char *type = "Content-Type: application/json\r\n\r\n";
    char contentLen[] = "";
    char *data = "{\"value1\":\"this is awesome\",\"value2\":\"test-ing\",\"value3\":\"data!!!\"}\r\n";
    sprintf(contentLen,"Content-Length: %d\r\n",strlen(data));
    socket.send_all(post, strlen(post));
    socket.send_all(host, strlen(host));
    socket.send_all(type, strlen(type));
    socket.send_all(contentLen, strlen(contentLen));
    socket.send_all(data, strlen(data));
     
    char buffer[300];
    int ret;
    while (true) {
        ret = socket.receive(buffer, sizeof(buffer)-1);
        if (ret <= 0)
            break;
        buffer[ret] = '\0';
        printf("Received %d chars from server:\n%s\n", ret, buffer);
    }
    printf("DONE!\n");
      
    socket.close();
    
    eth.disconnect();
    
    while(1) {}
}
extern "C" void mbed_mac_address(char *mac)
{
    mac[0] = 0xD4;
    mac[1] = 0x3F;
    mac[2] = 0x7E;
    mac[3] = 0x4C;
    mac[4] = 0x7E;
    mac[5] = 0x1C;
};