Python plugin: Bluetooth Beacon Presence Detection

Python and python framework

Moderator: leecollings

mrinner
Posts: 56
Joined: Tuesday 22 August 2017 20:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Belarus
Contact:

Python plugin: Bluetooth Beacon Presence Detection

Post by mrinner »

Plugin allows you control availability bluetooth beacons and supports multi-scanners for better place coverage.

Features:
- RSSI threshold on each beacon
- Timeout settings
- Supports several BLE scanners
- Battery check of each beacon with RSSI threshold + time to check + attempts to check in selected period of time

Plugin can be found here: https://github.com/mrin/domoticz-bt-presence

Consists of:
- Domoticz plugin: Manage beacons availability by timeout. Contains UDP server for receive data from several scanners.
- Bluetooth LE scanner: for listening of beacons, check battery level and sending info to the domoticz plugin through UDP.
.
ble.png
ble.png (16.23 KiB) Viewed 8149 times
Last edited by mrinner on Saturday 25 November 2017 20:18, edited 1 time in total.
zak45
Posts: 952
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by zak45 »

Great !!! ... have similar project under final phase :-)
I like your icon.. maybe use it.
schulpr
Posts: 137
Joined: Thursday 01 January 2015 9:10
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by schulpr »

Hi,

Thank you for the plugin, it works great for my two nut's. Is it possible to scan an iPhone too?

Kind regards, Rob
mrinner
Posts: 56
Joined: Tuesday 22 August 2017 20:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Belarus
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by mrinner »

@schulpr
Not yet, I want to add general bluetooth scanner, but I don't know (not tested yet) how it works together in parallel with LE scan...perhaps it will require separate bt dongle, but anyway, it's possible to do.
@zak45
Nice!
schulpr
Posts: 137
Joined: Thursday 01 January 2015 9:10
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by schulpr »

Hi mrinner,

I always thought that the iPhone (and also Android phones) also uses BLE but that it's necessary to connect them first to the Raspberry. I did try this but not successful up until now.

I hope someone can help with this. It would be very nice if it's possible to scan your phones too.

Regards, Rob
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by jvdz »

mrinner wrote: Saturday 25 November 2017 20:16 Not yet, I want to add general bluetooth scanner, but I don't know (not tested yet) how it works together in parallel with LE scan...perhaps it will require separate bt dongle, but anyway, it's possible to do.
@mrinner, Nice project!
I've been playing with your setup and an Android app called "Beacon Simulator" to generate Ibeacon and ALTBeacon messages. The issue with Android is that the MacAddress is rotated regularly so you can't use the MAcAddress to Identify the phone. Not sure how this works in the Apple world.
I've then played with your code in ble_scanner.py to decode the BT Packet and was able to retrieve the UUID for both iBeacon and AltBeacon types.
Then added the option to use either the MAC or UUID in the INI file to identify the device. This seems to be running fine for the moment. Just need to do much more testing to see if this is stable and what the battery consumption does.
Let me know when you are interested in this to incorporate in your setup as I have no intent to make this a separate GIT project.

Let me know in case there is anybody out there that knows how to do make an Ipad/Iphone and Ibeacon transmitter, so I can test the same mechanism with an Ipad I have.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
mrinner
Posts: 56
Joined: Tuesday 22 August 2017 20:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Belarus
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by mrinner »

@jvdz

I know about UUID as I tested it before plugin, but I didn't found good android app (don't know how about iOS) which can work in background as transmitter for beacons (starts after reboot phone, when closed accidentally). For me better to use beacon key-fob like Nut3 as more stable solution.
Maybe you know good stable app for beacon transmitting?
Anyway I can add this option in plugin. You can do pull request on github.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by jvdz »

Have you tested with "Beacon Simulator" and switched on the option "Broadcast resilience" in the settings?
It looks like it will stay up, but haven't done all possible scenario testing.
Ideally this would be an selectable option in the Domoticz APP. :-)

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
mrinner
Posts: 56
Joined: Tuesday 22 August 2017 20:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Belarus
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by mrinner »

@jvdz
Just checked. This app didn't started automatically, also when I close app manually they not auto-started in bg. I know it should be possible as other apps works great in this scenarios.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by jvdz »

Just tried rebooting and killing it manually and it did restart fine for me. You did make the Setup change as indicated?

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
mrinner
Posts: 56
Joined: Tuesday 22 August 2017 20:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Belarus
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by mrinner »

@jvdz

Yes, "Broadcast resilience" is ON. Also app added to whitelist (without energy saving). Maybe app doesn't work correctly with my phone... Android 7.0, MIUI 9 (Xiaomi Mi5).
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by jvdz »

I am running it on a Samsung s7 edge with standard android 7.
Working like a charm for the moment but will test a couple of days with the whole setup.
I have installed the service now on 2 RPI's and am running your plugin on my test Domoticz RPI to which both report.
Will make a pull request when all is stable indeed and have cleaned up the code with some better logging levels and logging to file.

Thanks for your efforts!
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by EddyG »

After installing I get the following 'error': Listen directive received, action initiation failed.
The ble_scanner.py interactive and as service works fine it detects my nut3 tags.
But nothing is recieved by Domoticz which runs as root. Everything runs as root.
Questions:
1. What means 'Port':'0'
2. What means Protocol set to: 'None'.
3. Why: Transport set to: 'UDP/IP', 127.0.0.1:2221. in the above mode2 is the correct IP-address. I checked the python variables and those are correct IP and Port.

I am using the latest Bluetooth daemon, and that works fine with my current BT presence detection.
My setup:
Version: 3.8153
Platform: Linux domoticz 4.9.35-v7+ #1014 SMP Fri Jun 30 14:47:43 BST 2017 armv7l GNU/Linux update to the lastest Debian version
Plugin/Hardware: Raspberry PI 3 with RFXCOM, RazBerry, etc....

What's the solution?

Code: Select all

(BLE Presence) Initialized version 0.0.1, author 'mrin'
2017-12-02 16:45:46.004  (BLE Presence) Debug log level set to: 'true'.
2017-12-02 16:45:46.004  (BLE Presence) 'Key':'bt-beacon-presence'
2017-12-02 16:45:46.004  (BLE Presence) 'Name':'BLE Presence'
2017-12-02 16:45:46.004  (BLE Presence) 'Version':'0.0.1'
2017-12-02 16:45:46.004  (BLE Presence) 'Port':'0'
2017-12-02 16:45:46.004  (BLE Presence) 'Mode2':'192.168.x.xxx:2221'
2017-12-02 16:45:46.004  (BLE Presence) 'HardwareID':'12'
2017-12-02 16:45:46.004  (BLE Presence) 'HomeFolder':'/home/pi/domoticz/plugins/bt-presence/'
2017-12-02 16:45:46.004  (BLE Presence) 'Author':'mrin'
2017-12-02 16:45:46.004  (BLE Presence) 'Mode1':'xx:xx:xx:xx:xx:xx|20, xx:xx:xx:xx:xx:xx|20, xx:xx:xx:xx:xx:xx|20'
2017-12-02 16:45:46.005  (BLE Presence) 'Mode6':'Debug'
2017-12-02 16:45:46.005  (BLE Presence) Device count: 3
2017-12-02 16:45:46.005  (BLE Presence) Device:............ etc.
2017-12-02 16:45:46.006  (BLE Presence) Device LastLevel: 0
2017-12-02 16:45:46.064  (BLE Presence) Protocol set to: 'None'.
2017-12-02 16:45:46.064  (BLE Presence) Transport set to: 'UDP/IP', 127.0.0.1:2221.
2017-12-02 16:45:46.064  (BLE Presence) Listen directive received, action initiation failed.
2017-12-02 16:45:46.064  (BLE Presence) Heartbeat interval set to: 2.
mrinner
Posts: 56
Joined: Tuesday 22 August 2017 20:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Belarus
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by mrinner »

@EddyG

1. This is domoticz debug, plugin not use "Port" variable
2. This means binary communication, without any parsing from domoticz side. Just required to set None (as string :) ) by plugin system documentation
3. Really don't know for now as I created and test plugin with latest beta... Will try to check plugin with stable release, maybe tomorrow as I don't have dev machine right now.

Also plugin need to be fixed to support dz stable release as I see some API diffs with beta.
mrinner
Posts: 56
Joined: Tuesday 22 August 2017 20:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Belarus
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by mrinner »

@EddyG

Checked today with 3.8153
'UDP/IP', 127.0.0.1:2221
The same on stable release.

Also added small fix for support old api domoticz plugin frameworj of stable release.
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by EddyG »

I downloaded the plugin.py, restarted domoticz, version 0.0.2 is now present.
But nothing changed still 127.0.0.1:2221 with Listen directive received, action initiation failed.

Nothing comes in from the ble_scanner service (and than one works)

Any ideas?

I suppose that this plugin will not run on the latest stable. :(

I put Domoticz.Debug('Host %s - Port %s' % (host, port)) directly after the split
Output: Host 192.168.2.xxx - Port 2221

And the same output is present after the Domoticz.Connection

and i Put Domoticz.Debug('Host %s - Port %s' % (self.udpConn.Address, self.udpConn.Port)) directly after .Listen()
Output: Host 127.0.0.1 - Port 2221

So the Listen() call changed the IP-address. :?:
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by EddyG »

Found it!!! :!: :!: :!:
The function self.udpConn.Listen() changed to: self.udpConn.Connect() that's the differance between beta and latest stable.

Now I get: Connect directive received, action initiated successfully.

But still no updates from the scanner, will look further, if it is solvable.
mrinner
Posts: 56
Joined: Tuesday 22 August 2017 20:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Belarus
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by mrinner »

@EddyG

Check the old version of manual for plugin framework:
https://www.domoticz.com/wiki/index.php ... oldid=9058

udpConn.Connect() is not right way, plugin should just listen incoming UDP packates. You cannot connect to udp:) this is not tcp/ip.

Also check the domoticz stable source code, 127.0.0.1 is hardcoded.
https://github.com/domoticz/domoticz/bl ... .cpp#L1115
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by EddyG »

Tnx for the info.
So it will never work with the latest stable because I also want a second Raspberry Pi as a BLE-scanner.
B.t.w. I also tried everything locally with address 127.0.0.1 but that did not work either.
Funny thing on UDP: Connect directive received, action initiated successfully.
mrinner
Posts: 56
Joined: Tuesday 22 August 2017 20:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Belarus
Contact:

Re: Python plugin: Bluetooth Beacon Presence Detection

Post by mrinner »

Seems there is only 1 way to support stable domoticz -> change connection type to TCP/IP for plugin and ble_scanner. UDP used as more easy way without any connections/reconnections etc...
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest