Weather info from OPenweathermap.org with TCPClient on WIZwiki-W7500

Dependencies:   WIZnetInterface mbed

Fork of TCPClient_HelloWorld_WIZwiki-W7500 by Lawrence Lee

Prerequisite

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

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


Hardware Configuration

WIZwiki-W7500 Pin map

pin map


Software

Select the city

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

Caution

This example requires an API key that can be obtained by signning up to the openweathmap site. //

Committer:
joon874
Date:
Thu Jun 25 03:01:49 2015 +0000
Revision:
0:d7c890f9ee69
Child:
2:c4fef61e151d
TCP Client Example to get weather info on WIZwiki-W7500

Who changed what in which revision?

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