TCP Client, WIZwiki-W7500ECO. It will get weather of South Korea from openweathermap.org

Dependencies:   WIZnetInterface mbed

Fork of TCPClient_HelloWorld by jehoon song

Prerequisite

This example is for obtainning and printting data from server(OpenWeatherMap). WIZwiki-W7500ECO is TCP client mode.

To implement this function, you need a Platform board, network Interface board.


Hardware Configuration

WIZwiki-W7500ECO Pin map

http://wizwiki.net/wiki/lib/exe/fetch.php?media=products:wizwiki-w7500eco:wizwiki-w7500eco_detailpinout.png


Software

Select the city

main.cpp

#include "mbed.h"
#include "EthernetInterface.h"

int main() {
   
    int phy_link;
    printf("Wait a second...\r\n");
    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x01, 0x02, 0x03};
    
    EthernetInterface eth;
    eth.init(mac_addr); //Use DHCP
    eth.connect();
    
     do{
     phy_link = eth.ethernet_link();
     printf("...");
     wait(2);
     }while(!phy_link);
     printf("\r\n");
     
    printf("IP Address is %s\r\n", eth.getIPAddress());
    
    TCPSocketConnection sock;
    sock.connect("api.openweathermap.org", 80);
    

    char http_cmd[] = "GET /data/2.5/weather?q=Seoul,kr HTTP/1.0\n\n";
    sock.send_all(http_cmd, sizeof(http_cmd)-1);

    char buffer[400];
    int ret;
    while (true) {
        ret = sock.receive(buffer, sizeof(buffer)-1);
        if (ret <= 0) break;
        buffer[ret] = '\0';
        printf("Received %d chars from server: %s\n", ret, buffer);     
    }

    sock.close();
    
    eth.disconnect();

}

Images

/media/uploads/bingdo/tcpclient_helloworld.jpg /media/uploads/bingdo/tcpclient_helloworld_WT9sVj9.png


Files at this revision

API Documentation at this revision

Comitter:
jehoon
Date:
Mon May 30 05:02:08 2016 +0000
Commit message:
TCP Client Example, WIZwiki-W7500.; It will get weather of South Korea from openweathermap.org;

Changed in this revision

WIZnetInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r af541535ef54 WIZnetInterface.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZnetInterface.lib	Mon May 30 05:02:08 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#c91884bd2713
diff -r 000000000000 -r af541535ef54 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon May 30 05:02:08 2016 +0000
@@ -0,0 +1,43 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+
+int main() {
+   
+    int phy_link;
+    printf("Wait a second...\r\n");
+    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x1c, 0xa8, 0x95}; 
+    
+    EthernetInterface eth;
+    eth.init(mac_addr); //Use DHCP
+    eth.connect();
+    
+     do{
+     phy_link = eth.ethernet_link();
+     printf("...");
+     wait(2);
+     }while(!phy_link);
+     printf("\r\n");
+     
+    printf("IP Address is %s\r\n", eth.getIPAddress());
+    
+    TCPSocketConnection sock;
+    sock.connect("api.openweathermap.org", 80);
+    
+
+    char http_cmd[] = "GET /data/2.5/weather?q=Seoul,kr HTTP/1.0\n\n";
+    sock.send_all(http_cmd, sizeof(http_cmd)-1);
+
+    char buffer[400];
+    int ret;
+    while (true) {
+        ret = sock.receive(buffer, sizeof(buffer)-1);
+        if (ret <= 0) break;
+        buffer[ret] = '\0';
+        printf("Received %d chars from server: %s\n", ret, buffer);     
+    }
+
+    sock.close();
+    
+    eth.disconnect();
+
+}
\ No newline at end of file
diff -r 000000000000 -r af541535ef54 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon May 30 05:02:08 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/6c34061e7c34
\ No newline at end of file