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


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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jehoon 0:af541535ef54 1 #include "mbed.h"
jehoon 0:af541535ef54 2 #include "EthernetInterface.h"
jehoon 0:af541535ef54 3
jehoon 0:af541535ef54 4 int main() {
jehoon 0:af541535ef54 5
jehoon 0:af541535ef54 6 int phy_link;
jehoon 0:af541535ef54 7 printf("Wait a second...\r\n");
jehoon 0:af541535ef54 8 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x1c, 0xa8, 0x95};
jehoon 0:af541535ef54 9
jehoon 0:af541535ef54 10 EthernetInterface eth;
jehoon 0:af541535ef54 11 eth.init(mac_addr); //Use DHCP
jehoon 0:af541535ef54 12 eth.connect();
jehoon 0:af541535ef54 13
jehoon 0:af541535ef54 14 do{
jehoon 0:af541535ef54 15 phy_link = eth.ethernet_link();
jehoon 0:af541535ef54 16 printf("...");
jehoon 0:af541535ef54 17 wait(2);
jehoon 0:af541535ef54 18 }while(!phy_link);
jehoon 0:af541535ef54 19 printf("\r\n");
jehoon 0:af541535ef54 20
jehoon 0:af541535ef54 21 printf("IP Address is %s\r\n", eth.getIPAddress());
jehoon 0:af541535ef54 22
jehoon 0:af541535ef54 23 TCPSocketConnection sock;
jehoon 0:af541535ef54 24 sock.connect("api.openweathermap.org", 80);
jehoon 0:af541535ef54 25
jehoon 0:af541535ef54 26
jehoon 0:af541535ef54 27 char http_cmd[] = "GET /data/2.5/weather?q=Seoul,kr HTTP/1.0\n\n";
jehoon 0:af541535ef54 28 sock.send_all(http_cmd, sizeof(http_cmd)-1);
jehoon 0:af541535ef54 29
jehoon 0:af541535ef54 30 char buffer[400];
jehoon 0:af541535ef54 31 int ret;
jehoon 0:af541535ef54 32 while (true) {
jehoon 0:af541535ef54 33 ret = sock.receive(buffer, sizeof(buffer)-1);
jehoon 0:af541535ef54 34 if (ret <= 0) break;
jehoon 0:af541535ef54 35 buffer[ret] = '\0';
jehoon 0:af541535ef54 36 printf("Received %d chars from server: %s\n", ret, buffer);
jehoon 0:af541535ef54 37 }
jehoon 0:af541535ef54 38
jehoon 0:af541535ef54 39 sock.close();
jehoon 0:af541535ef54 40
jehoon 0:af541535ef54 41 eth.disconnect();
jehoon 0:af541535ef54 42
jehoon 0:af541535ef54 43 }