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

Dependencies:   WIZnetInterface mbed

Fork of TCPClient_HelloWorld_WIZwiki-W7500 by Lawrence Lee

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 
00005 #define API_key "" //Insert API key
00006 Serial uart(USBTX, USBRX);
00007 
00008 int main() {
00009    
00010     int phy_link;
00011     uart.baud(115200);
00012     uart.printf("Wait a second...\r\n");
00013     uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; 
00014     
00015     EthernetInterface eth;
00016     eth.init(mac_addr); //Use DHCP
00017     eth.connect();
00018 
00019      do{
00020      phy_link = eth.ethernet_link();
00021      uart.printf("...");
00022      wait(2);
00023      }while(!phy_link);
00024      uart.printf("\r\n");
00025      
00026     uart.printf("IP Address is %s\r\n\n", eth.getIPAddress());
00027     
00028     TCPSocketConnection sock;
00029     sock.connect("api.openweathermap.org", 80);
00030     
00031 
00032     char http_cmd0[] = "GET /data/2.5/weather?q=Seoul,kr&appid=";
00033     char http_cmd1[] = API_key;
00034     char http_cmd2[] = " HTTP/1.0\n\n";
00035     
00036     sock.send_all(http_cmd0, sizeof(http_cmd0)-1);
00037     sock.send_all(http_cmd1, sizeof(http_cmd1)-1);
00038     sock.send_all(http_cmd2, sizeof(http_cmd2)-1);
00039     
00040     char buffer[400];
00041     int ret;
00042     while (true) {
00043         ret = sock.receive(buffer, sizeof(buffer)-1);
00044         if (ret <= 0) break;
00045         buffer[ret] = '\0';
00046         uart.printf("--Received %d chars from server:\r\n %s\r\n\n", ret, buffer);     
00047     }
00048 
00049     sock.close();
00050     
00051     eth.disconnect();
00052 
00053 }