![](/media/cache/group/WIZnet_logo_001.jpg.50x50_q85.jpg)
Geolocation and NTP Example for WizFi250 on WIZwiki-W7500
Dependencies: Adafruit_GFX HTTPClient NTPClient WizFi250Interface mbed
System Diagram
I want to make a watch, that can check my location and time. So I decide to make this watch using Wi-Fi module(WizFi250) and Cortex-M0 MCU(WIZwiki-W7500). For getting geolocation of this watch, I used ip-api.com which geolocation server and I used NTP server for getting current time. This picture is system diagram of my project.
Materials
WIZwiki-W7500 ( MCU )
WizFi250-EVB ( Wi-Fi Module )
Sensor Shield
SSD1306 OLED
Refer to this URL.
https://developer.mbed.org/components/Adafruit-OLED-128x32/
Hardware Configuration
UART0 RX/TX/CTS/RTS pins of WIZwiki-W7500 board are used to control WizFi250 which Wi-Fi module and It use I2C SDA/SCL pins for using SSD1306 OLED.
Demo Video
For more detailed information, refer to this URL.
main.cpp@2:3a84959399d3, 2015-07-30 (annotated)
- Committer:
- kaizen
- Date:
- Thu Jul 30 04:25:13 2015 +0000
- Revision:
- 2:3a84959399d3
- Parent:
- 1:741b858c0b92
- Child:
- 3:1f2938317c31
revert mbed-src to official version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kaizen | 0:bc0fe324d7e2 | 1 | #include "mbed.h" |
kaizen | 0:bc0fe324d7e2 | 2 | #include "Adafruit_SSD1306.h" |
kaizen | 0:bc0fe324d7e2 | 3 | #include "WizFi250Interface.h" |
kaizen | 0:bc0fe324d7e2 | 4 | #include "NTPClient.h" |
kaizen | 0:bc0fe324d7e2 | 5 | #include "HTTPClient.h" |
kaizen | 0:bc0fe324d7e2 | 6 | |
kaizen | 0:bc0fe324d7e2 | 7 | |
kaizen | 0:bc0fe324d7e2 | 8 | #define SECURE WizFi250::SEC_AUTO |
kaizen | 2:3a84959399d3 | 9 | #define SSID "ssid" |
kaizen | 2:3a84959399d3 | 10 | #define PASS "key" |
kaizen | 0:bc0fe324d7e2 | 11 | |
kaizen | 0:bc0fe324d7e2 | 12 | |
kaizen | 0:bc0fe324d7e2 | 13 | #if defined(TARGET_WIZwiki_W7500) |
kaizen | 1:741b858c0b92 | 14 | #define SDA PA_10 |
kaizen | 1:741b858c0b92 | 15 | #define SCL PA_9 |
kaizen | 1:741b858c0b92 | 16 | WizFi250Interface wizfi250(D1,D0,D7,D8,PA_12,NC,115200); |
kaizen | 1:741b858c0b92 | 17 | Serial pc(USBTX,USBRX); |
kaizen | 0:bc0fe324d7e2 | 18 | #endif |
kaizen | 0:bc0fe324d7e2 | 19 | |
kaizen | 0:bc0fe324d7e2 | 20 | |
kaizen | 0:bc0fe324d7e2 | 21 | // an SPI sub-class that provides a constructed default |
kaizen | 0:bc0fe324d7e2 | 22 | class I2CPreInit : public I2C |
kaizen | 0:bc0fe324d7e2 | 23 | { |
kaizen | 0:bc0fe324d7e2 | 24 | public: |
kaizen | 0:bc0fe324d7e2 | 25 | I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) |
kaizen | 0:bc0fe324d7e2 | 26 | { |
kaizen | 0:bc0fe324d7e2 | 27 | frequency(100000); |
kaizen | 0:bc0fe324d7e2 | 28 | start(); |
kaizen | 0:bc0fe324d7e2 | 29 | }; |
kaizen | 0:bc0fe324d7e2 | 30 | }; |
kaizen | 0:bc0fe324d7e2 | 31 | |
kaizen | 0:bc0fe324d7e2 | 32 | I2CPreInit gI2C(SDA,SCL); |
kaizen | 0:bc0fe324d7e2 | 33 | Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128); |
kaizen | 0:bc0fe324d7e2 | 34 | NTPClient ntpClient; |
kaizen | 0:bc0fe324d7e2 | 35 | HTTPClient httpClient; |
kaizen | 0:bc0fe324d7e2 | 36 | void parse(char buffer[], int *j, char *string); //FUNCTION TO PARSE HTTP GET DATA |
kaizen | 0:bc0fe324d7e2 | 37 | char httpGetData[200]; //BUFFER TO HOLD DATA FROM HTTP GET REQUEST |
kaizen | 0:bc0fe324d7e2 | 38 | |
kaizen | 0:bc0fe324d7e2 | 39 | |
kaizen | 0:bc0fe324d7e2 | 40 | int main() |
kaizen | 0:bc0fe324d7e2 | 41 | { |
kaizen | 0:bc0fe324d7e2 | 42 | time_t ctTime; //system time structure |
kaizen | 0:bc0fe324d7e2 | 43 | char success[10]={0}; //success first |
kaizen | 0:bc0fe324d7e2 | 44 | char countryFull[20]={0}; //Full Country Name |
kaizen | 0:bc0fe324d7e2 | 45 | char countryAbrv[5]={0}; //Abbreviated Country Name or country Code |
kaizen | 0:bc0fe324d7e2 | 46 | char stateAbrv[5]={0}; //Abbreviated State or region code |
kaizen | 0:bc0fe324d7e2 | 47 | char stateFull[15]={0}; //Full State Name |
kaizen | 0:bc0fe324d7e2 | 48 | char city[15]={0}; //City Name |
kaizen | 0:bc0fe324d7e2 | 49 | char zip[6]={0}; //ZIP CODE |
kaizen | 0:bc0fe324d7e2 | 50 | char latitude[10]={0}; //latitude |
kaizen | 0:bc0fe324d7e2 | 51 | char longitude[10]={0}; //longitude |
kaizen | 0:bc0fe324d7e2 | 52 | char timeZone[30]={0}; //timeZone |
kaizen | 0:bc0fe324d7e2 | 53 | int j=0, returnCode; |
kaizen | 0:bc0fe324d7e2 | 54 | |
kaizen | 1:741b858c0b92 | 55 | pc.baud(115200); |
kaizen | 1:741b858c0b92 | 56 | gOled.begin(); |
kaizen | 1:741b858c0b92 | 57 | gOled.clearDisplay(); |
kaizen | 1:741b858c0b92 | 58 | |
kaizen | 0:bc0fe324d7e2 | 59 | wizfi250.init(); |
kaizen | 0:bc0fe324d7e2 | 60 | returnCode = wizfi250.connect(SECURE, SSID, PASS); |
kaizen | 0:bc0fe324d7e2 | 61 | |
kaizen | 0:bc0fe324d7e2 | 62 | if ( returnCode == 0 ) |
kaizen | 0:bc0fe324d7e2 | 63 | { |
kaizen | 0:bc0fe324d7e2 | 64 | printf(" - WiFi Ready\r\n"); |
kaizen | 0:bc0fe324d7e2 | 65 | printf("IP Address is %s\r\n", wizfi250.getIPAddress()); |
kaizen | 0:bc0fe324d7e2 | 66 | } |
kaizen | 0:bc0fe324d7e2 | 67 | else |
kaizen | 0:bc0fe324d7e2 | 68 | { |
kaizen | 0:bc0fe324d7e2 | 69 | printf(" - Could not initialize WiFi - ending\r\n"); |
kaizen | 0:bc0fe324d7e2 | 70 | return 0; |
kaizen | 0:bc0fe324d7e2 | 71 | } |
kaizen | 0:bc0fe324d7e2 | 72 | |
kaizen | 0:bc0fe324d7e2 | 73 | //HANDLES THE HTTP GET REQUEST THE WAY THE FUNCTION IS CALLED HERE IS THE FOLLOWING |
kaizen | 0:bc0fe324d7e2 | 74 | // get(DOMAIN_NAME,BUFFER,TIMEOUT_VAL) |
kaizen | 0:bc0fe324d7e2 | 75 | //DOMAIN_NAME= domain name that get request is sent to |
kaizen | 0:bc0fe324d7e2 | 76 | //BUFFER= buffer to store data returned from the server |
kaizen | 0:bc0fe324d7e2 | 77 | //TIMEOUT_VAL= Time before the request times out |
kaizen | 0:bc0fe324d7e2 | 78 | HTTPResult r = httpClient.get("http://ip-api.com/csv",httpGetData,128); //GET GEOLOCATION DATA (CSV) |
kaizen | 0:bc0fe324d7e2 | 79 | |
kaizen | 0:bc0fe324d7e2 | 80 | if (r==HTTP_OK) { //IF THE DATA WAS RECIEVED |
kaizen | 0:bc0fe324d7e2 | 81 | j=0; |
kaizen | 0:bc0fe324d7e2 | 82 | //parse and display each of the API's location information strings on the LCD |
kaizen | 0:bc0fe324d7e2 | 83 | parse(httpGetData, &j, success); |
kaizen | 0:bc0fe324d7e2 | 84 | parse(httpGetData,&j,countryFull); |
kaizen | 0:bc0fe324d7e2 | 85 | parse(httpGetData,&j,countryAbrv); |
kaizen | 0:bc0fe324d7e2 | 86 | parse(httpGetData,&j,stateAbrv); |
kaizen | 0:bc0fe324d7e2 | 87 | parse(httpGetData,&j,stateFull); |
kaizen | 0:bc0fe324d7e2 | 88 | parse(httpGetData,&j,city); |
kaizen | 0:bc0fe324d7e2 | 89 | parse(httpGetData,&j,zip); |
kaizen | 0:bc0fe324d7e2 | 90 | parse(httpGetData,&j,latitude); |
kaizen | 0:bc0fe324d7e2 | 91 | parse(httpGetData,&j,longitude); |
kaizen | 0:bc0fe324d7e2 | 92 | parse(httpGetData,&j,timeZone); |
kaizen | 1:741b858c0b92 | 93 | gOled.printf("HTTP Data Received\r\n"); |
kaizen | 1:741b858c0b92 | 94 | gOled.display(); |
kaizen | 0:bc0fe324d7e2 | 95 | } |
kaizen | 0:bc0fe324d7e2 | 96 | else { //HTTP GET REQUEST ERRORED |
kaizen | 1:741b858c0b92 | 97 | gOled.printf("HTTP Error %d\r\n", r); |
kaizen | 1:741b858c0b92 | 98 | gOled.display(); |
kaizen | 0:bc0fe324d7e2 | 99 | return -1; |
kaizen | 0:bc0fe324d7e2 | 100 | } |
kaizen | 0:bc0fe324d7e2 | 101 | printf("Reading Time...\r\n"); |
kaizen | 0:bc0fe324d7e2 | 102 | char* domainName="kr.pool.ntp.org"; //SET TO DOMAIN NAME OF SERVER GETTING TIME FROM |
kaizen | 0:bc0fe324d7e2 | 103 | //GETS THE TIME FROM THE SERVER |
kaizen | 0:bc0fe324d7e2 | 104 | //setTime(DOMAIN_NAME,PORT_NUMBER,TIME_OUT) |
kaizen | 0:bc0fe324d7e2 | 105 | //DOMAIN_NAME= domain name |
kaizen | 0:bc0fe324d7e2 | 106 | //PORT NUMBER=port number (123 for NTP) |
kaizen | 0:bc0fe324d7e2 | 107 | //TIME_OUT= timeout value for request |
kaizen | 0:bc0fe324d7e2 | 108 | ntpClient.setTime(domainName,123,0x00005000); |
kaizen | 0:bc0fe324d7e2 | 109 | printf("Time Set\r\n"); |
kaizen | 0:bc0fe324d7e2 | 110 | //Delay for human time to read LCD display |
kaizen | 0:bc0fe324d7e2 | 111 | wait(3.0); |
kaizen | 0:bc0fe324d7e2 | 112 | |
kaizen | 0:bc0fe324d7e2 | 113 | char buffer[80]; //BUFFER TO HOLD FORMATTED TIME DATA |
kaizen | 1:741b858c0b92 | 114 | gOled.printf("%s, %s %s, %s\r\n",city,stateAbrv,zip,countryAbrv); //PRINT CITY STATE AND ZIP INFORMATION AND COUNTRY |
kaizen | 1:741b858c0b92 | 115 | gOled.printf("LAT:%s\r\nLONG:%s\r\n",latitude,longitude); //PRINT LATITUDE AND LONGITUDE |
kaizen | 1:741b858c0b92 | 116 | gOled.printf("Timezone:%s\r\n",timeZone); //PRINT TIMEZONE |
kaizen | 0:bc0fe324d7e2 | 117 | |
kaizen | 0:bc0fe324d7e2 | 118 | wizfi250.disconnect(); //DISCONNECT FROM THE NETWORK |
kaizen | 0:bc0fe324d7e2 | 119 | while (1) { |
kaizen | 0:bc0fe324d7e2 | 120 | //ctTime = time(NULL)-(3600*4); //TIME with offset for eastern time US |
kaizen | 0:bc0fe324d7e2 | 121 | ctTime = time(NULL)+(3600*9); //TIME with offset for eastern time KR |
kaizen | 0:bc0fe324d7e2 | 122 | //FORMAT TIME FOR DISPLAY AND STORE FORMATTED RESULT IN BUFFER |
kaizen | 0:bc0fe324d7e2 | 123 | strftime(buffer,80,"%a %b %d\r\n%T %p %z\r\n %Z\r\n",localtime(&ctTime)); |
kaizen | 1:741b858c0b92 | 124 | gOled.printf("Univ Time Clock\r\n%s\r\n", buffer); |
kaizen | 1:741b858c0b92 | 125 | gOled.display(); |
kaizen | 1:741b858c0b92 | 126 | gOled.setTextCursor(0,40); |
kaizen | 0:bc0fe324d7e2 | 127 | wait(1); |
kaizen | 0:bc0fe324d7e2 | 128 | } |
kaizen | 0:bc0fe324d7e2 | 129 | } |
kaizen | 0:bc0fe324d7e2 | 130 | |
kaizen | 0:bc0fe324d7e2 | 131 | //SET FOR CSV FORMAT: NEEDS TO BE EDITED IF DIFFERENT FORMAT |
kaizen | 0:bc0fe324d7e2 | 132 | void parse(char buffer[], int *j, char *string) { |
kaizen | 0:bc0fe324d7e2 | 133 | //extracts next location string data item from buffer |
kaizen | 0:bc0fe324d7e2 | 134 | int i=0; |
kaizen | 0:bc0fe324d7e2 | 135 | for (i=0; i<=strlen(buffer); i++) { //TOTAL SIZE OF RETURNED DATA |
kaizen | 0:bc0fe324d7e2 | 136 | if ((buffer[*j+i] == ',')||(buffer[*j+i] == '\0' )) { //IF comma or end of string |
kaizen | 0:bc0fe324d7e2 | 137 | //comma is the string field delimiter |
kaizen | 0:bc0fe324d7e2 | 138 | string[i]=0; //SETS END OF SRTRING TO 0 |
kaizen | 0:bc0fe324d7e2 | 139 | *j=*j+i+1; //UPDATES to 1 after comma seperated value |
kaizen | 0:bc0fe324d7e2 | 140 | break; |
kaizen | 0:bc0fe324d7e2 | 141 | } else string[i]=buffer[*j+i]; //Keep adding to the string |
kaizen | 0:bc0fe324d7e2 | 142 | } |
kaizen | 0:bc0fe324d7e2 | 143 | } |