WIZnet / My_Weatherforecast_WIZwiki-W7500

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

Committer:
joon874
Date:
Fri Jun 26 00:05:31 2015 +0000
Revision:
17:ca0b0402a4fe
Parent:
11:59dcefdda506
Child:
18:a02a73acd3c8
Using TCP Client with WIZwiki-W7500, Have your own Weatherforecast.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:bb128f0e952f 1 #include "mbed.h"
donatien 0:bb128f0e952f 2 #include "EthernetInterface.h"
joon874 17:ca0b0402a4fe 3 #include "Servo.h"
joon874 17:ca0b0402a4fe 4
joon874 17:ca0b0402a4fe 5 DigitalOut bled(D6);
joon874 17:ca0b0402a4fe 6 DigitalOut gled(D5);
joon874 17:ca0b0402a4fe 7 DigitalOut rled(D4);
joon874 17:ca0b0402a4fe 8
joon874 17:ca0b0402a4fe 9 Servo myservo(D15);
donatien 0:bb128f0e952f 10
emilmont 7:65188f4a8c25 11 int main() {
joon874 17:ca0b0402a4fe 12
joon874 17:ca0b0402a4fe 13 int phy_link;
joon874 17:ca0b0402a4fe 14 printf("Wait a second...\r\n");
joon874 17:ca0b0402a4fe 15 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
joon874 17:ca0b0402a4fe 16
donatien 0:bb128f0e952f 17 EthernetInterface eth;
joon874 17:ca0b0402a4fe 18 eth.init(mac_addr); //Use DHCP
joon874 17:ca0b0402a4fe 19
joon874 17:ca0b0402a4fe 20 while(1){
joon874 17:ca0b0402a4fe 21
donatien 0:bb128f0e952f 22 eth.connect();
donatien 0:bb128f0e952f 23
joon874 17:ca0b0402a4fe 24 /* phy link */
joon874 17:ca0b0402a4fe 25 do{
joon874 17:ca0b0402a4fe 26 phy_link = eth.ethernet_link();
joon874 17:ca0b0402a4fe 27 printf("...");
joon874 17:ca0b0402a4fe 28 wait(2);
joon874 17:ca0b0402a4fe 29 }while(!phy_link);
joon874 17:ca0b0402a4fe 30 printf("\r\n");
joon874 17:ca0b0402a4fe 31
joon874 17:ca0b0402a4fe 32 printf("IP Address is %s\r\n", eth.getIPAddress());
joon874 17:ca0b0402a4fe 33
joon874 17:ca0b0402a4fe 34 /* TCP socket connect */
emilmont 7:65188f4a8c25 35 TCPSocketConnection sock;
joon874 17:ca0b0402a4fe 36 sock.connect("api.openweathermap.org", 80);
donatien 0:bb128f0e952f 37
joon874 17:ca0b0402a4fe 38 /* weather */
joon874 17:ca0b0402a4fe 39 char http_cmd[] = "GET /data/2.5/weather?q=Seoul,kr HTTP/1.0\n\n";
joon874 17:ca0b0402a4fe 40 //char http_cmd[] = "GET /data/2.5/weather?q=London,uk HTTP/1.0\n\n";
joon874 17:ca0b0402a4fe 41 //char http_cmd[] = "GET /data/2.5/weather?q=Berlin,de HTTP/1.0\n\n";
emilmont 11:59dcefdda506 42 sock.send_all(http_cmd, sizeof(http_cmd)-1);
emilmont 7:65188f4a8c25 43
joon874 17:ca0b0402a4fe 44 /* get info */
joon874 17:ca0b0402a4fe 45 char buffer[500];
donatien 0:bb128f0e952f 46 int ret;
emilmont 7:65188f4a8c25 47 while (true) {
emilmont 9:4757a976148d 48 ret = sock.receive(buffer, sizeof(buffer)-1);
emilmont 7:65188f4a8c25 49 if (ret <= 0)
emilmont 7:65188f4a8c25 50 break;
emilmont 9:4757a976148d 51 buffer[ret] = '\0';
joon874 17:ca0b0402a4fe 52 printf("Received %d chars from server: %s\n", ret, buffer);
emilmont 7:65188f4a8c25 53 }
joon874 17:ca0b0402a4fe 54
joon874 17:ca0b0402a4fe 55 /* get weather, city, tempurature */
joon874 17:ca0b0402a4fe 56 char *weather;
joon874 17:ca0b0402a4fe 57 char *city;
joon874 17:ca0b0402a4fe 58 char *tempure;
joon874 17:ca0b0402a4fe 59
joon874 17:ca0b0402a4fe 60 char weather_stu[3];
joon874 17:ca0b0402a4fe 61 char city_name[6];
joon874 17:ca0b0402a4fe 62 char tempure_data[6];
joon874 17:ca0b0402a4fe 63
joon874 17:ca0b0402a4fe 64 weather = strstr(buffer, "main");
joon874 17:ca0b0402a4fe 65 printf("%.4s\n", weather + 7);
joon874 17:ca0b0402a4fe 66 for(int i=0; i<3;i++){
joon874 17:ca0b0402a4fe 67 weather_stu[i] = weather[i+7];
joon874 17:ca0b0402a4fe 68 }
joon874 17:ca0b0402a4fe 69
joon874 17:ca0b0402a4fe 70 city = strstr(buffer, "name");
joon874 17:ca0b0402a4fe 71 printf("%.6s\n", city + 7);
joon874 17:ca0b0402a4fe 72 for(int k=0; k<6;k++){
joon874 17:ca0b0402a4fe 73 city_name[k] = city[k+7];
joon874 17:ca0b0402a4fe 74 printf("%c",city_name[k]);
joon874 17:ca0b0402a4fe 75 }
joon874 17:ca0b0402a4fe 76
joon874 17:ca0b0402a4fe 77 tempure = strstr(buffer, "temp");
joon874 17:ca0b0402a4fe 78 printf("%.3s\n", tempure + 6);
joon874 17:ca0b0402a4fe 79 for(int j=0; j<6;j++){
joon874 17:ca0b0402a4fe 80 tempure_data[j] = tempure[j+6];
joon874 17:ca0b0402a4fe 81 printf("%c",tempure_data[j]);
joon874 17:ca0b0402a4fe 82 }
joon874 17:ca0b0402a4fe 83
joon874 17:ca0b0402a4fe 84 /*tempurature display */
joon874 17:ca0b0402a4fe 85 float data[2]={0};
joon874 17:ca0b0402a4fe 86 data[0] = tempure_data[1]-'7';
joon874 17:ca0b0402a4fe 87 data[1] = tempure_data[2]-'3';
joon874 17:ca0b0402a4fe 88
joon874 17:ca0b0402a4fe 89 myservo = (data[0]*10+data[1])/40;
joon874 17:ca0b0402a4fe 90
joon874 17:ca0b0402a4fe 91 printf("%f",(data[0]*10+data[1])/40);
joon874 17:ca0b0402a4fe 92
joon874 17:ca0b0402a4fe 93 /* weather display */
joon874 17:ca0b0402a4fe 94 if(strcmp(weather_stu,"Clo")==0) {
joon874 17:ca0b0402a4fe 95 rled = 1;
joon874 17:ca0b0402a4fe 96 gled = 1;
joon874 17:ca0b0402a4fe 97 bled = 1;
joon874 17:ca0b0402a4fe 98 }else if(strcmp(weather_stu,"Rai")==0) {
joon874 17:ca0b0402a4fe 99 rled = 0;
joon874 17:ca0b0402a4fe 100 gled = 0;
joon874 17:ca0b0402a4fe 101 bled = 1;
joon874 17:ca0b0402a4fe 102 }else if(strcmp(weather_stu,"Cle")==0){
joon874 17:ca0b0402a4fe 103 rled = 1;
joon874 17:ca0b0402a4fe 104 gled = 1;
joon874 17:ca0b0402a4fe 105 bled = 0;
joon874 17:ca0b0402a4fe 106 }else {
joon874 17:ca0b0402a4fe 107 rled = 0;
joon874 17:ca0b0402a4fe 108 bled = 0;
joon874 17:ca0b0402a4fe 109 gled = 0;
joon874 17:ca0b0402a4fe 110 }
joon874 17:ca0b0402a4fe 111
emilmont 7:65188f4a8c25 112 sock.close();
donatien 0:bb128f0e952f 113
emilmont 7:65188f4a8c25 114 eth.disconnect();
donatien 5:01f6c3e112af 115
joon874 17:ca0b0402a4fe 116 wait(60.0);
joon874 17:ca0b0402a4fe 117 };
joon874 17:ca0b0402a4fe 118
joon874 17:ca0b0402a4fe 119 }