Openweathermap example for WizFi310
Dependencies: WizFi310Interface_Legacy mbed
Fork of WizFi310_OpenWeatherMap by
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
/* NetworkSocketAPI Example Program * Copyright (c) 2015 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "mbed.h" #include "WizFi310Interface.h" #if defined(TARGET_WIZwiki_W7500) WizFi310Interface wizfi310(D1, D0, D7, D6, D9, NC, 115200); Serial pc(USBTX,USBRX); #endif #define SECURE WizFi310::SEC_AUTO #define SSID "SSID" #define PASS "PASS" #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"); wizfi310.init(); if( wizfi310.connect(SECURE, SSID, PASS) ) return -1; const char *ip = wizfi310.getIPAddress(); const char *mac = wizfi310.getMACAddress(); // printf("IP address is: %s\r\n", ip ? ip : "No IP"); // printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); TCPSocketConnection socket; socket.set_blocking(1000); // Set Block Mode. errConnect = socket.connect("api.openweathermap.org", 80); 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_all(http_cmd, sizeof(http_cmd)); socket.receive_all(buffer, sizeof(buffer)); printf("%s",buffer); socket.close(); wizfi310.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@1:e65e12b4c3cc, 2017-04-19 (annotated)
- Committer:
- cliff1
- Date:
- Wed Apr 19 08:09:00 2017 +0000
- Revision:
- 1:e65e12b4c3cc
- Parent:
- 0:dcc1cf05125a
WizFi310 OpenWeatherMap Legacy version
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 "mbed.h" |
cliff1 | 0:dcc1cf05125a | 18 | #include "WizFi310Interface.h" |
cliff1 | 0:dcc1cf05125a | 19 | |
cliff1 | 0:dcc1cf05125a | 20 | #if defined(TARGET_WIZwiki_W7500) |
cliff1 | 1:e65e12b4c3cc | 21 | WizFi310Interface wizfi310(D1, D0, D7, D6, D9, NC, 115200); |
cliff1 | 0:dcc1cf05125a | 22 | Serial pc(USBTX,USBRX); |
cliff1 | 0:dcc1cf05125a | 23 | #endif |
cliff1 | 0:dcc1cf05125a | 24 | |
cliff1 | 1:e65e12b4c3cc | 25 | #define SECURE WizFi310::SEC_AUTO |
cliff1 | 1:e65e12b4c3cc | 26 | #define SSID "SSID" |
cliff1 | 1:e65e12b4c3cc | 27 | #define PASS "PASS" |
cliff1 | 0:dcc1cf05125a | 28 | |
cliff1 | 0:dcc1cf05125a | 29 | #define CITY "Seoul" |
cliff1 | 0:dcc1cf05125a | 30 | #define API_KEY "API KEY" |
cliff1 | 0:dcc1cf05125a | 31 | |
cliff1 | 0:dcc1cf05125a | 32 | int main() |
cliff1 | 0:dcc1cf05125a | 33 | { |
cliff1 | 0:dcc1cf05125a | 34 | int errConnect; |
cliff1 | 0:dcc1cf05125a | 35 | char http_cmd[1000] = ""; |
cliff1 | 0:dcc1cf05125a | 36 | char buffer[2048] = ""; |
cliff1 | 0:dcc1cf05125a | 37 | |
cliff1 | 0:dcc1cf05125a | 38 | pc.baud(115200); |
cliff1 | 0:dcc1cf05125a | 39 | printf("WizFi310 NetworkSocketAPI TCP Client OpenWeatherMap Example\r\n"); |
cliff1 | 0:dcc1cf05125a | 40 | |
cliff1 | 1:e65e12b4c3cc | 41 | wizfi310.init(); |
cliff1 | 1:e65e12b4c3cc | 42 | if( wizfi310.connect(SECURE, SSID, PASS) ) return -1; |
cliff1 | 0:dcc1cf05125a | 43 | |
cliff1 | 1:e65e12b4c3cc | 44 | const char *ip = wizfi310.getIPAddress(); |
cliff1 | 1:e65e12b4c3cc | 45 | const char *mac = wizfi310.getMACAddress(); |
cliff1 | 1:e65e12b4c3cc | 46 | // printf("IP address is: %s\r\n", ip ? ip : "No IP"); |
cliff1 | 1:e65e12b4c3cc | 47 | // printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); |
cliff1 | 1:e65e12b4c3cc | 48 | |
cliff1 | 1:e65e12b4c3cc | 49 | TCPSocketConnection socket; |
cliff1 | 1:e65e12b4c3cc | 50 | socket.set_blocking(1000); // Set Block Mode. |
cliff1 | 1:e65e12b4c3cc | 51 | errConnect = socket.connect("api.openweathermap.org", 80); |
cliff1 | 0:dcc1cf05125a | 52 | |
cliff1 | 0:dcc1cf05125a | 53 | while (true) { |
cliff1 | 0:dcc1cf05125a | 54 | if(errConnect!=0) { |
cliff1 | 0:dcc1cf05125a | 55 | printf("\r\ncould not connect to socket : error = %d\r\n", errConnect); |
cliff1 | 0:dcc1cf05125a | 56 | errConnect = socket.connect("api.openweathermap.org", 80); |
cliff1 | 0:dcc1cf05125a | 57 | } else { |
cliff1 | 0:dcc1cf05125a | 58 | printf("socket connected\r\n"); |
cliff1 | 0:dcc1cf05125a | 59 | break; |
cliff1 | 0:dcc1cf05125a | 60 | } |
cliff1 | 0:dcc1cf05125a | 61 | } |
cliff1 | 0:dcc1cf05125a | 62 | |
cliff1 | 0:dcc1cf05125a | 63 | 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 | 64 | CITY, API_KEY); |
cliff1 | 0:dcc1cf05125a | 65 | |
cliff1 | 1:e65e12b4c3cc | 66 | socket.send_all(http_cmd, sizeof(http_cmd)); |
cliff1 | 0:dcc1cf05125a | 67 | |
cliff1 | 1:e65e12b4c3cc | 68 | socket.receive_all(buffer, sizeof(buffer)); |
cliff1 | 0:dcc1cf05125a | 69 | printf("%s",buffer); |
cliff1 | 0:dcc1cf05125a | 70 | |
cliff1 | 0:dcc1cf05125a | 71 | socket.close(); |
cliff1 | 1:e65e12b4c3cc | 72 | wizfi310.disconnect(); |
cliff1 | 0:dcc1cf05125a | 73 | |
cliff1 | 0:dcc1cf05125a | 74 | printf("Done\r\n"); |
cliff1 | 0:dcc1cf05125a | 75 | |
cliff1 | 0:dcc1cf05125a | 76 | } |
cliff1 | 0:dcc1cf05125a | 77 |