Problem
There are many different ways to implement presence detection with openHAB2 in your smarthome. When you take a look at Google, you get many variations / techniques, e.g. about Raspberry Pi as Beacon, Geofancy, FritzBox, Ping, etc. I have tested many of them and each variant / technique has its own special advantages and disadvantages - Often it depends on GPS and the Internet connection and fails with the battery life of the smartphone.
Requirement
- Independent of smartphone
- Without active internet connection
- Flexibility within the network
- Inexpensive / efficient variant
- Compatibility
Approach
The Raspberry Pi 3 is an excellent Bluetooth low energy transmitter / receiver. In most cases, openHAB2 runs on the same Pi - so we can use existing hardware. You could use the Rasberry Pi as a beacon and use the smartphone with the appropriate app as the recipient. Promotes a variable via network and script to openHAB2. This is basically a good option but with some potential problems.
Why not use the Raspberry Pi 3 as a classic receiver? Many people doesn’t leave their house / apartment without their mobile phone but also without their keychain. With a cheap / efficient Bluetooth low energy transmitter which you wear on your keychain we were able to building a much more reliable variant.
Setup
The script is designed to be supported by PHP5 + and should also work with Apache2.
Update package list
sudo apt update
Install required packages
sudo apt install nginx php5-fpm php5-curl bluez
With the commands “hciconfig” and “hcitool” you can then try to activate the dongle and scan the tags:
hciconfig hci0 up
hcitool lescan
Output will look like this:
LE Scan ...
7C:2F:80:CE:EF:44 (unknown)
7C:2F:80:CE:EF:44 Gigaset G-tag
If this doesn’t work, please try Google - there are several tutorials and solutions. The working scan is the basic requirement.
Basically, you can configure PHP as you like. For a little more security you should disable the following:
sudo nano /etc/php5/fpm/php.ini
# ;cgi.fix_pathinfo=1 to cgi.fix_pathinfo=0
Restart PHP
sudo service php5-fpm restart
Nginx configuration (/etc/nginx/sites-available/default)
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/presence/;
index index.php index.html index.htm index.nginx-debian.html;
server_name [your domain or ip];
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Test nginx and restart
sudo nginx -t
sudo service nginx restart
The files from G-tagPresence must be stored under /var/www/html/presence/
Now we have to assign the right permissions for the file, which will later do the scan:
chmod 500 /var/www/html/presence/script/scanspecifictag.sh
sudo chown -cR root:root /var/www/html/presence/script
Next we allow the PHP script to execute the scan script. This is done with the help of the following command and adding the entry for www-data:
sudo visudo
www-data ALL = NOPASSWD:/var/www/html/presence/script/scanspecifictag.sh
You have to adjust the url/ip and path in /var/www/html/presence/scanspecifictag.php
If everything is correct we can test the script
/usr/bin/php /var/www/html/presence/scanspecifictag.php tagMac=7C:2F:80:CE:EF:44 item=PresenceGtag_1
If the test worked, we can set up a cronjob to query the presence of the tag regularly. Since I query several tags and the bluthooth service can only be used by a cron, I have created a file, which I call through the cronjob, which performs all queries after the series. See cronjob.sh
sudo crontab -e
*/1 * * * * sh /var/www/html/presence/cronjob.sh > /var/log/cron_gtag.log 2>&1
Note
Alternatively, the script can be called via a URL:
http://deinedomain.local/scanspecifictag.php?tagMac=7C:2F:80:CE:EF:44&item=PresenceGtag_1
Originally, I wrote parts of this script for using Geofancy or Raspberry Pi as a beacon. I have continued to implement this feature in this revised version. So it is possible with the following parameters in the URL to change the state of a switch:
http://öffentlichedomain.de/scanspecifictag.php?item=Presence_1&itemValue=OFF
With this feature one can use various apps (e.g., Locative for iOS) to check the status on the basis of e.g. To change geolocation. For this it is necessary to access the script via the internet. One of the points, which is error-prone.
Sorry for the bad english…