![](/media/cache/group/WIZnet_logo_001.jpg.50x50_q85.jpg)
Openweathermap example for WizFi310
Dependencies: NetworkSocketAPI WizFi310Interface mbed
Prerequisite
This example is for obtainning and printting data from OpenWeatherMap(Server).
WIZwiki-W7500 and WizFi310 Shield act as TCP client mode.
To implement this function, you need a Platform board and Wi-Fi board. Below are what we used.
- WIZwiki-W7500 from WIZnet (Platform board)
- WizFi310 from WIZnet (Wi-Fi board)
Hardware Configuration
WIZwiki-W7500 Pin map
- D0 is for RXD, D1 is for TXD
- D6 is for CTS, D7 is for RTS
- D9 is for RESET
WizFi310 Pin map
- J1 is for RXD, J3 is for TXD
- SW6-1 is connected to D6 for RTS, SW6-2 is connected to D7 for CTS
- SW5-3 is connected to D9 for RESET
Software
main.cpp
#include "TCPSocket.h" #include "mbed.h" #include "WizFi310Interface.h" #if defined(TARGET_WIZwiki_W7500) WizFi310Interface wifi(D1, D0, D7, D6, D9, NC, 115200); Serial pc(USBTX,USBRX); #endif #define AP_SSID "SSID" #define AP_PASSWORD "PASS" #define AP_SECURITY NSAPI_SECURITY_WPA2 #define CITY "Seoul" #define API_KEY "API KEY" int main() { int errConnect; char http_cmd[1000] = ""; char buffer[2048] = ""; pc.baud(115200); printf("WizFi310 NetworkSocketAPI TCP Client OpenWeatherMap Example\r\n"); wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY); const char *ip = wifi.get_ip_address(); const char *mac = wifi.get_mac_address(); printf("IP address is: %s\r\n", ip ? ip : "No IP"); printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); SocketAddress addr(&wifi, "api.openweathermap.org"); printf("api.openweathermap.org resolved to: %s\r\n", addr.get_ip_address()); TCPSocket socket(&wifi); socket.set_timeout(1000); //Set Block Mode. errConnect = socket.connect("api.openweathermap.org", 80); //errConnect = socket.connect("222.98.173.233", 5000); while (true) { if(errConnect!=0) { printf("\r\ncould not connect to socket : error = %d\r\n", errConnect); errConnect = socket.connect("api.openweathermap.org", 80); } else { printf("socket connected\r\n"); break; } } 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", CITY, API_KEY); socket.send(http_cmd, sizeof(http_cmd)); socket.recv(buffer, sizeof(buffer)); printf("%s",buffer); socket.close(); wifi.disconnect(); printf("Done\r\n"); }
Caution
This example requires an API key that can be obtained by signning up to the openweathmap site. //
main.cpp@0:dcc1cf05125a, 2017-04-12 (annotated)
- Committer:
- cliff1
- Date:
- Wed Apr 12 01:12:07 2017 +0000
- Revision:
- 0:dcc1cf05125a
- Child:
- 1:d6af10793d2b
First release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cliff1 | 0:dcc1cf05125a | 1 | /* NetworkSocketAPI Example Program |
cliff1 | 0:dcc1cf05125a | 2 | * Copyright (c) 2015 ARM Limited |
cliff1 | 0:dcc1cf05125a | 3 | * |
cliff1 | 0:dcc1cf05125a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
cliff1 | 0:dcc1cf05125a | 5 | * you may not use this file except in compliance with the License. |
cliff1 | 0:dcc1cf05125a | 6 | * You may obtain a copy of the License at |
cliff1 | 0:dcc1cf05125a | 7 | * |
cliff1 | 0:dcc1cf05125a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
cliff1 | 0:dcc1cf05125a | 9 | * |
cliff1 | 0:dcc1cf05125a | 10 | * Unless required by applicable law or agreed to in writing, software |
cliff1 | 0:dcc1cf05125a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
cliff1 | 0:dcc1cf05125a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
cliff1 | 0:dcc1cf05125a | 13 | * See the License for the specific language governing permissions and |
cliff1 | 0:dcc1cf05125a | 14 | * limitations under the License. |
cliff1 | 0:dcc1cf05125a | 15 | */ |
cliff1 | 0:dcc1cf05125a | 16 | |
cliff1 | 0:dcc1cf05125a | 17 | #include "TCPSocket.h" |
cliff1 | 0:dcc1cf05125a | 18 | #include "mbed.h" |
cliff1 | 0:dcc1cf05125a | 19 | #include "WizFi310Interface.h" |
cliff1 | 0:dcc1cf05125a | 20 | |
cliff1 | 0:dcc1cf05125a | 21 | #if defined(TARGET_WIZwiki_W7500) |
cliff1 | 0:dcc1cf05125a | 22 | WizFi310Interface wifi(D1, D0, D7, D6, D9, NC, 115200); |
cliff1 | 0:dcc1cf05125a | 23 | Serial pc(USBTX,USBRX); |
cliff1 | 0:dcc1cf05125a | 24 | #endif |
cliff1 | 0:dcc1cf05125a | 25 | |
cliff1 | 0:dcc1cf05125a | 26 | #define AP_SSID "SSID" |
cliff1 | 0:dcc1cf05125a | 27 | #define AP_PASSWORD "PASS" |
cliff1 | 0:dcc1cf05125a | 28 | #define AP_SECURITY NSAPI_SECURITY_WPA2 |
cliff1 | 0:dcc1cf05125a | 29 | |
cliff1 | 0:dcc1cf05125a | 30 | #define CITY "Seoul" |
cliff1 | 0:dcc1cf05125a | 31 | #define API_KEY "API KEY" |
cliff1 | 0:dcc1cf05125a | 32 | |
cliff1 | 0:dcc1cf05125a | 33 | int main() |
cliff1 | 0:dcc1cf05125a | 34 | { |
cliff1 | 0:dcc1cf05125a | 35 | int errConnect; |
cliff1 | 0:dcc1cf05125a | 36 | char http_cmd[1000] = ""; |
cliff1 | 0:dcc1cf05125a | 37 | char buffer[2048] = ""; |
cliff1 | 0:dcc1cf05125a | 38 | |
cliff1 | 0:dcc1cf05125a | 39 | pc.baud(115200); |
cliff1 | 0:dcc1cf05125a | 40 | printf("WizFi310 NetworkSocketAPI TCP Client OpenWeatherMap Example\r\n"); |
cliff1 | 0:dcc1cf05125a | 41 | |
cliff1 | 0:dcc1cf05125a | 42 | wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY); |
cliff1 | 0:dcc1cf05125a | 43 | |
cliff1 | 0:dcc1cf05125a | 44 | const char *ip = wifi.get_ip_address(); |
cliff1 | 0:dcc1cf05125a | 45 | const char *mac = wifi.get_mac_address(); |
cliff1 | 0:dcc1cf05125a | 46 | printf("IP address is: %s\r\n", ip ? ip : "No IP"); |
cliff1 | 0:dcc1cf05125a | 47 | printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); |
cliff1 | 0:dcc1cf05125a | 48 | |
cliff1 | 0:dcc1cf05125a | 49 | SocketAddress addr(&wifi, "api.openweathermap.org"); |
cliff1 | 0:dcc1cf05125a | 50 | printf("api.openweathermap.org resolved to: %s\r\n", addr.get_ip_address()); |
cliff1 | 0:dcc1cf05125a | 51 | |
cliff1 | 0:dcc1cf05125a | 52 | TCPSocket socket(&wifi); |
cliff1 | 0:dcc1cf05125a | 53 | socket.set_timeout(1000); //Set Block Mode. |
cliff1 | 0:dcc1cf05125a | 54 | errConnect = socket.connect("api.openweathermap.org", 80); |
cliff1 | 0:dcc1cf05125a | 55 | //errConnect = socket.connect("222.98.173.233", 5000); |
cliff1 | 0:dcc1cf05125a | 56 | |
cliff1 | 0:dcc1cf05125a | 57 | while (true) { |
cliff1 | 0:dcc1cf05125a | 58 | if(errConnect!=0) { |
cliff1 | 0:dcc1cf05125a | 59 | printf("\r\ncould not connect to socket : error = %d\r\n", errConnect); |
cliff1 | 0:dcc1cf05125a | 60 | errConnect = socket.connect("api.openweathermap.org", 80); |
cliff1 | 0:dcc1cf05125a | 61 | } else { |
cliff1 | 0:dcc1cf05125a | 62 | printf("socket connected\r\n"); |
cliff1 | 0:dcc1cf05125a | 63 | break; |
cliff1 | 0:dcc1cf05125a | 64 | } |
cliff1 | 0:dcc1cf05125a | 65 | } |
cliff1 | 0:dcc1cf05125a | 66 | |
cliff1 | 0:dcc1cf05125a | 67 | 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", |
cliff1 | 0:dcc1cf05125a | 68 | CITY, API_KEY); |
cliff1 | 0:dcc1cf05125a | 69 | |
cliff1 | 0:dcc1cf05125a | 70 | socket.send(http_cmd, sizeof(http_cmd)); |
cliff1 | 0:dcc1cf05125a | 71 | |
cliff1 | 0:dcc1cf05125a | 72 | socket.recv(buffer, sizeof(buffer)); |
cliff1 | 0:dcc1cf05125a | 73 | printf("%s",buffer); |
cliff1 | 0:dcc1cf05125a | 74 | |
cliff1 | 0:dcc1cf05125a | 75 | socket.close(); |
cliff1 | 0:dcc1cf05125a | 76 | wifi.disconnect(); |
cliff1 | 0:dcc1cf05125a | 77 | |
cliff1 | 0:dcc1cf05125a | 78 | printf("Done\r\n"); |
cliff1 | 0:dcc1cf05125a | 79 | |
cliff1 | 0:dcc1cf05125a | 80 | } |
cliff1 | 0:dcc1cf05125a | 81 |