IFTTT: helloworld

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of IFTTT_Done by TaChing Yu

Committer:
liuminhaw
Date:
Sun Sep 11 07:40:59 2016 +0000
Revision:
3:a0efe12a75cf
Parent:
2:678f949dccd7
IFTTT: helloworld

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:0f0676c43e4b 1 #include "mbed.h"
mbedAustin 0:0f0676c43e4b 2 #include "EthernetInterface.h"
mbedAustin 1:3010b44f07ff 3 #include "TCPSocketConnection.h"
mbedAustin 1:3010b44f07ff 4
liuminhaw 3:a0efe12a75cf 5 const char cip[] = "192.168.10.89";
liuminhaw 3:a0efe12a75cf 6 const char cmask[] = "255.255.255.0";
liuminhaw 3:a0efe12a75cf 7 const char cgateway[] = "192.168.10.202";
liuminhaw 3:a0efe12a75cf 8
mbedAustin 0:0f0676c43e4b 9 EthernetInterface eth;
mbedAustin 1:3010b44f07ff 10 RawSerial pc(USBTX, USBRX); // tx, rx
mbedAustin 0:0f0676c43e4b 11
liuminhaw 3:a0efe12a75cf 12
mbedAustin 1:3010b44f07ff 13 int main()
mbedAustin 0:0f0676c43e4b 14 {
mbedAustin 1:3010b44f07ff 15 pc.baud(9600);
liuminhaw 3:a0efe12a75cf 16
liuminhaw 3:a0efe12a75cf 17 pc.printf("\n");
liuminhaw 3:a0efe12a75cf 18 pc.printf("\n");
liuminhaw 3:a0efe12a75cf 19 pc.printf("--------------------------------\n");
liuminhaw 3:a0efe12a75cf 20 pc.printf("Program starts ...\n");
liuminhaw 3:a0efe12a75cf 21 //eth.init(); //Use DHCP
liuminhaw 3:a0efe12a75cf 22
liuminhaw 3:a0efe12a75cf 23 // fix ip address
liuminhaw 3:a0efe12a75cf 24
liuminhaw 3:a0efe12a75cf 25 int init_rtn = eth.init(cip, //const char * ip,
liuminhaw 3:a0efe12a75cf 26 cmask, //const char * mask,
liuminhaw 3:a0efe12a75cf 27 cgateway //const char * gateway
liuminhaw 3:a0efe12a75cf 28 );
liuminhaw 3:a0efe12a75cf 29
liuminhaw 3:a0efe12a75cf 30 pc.printf("init() return : %d\n", init_rtn);
liuminhaw 3:a0efe12a75cf 31
liuminhaw 3:a0efe12a75cf 32 pc.printf("\n");
liuminhaw 3:a0efe12a75cf 33 pc.printf(" wait for ethernet connect()......\n");
liuminhaw 3:a0efe12a75cf 34 pc.printf("\n");
liuminhaw 3:a0efe12a75cf 35
liuminhaw 3:a0efe12a75cf 36 // eth.connect();
liuminhaw 3:a0efe12a75cf 37 int connect_rtn = eth.connect();
liuminhaw 3:a0efe12a75cf 38 pc.printf("connect() return : %d\n", connect_rtn);
liuminhaw 3:a0efe12a75cf 39
mbedAustin 0:0f0676c43e4b 40 printf("IP Address is %s \n\r", eth.getIPAddress());
liuminhaw 3:a0efe12a75cf 41
liuminhaw 3:a0efe12a75cf 42 pc.printf("IP Address is %s\n", eth.getMACAddress());
liuminhaw 3:a0efe12a75cf 43 pc.printf("\n");
liuminhaw 3:a0efe12a75cf 44
liuminhaw 3:a0efe12a75cf 45
mbedAustin 1:3010b44f07ff 46 TCPSocketConnection socket;
yu10078999 2:678f949dccd7 47
liuminhaw 3:a0efe12a75cf 48 int isocket_connect = socket.connect("maker.ifttt.com", 80);
liuminhaw 3:a0efe12a75cf 49 pc.printf("socket connectted... %d\n", isocket_connect);
liuminhaw 3:a0efe12a75cf 50
yu10078999 2:678f949dccd7 51 /*
yu10078999 2:678f949dccd7 52 char str[]="";
yu10078999 2:678f949dccd7 53 char *post = "POST /trigger/OWJMAN/with/key/bkJhw8soKcjx28zrqAgeW2 HTTP/1.1\r\n";
yu10078999 2:678f949dccd7 54 char *host = "Host: maker.ifttt.com\r\n";
yu10078999 2:678f949dccd7 55 char *type = "Content-Type: application/json\r\n\r\n";
yu10078999 2:678f949dccd7 56 char contentLen[50] = {0};
yu10078999 2:678f949dccd7 57 char *data = "{\"value1\":\"this is awesome\",\"value2\":\"test-ing\",\"value3\":\"data!!!\"}\r\n";
yu10078999 2:678f949dccd7 58 sprintf(contentLen,"Content-Length: %d\r\n",strlen(data));
yu10078999 2:678f949dccd7 59 sprintf(str,"%s%s%s%s%s",post,host,contentLen,type,data);
yu10078999 2:678f949dccd7 60 socket.send_all(str, strlen(str));*/
yu10078999 2:678f949dccd7 61
liuminhaw 3:a0efe12a75cf 62 char *post = "POST /trigger/helloworld/with/key/brbeSCjegrJbPkzvV-v0uc HTTP/1.1\r\n";
yu10078999 2:678f949dccd7 63 char *host = "Host: maker.ifttt.com\r\n";
yu10078999 2:678f949dccd7 64 char *type = "Content-Type: application/json\r\n\r\n";
yu10078999 2:678f949dccd7 65 char contentLen[] = "";
yu10078999 2:678f949dccd7 66 char *data = "{\"value1\":\"this is awesome\",\"value2\":\"test-ing\",\"value3\":\"data!!!\"}\r\n";
liuminhaw 3:a0efe12a75cf 67
yu10078999 2:678f949dccd7 68 sprintf(contentLen,"Content-Length: %d\r\n",strlen(data));
liuminhaw 3:a0efe12a75cf 69
liuminhaw 3:a0efe12a75cf 70 int i_post, i_host, i_type, i_content, i_data;
liuminhaw 3:a0efe12a75cf 71
liuminhaw 3:a0efe12a75cf 72 pc.printf("Post: %s", post);
liuminhaw 3:a0efe12a75cf 73 i_post = socket.send_all(post, strlen(post));
liuminhaw 3:a0efe12a75cf 74 pc.printf("Post: %d\n", i_post);
liuminhaw 3:a0efe12a75cf 75
liuminhaw 3:a0efe12a75cf 76 pc.printf("Host: %s", host);
liuminhaw 3:a0efe12a75cf 77 i_host = socket.send_all(host, strlen(host));
liuminhaw 3:a0efe12a75cf 78 pc.printf("Host: %d\n", i_host);
liuminhaw 3:a0efe12a75cf 79
liuminhaw 3:a0efe12a75cf 80 pc.printf("Type: %s", type);
liuminhaw 3:a0efe12a75cf 81 i_type = socket.send_all(type, strlen(type));
liuminhaw 3:a0efe12a75cf 82 pc.printf("Type: %d\n", i_type);
liuminhaw 3:a0efe12a75cf 83
liuminhaw 3:a0efe12a75cf 84 pc.printf("Content length: %s", contentLen);
liuminhaw 3:a0efe12a75cf 85 i_content = socket.send_all(contentLen, strlen(contentLen));
liuminhaw 3:a0efe12a75cf 86 pc.printf("Content: %d\n", i_content);
liuminhaw 3:a0efe12a75cf 87
liuminhaw 3:a0efe12a75cf 88 pc.printf("Data: %s", data);
liuminhaw 3:a0efe12a75cf 89 i_data = socket.send_all(data, strlen(data));
liuminhaw 3:a0efe12a75cf 90 pc.printf("Data: %d\n", i_data);
yu10078999 2:678f949dccd7 91
liuminhaw 3:a0efe12a75cf 92 char buffer[600];
yu10078999 2:678f949dccd7 93 int ret;
yu10078999 2:678f949dccd7 94 while (true) {
yu10078999 2:678f949dccd7 95 ret = socket.receive(buffer, sizeof(buffer)-1);
yu10078999 2:678f949dccd7 96 if (ret <= 0)
yu10078999 2:678f949dccd7 97 break;
yu10078999 2:678f949dccd7 98 buffer[ret] = '\0';
yu10078999 2:678f949dccd7 99 printf("Received %d chars from server:\n%s\n", ret, buffer);
yu10078999 2:678f949dccd7 100 }
yu10078999 2:678f949dccd7 101 printf("DONE!\n");
yu10078999 2:678f949dccd7 102
liuminhaw 3:a0efe12a75cf 103 int i_close;
liuminhaw 3:a0efe12a75cf 104
liuminhaw 3:a0efe12a75cf 105 i_close = socket.close();
liuminhaw 3:a0efe12a75cf 106 pc.printf("Socket close: %d\n", i_close);
yu10078999 2:678f949dccd7 107
mbedAustin 1:3010b44f07ff 108 eth.disconnect();
yu10078999 2:678f949dccd7 109
liuminhaw 3:a0efe12a75cf 110 pc.printf("\n");
liuminhaw 3:a0efe12a75cf 111 pc.printf("\n");
liuminhaw 3:a0efe12a75cf 112 pc.printf("Program end....\n");
liuminhaw 3:a0efe12a75cf 113 pc.printf("\n");
liuminhaw 3:a0efe12a75cf 114 pc.printf("\n");
liuminhaw 3:a0efe12a75cf 115
yu10078999 2:678f949dccd7 116 while(1) {}
mbedAustin 0:0f0676c43e4b 117 }
yu10078999 2:678f949dccd7 118 extern "C" void mbed_mac_address(char *mac)
yu10078999 2:678f949dccd7 119 {
liuminhaw 3:a0efe12a75cf 120 mac[0] = 0x00;
liuminhaw 3:a0efe12a75cf 121 mac[1] = 0x50;
liuminhaw 3:a0efe12a75cf 122 mac[2] = 0x56;
liuminhaw 3:a0efe12a75cf 123 mac[3] = 0xC0;
liuminhaw 3:a0efe12a75cf 124 mac[4] = 0x00;
liuminhaw 3:a0efe12a75cf 125 mac[5] = 0xAB;
yu10078999 2:678f949dccd7 126 };