-
-
Notifications
You must be signed in to change notification settings - Fork 60
Tutorial SensorClientGPS
(For version 1.000)
This tutorial describes the basic configuration of the AlertR Sensor Client GPS. It can be used as foundation to develop an own AlertR system setup. This tutorial assumes you have set up the AlertR Server according to the provided Server Tutorial.
The client is described as the following by the installation:
sqall@towel:~$ ./alertRinstaller.py -l
[...]
AlertR Sensor Client GPS
------------------------
Instance:
sensorClientGPS
Type:
sensor
Version:
0.901-0
Dependencies:
1: requests (pip packet: requests) (lowest version: 2.20.0)
2: Crypto (pip packet: pycryptodome) (lowest version: 3.10.1)
3: shapely (pip packet: Shapely) (lowest version: 1.7.1)
Description:
This client handles the GPS position of a device as a sensor. It allows you to build a geofence which triggers an alarm as soon as the device leaves it (or enters it). With this you can build home automation (e.g., turn on the thermostats as soon as I am coming home) as well as alarm systems for movable objects (e.g., car leaves the "home zone" in which it normally drives). Currently, only ChasR is supported as GPS provider (https://alertr.de/chasr).
[...]
In order to install the AlertR Sensor Client GPS, we first have to fulfill all prerequisites. On a Debian/Ubuntu Linux you can install everything with the following commands:
root@towel:/home/sqall# apt-get install python3-pip
root@towel:/home/sqall# pip3 install requests pycryptodome Shapely
It is always a good idea to have an own user for a daemon. Therefore, we create a user that runs the AlertR Sensor Client GPS called alertr
:
root@towel:/home/sqall# adduser --disabled-password alertr
Adding user `alertr' ...
Adding new group `alertr' (1002) ...
Adding new user `alertr' (1002) with group `alertr' ...
Creating home directory `/home/alertr' ...
Copying files from `/etc/skel' ...
Changing the user information for alertr
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
To switch into this user in the terminal you can use the following command:
root@towel:/home/sqall# su alertr
Please note that this is only possible as root
user. If you are trying this command as another user you will get a password prompt for the user alertr
, which does not have a password.
Afterwards, you can use the installation script provided by AlertR to install the client.
alertr@towel:~$ wget https://raw.githubusercontent.com/sqall01/alertR/master/alertRinstaller.py
alertr@towel:~$ mkdir sensorClientGPS
alertr@towel:~$ chmod 755 alertRinstaller.py
alertr@towel:~$ ./alertRinstaller.py -i sensorClientGPS -t ./sensorClientGPS/
This tutorial monitors a GPS device and uses geofencing to create an alarm if the device is outside its allowed areas. The end-to-end encrypted GPS tracking system ChasR is used as GPS provider. In order to use it, you have to register an account at alertr.de. Without the account, you cannot use this service or have to host it yourself. The ChasR tracking application is available for Android devices as well as daemon for Linux systems.
The scenario in this tutorial is that we monitor the position of our car. An article on how to use an old mobile phone for tracking your car is here available. Please note that the settings in this tutorial are just fictional and can be adapted to any other scenario.
We create two geofences for this tutorial:
-
An area that shows if the car is parked at our home.
-
An area that covers the places our car usually resides in.
The first geofence can be used for automation (e.g., change the temperature on your thermostats as soon as you come home) and the second geofence can be used as a car alarm system.
A great website to build your geofence and export the GPS coordinates can be found here.
The first geofence to monitor if our car is parked at home has the following coordinates:
52.52017719408913, 13.384780883789064
52.520500343932724, 13.385542631149294
52.51964513411749, 13.388434052467348
52.51943296116605, 13.384974002838137
This area looks visually like this (this is a screenshot from the website mentioned above):
The second geofence that we use as a car alarm system consists of two areas:
Area1
52.49657756892365, 13.26805114746094
52.535855381130304, 13.279724121093752
52.55694226242951, 13.42357635498047
52.507027222951635, 13.488464355468752
52.48633449800855, 13.385810852050783
Area2:
52.507027222951635, 13.488464355468752
52.48633449800855, 13.385810852050783
52.4101684069425, 13.387527465820314
52.43612988923729, 13.51524353027344
In this scenario, we usually drive our car inside Berlin but sometimes we are driving it to the south, hence the two areas. Visually it looks like the following:
The following describes the configuration of the AlertR Sensor Client GPS. It shows a basic configuration that can be used as a template for own installations.
For security reasons, it is strongly recommended to use a TLS connection for your AlertR system. Hence, all tutorials will use TLS. However, for testing purposes AlertR gives you the option to disable TLS. If you do so, you have to disable it for the complete AlertR system.
During the installation of the AlertR server, you created a certificate file with the name alertr_server.crt
. This file is needed by the client in order to verify the connection. Please copy it to the host you are installing this client on. This tutorial assumes that you have stored the certificate file in the following location: /home/alertr/sensorClientGPS/config/alertr_server.crt
.
The client has to authenticate itself before it can work with the server. Therefore, we need to set up user credentials on the AlertR server for this client. We execute the following command with the manageUsers.py
script of the server which is located in the installation directory of the server (see Users Management for further information):
alertr@towel:/home/alertr/server# ./manageUsers.py -a -u user_gps -p password_gps -t sensor -i sensorClientGPS
Please make sure that the AlertR Server is not running while adding a user.
Otherwise it can lead to an inconsistent state and a corrupted database.
Are you sure to continue?
(y/n): y
This tutorial assumes the Alert Levels configured in the AlertR Server tutorial. The client will use the following for its configuration:
Alert Level | Name | Profiles | Functionality |
---|---|---|---|
0 | Dummy | Activated, Deactivated | Used for sensors that do not trigger. |
1 | Silent Notification | Activated, Deactivated | Used to silently notify the user. |
Alert Level 0 is used for sensors that are not meant to trigger a Sensor Alert, but are still used for informational purposes.
Alert Level 1 is used for sensors that trigger a Sensor Alert, but not an urgent one. This Alert Level is member of every System Profile and thus triggers always.
Every AlertR installation has a template configuration file with detailed comments that describe the functionality of each option. To use it as a draft, you can just copy the template file and modify your new configuration file.
alertr@towel:~/sensorClientGPS/config$ cp config.xml.template config.xml
alertr@towel:~/sensorClientGPS/config$ chmod 700 config.xml
The configuration file itself is split into the following parts:
- General
- SMTP
- Update
- Sensors
In this tutorial, we are going through each of these parts separately and describe why we configured it like that.
The general section is used for options such as certificate file location. In our tutorial configuration, the section looks like this:
<general>
<log
file="./logfile.log"
level="INFO" />
<server
host="localhost"
port="44556" />
<ssl
enabled="True">
<server
caFile="./config/alertr_server.crt" />
<client
certificateRequired="False"
certFile="/path/to/client.crt"
keyFile="/path/to/client.key" />
</ssl>
<credentials
username="user_gps"
password="password_gps" />
<connection
persistent="True" />
</general>
Section log is used to configure the log file setup. The file attribute sets the location for the log file. Make sure it exists and is writable by the user that starts the AlertR client. Otherwise the client will not start. For example, the directory /var/log is usually only writable by the "root" user and users of the group "syslog". If you want the client log file in this directory, please create a sub-directory for the AlertR client log file and set the correct permissions on it. The level attribute sets the log level. Possible values are DEBUG, INFO, WARNING, ERROR, CRITICAL.
Section server is used to configure the server connection setup. The host and port attribute sets the address and port of the AlertR server. The port was previously configured in the server configuration file. The address can either be a name that can be resolved by the DNS system or an IP address.
Section ssl is used to configure the TLS/SSL setup. The enabled attributes gives you the option to disable or enable it. This setting has to be the same for your complete AlertR setup. It is strongly recommended to enable TLS/SSL and only disable it in a testing environment. In this tutorial, we enable TLS/SSL. The caFile attribute in the server section sets the location of the certificate file that is used to authenticate the server (see Certificate). Section client is used to configure the client connection setup. The certificateRequired attribute determines if the client needs a certificate to connect to the server or not. The certFile and keyFile are used to determine the client certificate and key file. Since this tutorial is not using client certificates, we do not have to set sane values here.
Section credentials is used to configure the login credentials of this client. The credentials were configured in a previous step of this Tutorial and must be the same as in this configuration file.
Section connection is used to configure the connection to the server. The persistent attribute is used to configure if the connection to the server has to be persistent. This means that if the client disconnects, a Sensor Alert is created by the server.
The smtp section is used for options that configure the used SMTP server. In our tutorial configuration, the section looks like this:
<smtp>
<general
activated="True"
fromAddr="sensorClientGPS@alertr.de"
toAddr="myalarmaddress@example.org" />
<server
host="127.0.0.1"
port="25" />
</smtp>
Section general is used to configure the basic options of the eMail notification. The eMail notification is used if problems on the AlertR client occur that can not be solved (like connection problems). The activated attribute determines if the eMail notification is activated or not. If it is deactivated, the rest of the options in the whole smtp section are ignored. The fromAddr attribute sets the used eMail address from which the eMail notification is sent. The toAddr attribute determines the eMail address the notification is sent to.
Section server is used to configure the SMTP server that is used to send the eMail notification. The host attribute sets the address and the port attribute sets the port of the used SMTP server. At the moment, only "127.0.0.1" is allowed as address and port 25. This means you have to set up a local SMTP server in order to send eMail notifications. Personally, I would suggest to set up a local Postfix SMTP server that can forward eMails like in this Tutorial. Please note that the local SMTP server has to accept eMails from localhost without any authentication since it is not supported by AlertR yet.
The update section is used to give the online repository for updates. In our tutorial configuration, the section looks like this:
<update>
<server
url="https://raw.githubusercontent.com/sqall01/alertR/master/" />
</update>
Section server is used to configure the remote update repository. Normally, the default configuration can be used here (which is this repository). The url attribute gives the remote server location of the repository. Only the https
protocol is allowed here if you want to change it.
The sensors section configures the sensors of this client. In our tutorial configuration, the section looks like this:
<sensors>
<sensor>
<general
id="0"
description="Car@Home"
alertDelay="0"
triggerAlert="True"
triggerAlertNormal="True" />
<alertLevel>0</alertLevel>
<gps
interval="30"
triggerWithin="False"
provider="chasr"
device="my_car"
username="alertr_account@example.org"
password="my_account_password"
secret="my_encryption_secret">
<polygon>
<position
lat="52.52017719408913"
lon="13.384780883789064" />
<position
lat="52.520500343932724"
lon="13.385542631149294" />
<position
lat="52.51964513411749"
lon="13.388434052467348" />
<position
lat="52.51943296116605"
lon="13.384974002838137" />
</polygon>
</gps>
</sensor>
<sensor>
<general
id="1"
description="Car Home Zone"
alertDelay="0"
triggerAlert="True"
triggerAlertNormal="True" />
<alertLevel>1</alertLevel>
<gps
interval="30"
triggerWithin="False"
provider="chasr"
device="my_car"
username="alertr_account@example.org"
password="my_account_password"
secret="my_encryption_secret">
<!-- Area 1 -->
<polygon>
<position
lat="52.49657756892365"
lon="13.26805114746094" />
<position
lat="52.535855381130304"
lon="13.279724121093752" />
<position
lat="52.55694226242951"
lon="13.42357635498047" />
<position
lat="52.507027222951635"
lon="13.488464355468752" />
<position
lat="52.48633449800855"
lon="13.385810852050783" />
</polygon>
<!-- Area 2 -->
<polygon>
<position
lat="52.507027222951635"
lon="13.488464355468752" />
<position
lat="52.48633449800855"
lon="13.385810852050783" />
<position
lat="52.4101684069425"
lon="13.387527465820314" />
<position
lat="52.43612988923729"
lon="13.51524353027344" />
</polygon>
</gps>
</sensor>
</sensors>
Each configured sensor is set up in its own <sensor>...</sensor> context. The context has three main sections: general, alertLevel and gps. The following describes the settings of each section.
Section general is used to configure the basic sensor settings. The id attribute gives the local id of the configured sensor. This id has to be unique for each sensor of this client. Since we configure two sensors, we give them the id 0 and 1. The description attribute gives a short description of the sensor. The alertDelay attribute sets how long in seconds the server has to wait until a Sensor Alert is triggered. Normally, this is set to 0 to immediate react on a Sensor Alert. But in some situations it might be necessary to react on a Sensor Alert after a certain time and not directly. For the sensors in our tutorial we set this value to 0. The triggerAlert attribute sets if the sensor triggers a Sensor Alert with the state "triggered" and triggerAlertNormal sets if the sensor triggers a Sensor Alert with the state "normal". Both our sensors should trigger both Sensor Alerts.
The alertLevel sections set the Alert Level the sensor triggers. In our tutorial, one sensors triggers Alert Level 0 and one Alert Level 1. But it is not limited to only one Alert Level. If another Alert Level should be triggered, an additional alertLevel section is added with the corresponding Alert Level.
Section gps configures the GPS sensor settings. The interval attribute sets the interval in seconds in which new GPS data is fetched from the used provider. In our tutorial, both sensors should fetch new GPS data for processing every 30 seconds. The triggerWithin attribute sets if a Sensor Alert should be triggered if the GPS device is inside the given geofence or outside of it. In this case, both sensors trigger if the GPS device we are monitoring leaves the geofence. The provider attribute configures the used GPS provider. In our case, the end-to-end encrypted GPS tracking system ChasR is used. The device attribute sets the name of the GPS device we want to monitor. Both our sensors should monitor the same device named my_car
. The username and password attributes configure the credentials of your ChasR account. The secret attribute sets the secret used to encrypt the devices GPS data. It is needed to decrypt the GPS data fetched from the ChasR server in order to process it. The polygon attribute sets one polygon which represents a geofence for the GPS device you want to monitor. A polygon consists of at least three positions and multiple polygons can be configured for a sensor. A position needs to have the settings lat (latitude) and lon (longitude) configured. As described in the Setup section, in our example the first sensor is used to monitor if the car is parked at home and the second sensor monitors if the car resides inside its usual area (in our example scenario inside Berlin). The second sensor also configures not one, but two polygons in which the car can drive without triggering a Sensor Alert.
If you want the AlertR client to start automatically after a reboot of the host, you have to set it up. In this tutorial, I will give two examples on how to set up the autostart of the AlertR client depending on your system using systemd or just init.d.
An init.d script is provided by the AlertR installation. The only thing you have to do is to copy and configure it correctly. The following command copies it to the correct location, sets the permissions correctly and installs it:
root@towel:/home/alertr/sensorClientGPS/init.d_example# cp alertRsensorGPS.sh /etc/init.d/
root@towel:/etc/init.d# chown root:root alertRsensorGPS.sh
root@towel:/etc/init.d# chmod 755 alertRsensorGPS.sh
root@towel:/etc/init.d# update-rc.d alertRsensorGPS.sh defaults
Adding system startup for /etc/init.d/alertRsensorGPS.sh ...
/etc/rc0.d/K20alertRsensorGPS.sh -> ../init.d/alertRsensorGPS.sh
/etc/rc1.d/K20alertRsensorGPS.sh -> ../init.d/alertRsensorGPS.sh
/etc/rc6.d/K20alertRsensorGPS.sh -> ../init.d/alertRsensorGPS.sh
/etc/rc2.d/S20alertRsensorGPS.sh -> ../init.d/alertRsensorGPS.sh
/etc/rc3.d/S20alertRsensorGPS.sh -> ../init.d/alertRsensorGPS.sh
/etc/rc4.d/S20alertRsensorGPS.sh -> ../init.d/alertRsensorGPS.sh
/etc/rc5.d/S20alertRsensorGPS.sh -> ../init.d/alertRsensorGPS.sh
Next we have to configure the init.d script correctly. The following changes have to be made to the script:
[...]
# change USER to the user which runs the alertRclient
USER=alertr
# change DAEMON to the path to run the alertRclient
DAEMON=/home/alertr/sensorClientGPS/alertRclient.py
[...]
In order to work correctly, the AlertR client has to be executable. With the following, we change the permissions of the AlertR client:
alertr@towel:~/sensorClientGPS$ chmod 755 alertRclient.py
To start the client as daemon, we can now execute:
root@towel:/home/alertr/sensorClientGPS# /etc/init.d/alertRsensorGPS.sh start
The log file of the client (it is created in the directory you have configured in the General Section) should now show the following:
[...]
07/23/2016 10:26:16 INFO: [alertRclient.py] Initializing sensors.
07/23/2016 10:26:16 INFO: [alertRclient.py] Connecting to server.
07/23/2016 10:26:16 INFO: [alertRclient.py] Starting watchdog thread.
07/23/2016 10:26:16 INFO: [alertRclient.py] Starting update check thread.
07/23/2016 10:26:16 INFO: [alertRclient.py] Client started.
If your system uses systemd for its autostart, you have to configure the AlertR client a bit different. Copying the template and setting permissions is still the same:
root@towel:/home/alertr/sensorClientGPS/init.d_example# cp alertRsensorGPS.sh /etc/init.d/
root@towel:/etc/init.d# chown root:root alertRsensorGPS.sh
root@towel:/etc/init.d# chmod 755 alertRsensorGPS.sh
Configuring the init.d script correctly is also the same:
[...]
# change USER to the user which runs the alertRclient
USER=alertr
# change DAEMON to the path to run the alertRclient
DAEMON=/home/alertr/sensorClientGPS/alertRclient.py
[...]
Now, systemd has to be configured. For this we copy the template service file to the correct location and install the service:
root@towel:/home/alertr/sensorClientGPS/init.d_example# cp alertRsensorGPS.service /etc/systemd/system/
root@towel:/home/alertr/sensorClientGPS# systemctl enable alertRsensorGPS.service
The alertR client can be started with the following command:
root@towel:/home/alertr/sensorClientGPS# service alertRsensorGPS.sh status
The log file of the client (it is created in the directory you have configured in the General Section) should now show the following:
[...]
07/23/2016 10:26:16 INFO: [alertRclient.py] Initializing sensors.
07/23/2016 10:26:16 INFO: [alertRclient.py] Connecting to server.
07/23/2016 10:26:16 INFO: [alertRclient.py] Starting watchdog thread.
07/23/2016 10:26:16 INFO: [alertRclient.py] Starting update check thread.
07/23/2016 10:26:16 INFO: [alertRclient.py] Client started.
If you experience problems, please check the log file first. If it is not helpful, change the log level to DEBUG and check again. If no error can be seen, please start the AlertR client manually and check if an error occurs that is not printed into the log file. This can be done by just executing the AlertR client as the user that it normally runs with.
alertr@towel:~/sensorClientGPS$ ./alertRclient.py
If you still have problems and do not know how to solve them, you can ask on the community page on reddit or you can use the Github Issues.