mbed determines its location using a web-based geolocation API that uses the IP address and displays it on the LCD. Timezone is also included. A free API key must be inserted in the URL. See http://mbed.org/users/4180_1/notebook/geolocation-lcd-display/

Dependencies:   NetServices TextLCD mbed HTTPClient_ToBeRemoved

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "HTTPClient.h"
00004 #include "TextLCD.h"
00005 //Geolocation using IP address - get web page with location data
00006 // displays location fields on LCD  from web page ";....location text...;"
00007 // see http://mbed.org/users/4180_1/notebook/geolocation-lcd-display/
00008 TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d0-d3
00009 EthernetNetIf eth;
00010 HTTPClient http;
00011 
00012 void parse(char buffer[], int *j, char *string) {
00013 //extracts next location string data item from buffer
00014     int i=0;
00015     for (i=0; i<=strlen(buffer); i++) {
00016         if ((buffer[*j+i] == ';')||(buffer[*j+i] == '\0' )) {
00017             //semicolon is the string field delimiter
00018             string[i]=0;
00019             *j=*j+i+1;
00020             break;
00021         } else string[i]=buffer[*j+i];
00022     }
00023 }
00024 
00025 int main() {
00026     char result [4]={0};
00027     char ip [17]={0};
00028     char country_abbr[10]={0};
00029     char country[60]={0};
00030     char region[40]={0};
00031     char city[60]={0};
00032     char zipcode[10]={0};
00033     char latitude[10]={0};
00034     char longitude[10]={0};
00035     char timezone[7]={0};
00036     char buffer[256]={0};
00037     float flatitude=0.0;
00038     float flongitude=0.0;
00039     float ftimezone=0.0;
00040     int j=0;
00041 
00042     //Setup network - get IP address using DHCP
00043     lcd.cls();
00044     lcd.printf("Net setup\n");
00045     EthernetErr ethErr = eth.setup();
00046     if (ethErr) {
00047         lcd.printf("Error %d", ethErr);
00048         return -1;
00049     }
00050     lcd.printf(" Net OK");
00051     wait(.5);
00052     lcd.cls();
00053     lcd.printf("IP Address\nGeolocation API");
00054     HTTPText txt;
00055     //iPinfoDB API  - get web page with location data
00056     //Insert your free key from www.ipinfo.com for the API in the URL below
00057     HTTPResult r = http.get("http://api.ipinfodb.com/v3/ip-city/?key=PUT_YOUR_API_KEY_HERE", &txt);
00058     if (r==HTTP_OK) {
00059         //got web page text data OK
00060         strcpy(buffer,txt.gets());
00061         wait(1);
00062         while (1) {
00063             j=0;
00064             //parse and display each of the API's location information strings on the LCD
00065             parse(buffer, &j, result);
00066             lcd.cls();
00067             lcd.printf("result: %s\n", result);
00068             if (result[0]!='O') { //needs valid key
00069                 wait(1);
00070                 lcd.cls();
00071                 lcd.printf("Get Free API key");
00072                 lcd.printf("www.iPinfoDB.com");
00073                 return(-1);
00074             }
00075             wait(1);
00076             j++;
00077             parse(buffer, &j, ip);
00078             lcd.cls();
00079             lcd.printf("IP address: \n %s", ip);
00080             wait(2);
00081             parse(buffer, &j, country_abbr);
00082             lcd.cls();
00083             lcd.printf("Country code: \n %s", country_abbr);
00084             wait(2);
00085             parse(buffer, &j, country);
00086             lcd.cls();
00087             lcd.printf("Country: \n%s", country);
00088             wait(2);
00089             parse(buffer, &j, region);
00090             lcd.cls();
00091             lcd.printf("Region or State:%s", region);
00092             wait(2);
00093             parse(buffer, &j, city);
00094             lcd.cls();
00095             lcd.printf("City: \n%s", city);
00096             wait(2);
00097             parse(buffer, &j, zipcode);
00098             lcd.cls();
00099             lcd.printf("Zipcode: \n %s", zipcode);
00100             wait(2);
00101             parse(buffer, &j, latitude);
00102             sscanf(latitude,"%f",&flatitude);
00103             lcd.cls();
00104             lcd.printf("Latitude: \n %f", flatitude);
00105             wait(2);
00106             parse(buffer, &j, longitude);
00107             sscanf(longitude,"%f",&flongitude);
00108             lcd.cls();
00109             lcd.printf("Longitude: \n %f", flongitude);
00110             wait(2);
00111             parse(buffer, &j, timezone);
00112             sscanf(timezone,"%f",&ftimezone);
00113             lcd.cls();
00114             lcd.printf("Timezone: \n %f", ftimezone);
00115             wait(4);
00116         }
00117     } else {
00118         lcd.cls();
00119         lcd.printf("HTTP Error %d", r);
00120         return -1;
00121     }
00122 }