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

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

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

Files at this revision

API Documentation at this revision

Comitter:
cliff1
Date:
Thu Dec 07 07:35:35 2017 +0000
Parent:
2:c243e5fe8820
Commit message:
20171207

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r c243e5fe8820 -r 5111c1c1c59e main.cpp
--- a/main.cpp	Thu Aug 24 01:54:36 2017 +0000
+++ b/main.cpp	Thu Dec 07 07:35:35 2017 +0000
@@ -63,7 +63,7 @@
         }
     }    
      
-    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",
+    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",
     CITY, API_KEY);
 
     socket.send(http_cmd, sizeof(http_cmd));