Skip to content
Charles Haynes edited this page Aug 9, 2021 · 3 revisions

Your current location is useful for getting the local weather, and for knowing your local timezone. The Watchy example allows you to set your location in the config.h file, but it's often useful to be able to set your location dynamically. Watchy doesn't have any location hardware like a GPS, but you can sometimes get a good enough approximation by geolocating your IP address.

ip-api.com

There's a free IP geolocation service at http://ip-api.com that will look up your IP's location and return information about it.

The weather provider that the default Watchy sketch uses, OpenWeather, will give results based on city name, but that can occasionally be ambiguous if your city name is common. I find that latitude and logitude work better but ip-api will return either one.

To use ip-api.com you just do a GET to http://ip-api.com/json/24.48.0.1?fields=49600 and it will return a JSON string of the form {"status":"success","lat":-37.8226,"lon":144.932,"timezone":"Australia/Melbourne"}

Besides your location, ip-api can return the Timezone name for your location which is useful for displaying local time. Unfortunately the timezone name can't be used directly. It first has to be converted into the "posix timezone" specification used internally by the time routines. There is a free (libre) service to convert it for you at timezoned.rop.nl. This service listens on UDP port 2342 and expects a packet containing a timezone name. It replies with a packet containing OK <the timezone name> <the posix timezone spec> you can use the posix timezone spec directly in setenv("TZ",...) and tzset()

other geolocation providers

  • Geonames includes timezone names, no cost, registration required
  • Google can include timezone names, not free, registration required The Google Geolocation API can take a list of visible Wifi access point SSIDs to return a more accurate location. It's not free and requires registration, but the cost is quite low (less than one cent each.)
  • timezonedb.com convert lat, lon to timezone name (Olson), no cost, registration required