WizFi310 Geolocation_NTP library 2.0 version
Dependencies: Adafruit_GFX HTTPClient NTPClient WizFi310Interface_Legacy mbed
Prerequisite
This example shows that Wizwiki-W7500 and WizFi310 gets current geolocation/time information.
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)
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
Connect to Wi-Fi
returnCode = wizfi310.connect(SECURE, SSID, PASS);
Get and Parse a geolocation information
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); printf("HTTP Data Received\r\n"); }
Set time
ntpClient.setTime(domainName,123,0x00005000);
main.cpp@0:0b8f5c3e549f, 2017-04-19 (annotated)
- Committer:
- stkim92
- Date:
- Wed Apr 19 08:15:13 2017 +0000
- Revision:
- 0:0b8f5c3e549f
WizFi310 Geolocation_NTP library 2.0 version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stkim92 | 0:0b8f5c3e549f | 1 | #include "mbed.h" |
stkim92 | 0:0b8f5c3e549f | 2 | #include "Adafruit_SSD1306.h" |
stkim92 | 0:0b8f5c3e549f | 3 | #include "WizFi310Interface.h" |
stkim92 | 0:0b8f5c3e549f | 4 | #include "NTPClient.h" |
stkim92 | 0:0b8f5c3e549f | 5 | #include "HTTPClient.h" |
stkim92 | 0:0b8f5c3e549f | 6 | |
stkim92 | 0:0b8f5c3e549f | 7 | |
stkim92 | 0:0b8f5c3e549f | 8 | #define SECURE WizFi310::SEC_AUTO |
stkim92 | 0:0b8f5c3e549f | 9 | #define SSID "wizms1" |
stkim92 | 0:0b8f5c3e549f | 10 | #define PASS "maker0701" |
stkim92 | 0:0b8f5c3e549f | 11 | |
stkim92 | 0:0b8f5c3e549f | 12 | |
stkim92 | 0:0b8f5c3e549f | 13 | #if defined(TARGET_WIZwiki_W7500) |
stkim92 | 0:0b8f5c3e549f | 14 | // #define SDA PA_10 |
stkim92 | 0:0b8f5c3e549f | 15 | // #define SCL PA_9 |
stkim92 | 0:0b8f5c3e549f | 16 | WizFi310Interface wizfi310(D1, D0, D7, D6, D8, NC, 115200); |
stkim92 | 0:0b8f5c3e549f | 17 | Serial pc(USBTX,USBRX); |
stkim92 | 0:0b8f5c3e549f | 18 | #endif |
stkim92 | 0:0b8f5c3e549f | 19 | |
stkim92 | 0:0b8f5c3e549f | 20 | |
stkim92 | 0:0b8f5c3e549f | 21 | // an SPI sub-class that provides a constructed default |
stkim92 | 0:0b8f5c3e549f | 22 | //class I2CPreInit : public I2C |
stkim92 | 0:0b8f5c3e549f | 23 | //{ |
stkim92 | 0:0b8f5c3e549f | 24 | //public: |
stkim92 | 0:0b8f5c3e549f | 25 | // I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) |
stkim92 | 0:0b8f5c3e549f | 26 | // { |
stkim92 | 0:0b8f5c3e549f | 27 | // frequency(100000); |
stkim92 | 0:0b8f5c3e549f | 28 | // start(); |
stkim92 | 0:0b8f5c3e549f | 29 | // }; |
stkim92 | 0:0b8f5c3e549f | 30 | //}; |
stkim92 | 0:0b8f5c3e549f | 31 | |
stkim92 | 0:0b8f5c3e549f | 32 | //I2CPreInit gI2C(SDA,SCL); |
stkim92 | 0:0b8f5c3e549f | 33 | //Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128); |
stkim92 | 0:0b8f5c3e549f | 34 | NTPClient ntpClient; |
stkim92 | 0:0b8f5c3e549f | 35 | HTTPClient httpClient; |
stkim92 | 0:0b8f5c3e549f | 36 | void parse(char buffer[], int *j, char *string); //FUNCTION TO PARSE HTTP GET DATA |
stkim92 | 0:0b8f5c3e549f | 37 | char httpGetData[1024]; //BUFFER TO HOLD DATA FROM HTTP GET REQUEST |
stkim92 | 0:0b8f5c3e549f | 38 | |
stkim92 | 0:0b8f5c3e549f | 39 | |
stkim92 | 0:0b8f5c3e549f | 40 | int main() |
stkim92 | 0:0b8f5c3e549f | 41 | { |
stkim92 | 0:0b8f5c3e549f | 42 | time_t ctTime; //system time structure |
stkim92 | 0:0b8f5c3e549f | 43 | char success[10]={0}; //success first |
stkim92 | 0:0b8f5c3e549f | 44 | char countryFull[20]={0}; //Full Country Name |
stkim92 | 0:0b8f5c3e549f | 45 | char countryAbrv[5]={0}; //Abbreviated Country Name or country Code |
stkim92 | 0:0b8f5c3e549f | 46 | char stateAbrv[5]={0}; //Abbreviated State or region code |
stkim92 | 0:0b8f5c3e549f | 47 | char stateFull[15]={0}; //Full State Name |
stkim92 | 0:0b8f5c3e549f | 48 | char city[15]={0}; //City Name |
stkim92 | 0:0b8f5c3e549f | 49 | char zip[6]={0}; //ZIP CODE |
stkim92 | 0:0b8f5c3e549f | 50 | char latitude[10]={0}; //latitude |
stkim92 | 0:0b8f5c3e549f | 51 | char longitude[10]={0}; //longitude |
stkim92 | 0:0b8f5c3e549f | 52 | char timeZone[30]={0}; //timeZone |
stkim92 | 0:0b8f5c3e549f | 53 | int j=0, returnCode; |
stkim92 | 0:0b8f5c3e549f | 54 | |
stkim92 | 0:0b8f5c3e549f | 55 | pc.baud(115200); |
stkim92 | 0:0b8f5c3e549f | 56 | // gOled.begin(); |
stkim92 | 0:0b8f5c3e549f | 57 | // gOled.clearDisplay(); |
stkim92 | 0:0b8f5c3e549f | 58 | |
stkim92 | 0:0b8f5c3e549f | 59 | wizfi310.init(); |
stkim92 | 0:0b8f5c3e549f | 60 | returnCode = wizfi310.connect(SECURE, SSID, PASS); |
stkim92 | 0:0b8f5c3e549f | 61 | |
stkim92 | 0:0b8f5c3e549f | 62 | if ( returnCode == 0 ) |
stkim92 | 0:0b8f5c3e549f | 63 | { |
stkim92 | 0:0b8f5c3e549f | 64 | printf(" - WiFi Ready\r\n"); |
stkim92 | 0:0b8f5c3e549f | 65 | printf("IP Address is %s\r\n", wizfi310.getIPAddress()); |
stkim92 | 0:0b8f5c3e549f | 66 | } |
stkim92 | 0:0b8f5c3e549f | 67 | else |
stkim92 | 0:0b8f5c3e549f | 68 | { |
stkim92 | 0:0b8f5c3e549f | 69 | printf(" - Could not initialize WiFi - ending\r\n"); |
stkim92 | 0:0b8f5c3e549f | 70 | return 0; |
stkim92 | 0:0b8f5c3e549f | 71 | } |
stkim92 | 0:0b8f5c3e549f | 72 | |
stkim92 | 0:0b8f5c3e549f | 73 | //HANDLES THE HTTP GET REQUEST THE WAY THE FUNCTION IS CALLED HERE IS THE FOLLOWING |
stkim92 | 0:0b8f5c3e549f | 74 | // get(DOMAIN_NAME,BUFFER,TIMEOUT_VAL) |
stkim92 | 0:0b8f5c3e549f | 75 | //DOMAIN_NAME= domain name that get request is sent to |
stkim92 | 0:0b8f5c3e549f | 76 | //BUFFER= buffer to store data returned from the server |
stkim92 | 0:0b8f5c3e549f | 77 | //TIMEOUT_VAL= Time before the request times out |
stkim92 | 0:0b8f5c3e549f | 78 | HTTPResult r = httpClient.get("http://ip-api.com/csv",httpGetData,128); //GET GEOLOCATION DATA (CSV) |
stkim92 | 0:0b8f5c3e549f | 79 | |
stkim92 | 0:0b8f5c3e549f | 80 | if (r==HTTP_OK) { //IF THE DATA WAS RECIEVED |
stkim92 | 0:0b8f5c3e549f | 81 | j=0; |
stkim92 | 0:0b8f5c3e549f | 82 | //parse and display each of the API's location information strings on the LCD |
stkim92 | 0:0b8f5c3e549f | 83 | parse(httpGetData, &j, success); |
stkim92 | 0:0b8f5c3e549f | 84 | parse(httpGetData,&j,countryFull); |
stkim92 | 0:0b8f5c3e549f | 85 | parse(httpGetData,&j,countryAbrv); |
stkim92 | 0:0b8f5c3e549f | 86 | parse(httpGetData,&j,stateAbrv); |
stkim92 | 0:0b8f5c3e549f | 87 | parse(httpGetData,&j,stateFull); |
stkim92 | 0:0b8f5c3e549f | 88 | parse(httpGetData,&j,city); |
stkim92 | 0:0b8f5c3e549f | 89 | parse(httpGetData,&j,zip); |
stkim92 | 0:0b8f5c3e549f | 90 | parse(httpGetData,&j,latitude); |
stkim92 | 0:0b8f5c3e549f | 91 | parse(httpGetData,&j,longitude); |
stkim92 | 0:0b8f5c3e549f | 92 | parse(httpGetData,&j,timeZone); |
stkim92 | 0:0b8f5c3e549f | 93 | // gOled.printf("HTTP Data Received\r\n"); |
stkim92 | 0:0b8f5c3e549f | 94 | // gOled.display(); |
stkim92 | 0:0b8f5c3e549f | 95 | printf("HTTP Data Received\r\n"); |
stkim92 | 0:0b8f5c3e549f | 96 | } |
stkim92 | 0:0b8f5c3e549f | 97 | else { //HTTP GET REQUEST ERRORED |
stkim92 | 0:0b8f5c3e549f | 98 | // gOled.printf("HTTP Error %d\r\n", r); |
stkim92 | 0:0b8f5c3e549f | 99 | // gOled.display(); |
stkim92 | 0:0b8f5c3e549f | 100 | printf("HTTP Error %d\r\n", r); |
stkim92 | 0:0b8f5c3e549f | 101 | return -1; |
stkim92 | 0:0b8f5c3e549f | 102 | } |
stkim92 | 0:0b8f5c3e549f | 103 | printf("Reading Time...\r\n"); |
stkim92 | 0:0b8f5c3e549f | 104 | char* domainName="kr.pool.ntp.org"; //SET TO DOMAIN NAME OF SERVER GETTING TIME FROM |
stkim92 | 0:0b8f5c3e549f | 105 | //GETS THE TIME FROM THE SERVER |
stkim92 | 0:0b8f5c3e549f | 106 | //setTime(DOMAIN_NAME,PORT_NUMBER,TIME_OUT) |
stkim92 | 0:0b8f5c3e549f | 107 | //DOMAIN_NAME= domain name |
stkim92 | 0:0b8f5c3e549f | 108 | //PORT NUMBER=port number (123 for NTP) |
stkim92 | 0:0b8f5c3e549f | 109 | //TIME_OUT= timeout value for request |
stkim92 | 0:0b8f5c3e549f | 110 | ntpClient.setTime(domainName,123,0x00005000); |
stkim92 | 0:0b8f5c3e549f | 111 | printf("Time Set\r\n"); |
stkim92 | 0:0b8f5c3e549f | 112 | //Delay for human time to read LCD display |
stkim92 | 0:0b8f5c3e549f | 113 | wait(3.0); |
stkim92 | 0:0b8f5c3e549f | 114 | |
stkim92 | 0:0b8f5c3e549f | 115 | char buffer[80]; //BUFFER TO HOLD FORMATTED TIME DATA |
stkim92 | 0:0b8f5c3e549f | 116 | // gOled.printf("%s, %s %s, %s\r\n",city,stateAbrv,zip,countryAbrv); //PRINT CITY STATE AND ZIP INFORMATION AND COUNTRY |
stkim92 | 0:0b8f5c3e549f | 117 | // gOled.printf("LAT:%s\r\nLONG:%s\r\n",latitude,longitude); //PRINT LATITUDE AND LONGITUDE |
stkim92 | 0:0b8f5c3e549f | 118 | // gOled.printf("Timezone:%s\r\n",timeZone); //PRINT TIMEZONE |
stkim92 | 0:0b8f5c3e549f | 119 | |
stkim92 | 0:0b8f5c3e549f | 120 | |
stkim92 | 0:0b8f5c3e549f | 121 | wizfi310.disconnect(); //DISCONNECT FROM THE NETWORK |
stkim92 | 0:0b8f5c3e549f | 122 | while (1) { |
stkim92 | 0:0b8f5c3e549f | 123 | //ctTime = time(NULL)-(3600*4); //TIME with offset for eastern time US |
stkim92 | 0:0b8f5c3e549f | 124 | ctTime = time(NULL)+(3600*9); //TIME with offset for eastern time KR |
stkim92 | 0:0b8f5c3e549f | 125 | //FORMAT TIME FOR DISPLAY AND STORE FORMATTED RESULT IN BUFFER |
stkim92 | 0:0b8f5c3e549f | 126 | strftime(buffer,80,"%a %b %d\r\n%T %p %z\r\n %Z\r\n",localtime(&ctTime)); |
stkim92 | 0:0b8f5c3e549f | 127 | // gOled.printf("Univ Time Clock\r\n%s\r\n", buffer); |
stkim92 | 0:0b8f5c3e549f | 128 | // gOled.display(); |
stkim92 | 0:0b8f5c3e549f | 129 | // gOled.setTextCursor(0,40); |
stkim92 | 0:0b8f5c3e549f | 130 | printf("%s, %s %s, %s\r\n",city,stateAbrv,zip,countryAbrv); //PRINT CITY STATE AND ZIP INFORMATION AND COUNTRY |
stkim92 | 0:0b8f5c3e549f | 131 | printf("LAT:%s\r\nLONG:%s\r\n",latitude,longitude); //PRINT LATITUDE AND LONGITUDE |
stkim92 | 0:0b8f5c3e549f | 132 | printf("Timezone:%s\r\n",timeZone); //PRINT TIMEZONE |
stkim92 | 0:0b8f5c3e549f | 133 | printf("Univ Time Clock\r\n%s\r\n", buffer); |
stkim92 | 0:0b8f5c3e549f | 134 | wait(1); |
stkim92 | 0:0b8f5c3e549f | 135 | } |
stkim92 | 0:0b8f5c3e549f | 136 | } |
stkim92 | 0:0b8f5c3e549f | 137 | |
stkim92 | 0:0b8f5c3e549f | 138 | //SET FOR CSV FORMAT: NEEDS TO BE EDITED IF DIFFERENT FORMAT |
stkim92 | 0:0b8f5c3e549f | 139 | void parse(char buffer[], int *j, char *string) { |
stkim92 | 0:0b8f5c3e549f | 140 | //extracts next location string data item from buffer |
stkim92 | 0:0b8f5c3e549f | 141 | int i=0; |
stkim92 | 0:0b8f5c3e549f | 142 | for (i=0; i<=strlen(buffer); i++) { //TOTAL SIZE OF RETURNED DATA |
stkim92 | 0:0b8f5c3e549f | 143 | if ((buffer[*j+i] == ',')||(buffer[*j+i] == '\0' )) { //IF comma or end of string |
stkim92 | 0:0b8f5c3e549f | 144 | //comma is the string field delimiter |
stkim92 | 0:0b8f5c3e549f | 145 | string[i]=0; //SETS END OF SRTRING TO 0 |
stkim92 | 0:0b8f5c3e549f | 146 | *j=*j+i+1; //UPDATES to 1 after comma seperated value |
stkim92 | 0:0b8f5c3e549f | 147 | break; |
stkim92 | 0:0b8f5c3e549f | 148 | } else string[i]=buffer[*j+i]; //Keep adding to the string |
stkim92 | 0:0b8f5c3e549f | 149 | } |
stkim92 | 0:0b8f5c3e549f | 150 | } |