WizFi250 Geolocation and NTP Example on WIZwiki-W7500 of mbed platform

System Diagram

I would make a watch that can check my location and time using WizFi250 and WIZwiki-W7500. I used Geolocation Server to get my location and NTP Server to get current time. The following picture is system diagram of this project.

20150626_171106

Materials

For more detailed information about following materials, please refer to the following link; http://www.life4iot.com/2015/06/30/wizfi250-hello-world-demo-on-wizwiki-w7500-of-mbed-platform/?lang=en.

WIZwiki-W7500 (MCU Platform)

wizwiki-w7500_main

WizFi250-EVB (Wi-Fi Module)

ShopDtl_1203_20140918154919

Sensor Shield

TB2wSGHaFXXXXcLXXXXXXXXXXXX_!!33841454

SSD1306 OLED

  • Features
    • Resolution: 128 x 64 dot matrix panel
    • Power supply
      • VDD = 1.65V to 3.3V for IC logic
      • VCC = 7V to 15V for Panel driving
    • For matrix display
      • OLED driving output voltage, 15V maximum
      • Segment maximum source current: 100uA
      • Common maximum sink current: 15mA
      • 256 step contrast brightness current control
    • Embedded 128 x 64 bit SRAM display buffer
    • Pin selectable MCU Interfaces:
      • 8-bit 6800/8080-series parallel interface
      • 3 /4 wire Serial Peripheral Interface
      • I2C Interface
    • Screen saving continuous scrolling function in both horizontal and vertical direction
    • RAM write synchronization signal
    • Programmable Frame Rate and Multiplexing Ratio
    • Row Re-mapping and Column Re-mapping
  • On-Chip Oscillator
  • Chip layout for COG & COF
  • Wide range of operating temperature: -40°C to 85°C

Hardware Configuartion

WIZwiki-W7500 Board uses UART0 RX/TX/CTS/RTS pins to control WizFi250 and I2C SDA/SCL pins to control SSD1306 OLED.

20150626_204959

WizFi250-EVB Sensor Shield WIZwiki-W7500 SSD1306
RX-TX RXD TXD
TX-RX TXD RXD
CTS-RTS CTS D8
RTS-CTS RTS D7
WizFi250-RESET JP10-2 PA12
I2C SCL SCL PA9 SCL
I2C SDA SDA PA10 SDA
VCC VCC VCC
GND GND

Compile WizFi250 Geolocation and NTP Example

You can use Geolocation and NTP Example for WizFi250 on WIZwiki-W7500 by entering the following site; https://developer.mbed.org/teams/WIZnet/code/WizFi250-Geolocation_NTP/.

SSD1306 Library

I used Adafruit GFX Library in order to use SSD1306 Library. I found there is some sort of bug in this Display function while I used this library. So, I modified them and sent the “Pull Request” message to original author.

Modified Adafruit GFX Library

Orignal Adafruit GFX Library

HTTP Client

HTTP Client Library is used for sending any request to the web server and receiving any response from the web server. In this example, we would contact the ip-api.com server to get the Geolocation. If connecting ip-api.com/csv sever, we would get all the information such as country name, address, latitude, longitude, and TimeZone in CSV format.

You would be able to download HTTP Client Library by clicking following link; https://developer.mbed.org/users/donatien/code/HTTPClient/.

NTP Client

NTP Client Library is used to gain UTC(Universal Time Coordinated) information at Network Time Server using UDP communication. In this example, we would use kr.pool.ntp.org as NTP Server Domain of South Korea. In order to get to know the time of South Korea, we should add 9 hours to the information from NTP Server because it is UTC.

NTP Client Library

Example Source Code

WizFi250-Geolocation NTP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#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] == '' )) { //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
    }
}

Demo Video

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s