GeoLocation

This example sketch queries the freegeoip.net service to get the local approximate geographic location based on IP address.

Combined with code in the ntpTest or InternetTime sketches, this can give absolute position and time, extremely useful for seasonal calculations like sun position, insolation, day length, etc. One could always add a GPS module or just plug in values from your GPS or phone, but for applications where extreme accuracy isn't required, this has the luxury of coming 'free' with the CC3000 already in use.

Positional accuracy depends on the freegeoip.net database, in turn based on data collected by maxmind.com. No guarantees this will work for every location. This software is provided as-is.
Position should be polled only once, at startup, or very infrequently if making a mobile network-hopping thing, so as not to overwhelm the kindly-provided free geolocation service.

Before you run the sketch, edit it to replace the dummy SSID and password with your own:
#define WLAN_SSID       "yourNetwork"   // cannot be longer than 32 characters!
#define WLAN_PASS       "yourPassword"
Also, make sure that the right wireless security scheme is selected (unsecured, WEP, WPA, or WPA2)
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2
Here's a sample of the Serial Monitor output of GeoLocation. You should see something similar:
Hello, CC3000!
Free RAM: 837
Initializing...OK.
Connecting to network...Started AP/SSID scan


Connecting to Turlingdrome...Waiting to connect...connected!
Requesting address from DHCP server...OK

IP Addr: 192.168.0.4
Netmask: 255.255.255.0
Gateway: 192.168.0.1
DHCPsrv: 192.168.0.1
DNSserv: 192.168.0.1

Getting server IP address...192.151.154.154
Connecting to geo server...
Connect to 192.151.154.154:80
connected.
Requesting data...
Reading response...OK

Disconnecting

RESULTS:
  Country: United States
  Region: California
  City: Richmond
  Longitude: -122.35
  Latitude: 37.94

This guide was first published on Sep 16, 2013. It was last updated on Mar 08, 2024.

This page (GeoLocation) was last updated on Sep 12, 2013.

Text editor powered by tinymce.