1
Dependencies: EthernetInterface IFTTT mbed-rtos mbed
Fork of IFTTT_Ethernet_Example by
main.cpp@0:0f0676c43e4b, 2015-06-28 (annotated)
- Committer:
- mbedAustin
- Date:
- Sun Jun 28 03:41:06 2015 +0000
- Revision:
- 0:0f0676c43e4b
- Child:
- 1:3010b44f07ff
Initial Commit of working GET/ POST interface with IFTT service;
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 | 0:0f0676c43e4b | 3 | #include "HTTPClient.h" |
mbedAustin | 0:0f0676c43e4b | 4 | |
mbedAustin | 0:0f0676c43e4b | 5 | EthernetInterface eth; |
mbedAustin | 0:0f0676c43e4b | 6 | HTTPClient http; |
mbedAustin | 0:0f0676c43e4b | 7 | char str[512] = {}; |
mbedAustin | 0:0f0676c43e4b | 8 | char json[125] = {}; |
mbedAustin | 0:0f0676c43e4b | 9 | char eventName[] = "helloworld"; |
mbedAustin | 0:0f0676c43e4b | 10 | char key[] = "ChangeThisToTheKeyProvidedByIFTTonTheMakerChannel"; |
mbedAustin | 0:0f0676c43e4b | 11 | char value1[] = {"A"}, value2[] = {"B"}, value3[] = {"C"}; |
mbedAustin | 0:0f0676c43e4b | 12 | |
mbedAustin | 0:0f0676c43e4b | 13 | int main() |
mbedAustin | 0:0f0676c43e4b | 14 | { |
mbedAustin | 0:0f0676c43e4b | 15 | eth.init(); //Use DHCP |
mbedAustin | 0:0f0676c43e4b | 16 | eth.connect(); |
mbedAustin | 0:0f0676c43e4b | 17 | printf("IP Address is %s \n\r", eth.getIPAddress()); |
mbedAustin | 0:0f0676c43e4b | 18 | |
mbedAustin | 0:0f0676c43e4b | 19 | //GET |
mbedAustin | 0:0f0676c43e4b | 20 | printf("GETing data... \n\r"); |
mbedAustin | 0:0f0676c43e4b | 21 | sprintf(str, "https://maker.ifttt.com/trigger/%s/with/key/%s?value1=%s&value2=%s&value3=%s",eventName,key,value1,value2,value3); |
mbedAustin | 0:0f0676c43e4b | 22 | printf("String is : %s\n\r",str); |
mbedAustin | 0:0f0676c43e4b | 23 | int ret = http.get(str, str, 128); |
mbedAustin | 0:0f0676c43e4b | 24 | if (!ret) |
mbedAustin | 0:0f0676c43e4b | 25 | { |
mbedAustin | 0:0f0676c43e4b | 26 | printf("Page fetched successfully - read %d characters\n\r", strlen(str)); |
mbedAustin | 0:0f0676c43e4b | 27 | printf("Result: %s\n", str); |
mbedAustin | 0:0f0676c43e4b | 28 | } |
mbedAustin | 0:0f0676c43e4b | 29 | else |
mbedAustin | 0:0f0676c43e4b | 30 | { |
mbedAustin | 0:0f0676c43e4b | 31 | printf("Error - ret = %d - HTTP return code = %d \n\r", ret, http.getHTTPResponseCode()); |
mbedAustin | 0:0f0676c43e4b | 32 | } |
mbedAustin | 0:0f0676c43e4b | 33 | |
mbedAustin | 0:0f0676c43e4b | 34 | //POST |
mbedAustin | 0:0f0676c43e4b | 35 | HTTPMap map; |
mbedAustin | 0:0f0676c43e4b | 36 | HTTPText inText(str, 512); |
mbedAustin | 0:0f0676c43e4b | 37 | map.put("value1", value1); |
mbedAustin | 0:0f0676c43e4b | 38 | map.put("value2", value2); |
mbedAustin | 0:0f0676c43e4b | 39 | map.put("value3", value3); |
mbedAustin | 0:0f0676c43e4b | 40 | sprintf(str, "https://maker.ifttt.com/trigger/%s/with/key/%s",eventName,key); |
mbedAustin | 0:0f0676c43e4b | 41 | printf("String is : %s\n\r",str); |
mbedAustin | 0:0f0676c43e4b | 42 | printf("POSTing data ....\n\r"); |
mbedAustin | 0:0f0676c43e4b | 43 | ret = http.post(str, map, &inText); |
mbedAustin | 0:0f0676c43e4b | 44 | if (!ret) |
mbedAustin | 0:0f0676c43e4b | 45 | { |
mbedAustin | 0:0f0676c43e4b | 46 | printf("Executed POST successfully - read %d characters \n\r", strlen(str)); |
mbedAustin | 0:0f0676c43e4b | 47 | printf("Result: %s \n\r", str); |
mbedAustin | 0:0f0676c43e4b | 48 | } |
mbedAustin | 0:0f0676c43e4b | 49 | else |
mbedAustin | 0:0f0676c43e4b | 50 | { |
mbedAustin | 0:0f0676c43e4b | 51 | printf("Error - ret = %d - HTTP return code = %d \n\r", ret, http.getHTTPResponseCode()); |
mbedAustin | 0:0f0676c43e4b | 52 | } |
mbedAustin | 0:0f0676c43e4b | 53 | |
mbedAustin | 0:0f0676c43e4b | 54 | |
mbedAustin | 0:0f0676c43e4b | 55 | |
mbedAustin | 0:0f0676c43e4b | 56 | eth.disconnect(); |
mbedAustin | 0:0f0676c43e4b | 57 | |
mbedAustin | 0:0f0676c43e4b | 58 | while(1) { |
mbedAustin | 0:0f0676c43e4b | 59 | } |
mbedAustin | 0:0f0676c43e4b | 60 | } |