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 W5500-Ethernet-Interface Makers

Revision:
2:b63f21ff9817
Parent:
0:a0431fb6c6e0
--- a/main.cpp	Tue Sep 02 01:19:33 2014 +0000
+++ b/main.cpp	Wed Apr 29 23:50:41 2015 +0000
@@ -1,54 +1,65 @@
 #include "mbed.h"
-#include "W5500Interface/EthernetInterface.h"
+#include "WIZnetInterface.h"
 #include "HTTPClient.h"
 
- 
+#define USE_DHCP    0
+
+#define LOOPBACKPORT    5000
+
+const char * IP_Addr    = "222.98.173.212";
+const char * IP_Subnet  = "255.255.255.192";
+const char * IP_Gateway = "222.98.173.254";
+unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56};
+
+Serial pc(USBTX, USBRX);
+
+#ifdef TARGET_LPC11U68
+SPI spi(P0_9,P0_8,P1_29);
+WIZnetInterface ethernet(&spi,P0_2,P1_13);
+#endif
+
 int main() {
 //    EthernetInterface eth;
 // change for W5500 interface.
-#if defined(TARGET_LPC1114)
-    SPI spi(dp2, dp1, dp6); // mosi, miso, sclk
-    EthernetInterface eth(&spi, dp25, dp26); // spi, cs, reset
 
-#elif defined(TARGET_LPC1768)
-    SPI spi(p11, p12, p13); // mosi, miso, sclk
-    EthernetInterface eth(&spi, p14, p15); // spi, cs, reset
-#elif defined(TARGET_LPC11U68)
-    SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk
-    EthernetInterface eth(&spi, P0_2, P1_28);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io
-    spi.format(8,0); // 8bit, mode 0
-    spi.frequency(7000000); // 7MHz
-    wait(1); // 1 second for stable state
-#elif defined(TARGET_KL25Z)
-    Serial pc(USBTX, USBRX);
+    mbed_mac_address((char *)MAC_Addr); //Use mbed mac addres
     pc.baud(115200);
-    printf("spi init\r\n");
-    SPI spi(D11, D12, D13); // mosi, miso, sclk
-    wait(1); // 1 second for stable state
-    EthernetInterface eth(&spi, D10, D9);//scs(D10), nRESET(PTA20)
-    printf("App Start\r\n");
-    wait(1); // 1 second for stable state
-#endif
-    eth.init(); //Use DHCP
-    eth.connect();
-
-    printf("IP Address is %s\r\n", eth.getIPAddress());
+    
+    #if USE_DHCP
+    int ret = ethernet.init(MAC_Addr);
+    #else
+    int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
+    #endif
+    if (!ret) {
+        pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
+        ret = ethernet.connect();
+        if (!ret) {
+            pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
+                      ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
+        } else {
+            pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
+            exit(0);
+        }
+    } else {
+        pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
+        exit(0);
+    }
     
     char str[512];
     char get_msg[512]= "http://api.openweathermap.org/data/2.5/weather?q=Seoul";
     HTTPClient http;
     
-    printf("Send get Message to openeathermap.org\r\n");
-    printf("msg : %s\r\n",get_msg);
-    int ret = http.get(get_msg, str, sizeof(str));
+    pc.printf("Send get Message to openeathermap.org\r\n");
+    pc.printf("msg : %s\r\n",get_msg);
+    ret = http.get(get_msg, str, sizeof(str));
     if(!ret)
     {
-      printf("\r\nPage fetched successfully - read %d characters\r\n", strlen(str));
-      printf("Result: %s\r\n", str);
+      pc.printf("\r\nPage fetched successfully - read %d characters\r\n", strlen(str));
+      pc.printf("Result: %s\r\n", str);
     }
     else
     {
-      printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
+      pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
     }
    
 }