HTTP client example using W5500 ethernet kit for IoT. Get weather information of Seoul, South Korea.
Dependencies: HTTPClient WIZnet_Library mbed
Fork of CurrentWeatherData_W5500 by
main.cpp@0:a0431fb6c6e0, 2014-09-02 (annotated)
- Committer:
- kaizen
- Date:
- Tue Sep 02 01:17:05 2014 +0000
- Revision:
- 0:a0431fb6c6e0
- Child:
- 2:b63f21ff9817
First Commit for getting weather information using W5500
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kaizen | 0:a0431fb6c6e0 | 1 | #include "mbed.h" |
kaizen | 0:a0431fb6c6e0 | 2 | #include "W5500Interface/EthernetInterface.h" |
kaizen | 0:a0431fb6c6e0 | 3 | #include "HTTPClient.h" |
kaizen | 0:a0431fb6c6e0 | 4 | |
kaizen | 0:a0431fb6c6e0 | 5 | |
kaizen | 0:a0431fb6c6e0 | 6 | int main() { |
kaizen | 0:a0431fb6c6e0 | 7 | // EthernetInterface eth; |
kaizen | 0:a0431fb6c6e0 | 8 | // change for W5500 interface. |
kaizen | 0:a0431fb6c6e0 | 9 | #if defined(TARGET_LPC1114) |
kaizen | 0:a0431fb6c6e0 | 10 | SPI spi(dp2, dp1, dp6); // mosi, miso, sclk |
kaizen | 0:a0431fb6c6e0 | 11 | EthernetInterface eth(&spi, dp25, dp26); // spi, cs, reset |
kaizen | 0:a0431fb6c6e0 | 12 | |
kaizen | 0:a0431fb6c6e0 | 13 | #elif defined(TARGET_LPC1768) |
kaizen | 0:a0431fb6c6e0 | 14 | SPI spi(p11, p12, p13); // mosi, miso, sclk |
kaizen | 0:a0431fb6c6e0 | 15 | EthernetInterface eth(&spi, p14, p15); // spi, cs, reset |
kaizen | 0:a0431fb6c6e0 | 16 | #elif defined(TARGET_LPC11U68) |
kaizen | 0:a0431fb6c6e0 | 17 | SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk |
kaizen | 0:a0431fb6c6e0 | 18 | EthernetInterface eth(&spi, P0_2, P1_28);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io |
kaizen | 0:a0431fb6c6e0 | 19 | spi.format(8,0); // 8bit, mode 0 |
kaizen | 0:a0431fb6c6e0 | 20 | spi.frequency(7000000); // 7MHz |
kaizen | 0:a0431fb6c6e0 | 21 | wait(1); // 1 second for stable state |
kaizen | 0:a0431fb6c6e0 | 22 | #elif defined(TARGET_KL25Z) |
kaizen | 0:a0431fb6c6e0 | 23 | Serial pc(USBTX, USBRX); |
kaizen | 0:a0431fb6c6e0 | 24 | pc.baud(115200); |
kaizen | 0:a0431fb6c6e0 | 25 | printf("spi init\r\n"); |
kaizen | 0:a0431fb6c6e0 | 26 | SPI spi(D11, D12, D13); // mosi, miso, sclk |
kaizen | 0:a0431fb6c6e0 | 27 | wait(1); // 1 second for stable state |
kaizen | 0:a0431fb6c6e0 | 28 | EthernetInterface eth(&spi, D10, D9);//scs(D10), nRESET(PTA20) |
kaizen | 0:a0431fb6c6e0 | 29 | printf("App Start\r\n"); |
kaizen | 0:a0431fb6c6e0 | 30 | wait(1); // 1 second for stable state |
kaizen | 0:a0431fb6c6e0 | 31 | #endif |
kaizen | 0:a0431fb6c6e0 | 32 | eth.init(); //Use DHCP |
kaizen | 0:a0431fb6c6e0 | 33 | eth.connect(); |
kaizen | 0:a0431fb6c6e0 | 34 | |
kaizen | 0:a0431fb6c6e0 | 35 | printf("IP Address is %s\r\n", eth.getIPAddress()); |
kaizen | 0:a0431fb6c6e0 | 36 | |
kaizen | 0:a0431fb6c6e0 | 37 | char str[512]; |
kaizen | 0:a0431fb6c6e0 | 38 | char get_msg[512]= "http://api.openweathermap.org/data/2.5/weather?q=Seoul"; |
kaizen | 0:a0431fb6c6e0 | 39 | HTTPClient http; |
kaizen | 0:a0431fb6c6e0 | 40 | |
kaizen | 0:a0431fb6c6e0 | 41 | printf("Send get Message to openeathermap.org\r\n"); |
kaizen | 0:a0431fb6c6e0 | 42 | printf("msg : %s\r\n",get_msg); |
kaizen | 0:a0431fb6c6e0 | 43 | int ret = http.get(get_msg, str, sizeof(str)); |
kaizen | 0:a0431fb6c6e0 | 44 | if(!ret) |
kaizen | 0:a0431fb6c6e0 | 45 | { |
kaizen | 0:a0431fb6c6e0 | 46 | printf("\r\nPage fetched successfully - read %d characters\r\n", strlen(str)); |
kaizen | 0:a0431fb6c6e0 | 47 | printf("Result: %s\r\n", str); |
kaizen | 0:a0431fb6c6e0 | 48 | } |
kaizen | 0:a0431fb6c6e0 | 49 | else |
kaizen | 0:a0431fb6c6e0 | 50 | { |
kaizen | 0:a0431fb6c6e0 | 51 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
kaizen | 0:a0431fb6c6e0 | 52 | } |
kaizen | 0:a0431fb6c6e0 | 53 | |
kaizen | 0:a0431fb6c6e0 | 54 | } |