Simple code to demonstrate how to make an HTTP GET request to fetch data from a specific file form an URL. Note: Demo program to be used on the GeekSessionLab Talk (November 2011). http://devrendezvous.com/?lang=en
Dependencies: EthernetNetIf mbed
main.cpp@0:81f7bb57ffcf, 2011-10-27 (annotated)
- Committer:
- botdream
- Date:
- Thu Oct 27 22:34:13 2011 +0000
- Revision:
- 0:81f7bb57ffcf
GeekSessionLab talk.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
botdream | 0:81f7bb57ffcf | 1 | //--------------------------------------------------------------------------------------------- |
botdream | 0:81f7bb57ffcf | 2 | #include "mbed.h" |
botdream | 0:81f7bb57ffcf | 3 | #include "EthernetNetIf.h" |
botdream | 0:81f7bb57ffcf | 4 | #include "HTTPClient.h" |
botdream | 0:81f7bb57ffcf | 5 | //--------------------------------------------------------------------------------------------- |
botdream | 0:81f7bb57ffcf | 6 | DigitalOut myled(LED1); |
botdream | 0:81f7bb57ffcf | 7 | Serial pc(USBTX, USBRX); // tx, rx |
botdream | 0:81f7bb57ffcf | 8 | //--------------------------------------------------------------------------------------------- |
botdream | 0:81f7bb57ffcf | 9 | #define internaldebug // send debug messages to USB Serial port (9600,1,N) |
botdream | 0:81f7bb57ffcf | 10 | #define dhcpenable // auto-setup IP Address from DHCP router |
botdream | 0:81f7bb57ffcf | 11 | //--------------------------------------------------------------------------------------------- |
botdream | 0:81f7bb57ffcf | 12 | // Ethernet Object Setup |
botdream | 0:81f7bb57ffcf | 13 | //--------------------------------------------------------------------------------------------- |
botdream | 0:81f7bb57ffcf | 14 | #ifdef dhcpenable |
botdream | 0:81f7bb57ffcf | 15 | EthernetNetIf eth; |
botdream | 0:81f7bb57ffcf | 16 | #else |
botdream | 0:81f7bb57ffcf | 17 | EthernetNetIf eth( |
botdream | 0:81f7bb57ffcf | 18 | IpAddr(192,168,1,100), //IP Address |
botdream | 0:81f7bb57ffcf | 19 | IpAddr(255,255,255,0), //Network Mask |
botdream | 0:81f7bb57ffcf | 20 | IpAddr(192,168,1,254), //Gateway |
botdream | 0:81f7bb57ffcf | 21 | IpAddr(192,168,1,254) //DNS |
botdream | 0:81f7bb57ffcf | 22 | ); |
botdream | 0:81f7bb57ffcf | 23 | #endif |
botdream | 0:81f7bb57ffcf | 24 | |
botdream | 0:81f7bb57ffcf | 25 | HTTPClient http; |
botdream | 0:81f7bb57ffcf | 26 | //--------------------------------------------------------------------------------------------- |
botdream | 0:81f7bb57ffcf | 27 | // MAIN |
botdream | 0:81f7bb57ffcf | 28 | //--------------------------------------------------------------------------------------------- |
botdream | 0:81f7bb57ffcf | 29 | int main() |
botdream | 0:81f7bb57ffcf | 30 | { |
botdream | 0:81f7bb57ffcf | 31 | // Set Serial Port Transfer Rate |
botdream | 0:81f7bb57ffcf | 32 | pc.baud(115200); |
botdream | 0:81f7bb57ffcf | 33 | |
botdream | 0:81f7bb57ffcf | 34 | //-------------------------------------------------------- |
botdream | 0:81f7bb57ffcf | 35 | // Setting Ethernet |
botdream | 0:81f7bb57ffcf | 36 | //-------------------------------------------------------- |
botdream | 0:81f7bb57ffcf | 37 | #ifdef internaldebug |
botdream | 0:81f7bb57ffcf | 38 | printf("\r\nSetting up Ethernet interface!\r\n"); |
botdream | 0:81f7bb57ffcf | 39 | #endif |
botdream | 0:81f7bb57ffcf | 40 | // Create return object for error check |
botdream | 0:81f7bb57ffcf | 41 | EthernetErr ethErr = eth.setup(); |
botdream | 0:81f7bb57ffcf | 42 | if(ethErr) |
botdream | 0:81f7bb57ffcf | 43 | { |
botdream | 0:81f7bb57ffcf | 44 | #ifdef internaldebug |
botdream | 0:81f7bb57ffcf | 45 | printf("\r\nError %d in Ethernet setup.\r\n", ethErr); |
botdream | 0:81f7bb57ffcf | 46 | #endif |
botdream | 0:81f7bb57ffcf | 47 | return -1; |
botdream | 0:81f7bb57ffcf | 48 | } |
botdream | 0:81f7bb57ffcf | 49 | #ifdef internaldebug |
botdream | 0:81f7bb57ffcf | 50 | printf("\r\nEthernet setup completed with success!\r\n"); |
botdream | 0:81f7bb57ffcf | 51 | #endif |
botdream | 0:81f7bb57ffcf | 52 | //-------------------------------------------------------- |
botdream | 0:81f7bb57ffcf | 53 | |
botdream | 0:81f7bb57ffcf | 54 | //-------------------------------------------------------- |
botdream | 0:81f7bb57ffcf | 55 | // HTTP Get Request |
botdream | 0:81f7bb57ffcf | 56 | //-------------------------------------------------------- |
botdream | 0:81f7bb57ffcf | 57 | HTTPText txt; |
botdream | 0:81f7bb57ffcf | 58 | |
botdream | 0:81f7bb57ffcf | 59 | HTTPResult r = http.get("http://botdream.com/robots.txt", &txt); |
botdream | 0:81f7bb57ffcf | 60 | if(r==HTTP_OK) |
botdream | 0:81f7bb57ffcf | 61 | { |
botdream | 0:81f7bb57ffcf | 62 | printf("Result :\"%s\"\n", txt.gets()); |
botdream | 0:81f7bb57ffcf | 63 | } |
botdream | 0:81f7bb57ffcf | 64 | else |
botdream | 0:81f7bb57ffcf | 65 | { |
botdream | 0:81f7bb57ffcf | 66 | printf("Error %d\n", r); |
botdream | 0:81f7bb57ffcf | 67 | } |
botdream | 0:81f7bb57ffcf | 68 | //-------------------------------------------------------- |
botdream | 0:81f7bb57ffcf | 69 | |
botdream | 0:81f7bb57ffcf | 70 | // main loop |
botdream | 0:81f7bb57ffcf | 71 | while(1) |
botdream | 0:81f7bb57ffcf | 72 | { |
botdream | 0:81f7bb57ffcf | 73 | myled = 1; |
botdream | 0:81f7bb57ffcf | 74 | wait(0.5); |
botdream | 0:81f7bb57ffcf | 75 | myled = 0; |
botdream | 0:81f7bb57ffcf | 76 | wait(0.5); |
botdream | 0:81f7bb57ffcf | 77 | } |
botdream | 0:81f7bb57ffcf | 78 | } |
botdream | 0:81f7bb57ffcf | 79 | //--------------------------------------------------------------------------------------------- |