Wi-Fi Geolocation Watch

details

This posting explains how you can acquire the position information from Geolocation server and time information from NTP server using WizFi250 & WIZwiki-W7500.

Below is the system diagram.

geolo-1

 

Hardware

You need below hardware parts :

geolo-2

  • WizFi250-EVB :  WizFi250-EVB is the evaluation board for WizFi250 Wi-Fi module. As it is compatible with Arduino, it could be used for Wi-Fi Shield for Arduino boards.

geolo-3

  • Sensor Shield : On this sensor shied, various sensors are mounted. Just by plugging to the WIZwiki-W7500, you can test the all the peripherals of the W7500.

geolo-4

  • SSD1306  OLED : 128 x 64 dot matrix panel

 

Hardware Configuration

WIZwiki-W7500 board controls the WizFi250 using UART0 RX/TX/CTS/RTS pins and SSE1306 OLED using I2C SDA/SCL pins.

geolo-5

geolo-6

Compile the example codes of WizFi250 Geolocation and NTP

You can download the example code from below link.

WizFi250 Gelocation_NTP Example 

The libraries of the example are as below.

<SSD 1306 Library>

To use this SSD1306 OLED, we used the GFX Library provided by Adafruit. We also found some bugs in Display() function and sends the “Pull Request” to modify the code.

<HTTP Client>

HTTP Client library is used for transmitting or receiving the Web server request/Response messages. The example connects to the ‘ip-api.com’ server to acquire the geolocation information. If you try to connect the server with ‘ip-api.com/csv’ address, you can get the information of the country, address, latitude, longitude and Time zone in csv format.

Click below link to download the HTTP Client library.

HTTP Client

 

<NTP Client>

NTP Client library acquires the UTC information from Network Time server by using UDP protocol. The example code will connect to ‘kr.pool.ntp.org’ the Korean NTP server domain.

NTP Client

 

<Example Source Code>

WizFi250-Geolocation_NTP

#include "mbed.h"
#include "Adafruit_SSD1306.h"
#include "WizFi250Interface.h"
#include "NTPClient.h"
#include "HTTPClient.h"
 
 
#define SECURE WizFi250::SEC_AUTO
#define SSID "wizohp"
#define PASS "wiznet218"
 
 
#if defined(TARGET_WIZwiki_W7500)
    #define SDA                  PA_10
    #define SCL                  PA_9
    WizFi250Interface wizfi250(D1,D0,D7,D8,PA_12,NC,115200);
    Serial pc(USBTX,USBRX);
#endif
 
 
// an SPI sub-class that provides a constructed default
class I2CPreInit : public I2C
{
public:
    I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl)
    {
        frequency(100000);
        start();
    };
};
 
I2CPreInit gI2C(SDA,SCL);
Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128);
NTPClient ntpClient;
HTTPClient httpClient;
void parse(char buffer[], int *j, char *string); //FUNCTION TO PARSE HTTP GET DATA
char httpGetData[200]; //BUFFER TO HOLD DATA FROM HTTP GET REQUEST
 
 
int main()
{
    time_t ctTime; //system time structure
    char success[10]={0};  //success first
    char countryFull[20]={0}; //Full Country Name
    char countryAbrv[5]={0}; //Abbreviated Country Name or country Code
    char stateAbrv[5]={0}; //Abbreviated State or region code
    char stateFull[15]={0}; //Full State Name
    char city[15]={0}; //City Name
    char zip[6]={0}; //ZIP CODE
    char latitude[10]={0}; //latitude
    char longitude[10]={0}; //longitude
    char timeZone[30]={0}; //timeZone
    int j=0, returnCode;
 
    pc.baud(115200);
    gOled.begin();
    gOled.clearDisplay();
 
    wizfi250.init();
    returnCode = wizfi250.connect(SECURE, SSID, PASS);
 
    if ( returnCode == 0 )
    {
        printf(" - WiFi Ready\r\n");
        printf("IP Address is %s\r\n", wizfi250.getIPAddress());
    }
    else
    {
        printf(" - Could not initialize WiFi - ending\r\n");
        return 0;
    }
    
    //HANDLES THE HTTP GET REQUEST THE WAY THE FUNCTION IS CALLED HERE IS THE FOLLOWING
    // get(DOMAIN_NAME,BUFFER,TIMEOUT_VAL)
    //DOMAIN_NAME= domain name that get request is sent to
    //BUFFER= buffer to store data returned from the server
    //TIMEOUT_VAL= Time before the request times out
    HTTPResult r = httpClient.get("http://ip-api.com/csv",httpGetData,128); //GET GEOLOCATION DATA (CSV)
 
    if (r==HTTP_OK) { //IF THE DATA WAS RECIEVED
        j=0;
        //parse and display each of the API's location information strings on the LCD
        parse(httpGetData, &j, success); 
        parse(httpGetData,&j,countryFull);
        parse(httpGetData,&j,countryAbrv);
        parse(httpGetData,&j,stateAbrv);
        parse(httpGetData,&j,stateFull);
        parse(httpGetData,&j,city);
        parse(httpGetData,&j,zip);
        parse(httpGetData,&j,latitude);
        parse(httpGetData,&j,longitude);
        parse(httpGetData,&j,timeZone);
        gOled.printf("HTTP Data Received\r\n");
        gOled.display();
    } 
    else { //HTTP GET REQUEST ERRORED
        gOled.printf("HTTP Error %d\r\n", r);
        gOled.display();
        return -1;
    }
    printf("Reading Time...\r\n");
    char* domainName="kr.pool.ntp.org"; //SET TO DOMAIN NAME OF SERVER GETTING TIME FROM
    //GETS THE TIME FROM THE SERVER
    //setTime(DOMAIN_NAME,PORT_NUMBER,TIME_OUT)
    //DOMAIN_NAME= domain name
    //PORT NUMBER=port number (123 for NTP)
    //TIME_OUT= timeout value for request
    ntpClient.setTime(domainName,123,0x00005000);
    printf("Time Set\r\n");
    //Delay for human time to read LCD display
    wait(3.0);
 
    char buffer[80]; //BUFFER TO HOLD FORMATTED TIME DATA
    gOled.printf("%s, %s %s, %s\r\n",city,stateAbrv,zip,countryAbrv); //PRINT CITY STATE AND ZIP INFORMATION AND COUNTRY
    gOled.printf("LAT:%s\r\nLONG:%s\r\n",latitude,longitude); //PRINT LATITUDE AND LONGITUDE 
    gOled.printf("Timezone:%s\r\n",timeZone); //PRINT TIMEZONE
 
    wizfi250.disconnect(); //DISCONNECT FROM THE NETWORK 
    while (1) {
        //ctTime = time(NULL)-(3600*4);  //TIME with offset for eastern time US
        ctTime = time(NULL)+(3600*9);  //TIME with offset for eastern time KR
        //FORMAT TIME FOR DISPLAY AND STORE FORMATTED RESULT IN BUFFER
        strftime(buffer,80,"%a %b %d\r\n%T %p %z\r\n %Z\r\n",localtime(&ctTime));
        gOled.printf("Univ Time Clock\r\n%s\r\n", buffer);
        gOled.display();
        gOled.setTextCursor(0,40);
        wait(1);
    }      
}
 
//SET FOR CSV FORMAT: NEEDS TO BE EDITED IF DIFFERENT FORMAT
void parse(char buffer[], int *j, char *string) {
//extracts next location string data item from buffer
    int i=0;
    for (i=0; i<=strlen(buffer); i++) {  //TOTAL SIZE OF RETURNED DATA
        if ((buffer[*j+i] == ',')||(buffer[*j+i] == '\0' )) { //IF comma or end of string
            //comma is the string field delimiter
            string[i]=0; //SETS END OF SRTRING TO 0
            *j=*j+i+1; //UPDATES to 1 after comma seperated value
            break;
        } else string[i]=buffer[*j+i]; //Keep adding to the string
    }
}

 

Below is the demonstration.

 

This posting explains how you can acquire the position information from Geolocation server and time information from NTP server using WizFi250 & WIZwiki-W7500.

Below is the system diagram.

geolo-1

 

Hardware

You need below hardware parts :

geolo-2

  • WizFi250-EVB :  WizFi250-EVB is the evaluation board for WizFi250 Wi-Fi module. As it is compatible with Arduino, it could be used for Wi-Fi Shield for Arduino boards.

geolo-3

  • Sensor Shield : On this sensor shied, various sensors are mounted. Just by plugging to the WIZwiki-W7500, you can test the all the peripherals of the W7500.

geolo-4

  • SSD1306  OLED : 128 x 64 dot matrix panel

 

Hardware Configuration

WIZwiki-W7500 board controls the WizFi250 using UART0 RX/TX/CTS/RTS pins and SSE1306 OLED using I2C SDA/SCL pins.

geolo-5

geolo-6

Compile the example codes of WizFi250 Geolocation and NTP

You can download the example code from below link.

WizFi250 Gelocation_NTP Example 

The libraries of the example are as below.

<SSD 1306 Library>

To use this SSD1306 OLED, we used the GFX Library provided by Adafruit. We also found some bugs in Display() function and sends the “Pull Request” to modify the code.

<HTTP Client>

HTTP Client library is used for transmitting or receiving the Web server request/Response messages. The example connects to the ‘ip-api.com’ server to acquire the geolocation information. If you try to connect the server with ‘ip-api.com/csv’ address, you can get the information of the country, address, latitude, longitude and Time zone in csv format.

Click below link to download the HTTP Client library.

HTTP Client

 

<NTP Client>

NTP Client library acquires the UTC information from Network Time server by using UDP protocol. The example code will connect to ‘kr.pool.ntp.org’ the Korean NTP server domain.

NTP Client

 

<Example Source Code>

WizFi250-Geolocation_NTP

#include "mbed.h"
#include "Adafruit_SSD1306.h"
#include "WizFi250Interface.h"
#include "NTPClient.h"
#include "HTTPClient.h"
 
 
#define SECURE WizFi250::SEC_AUTO
#define SSID "wizohp"
#define PASS "wiznet218"
 
 
#if defined(TARGET_WIZwiki_W7500)
    #define SDA                  PA_10
    #define SCL                  PA_9
    WizFi250Interface wizfi250(D1,D0,D7,D8,PA_12,NC,115200);
    Serial pc(USBTX,USBRX);
#endif
 
 
// an SPI sub-class that provides a constructed default
class I2CPreInit : public I2C
{
public:
    I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl)
    {
        frequency(100000);
        start();
    };
};
 
I2CPreInit gI2C(SDA,SCL);
Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128);
NTPClient ntpClient;
HTTPClient httpClient;
void parse(char buffer[], int *j, char *string); //FUNCTION TO PARSE HTTP GET DATA
char httpGetData[200]; //BUFFER TO HOLD DATA FROM HTTP GET REQUEST
 
 
int main()
{
    time_t ctTime; //system time structure
    char success[10]={0};  //success first
    char countryFull[20]={0}; //Full Country Name
    char countryAbrv[5]={0}; //Abbreviated Country Name or country Code
    char stateAbrv[5]={0}; //Abbreviated State or region code
    char stateFull[15]={0}; //Full State Name
    char city[15]={0}; //City Name
    char zip[6]={0}; //ZIP CODE
    char latitude[10]={0}; //latitude
    char longitude[10]={0}; //longitude
    char timeZone[30]={0}; //timeZone
    int j=0, returnCode;
 
    pc.baud(115200);
    gOled.begin();
    gOled.clearDisplay();
 
    wizfi250.init();
    returnCode = wizfi250.connect(SECURE, SSID, PASS);
 
    if ( returnCode == 0 )
    {
        printf(" - WiFi Ready\r\n");
        printf("IP Address is %s\r\n", wizfi250.getIPAddress());
    }
    else
    {
        printf(" - Could not initialize WiFi - ending\r\n");
        return 0;
    }
    
    //HANDLES THE HTTP GET REQUEST THE WAY THE FUNCTION IS CALLED HERE IS THE FOLLOWING
    // get(DOMAIN_NAME,BUFFER,TIMEOUT_VAL)
    //DOMAIN_NAME= domain name that get request is sent to
    //BUFFER= buffer to store data returned from the server
    //TIMEOUT_VAL= Time before the request times out
    HTTPResult r = httpClient.get("http://ip-api.com/csv",httpGetData,128); //GET GEOLOCATION DATA (CSV)
 
    if (r==HTTP_OK) { //IF THE DATA WAS RECIEVED
        j=0;
        //parse and display each of the API's location information strings on the LCD
        parse(httpGetData, &j, success); 
        parse(httpGetData,&j,countryFull);
        parse(httpGetData,&j,countryAbrv);
        parse(httpGetData,&j,stateAbrv);
        parse(httpGetData,&j,stateFull);
        parse(httpGetData,&j,city);
        parse(httpGetData,&j,zip);
        parse(httpGetData,&j,latitude);
        parse(httpGetData,&j,longitude);
        parse(httpGetData,&j,timeZone);
        gOled.printf("HTTP Data Received\r\n");
        gOled.display();
    } 
    else { //HTTP GET REQUEST ERRORED
        gOled.printf("HTTP Error %d\r\n", r);
        gOled.display();
        return -1;
    }
    printf("Reading Time...\r\n");
    char* domainName="kr.pool.ntp.org"; //SET TO DOMAIN NAME OF SERVER GETTING TIME FROM
    //GETS THE TIME FROM THE SERVER
    //setTime(DOMAIN_NAME,PORT_NUMBER,TIME_OUT)
    //DOMAIN_NAME= domain name
    //PORT NUMBER=port number (123 for NTP)
    //TIME_OUT= timeout value for request
    ntpClient.setTime(domainName,123,0x00005000);
    printf("Time Set\r\n");
    //Delay for human time to read LCD display
    wait(3.0);
 
    char buffer[80]; //BUFFER TO HOLD FORMATTED TIME DATA
    gOled.printf("%s, %s %s, %s\r\n",city,stateAbrv,zip,countryAbrv); //PRINT CITY STATE AND ZIP INFORMATION AND COUNTRY
    gOled.printf("LAT:%s\r\nLONG:%s\r\n",latitude,longitude); //PRINT LATITUDE AND LONGITUDE 
    gOled.printf("Timezone:%s\r\n",timeZone); //PRINT TIMEZONE
 
    wizfi250.disconnect(); //DISCONNECT FROM THE NETWORK 
    while (1) {
        //ctTime = time(NULL)-(3600*4);  //TIME with offset for eastern time US
        ctTime = time(NULL)+(3600*9);  //TIME with offset for eastern time KR
        //FORMAT TIME FOR DISPLAY AND STORE FORMATTED RESULT IN BUFFER
        strftime(buffer,80,"%a %b %d\r\n%T %p %z\r\n %Z\r\n",localtime(&ctTime));
        gOled.printf("Univ Time Clock\r\n%s\r\n", buffer);
        gOled.display();
        gOled.setTextCursor(0,40);
        wait(1);
    }      
}
 
//SET FOR CSV FORMAT: NEEDS TO BE EDITED IF DIFFERENT FORMAT
void parse(char buffer[], int *j, char *string) {
//extracts next location string data item from buffer
    int i=0;
    for (i=0; i<=strlen(buffer); i++) {  //TOTAL SIZE OF RETURNED DATA
        if ((buffer[*j+i] == ',')||(buffer[*j+i] == '\0' )) { //IF comma or end of string
            //comma is the string field delimiter
            string[i]=0; //SETS END OF SRTRING TO 0
            *j=*j+i+1; //UPDATES to 1 after comma seperated value
            break;
        } else string[i]=buffer[*j+i]; //Keep adding to the string
    }
}

 

Below is the demonstration.

 

COMMENTS

Please Login to comment
  Subscribe  
Notify of