Scripting Geozones
Checking whether device is inside Geozone
To check if device is inside geozone - use simple get_val function. If it's inside or outside geozone, then do something.
Simple Geozone check
main()
{
while(1)
{
Delay(500);
if (get_val(Sensor_SignalGeozoneInside) == 1) //Check if device is inside geozone
{
set_val(Sensor_OUT2, 1); //Do something if inside geozone
}
else
{
set_val(Sensor_OUT2, 0); //Do something if outside geozone
}
}
}Checking multiple Geozones
In the next example we're going to show you how to use Geozones, while using SMS function. First we define our variables and Geozones.
Next we'll read and remember Geozone status, this is made so that device wouldn't constantly send messages every half of second, only when Geozone status changes.
Lastly we're checking whether device entered Geozone or left Geozone:
Multiple Geozones Main
#define HOME 1 //Geozone ID's
#define WORK 2
new lastGeozoneState = 0;
new tempGeozoneID = 0;
new lastGeozoneID = 0;
main()
{
lastGeozoneState = get_val(Sensor_SignalGeozoneInside); //Remember last state of geozone
while(1)
{
Delay(500);
if (get_val(Sensor_SignalGeozoneInside) != lastGeozoneState) //Check if device geozone state changed
{
lastGeozoneState = get_val(Sensor_SignalGeozoneInside);
if (lastGeozoneState == 1) //If module entered geozone
{
tempGeozoneID = get_val(Sensor_GeozoneID); //Find which geozone device has entered
switch (tempGeozoneID)
{
case HOME:
{
write_buf(SMStext, 0, "+37060000XX", "Welcome home");
}
case WORK:
{
write_buf(SMStext, 0, "+37060000XX", "Welcome to workplace");
}
}
}
else
{
switch (lastGeozoneID)
{
case HOME:
{
write_buf(SMStext, 0, "+37060000XX", "Have a nice ride");
}
case WORK:
{
write_buf(SMStext, 0, "+37060000XX", "Enjoy your free time");
}
}
}
lastGeozoneID = get_val(Sensor_GeozoneID);
}
}
}Geozone inside Geozone
Optionally user could ask directly in which Geozone their vehicle is, by using function "GetGeozoneStatus();". This allows for multi-functionality of our device, since not only user can check directly whether device is inside that Geozone, but also it's easy to detect whether vehicle is inside multiple Geozones.
For example, we want to get a message when a vehicle enters the city and we want to get a message when vehicle arrives at the final destination. This means that vehicle is going to be in multiple Geozones. By using "GetGeozoneStatus();" function it's easy to do that:
Geozone inside Geozone
new statusGeozone1;
new statusGeozone2;
new statusGeozone3;
new statusGeozone4;
new string[160];
checkIfInNewGeozone()
{
if (GetGeozoneStatus(1) != statusGeozone1) //check if the state has changed
{
statusGeozone1 = GetGeozoneStatus(1); //if state has changed, save new value
if (statusGeozone1 == 0) //if state is 0, means that device is leaving geozone
{
write_buf(SMS, 0, "+370608XX007", "Leaving geozone 1");
}
else if (statusGeozone1 == 1) //if state is 1, means that device is entering geozone
{
write_buf(SMS, 0, "+370608XX007", "Entering geozone 1");
}
else //in this case the return will be 2 and that means that there's no such geozone
{
//no such geozone
}
}
if (GetGeozoneStatus(2) != statusGeozone2) //check if the state has changed
{
statusGeozone2 = GetGeozoneStatus(2); //if state has changed, save new value
if (statusGeozone2 == 0) //if state is 0, means that device is leaving geozone
{
write_buf(SMS, 0, "+370608XX007", "Leaving geozone 2");
}
else if (statusGeozone2 == 1) //if state is 1, means that device is entering geozone
{
write_buf(SMS, 0, "+370608XX007", "Entering geozone 2");
}
else //in this case the return will be 2 and that means that there's no such geozone
{
//no such geozone
}
}
if (GetGeozoneStatus(3) != statusGeozone3) //check if the state has changed
{
statusGeozone3 = GetGeozoneStatus(3); //if state has changed, save new value
if (statusGeozone3 == 0) //if state is 0, means that device is leaving geozone
{
write_buf(SMS, 0, "+370608XX007", "Leaving geozone 3");
}
else if (statusGeozone3 == 1) //if state is 1, means that device is entering geozone
{
write_buf(SMS, 0, "+370608XX007", "Entering geozone 3");
}
else //in this case the return will be 2 and that means that there's no such geozone
{
//no such geozone
}
}
if (GetGeozoneStatus(4) != statusGeozone4) //check if the state has changed
{
statusGeozone4 = GetGeozoneStatus(4); //if state has changed, save new value
if (statusGeozone4 == 0) //if state is 0, means that device is leaving geozone
{
write_buf(SMS, 0, "+370608XX007", "Leaving geozone 4");
}
else if (statusGeozone4 == 1) //if state is 1, means that device is entering geozone
{
write_buf(SMS, 0, "+370608XX007", "Entering geozone 4");
}
else //in this case the return will be 2 and that means that there's no such geozone
{
//no such geozone
}
}
}
main()
{
statusGeozone1 = GetGeozoneStatus(1); //save value before checking
statusGeozone2 = GetGeozoneStatus(2); //save value before checking
statusGeozone3 = GetGeozoneStatus(3); //save value before checking
statusGeozone4 = GetGeozoneStatus(4); //save value before checking
while(1)
{
checkIfInNewGeozone();
Delay(1000);
}
}Also, it's possible to send SMS with a list of all the Geozones vehicle is at that moment:
Geozone inside Geozone 2
new statusGeozone1;
new statusGeozone2;
new statusGeozone3;
new statusGeozone4;
new statusGeo1;
new statusGeo2;
new statusGeo3;
new statusGeo4;
new string[160];
checkInWhichGeozones()
{
statusGeo1 = GetGeozoneStatus(1);
statusGeo2 = GetGeozoneStatus(2);
statusGeo3 = GetGeozoneStatus(3);
statusGeo4 = GetGeozoneStatus(4);
strcat(string,"You are in: ");
if(statusGeo1 == 1)
{
strcat(string,"Geozone1, ");
}
if(statusGeo2 == 1)
{
strcat(string,"Geozone2, ");
}
if(statusGeo3 == 1)
{
strcat(string,"Geozone3, ");
}
if(statusGeo4 == 1)
{
strcat(string,"Geozone4, ");
}
strdel(string, strlen(string)-2, strlen(string));
write_buf(SMS, 0, "+37069308XXX", string);
}
main()
{
statusGeozone1 = GetGeozoneStatus(1);
statusGeozone2 = GetGeozoneStatus(2);
statusGeozone3 = GetGeozoneStatus(3);
statusGeozone4 = GetGeozoneStatus(4);
while(1)
{
checkInWhichGeozones();
Delay(10000);
}
}