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

History

Modify old code. default tip

2017-04-07, by kei44 [Fri, 07 Apr 2017 08:51:49 +0000] rev 19

Modify old code.


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

2015-06-29, by joon874 [Mon, 29 Jun 2015 07:41:31 +0000] rev 18

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


Using TCP Client with WIZwiki-W7500, Have your own Weatherforecast.

2015-06-26, by joon874 [Fri, 26 Jun 2015 00:05:31 +0000] rev 17

Using TCP Client with WIZwiki-W7500, Have your own Weatherforecast.


updated libraries

2014-09-21, by mbedAustin [Sun, 21 Sep 2014 05:55:13 +0000] rev 16

updated libraries


Using TCP client, do have your own weatherforecast

2015-06-25, by joon874 [Thu, 25 Jun 2015 23:54:47 +0000] rev 15

Using TCP client, do have your own weatherforecast


Update to the latest EthernetInterface and mbed RTOS revision

2014-05-14, by Kojto [Wed, 14 May 2014 15:07:26 +0000] rev 14

Update to the latest EthernetInterface and mbed RTOS revision


Point to the latest libraries

2013-06-04, by emilmont [Tue, 04 Jun 2013 16:07:37 +0100] rev 13

Point to the latest libraries


Update libraries

2013-03-01, by emilmont [Fri, 01 Mar 2013 16:05:34 +0000] rev 12

Update libraries


Avoid sending null character in HTTP request; Update Ethernet and RTOS libraries to latest releases

2012-08-10, by emilmont [Fri, 10 Aug 2012 09:41:51 +0000] rev 11

Avoid sending null character in HTTP request; Update Ethernet and RTOS libraries to latest releases


Update socket library

2012-08-01, by emilmont [Wed, 01 Aug 2012 13:32:17 +0000] rev 10

Update socket library