Simple code to demonstrate how to make an HTTP GET request to fetch a stream from an RSS feed. Note: Demo program to be used on the GeekSessionLab Talk (November 2011). http://devrendezvous.com/?lang=en
Dependencies: EthernetNetIf mbed
main.cpp@0:0946c896455e, 2011-10-27 (annotated)
- Committer:
- botdream
- Date:
- Thu Oct 27 22:37:47 2011 +0000
- Revision:
- 0:0946c896455e
GeekSessionLab talk.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
botdream | 0:0946c896455e | 1 | //--------------------------------------------------------------------------------------------- |
botdream | 0:0946c896455e | 2 | #include "mbed.h" |
botdream | 0:0946c896455e | 3 | #include "EthernetNetIf.h" |
botdream | 0:0946c896455e | 4 | #include "HTTPClient.h" |
botdream | 0:0946c896455e | 5 | //--------------------------------------------------------------------------------------------- |
botdream | 0:0946c896455e | 6 | DigitalOut myled(LED1); |
botdream | 0:0946c896455e | 7 | Serial pc(USBTX, USBRX); // tx, rx |
botdream | 0:0946c896455e | 8 | //--------------------------------------------------------------------------------------------- |
botdream | 0:0946c896455e | 9 | #define internaldebug // send debug messages to USB Serial port (9600,1,N) |
botdream | 0:0946c896455e | 10 | #define dhcpenable // auto-setup IP Address from DHCP router |
botdream | 0:0946c896455e | 11 | //--------------------------------------------------------------------------------------------- |
botdream | 0:0946c896455e | 12 | // Ethernet Object Setup |
botdream | 0:0946c896455e | 13 | //--------------------------------------------------------------------------------------------- |
botdream | 0:0946c896455e | 14 | #ifdef dhcpenable |
botdream | 0:0946c896455e | 15 | EthernetNetIf eth; |
botdream | 0:0946c896455e | 16 | #else |
botdream | 0:0946c896455e | 17 | EthernetNetIf eth( |
botdream | 0:0946c896455e | 18 | IpAddr(192,168,1,100), //IP Address |
botdream | 0:0946c896455e | 19 | IpAddr(255,255,255,0), //Network Mask |
botdream | 0:0946c896455e | 20 | IpAddr(192,168,1,254), //Gateway |
botdream | 0:0946c896455e | 21 | IpAddr(192,168,1,254) //DNS |
botdream | 0:0946c896455e | 22 | ); |
botdream | 0:0946c896455e | 23 | #endif |
botdream | 0:0946c896455e | 24 | //--------------------------------------------------------------------------------------------- |
botdream | 0:0946c896455e | 25 | // HttpClient |
botdream | 0:0946c896455e | 26 | HTTPClient http; |
botdream | 0:0946c896455e | 27 | |
botdream | 0:0946c896455e | 28 | HTTPResult result; |
botdream | 0:0946c896455e | 29 | bool completed = false; |
botdream | 0:0946c896455e | 30 | void request_callback(HTTPResult r) |
botdream | 0:0946c896455e | 31 | { |
botdream | 0:0946c896455e | 32 | result = r; |
botdream | 0:0946c896455e | 33 | completed = true; |
botdream | 0:0946c896455e | 34 | } |
botdream | 0:0946c896455e | 35 | //--------------------------------------------------------------------------------------------- |
botdream | 0:0946c896455e | 36 | // MAIN |
botdream | 0:0946c896455e | 37 | //--------------------------------------------------------------------------------------------- |
botdream | 0:0946c896455e | 38 | int main() |
botdream | 0:0946c896455e | 39 | { |
botdream | 0:0946c896455e | 40 | // Set Serial Port Transfer Rate |
botdream | 0:0946c896455e | 41 | pc.baud(115200); |
botdream | 0:0946c896455e | 42 | |
botdream | 0:0946c896455e | 43 | //-------------------------------------------------------- |
botdream | 0:0946c896455e | 44 | // Setting Ethernet |
botdream | 0:0946c896455e | 45 | //-------------------------------------------------------- |
botdream | 0:0946c896455e | 46 | #ifdef internaldebug |
botdream | 0:0946c896455e | 47 | printf("\r\nSetting up Ethernet interface!\r\n"); |
botdream | 0:0946c896455e | 48 | #endif |
botdream | 0:0946c896455e | 49 | // Create return object for error check |
botdream | 0:0946c896455e | 50 | EthernetErr ethErr = eth.setup(); |
botdream | 0:0946c896455e | 51 | if(ethErr) |
botdream | 0:0946c896455e | 52 | { |
botdream | 0:0946c896455e | 53 | #ifdef internaldebug |
botdream | 0:0946c896455e | 54 | printf("\r\nError %d in Ethernet setup.\r\n", ethErr); |
botdream | 0:0946c896455e | 55 | #endif |
botdream | 0:0946c896455e | 56 | return -1; |
botdream | 0:0946c896455e | 57 | } |
botdream | 0:0946c896455e | 58 | #ifdef internaldebug |
botdream | 0:0946c896455e | 59 | printf("\r\nEthernet setup completed with success!\r\n"); |
botdream | 0:0946c896455e | 60 | #endif |
botdream | 0:0946c896455e | 61 | //-------------------------------------------------------- |
botdream | 0:0946c896455e | 62 | |
botdream | 0:0946c896455e | 63 | //-------------------------------------------------------- |
botdream | 0:0946c896455e | 64 | // HTTP Get Stream Request |
botdream | 0:0946c896455e | 65 | //-------------------------------------------------------- |
botdream | 0:0946c896455e | 66 | HTTPStream stream; |
botdream | 0:0946c896455e | 67 | |
botdream | 0:0946c896455e | 68 | char BigBuf[512 + 1] = {0}; |
botdream | 0:0946c896455e | 69 | stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read |
botdream | 0:0946c896455e | 70 | |
botdream | 0:0946c896455e | 71 | printf("\r\nHttpClient Get Stream Request ...\r\n"); |
botdream | 0:0946c896455e | 72 | |
botdream | 0:0946c896455e | 73 | HTTPResult r = http.get("http://services.sapo.pt/RSS/Feed/tek", &stream, request_callback); //Load a very large page, such as the hackaday RSS feed |
botdream | 0:0946c896455e | 74 | |
botdream | 0:0946c896455e | 75 | while(!completed) |
botdream | 0:0946c896455e | 76 | { |
botdream | 0:0946c896455e | 77 | Net::poll(); //Polls the Networking stack |
botdream | 0:0946c896455e | 78 | if(stream.readable()) |
botdream | 0:0946c896455e | 79 | { |
botdream | 0:0946c896455e | 80 | BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string |
botdream | 0:0946c896455e | 81 | printf("%s\r\n",BigBuf); //Display it while loading |
botdream | 0:0946c896455e | 82 | //Note: some servers do not like if you throttle them too much, so printf'ing during a request is generally bad practice |
botdream | 0:0946c896455e | 83 | stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it |
botdream | 0:0946c896455e | 84 | } |
botdream | 0:0946c896455e | 85 | } |
botdream | 0:0946c896455e | 86 | printf("\n--------------\n"); |
botdream | 0:0946c896455e | 87 | if(result == HTTP_OK) |
botdream | 0:0946c896455e | 88 | { |
botdream | 0:0946c896455e | 89 | printf("Read completely\n"); |
botdream | 0:0946c896455e | 90 | } |
botdream | 0:0946c896455e | 91 | else |
botdream | 0:0946c896455e | 92 | { |
botdream | 0:0946c896455e | 93 | printf("Error %d\n", result); |
botdream | 0:0946c896455e | 94 | } |
botdream | 0:0946c896455e | 95 | //-------------------------------------------------------- |
botdream | 0:0946c896455e | 96 | |
botdream | 0:0946c896455e | 97 | // main loop |
botdream | 0:0946c896455e | 98 | while(1) |
botdream | 0:0946c896455e | 99 | { |
botdream | 0:0946c896455e | 100 | myled = 1; |
botdream | 0:0946c896455e | 101 | wait(0.5); |
botdream | 0:0946c896455e | 102 | myled = 0; |
botdream | 0:0946c896455e | 103 | wait(0.5); |
botdream | 0:0946c896455e | 104 | } |
botdream | 0:0946c896455e | 105 | } |
botdream | 0:0946c896455e | 106 | //--------------------------------------------------------------------------------------------- |