Weather info from OPenweathermap.org with TCPClient on WIZwiki-W7500
Dependencies: WIZnetInterface mbed
Fork of TCPClient_HelloWorld_WIZwiki-W7500 by
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.
- WIZwiki-W7500 from WIZnet (Platform board and Ethernet I/F board)
Hardware Configuration
WIZwiki-W7500 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. //
Diff: main.cpp
- Revision:
- 2:c4fef61e151d
- Parent:
- 0:d7c890f9ee69
- Child:
- 3:7b25a01a7f62
--- a/main.cpp Mon Jun 29 00:43:27 2015 +0000 +++ b/main.cpp Thu Apr 06 11:50:11 2017 +0000 @@ -1,39 +1,49 @@ #include "mbed.h" #include "EthernetInterface.h" + +#define API_key "" //Insert API key +Serial uart(USBTX, USBRX); + int main() { int phy_link; - printf("Wait a second...\r\n"); - uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; + uart.baud(115200); + uart.printf("Wait a second...\r\n"); + //uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; EthernetInterface eth; - eth.init(mac_addr); //Use DHCP + eth.connect(); - + do{ phy_link = eth.ethernet_link(); - printf("..."); + uart.printf("..."); wait(2); }while(!phy_link); - printf("\r\n"); + uart.printf("\r\n"); - printf("IP Address is %s\r\n", eth.getIPAddress()); + uart.printf("IP Address is %s\r\n", eth.getIPAddress()); TCPSocketConnection sock; sock.connect("api.openweathermap.org", 80); - char http_cmd[] = "GET /data/2.5/weather?q=Seoul,kr HTTP/1.0\n\n"; - sock.send_all(http_cmd, sizeof(http_cmd)-1); - + 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); + char buffer[400]; int ret; while (true) { ret = sock.receive(buffer, sizeof(buffer)-1); if (ret <= 0) break; buffer[ret] = '\0'; - printf("Received %d chars from server: %s\n", ret, buffer); + uart.printf("Received %d chars from server: %s\n", ret, buffer); } sock.close();