Since you don’t really care about the IP address, and really just want a way to determine timezone, I found a great piece of code from @ghawkins in this thread
http://forums.electricimp.com/discussion/2376/feature-request-device-getipaddress#Item_5
That is an automatic way to find your geolocation using the google map api, based on the RSSI of the routers in your vicinity - more than a little creepy but it works well. Caveat is that it might not work in rural areas so you will need to test to see if it works for you.
Geolocation determines your timezone, so I have modified the code from @ghawkins to add timezone determination, using the Google Timezone API.
On the agent side:
`
function addColons(bssid) {
local result = bssid.slice(0, 2);
for (local i = 2; i < 12; i += 2) {
result += ":" + bssid.slice(i, (i + 2));
}
return result;
}
device.on(“location”, function (location) {
local url = "https://maps.googleapis.com/maps/api/browserlocation/json?browser=electric-imp&sensor=false";
foreach (network in location) {
url += ("&wifi=mac:" + addColons(network.bssid) + "|ss:" + network.rssi);
server.log( network.bssid + " = " + network.rssi );
}
local request = http.get(url);
local response = request.sendsync();
if (response.statuscode == 200) {
local data = http.jsondecode(response.body);
local timezoneURL = "https://maps.googleapis.com/maps/api/timezone/json?location=" + data.location.lat + "," + data.location.lng+"×tamp="+time()+"&sensor=false";
request = http.get( timezoneURL );
response = request.sendsync();
if (response.statuscode == 200) {
data = http.jsondecode( response.body );
foreach( i,v in data ) {
server.log(i + ":" + v);
}
server.log("Local time is " +getLocalTime( data.rawOffset + data.dstOffset ) );
device.send("timezoneoffset", data.rawOffset + data.dstOffset );
}
else
{
server.log("Response Error " + response.statuscode + " " + response.body );
}
}
else
{
server.log("Response Error " + response.statuscode + " " + response.body );
}
});
function getLocalTime( offset ) {
local dateTime = date( time() + offset,“u”);
local timeStamp = format("%02d-%02d-%02d %02d:%02d:%02d",dateTime.year,dateTime.month+1,dateTime.day, dateTime.hour, dateTime.min, dateTime.sec );
return timeStamp;
}
`
And on the device side:
`
timezoneOffset <- 0;
agent.send(“location”, imp.scanwifinetworks());
agent.on(“timezoneoffset”, function(offset) {
timezoneOffset = offset;
server.log("Imp time is : " + getTimeStamp() );
});
function getTimeStamp( ) {
local dateTime = date( localTime() );
local timeStamp = format("%02d-%02d-%02d %02d:%02d:%02d",
dateTime.year,dateTime.month+1,dateTime.day, dateTime.hour, dateTime.min, dateTime.sec );
return timeStamp;
}
function localTime(){
return time() + timezoneOffset;
}
`
That is better than having to figure out the IP address from a client HTML request that I proposed earlier. We can assume google APIs will be there for a while…