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:
jcm931213
Date:
Thu Apr 06 12:13:11 2017 +0000
Revision:
3:7b25a01a7f62
Parent:
2:c4fef61e151d
Revert mac_addr and eth.init

Who changed what in which revision?

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