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