Geolocation LCD Display

geolcd

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.

/media/uploads/4180_1/ipv4-assignments-07.png
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:

http://www.ip2location.com
http://www.maxmind.com/app/locate_my_ip
http://www.ipligence.com/geolocation
http://whatismyipaddress.com

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.

Wiring

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
TD+P1
TD-P2
RD+P7
RD-P8

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
GNDGND
5V or 3.3V*VCC
VO to GND via 1k resistor
P15RS
GNDRW
P16E
N/CD0
N/CD1
N/CD2
N/CD3
P17D4
P18D5
P19D6
P20D7

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

Note

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 http://api.ipinfodb.com/v3/ip-city/?key=PUT_YOUR_API_KEY_HERE 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 http://mbed.org/users/4180_1/notebook/geolocation-lcd-display/


#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 http://mbed.org/users/4180_1/notebook/geolocation-lcd-display/
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
            string[i]=0;
            *j=*j+i+1;
            break;
        } 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.cls();
    lcd.printf("Net setup\n");
    EthernetErr ethErr = eth.setup();
    if (ethErr) {
        lcd.printf("Error %d", ethErr);
        return -1;
    }
    lcd.printf(" Net OK");
    wait(.5);
    lcd.cls();
    lcd.printf("IP Address\nGeolocation API");
    HTTPText txt;
    //iPinfoDB API  - get web page with location data
    //Insert your free key from www.ipinfo.com for the API in the URL below
    HTTPResult r = http.get("http://api.ipinfodb.com/v3/ip-city/?key=PUT_YOUR_API_KEY_HERE", &txt);
    if (r==HTTP_OK) {
        //got web page text data OK
        strcpy(buffer,txt.gets());
        wait(1);
        while (1) {
            j=0;
            //parse and display each of the API's location information strings on the LCD
            parse(buffer, &j, result);
            lcd.cls();
            lcd.printf("result: %s\n", result);
            if (result[0]!='O') { //needs valid key
                wait(1);
                lcd.cls();
                lcd.printf("Get Free API key");
                lcd.printf("www.iPinfoDB.com");
                return(-1);
            }
            wait(1);
            j++;
            parse(buffer, &j, ip);
            lcd.cls();
            lcd.printf("IP address: \n %s", ip);
            wait(2);
            parse(buffer, &j, country_abbr);
            lcd.cls();
            lcd.printf("Country code: \n %s", country_abbr);
            wait(2);
            parse(buffer, &j, country);
            lcd.cls();
            lcd.printf("Country: \n%s", country);
            wait(2);
            parse(buffer, &j, region);
            lcd.cls();
            lcd.printf("Region or State:%s", region);
            wait(2);
            parse(buffer, &j, city);
            lcd.cls();
            lcd.printf("City: \n%s", city);
            wait(2);
            parse(buffer, &j, zipcode);
            lcd.cls();
            lcd.printf("Zipcode: \n %s", zipcode);
            wait(2);
            parse(buffer, &j, latitude);
            sscanf(latitude,"%f",&flatitude);
            lcd.cls();
            lcd.printf("Latitude: \n %f", flatitude);
            wait(2);
            parse(buffer, &j, longitude);
            sscanf(longitude,"%f",&flongitude);
            lcd.cls();
            lcd.printf("Longitude: \n %f", flongitude);
            wait(2);
            parse(buffer, &j, timezone);
            sscanf(timezone,"%f",&ftimezone);
            lcd.cls();
            lcd.printf("Timezone: \n %f", ftimezone);
            wait(4);
        }
    } else {
        lcd.cls();
        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.

Example_Location_Data

OK;;98.88.128.174;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 mbed.org (IP=217.140.96.42 from nslookup).

Example_Data_for_mbed.org

OK;;217.140.96.42;UK;UNITED KINGDOM;ENGLAND;CAMBRIDGE;-;52.205;0.144;+00:00

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!

/media/uploads/4180_1/time-zone.png
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;;202.141.68.2;IN;INDIA;DELHI;NEW DELHI;-;28.6328;77.2195;+05:30



Update: http://mbed.org/users/tlisowski3/notebook/geolocation-and-ntp-clock-on-ulcd-144-g2/ has code for an easier to use Geolocation API. Example http://ip-api.com/csv



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.


Report

Please log in to post comments.