Geolocation LCD Display


Geolocation API used to determine the location of mbed based on the IP address. The city is displayed on the LCD. Larger Image

Geolocation is the geographic location of an object such as a computer or cell phone. This is typically accomplished using the IP address, WiFi signal strength, or GPS data. If latitude and longitude are available from a GPS, Yahoo’s Placefinder API will convert it into a city and address or vice versa. AskGeo has a timezone API using the latitude and longitude that is free for non-commercial use. Many web browsers and cell phones have geolocation apps. Not every device has a GPS and GPS will not work inside large buildings and urban canyons.

IP-based Geolocation

When GPS data is not available and a device has a network connection, the IP address can be used to estimate location. It is a complex near random mapping and only a very large database can be used to determine the location.

IP4 address map from ICANN 2007.

In the map above, blue is North America, yellow is Europe, purple is Asia-Pacific, green is Latin America/Caribbean, and orange is Africa. Dark grey is space that, for protocol reasons cannot be assigned, and black is the space for a special kind of assignment called Multicast. Without a large data base, it would be difficult to even get the continent correct. Commercial data bases are available from several vendors to map IP addresses to cities and time zones. There is even a new geolocation API standard, W3C Geolocation API. Web sites often use geolocation to adjust content for each locale and to detect fraud in ecommerce transactions. The time zone setting can come in handy for local time settings.

Google has a Latitude API used on many smartphones such as Android and iPhone, but a special key is needed along with a prior user authorization step to disclose location. Some social networking apps use it to determine the location of your friends. As one would imagine, disclosing a precise location raises privacy concerns on devices such as cell phones. Skyhook also works off a database of Wi Fi and cell tower locations, but a license is required. Commercial APIs are also available from Quova and Fraud Labs. iPInfoDB also has a free API but registration is required to obtain a key used in the URL. The iPinfoDB API returns the location of an IP address (country, region, city, zipcode, latitude, longitude) and the associated time zone in a simple text format and is easier to use. There are also a number of web pages that will use a device’s external IP address and report the device’s location at least to perhaps the city level. A device can access them free to determine location (at least a few times a day before it gets locked out). Here are some sites to try:

For a quick geolocation demo with mbed (without a user key), the web page from one of these sites can be read, and by parsing the web page’s HTML the data can be used for display in the LCD. In a web browser, use view source to examine the HTML to find the data fields of interest and search for the strings that can be used to locate and recover the data in a program. Of course the problem with this approach is that any changes to their IP location web page will require a program update, unlike something like the Google Latitude API or the iPInfoDB API. In most cases, it would likely require a change in the search strings. For the demo, the iPInfoDB API was used after getting a free key via email.


The same hardware and wiring setup is used as the Internet LCD Clock, Weather LCD Display and News LCD Display.

Ethernet Magjack Connector

MbedSparkfun Ethernet Breakout

Note: Different magjack connectors can use different pin outs even though they look the same. These pins are for connectors from Sparkfun in 2011. If you are having trouble with your initial network setup, try this example code first. Once it works, it will confirm correct wiring on the connector (at least on the input side). Magjack Pinouts are posted for other common connectors. More connections to the unused pins including one to 3.3V and a capacitor should be added for long cables, but this setup works well with short cables. If you still get the net error message, make sure that DHCP service is enabled for your mbed. On some networks, you need to add the module's MAC address to enable the DHCP server to give it an IP address. It times out with an error, if no IP address is returned in about fifteen seconds. If the location does not display on the LCD after seeing "net OK", you may have DNS problems or a very slow network.

Text LCD

Mbed pinsText LCD pins
5V or 3.3V*VCC
VO to GND via 1k resistor

Where N/C is not connected and the top left pin of the TextLCD’s header is GND and the right most pin is D7 in the LCD orientation shown in the images above. The resistor sets the contrast and some LCDs may need a different value or a contrast adjust potentiometer. See Text LCD for additional help, if needed.

(*) Note: some older LCDs may use 5V, but 3.3V is more common on current LCDs. If you cannot find a data sheet to confirm this, try 3.3V first and if the display is still blank (after double checking all other connections and code) it might need 5V. To save jumper wires, this setup uses the four bit interface to transfer ASCII characters to the LCD.

Example Code


To run the demo code you need to obtain a free key by filling out a short web form at iPInfoDB. Then click on the link in the email. This code must be added as a suffix to the URL in the source code to run it on your mbed. Find in the source code after downloading it.

Import programGeoLocation_LCD_Display

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

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"
#include "TextLCD.h"
//Geolocation using IP address - get web page with location data
// displays location fields on LCD  from web page ";....location text...;"
// see
TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d0-d3
EthernetNetIf eth;
HTTPClient http;

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++) {
        if ((buffer[*j+i] == ';')||(buffer[*j+i] == '\0' )) {
            //semicolon is the string field delimiter
        } else string[i]=buffer[*j+i];

int main() {
    char result [4]={0};
    char ip [17]={0};
    char country_abbr[10]={0};
    char country[60]={0};
    char region[40]={0};
    char city[60]={0};
    char zipcode[10]={0};
    char latitude[10]={0};
    char longitude[10]={0};
    char timezone[7]={0};
    char buffer[256]={0};
    float flatitude=0.0;
    float flongitude=0.0;
    float ftimezone=0.0;
    int j=0;

    //Setup network - get IP address using DHCP
    lcd.printf("Net setup\n");
    EthernetErr ethErr = eth.setup();
    if (ethErr) {
        lcd.printf("Error %d", ethErr);
        return -1;
    lcd.printf(" Net OK");
    lcd.printf("IP Address\nGeolocation API");
    HTTPText txt;
    //iPinfoDB API  - get web page with location data
    //Insert your free key from for the API in the URL below
    HTTPResult r = http.get("", &txt);
    if (r==HTTP_OK) {
        //got web page text data OK
        while (1) {
            //parse and display each of the API's location information strings on the LCD
            parse(buffer, &j, result);
            lcd.printf("result: %s\n", result);
            if (result[0]!='O') { //needs valid key
                lcd.printf("Get Free API key");
            parse(buffer, &j, ip);
            lcd.printf("IP address: \n %s", ip);
            parse(buffer, &j, country_abbr);
            lcd.printf("Country code: \n %s", country_abbr);
            parse(buffer, &j, country);
            lcd.printf("Country: \n%s", country);
            parse(buffer, &j, region);
            lcd.printf("Region or State:%s", region);
            parse(buffer, &j, city);
            lcd.printf("City: \n%s", city);
            parse(buffer, &j, zipcode);
            lcd.printf("Zipcode: \n %s", zipcode);
            parse(buffer, &j, latitude);
            lcd.printf("Latitude: \n %f", flatitude);
            parse(buffer, &j, longitude);
            lcd.printf("Longitude: \n %f", flongitude);
            parse(buffer, &j, timezone);
            lcd.printf("Timezone: \n %f", ftimezone);
    } else {
        lcd.printf("HTTP Error %d", r);
        return -1;

Example Geolocation API Data from web

The data returned by the API contains several string fields terminated by a semicolon (and not a space), so it is very easy to parse.


OK;;;US;UNITED STATES;GEORGIA;ATLANTA;30324;33.809;-84.3548;-05:00

As seen above, the API returns the location of the external IP address (country, region, city, zipcode, latitude, longitude) and the associated timezone. Accuracy is very high on country and a bit lower on the city. If the city is incorrect, it is typically another city located within a few miles. The database is updated every month and some ISPs report changes quicker than others. Some city names require the use of UTF-8 coding. The API key code is free for non-commercial use, but the terms of use require that each user signup for his own key.

Here is another example in a different country of the API response for (IP= from nslookup).


Demo Video

Geolocation using the IP address to determine where mbed is located.

More on Time Zones

If longitude data was available a rough estimate would be timezone=int(longitude/(360/24)) but it is a bit more complex than that. A large database needs to be checked and the timezone value is not always an integer!

Time zone map from AskGeo API database.

"The vertical stripes in the open ocean are the natural time zones implied solely by the longitude. Time zones that don't observe Daylight Savings Time (DST) are colored the same as their corresponding natural time zone; those that do are colored slightly darker but with the same hue. Time zones that do not use an offset from UTC that is an integer number of hours have a color that is a proportional mixture of the colors of the bracketing natural time zones (e.g., Iran and India)."

As an example, here is the geolocation API response from an IP address at the Indian Institute of Technology. Note the timezone of 5:30 at the end.

OK;;;IN;INDIA;DELHI;NEW DELHI;-;28.6328;77.2195;+05:30

Update: has code for an easier to use Geolocation API. Example

Ideas for further enhancements

1. Register at iPInfoDB, get a key, and run the code on your mbed to check your location.
2. Use the time zone information to set the local time on the Internet LCD clock.
3. Use the city information for a local weather forecast in the Weather LCD display.
4. Fix the code that scans the time zone so that it works for non-integer timezone values.
5. Register at Google, get a key, and develop code to use Google’s Latitude API.
6. Add a GPS and use Yahoo’s Placefinder API to display the local address.
7. Add a GPS and use AskGeo's timezone API to set the time zone on the Internet LCD clock.

Please log in to post comments.