Openweathermap example for WizFi310

Dependencies:   WizFi310Interface_Legacy mbed

Fork of WizFi310_OpenWeatherMap by WIZnet

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

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

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. //

Files at this revision

API Documentation at this revision

Comitter:
cliff1
Date:
Wed Apr 19 08:09:00 2017 +0000
Parent:
0:dcc1cf05125a
Commit message:
WizFi310 OpenWeatherMap Legacy version

Changed in this revision

NetworkSocketAPI.lib Show diff for this revision Revisions of this file
WizFi310Interface.lib Show diff for this revision Revisions of this file
WizFi310Interface_Legacy.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r dcc1cf05125a -r e65e12b4c3cc NetworkSocketAPI.lib
--- a/NetworkSocketAPI.lib	Wed Apr 12 01:12:07 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://mbed.org/teams/NetworkSocketAPI/code/NetworkSocketAPI/#ea3a618e0818
diff -r dcc1cf05125a -r e65e12b4c3cc WizFi310Interface.lib
--- a/WizFi310Interface.lib	Wed Apr 12 01:12:07 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/teams/WIZnet/code/WizFi310Interface/#04c8d61984a3
diff -r dcc1cf05125a -r e65e12b4c3cc WizFi310Interface_Legacy.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WizFi310Interface_Legacy.lib	Wed Apr 19 08:09:00 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/WIZnet/code/WizFi310Interface_Legacy/#774ff1e8b26b
diff -r dcc1cf05125a -r e65e12b4c3cc main.cpp
--- a/main.cpp	Wed Apr 12 01:12:07 2017 +0000
+++ b/main.cpp	Wed Apr 19 08:09:00 2017 +0000
@@ -14,18 +14,17 @@
  * limitations under the License.
  */
  
-#include "TCPSocket.h"
 #include "mbed.h"
 #include "WizFi310Interface.h"
 
 #if defined(TARGET_WIZwiki_W7500)
-    WizFi310Interface wifi(D1, D0, D7, D6, D9, NC, 115200);
+    WizFi310Interface wizfi310(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 SECURE WizFi310::SEC_AUTO
+#define SSID "SSID"
+#define PASS "PASS"
 
 #define CITY        "Seoul"
 #define API_KEY     "API KEY"
@@ -39,20 +38,17 @@
     pc.baud(115200);
     printf("WizFi310 NetworkSocketAPI TCP Client OpenWeatherMap Example\r\n");
     
-    wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY);
+    wizfi310.init();
+    if( wizfi310.connect(SECURE, SSID, PASS) )    return -1;
     
-    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);
+    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) {
@@ -67,13 +63,13 @@
     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.send_all(http_cmd, sizeof(http_cmd));
     
-    socket.recv(buffer, sizeof(buffer));
+    socket.receive_all(buffer, sizeof(buffer));
     printf("%s",buffer);
  
     socket.close();
-    wifi.disconnect();
+    wizfi310.disconnect();
     
     printf("Done\r\n");
 
diff -r dcc1cf05125a -r e65e12b4c3cc mbed.bld
--- a/mbed.bld	Wed Apr 12 01:12:07 2017 +0000
+++ b/mbed.bld	Wed Apr 19 08:09:00 2017 +0000
@@ -1,1 +1,1 @@
-https://mbed.org/users/mbed_official/code/mbed/builds/856d2700e60b
\ No newline at end of file
+https://mbed.org/users/mbed_official/code/mbed/builds/97feb9bacc10
\ No newline at end of file