Openweathermap example for WizFi310

Dependencies:   WizFi310Interface_Legacy mbed

Fork of WizFi310_OpenWeatherMap by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* NetworkSocketAPI Example Program
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 #include "mbed.h"
00018 #include "WizFi310Interface.h"
00019 
00020 #if defined(TARGET_WIZwiki_W7500)
00021     WizFi310Interface wizfi310(D1, D0, D7, D6, D9, NC, 115200);
00022     Serial pc(USBTX,USBRX);
00023 #endif
00024 
00025 #define SECURE WizFi310::SEC_AUTO
00026 #define SSID "SSID"
00027 #define PASS "PASS"
00028 
00029 #define CITY        "Seoul"
00030 #define API_KEY     "API KEY"
00031 
00032 int main()
00033 {
00034     int errConnect;
00035     char http_cmd[1000] = "";
00036     char buffer[2048] = "";
00037     
00038     pc.baud(115200);
00039     printf("WizFi310 NetworkSocketAPI TCP Client OpenWeatherMap Example\r\n");
00040     
00041     wizfi310.init();
00042     if( wizfi310.connect(SECURE, SSID, PASS) )    return -1;
00043     
00044     const char *ip = wizfi310.getIPAddress();
00045     const char *mac = wizfi310.getMACAddress();
00046 //    printf("IP address is: %s\r\n", ip ? ip : "No IP");
00047 //    printf("MAC address is: %s\r\n", mac ? mac : "No MAC");
00048       
00049     TCPSocketConnection socket;
00050     socket.set_blocking(1000);   // Set Block Mode.
00051     errConnect = socket.connect("api.openweathermap.org", 80);    
00052     
00053     while (true) {
00054         if(errConnect!=0) {
00055             printf("\r\ncould not connect to socket : error = %d\r\n", errConnect);
00056             errConnect = socket.connect("api.openweathermap.org", 80);
00057         } else {
00058             printf("socket connected\r\n");
00059             break;
00060         }
00061     }    
00062      
00063     sprintf((char *)http_cmd,"GET /data/2.5/weather?q=%s,uk&appid=%s HTTP/1.0\r\nHost: api.openweathermap.org\r\nConnection: close\r\n\r\n",
00064     CITY, API_KEY);
00065 
00066     socket.send_all(http_cmd, sizeof(http_cmd));
00067     
00068     socket.receive_all(buffer, sizeof(buffer));
00069     printf("%s",buffer);
00070  
00071     socket.close();
00072     wizfi310.disconnect();
00073     
00074     printf("Done\r\n");
00075 
00076 }
00077