Openweathermap example for WizFi310
Dependencies: NetworkSocketAPI WizFi310Interface mbed
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 "TCPSocket.h" 00018 #include "mbed.h" 00019 #include "WizFi310Interface.h" 00020 00021 #if defined(TARGET_WIZwiki_W7500) 00022 WizFi310Interface wifi(D1, D0, D7, D6, D9, NC, 115200); 00023 Serial pc(USBTX,USBRX); 00024 #endif 00025 00026 #define AP_SSID "SSID" 00027 #define AP_PASSWORD "PASS" 00028 #define AP_SECURITY NSAPI_SECURITY_WPA2 00029 00030 #define CITY "Seoul" 00031 #define API_KEY "API KEY" 00032 00033 int main() 00034 { 00035 int errConnect; 00036 char http_cmd[1000] = ""; 00037 char buffer[2048] = ""; 00038 00039 pc.baud(115200); 00040 printf("WizFi310 NetworkSocketAPI TCP Client OpenWeatherMap Example\r\n"); 00041 00042 wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY); 00043 00044 const char *ip = wifi.get_ip_address(); 00045 const char *mac = wifi.get_mac_address(); 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 SocketAddress addr(&wifi, "api.openweathermap.org"); 00050 printf("api.openweathermap.org resolved to: %s\r\n", addr.get_ip_address()); 00051 00052 TCPSocket socket(&wifi); 00053 socket.set_timeout(1000); //Set Block Mode. 00054 errConnect = socket.connect("api.openweathermap.org", 80); 00055 00056 while (true) { 00057 if(errConnect!=0) { 00058 printf("\r\ncould not connect to socket : error = %d\r\n", errConnect); 00059 errConnect = socket.connect("api.openweathermap.org", 80); 00060 } else { 00061 printf("socket connected\r\n"); 00062 break; 00063 } 00064 } 00065 00066 sprintf((char *)http_cmd,"GET /data/2.5/weather?q=%s&appid=%s HTTP/1.0\r\nHost: api.openweathermap.org\r\nConnection: close\r\n\r\n", 00067 CITY, API_KEY); 00068 00069 socket.send(http_cmd, sizeof(http_cmd)); 00070 00071 socket.recv(buffer, sizeof(buffer)); 00072 printf("%s\r\n",buffer); 00073 00074 socket.close(); 00075 wifi.disconnect(); 00076 00077 printf("Done\r\n"); 00078 00079 } 00080
Generated on Tue Jul 19 2022 07:27:24 by 1.7.2