Skip to content

SmartHome – GPS data changes

Reading time 2 minutes

GPS data changes constantly while traveling. Recording these and, if necessary, updating other parameters that depend on them should be achieved here using SmartHome on HomeMatic or RaspberryMatic.

Of course, you could continuously determine the location and thus always access the weather data based on the location, but this would generate unnecessary online traffic and processor load. Therefore, only a change in position is checked and only when a change in location is detected is the weather data determined based on the current location and the data updated accordingly.

Adjustment of GPS data for comparison

Since GPS coordinates always fluctuate in the last two digits of the longitude and latitude information due to the intended inaccuracy of the GPS service providers, even if you remain unchanged at the same location, only four decimal places are taken into account for comparison. A change in location is only recognized when at least the fourth decimal place changes.

Script addition (GPS_Data)

The following script lines are: here discussed script GPS_Data attached. Lines starting with an exclamation mark represent comments. WriteLine statements are only used for output control and can be deleted.

The following must be created as new variables of type STRING:

  • W_Lat_short_akt (To save the current geographical latitude shortened to four decimal places)
  • W_Lon_short_akt (To save the current geographical length shortened to four decimal places)
  • W_Lat_short_alt (To save the old geographical latitude shortened to four decimal places)
  • W_Lon_short_alt (To save the old geographical length shortened to four decimal places)

script

! Save geodata shortened to 4 decimal places (ToString(4)).

var lat_short_akt = lat.ToString(4);
WriteLine(“lat_short_akt”);WriteLine(lat_short_akt);
dom.GetObject('W_Lat_short_akt').State(lat_short_akt);

var lon_short_akt = lon.ToString(4);
WriteLine(“lon_short”);WriteLine(lon_short_akt);
dom.GetObject('W_Lon_short_akt').State(lon_short_akt);

! Comparison of current and old locations

var W_lat_short_alt = dom.GetObject(“W_Lat_short_alt”).Value();WriteLine(W_lat_short_alt);
var W_lon_short_alt = dom.GetObject(“W_Lon_short_alt”).Value();WriteLine(W_lon_short_alt);

if ((lat_short_akt <> W_lat_short_alt) && (lon_short_akt <> W_lon_short_alt))
{
dom.GetObject(“W_Lat_short_alt”).State(lat_short_akt);
dom.GetObject(“W_Lon_short_alt”).State(lon_short_akt);

! Composing the URL with the current ones. GPS data – the AppID “.. xxx ..” must be filled with your own ID

var url = “https://api.openweathermap.org/geo/1.0/reverse?lat=”#Latitude#”&lon=”#Longitude#”&limit=5&appid=[xxxxxxxxxxxxxxxxxxxxxxxx]”;
WriteLine(“new URL for new geodata”);WriteLine(url);

! Sending the URL request

var posValueStart;
var posValueEnd;
var pos;
var data;
var posStart;
var posEnd; string stderr; string stdout;
system.Exec(“wget -q -O – ‘“#url#“‚”, &stdout, &stderr);
WriteLine(stdout);

! Extracting the data

! City_name

pos = 0;
posStart = 'name“:“‚;
posEnd = '","local_names';
posValueStart = stdout.Find(posStart) + posStart.Length();
posValueEnd = stdout.Find(posEnd)-posValueStart;
string city_name = stdout.Substr(posValueStart, posValueEnd);
WriteLine(“city_name”);WriteLine(city_name);
dom.GetObject(“W_city_name”).State(city_name);

! Country code

pos = 0;
posStart = 'country":"';
posEnd = '","state';
posValueStart = stdout.Find(posStart) + posStart.Length();
posValueEnd = stdout.Find(posEnd)-posValueStart;
string country_code = stdout.Substr(posValueStart, posValueEnd);
WriteLine(“country_code”);WriteLine(country_code);
dom.GetObject(“W_country_code”).State(country_code);

! region

pos = 0;
posStart = 'state“:“‚;
posEnd = '"}]';
posValueStart = stdout.Find(posStart) + posStart.Length();
posValueEnd = stdout.Find(posEnd)-posValueStart;
string W_region = stdout.Substr(posValueStart, posValueEnd);
WriteLine(“W_region”);WriteLine(W_region);
dom.GetObject(“W_region”).State(W_region);

}

! The ELSE branch can be omitted and is only used to verify that the script has been run to the end if the location has not changed

else
{
WriteLine(“Data equals”);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish