IFTTT: helloworld
Dependencies: EthernetInterface mbed-rtos mbed
Fork of IFTTT_Done by
main.cpp@2:678f949dccd7, 2016-09-11 (annotated)
- Committer:
- yu10078999
- Date:
- Sun Sep 11 07:08:30 2016 +0000
- Revision:
- 2:678f949dccd7
- Parent:
- 1:3010b44f07ff
- Child:
- 3:a0efe12a75cf
0
Who changed what in which revision?
User | Revision | Line number | New 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 | |
mbedAustin | 0:0f0676c43e4b | 5 | EthernetInterface eth; |
mbedAustin | 1:3010b44f07ff | 6 | RawSerial pc(USBTX, USBRX); // tx, rx |
mbedAustin | 0:0f0676c43e4b | 7 | |
mbedAustin | 1:3010b44f07ff | 8 | int main() |
mbedAustin | 0:0f0676c43e4b | 9 | { |
mbedAustin | 1:3010b44f07ff | 10 | pc.baud(9600); |
mbedAustin | 0:0f0676c43e4b | 11 | eth.init(); //Use DHCP |
mbedAustin | 0:0f0676c43e4b | 12 | eth.connect(); |
mbedAustin | 0:0f0676c43e4b | 13 | printf("IP Address is %s \n\r", eth.getIPAddress()); |
mbedAustin | 1:3010b44f07ff | 14 | TCPSocketConnection socket; |
yu10078999 | 2:678f949dccd7 | 15 | |
yu10078999 | 2:678f949dccd7 | 16 | socket.connect("maker.ifttt.com", 80); |
yu10078999 | 2:678f949dccd7 | 17 | /* |
yu10078999 | 2:678f949dccd7 | 18 | char str[]=""; |
yu10078999 | 2:678f949dccd7 | 19 | char *post = "POST /trigger/OWJMAN/with/key/bkJhw8soKcjx28zrqAgeW2 HTTP/1.1\r\n"; |
yu10078999 | 2:678f949dccd7 | 20 | char *host = "Host: maker.ifttt.com\r\n"; |
yu10078999 | 2:678f949dccd7 | 21 | char *type = "Content-Type: application/json\r\n\r\n"; |
yu10078999 | 2:678f949dccd7 | 22 | char contentLen[50] = {0}; |
yu10078999 | 2:678f949dccd7 | 23 | char *data = "{\"value1\":\"this is awesome\",\"value2\":\"test-ing\",\"value3\":\"data!!!\"}\r\n"; |
yu10078999 | 2:678f949dccd7 | 24 | sprintf(contentLen,"Content-Length: %d\r\n",strlen(data)); |
yu10078999 | 2:678f949dccd7 | 25 | sprintf(str,"%s%s%s%s%s",post,host,contentLen,type,data); |
yu10078999 | 2:678f949dccd7 | 26 | socket.send_all(str, strlen(str));*/ |
yu10078999 | 2:678f949dccd7 | 27 | |
yu10078999 | 2:678f949dccd7 | 28 | char *post = "POST /trigger/OWJMAN/with/key/bkJhw8soKcjx28zrqAgeW2 HTTP/1.1\r\n"; |
yu10078999 | 2:678f949dccd7 | 29 | char *host = "Host: maker.ifttt.com\r\n"; |
yu10078999 | 2:678f949dccd7 | 30 | char *type = "Content-Type: application/json\r\n\r\n"; |
yu10078999 | 2:678f949dccd7 | 31 | char contentLen[] = ""; |
yu10078999 | 2:678f949dccd7 | 32 | char *data = "{\"value1\":\"this is awesome\",\"value2\":\"test-ing\",\"value3\":\"data!!!\"}\r\n"; |
yu10078999 | 2:678f949dccd7 | 33 | sprintf(contentLen,"Content-Length: %d\r\n",strlen(data)); |
yu10078999 | 2:678f949dccd7 | 34 | socket.send_all(post, strlen(post)); |
yu10078999 | 2:678f949dccd7 | 35 | socket.send_all(host, strlen(host)); |
yu10078999 | 2:678f949dccd7 | 36 | socket.send_all(type, strlen(type)); |
yu10078999 | 2:678f949dccd7 | 37 | socket.send_all(contentLen, strlen(contentLen)); |
yu10078999 | 2:678f949dccd7 | 38 | socket.send_all(data, strlen(data)); |
yu10078999 | 2:678f949dccd7 | 39 | |
yu10078999 | 2:678f949dccd7 | 40 | char buffer[300]; |
yu10078999 | 2:678f949dccd7 | 41 | int ret; |
yu10078999 | 2:678f949dccd7 | 42 | while (true) { |
yu10078999 | 2:678f949dccd7 | 43 | ret = socket.receive(buffer, sizeof(buffer)-1); |
yu10078999 | 2:678f949dccd7 | 44 | if (ret <= 0) |
yu10078999 | 2:678f949dccd7 | 45 | break; |
yu10078999 | 2:678f949dccd7 | 46 | buffer[ret] = '\0'; |
yu10078999 | 2:678f949dccd7 | 47 | printf("Received %d chars from server:\n%s\n", ret, buffer); |
yu10078999 | 2:678f949dccd7 | 48 | } |
yu10078999 | 2:678f949dccd7 | 49 | printf("DONE!\n"); |
yu10078999 | 2:678f949dccd7 | 50 | |
yu10078999 | 2:678f949dccd7 | 51 | socket.close(); |
yu10078999 | 2:678f949dccd7 | 52 | |
mbedAustin | 1:3010b44f07ff | 53 | eth.disconnect(); |
yu10078999 | 2:678f949dccd7 | 54 | |
yu10078999 | 2:678f949dccd7 | 55 | while(1) {} |
mbedAustin | 0:0f0676c43e4b | 56 | } |
yu10078999 | 2:678f949dccd7 | 57 | extern "C" void mbed_mac_address(char *mac) |
yu10078999 | 2:678f949dccd7 | 58 | { |
yu10078999 | 2:678f949dccd7 | 59 | mac[0] = 0xD4; |
yu10078999 | 2:678f949dccd7 | 60 | mac[1] = 0x3F; |
yu10078999 | 2:678f949dccd7 | 61 | mac[2] = 0x7E; |
yu10078999 | 2:678f949dccd7 | 62 | mac[3] = 0x4C; |
yu10078999 | 2:678f949dccd7 | 63 | mac[4] = 0x7E; |
yu10078999 | 2:678f949dccd7 | 64 | mac[5] = 0x1C; |
yu10078999 | 2:678f949dccd7 | 65 | }; |