Using TCP Client on WIZwiki-W7500, display weather conditions on led and temperature with servo-motor

Dependencies:   WIZnetInterface mbed-src

Prerequisite

This example is for PIR test using digital I/O.

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

  • WIZwiki-W7500 from WIZnet (Platform board and Ethernet I/F board)

Hardware Configuration

WIZwiki-W7500 Pin map

pin map

Just connect Ethernet Cable & USB Cable


Software

Init Ethernet

void initEthernet(uint8_t* mac_addr) {
    int phy_link;

    eth.init(mac_addr); //Use DHCP
    
    eth.connect();
        
    /* phy link */
    do{
       phy_link = eth.ethernet_link();
       printf(".");
       wait(2);
    }while(!phy_link);
    printf("\r\n");
         
    printf("IP Address is %s\r\n", eth.getIPAddress());
}

Request to server using HTTP

void requestHTTP(void)
{
    char req_buf[256];
    
    /* TCP socket connect */   
    sock.connect(WEB_SERVER, 80);
    
    /* Request to WEB Server using HTTP */
    sprintf(req_buf,"GET /data/2.5/weather?q=%s,%s&appid=%s HTTP/1.1\nHost: %s\nConnection: close\n\n",
        CITY,COUNTRY, API_KEY, WEB_SERVER);
    sock.send_all(req_buf, strlen(req_buf));
}

Get data from server & Parsing it

void parsingGetData(void)
{
    char buffer[1024];
        
    /* get info */
    int ret;
    while (true) {
        ret = sock.receive_all(buffer, sizeof(buffer)-1);
        if (ret <= 0) break;
        buffer[ret] = '\0';
        pc.printf("Received %d chars from server: %s\n", ret, buffer);     
    }
    
    /* parsing weather, city, tempurature */
    char *weather;
    char *city;
    char *temp;
    uint8_t i;
    
    pc.printf("\r\n\r\n======== WeatherForecast ========\r\n");  
    weather = strstr(buffer, "main");
    pc.printf("\t State : ");
    for(i = 7; i < 20; i++)
    {
        if(*(weather+i) == '\"') break;
        pc.printf("%c", *(weather+i));
    }
            
    city = strstr(buffer, "name");
    pc.printf("\r\n\t City : ");
    for(i = 7; i < 20; i++)
    {
        if(*(city+i) == '\"') break;
        pc.printf("%c", *(city+i));
    }
        
    temp = strstr(buffer, "temp");
    pc.printf("\r\n\t temp(kelvin) : ");
    for(i = 6; i < 12; i++)
    {
        if((*(temp+i) == '\"')||(*(temp+i) == ',')) break;
        pc.printf("%c", *(temp+i));
    }
    pc.printf("\r\n\r\n");
}

Caution

Must fix API_KEY & MAC Address

Changes

RevisionDateWhoCommit message
19:46fa6a0a9b5c 2017-04-07 kei44 Modify old code. default tip
18:a02a73acd3c8 2015-06-29 joon874 Using TCP Client with WIZwiki-W7500, display weather conditions on led and temperatures with servo-motor
17:ca0b0402a4fe 2015-06-26 joon874 Using TCP Client with WIZwiki-W7500, Have your own Weatherforecast.
16:69e41cbbc8ac 2014-09-21 mbedAustin updated libraries
15:d3b1e25d3e23 2015-06-25 joon874 Using TCP client, do have your own weatherforecast
14:72be2b8b7f24 2014-05-14 Kojto Update to the latest EthernetInterface and mbed RTOS revision
13:3f8407714419 2013-06-04 emilmont Point to the latest libraries
12:7716b3728689 2013-03-01 emilmont Update libraries
11:59dcefdda506 2012-08-10 emilmont Avoid sending null character in HTTP request; Update Ethernet and RTOS libraries to latest releases
10:8aeafea0e859 2012-08-01 emilmont Update socket library
9:4757a976148d 2012-07-27 emilmont Cleanup example
8:2570d35b0c47 2012-07-26 emilmont Update Socket and RTOS libraries
7:65188f4a8c25 2012-07-25 emilmont Update EthernetInterface
6:b011e8862694 2012-07-23 emilmont tidyup
5:01f6c3e112af 2012-07-16 donatien Updated EthernetInterface OS implementation to latest RTOS revision
4:dfdcadcd0ff1 2012-07-15 donatien Updated Ethernetinterface Library
3:46a2ada58e97 2012-07-03 donatien Updated EthernetInterface library
2:e087e9b789e9 2012-07-02 emilmont Remove debug directives avoiding to change the default settings of the serial port. Add print of the IP address assigned by DHCP.
1:e2652bd064c6 2012-06-26 donatien Switched from uint8_t* to char* for data type
0:bb128f0e952f 2012-06-26 donatien Initial commit