How to check presence of Beacon ?

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.
User avatar
emme
Posts: 508
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: How to check presence of Beacon ?

Post by emme » Tuesday 21 February 2017 17:55

welcome to my world!!! ;) ;)

there are weeks I'm struggling (and my domobuddies either) on this issue!!!

First of all the uuid for battery is 0x2A19 (0x180F is the Battery service, but the percentage is the 2 bit at 2a19 ;) )
thena... it looks like the nut mini does not reply to standard calls OR... gatttool is not able to...

I tries to use Bluez stack from 5.3 to 5.48 (latest) with no success....
based on almost all forums... it looks it is a kernel issue and the best stack to use was bluez 5.11
...but it does not support the internal bt chip (brc43xx)
I was unable to use external BT dongles due to an enumeration error (could it be broken or a kernel issue?!) so the device is not loaded correctly

As per now... I only able to get connection error 107 (endpoint not connected) 111 (connection refused)

MORE... if you bound the nut to its app... it disappear from the network :P

and least but not last... usign nRF app you're able to see almost all the characteristics... so it's defenetly a software issue! :evil: :evil:
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

korniza
Posts: 184
Joined: Thursday 27 August 2015 18:12
Target OS: Raspberry Pi
Domoticz version: V3.6028
Location: Greece
Contact:

Re: How to check presence of Beacon ?

Post by korniza » Tuesday 21 February 2017 20:23

possible the errors are coming from the script you use to monitor presence. I have a second ble dongle and use it for non-dedicated uses (1st is just for scanning the beacons using FHEM).
>>>> Google Home <<<<<
SBC: Odroid XU4 * Raspberry Pi2 * banana Pi v1
Peripherals: rfxtrx433E, aeon z-stick gen5, bluetooth dongles
Extended Software packages: Xeoma (video NVR), FHEM (extra home automation software)

User avatar
emme
Posts: 508
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: How to check presence of Beacon ?

Post by emme » Wednesday 22 February 2017 11:32

a small step ahead...

It could be that the new kernel helps (4.4.38 and 4.9)... but this is what I was able to do:

First of all... the hci interface must be shutdown and restart...
then you can issue the following commands:

Code: Select all

sudo hcitool lewladd --random <beaconMacAddress>
sudo gatttool -b <beaconMacAddress> -t random --char-read --uuid 0x2a19
this should return a string like:
handle: 0x00017 value: 64 00
for a script purpuse... and willing to implement the value in domoticz (remember it is hex!)
the command should look like:

Code: Select all

sudo gatttool -b <beaconMacAddress> -t random --char-read --uuid 0x2a19 | grep value | awk '{print $4}' 
to return only the value (64)

the problem is that after few tentatives the beacon disappear and it is no longer reachable.... (privacy/ddos prevention?!)


=== UPDATE ===
Ok... this would be probably the last update with the final script...
I'm testing it and it works as it is supposed to.

It still miss the Domoticz sensors/variable/whatever, but it would be a simple curl request (I'll work on this this evening)

here it is! :P
$ sudo nano nutbattery.sh
of course replace the 00:... with your NUT Mini Mac address

Code: Select all

#!/bin/bash

NUTMAC=$"00:00:00:00:00:00"
HCIIF=$"hci0"

sudo hciconfig $HCIIF down
sudo hciconfig $HCIIF up

HANDLE=$(sudo hcitool lecc --random $NUTMAC | awk '{print $3}')
sleep 2
sudo hcitool ledc $HANDLE 

BATHEX=$(sudo gatttool -t random --char-read --uuid 0x2a19 -b $NUTMAC | awk '{print $4}')
BATDEC=$((0x$BATHEX))

echo "BATTERY HEX:" $BATHEX "CONVERTED:" $BATDEC "%"
save it and have it executable

Code: Select all

$ sudo chmod +x nutbattery.sh
test it... if you get a timeout error retry
this code SHOULD NOT run with the beacon presence scrit/service running... you have first to disable it, run the script and reenable the presence beacon
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

bigpea
Posts: 61
Joined: Thursday 11 August 2016 12:30
Target OS: Raspberry Pi
Domoticz version:
Location: IT
Contact:

Re: How to check presence of Beacon ?

Post by bigpea » Wednesday 22 February 2017 17:21

You are a genius! :D
Thanks emme!

kwintex
Posts: 1
Joined: Thursday 23 February 2017 13:24
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: How to check presence of Beacon ?

Post by kwintex » Thursday 23 February 2017 13:34

Suggestion for jmleglise: https://github.com/jmleglise/mylittle-d ... resence.py
If you change line 232 to...
elif tag[5]==REPEAT_MODE and (elapsed_time>3 or int(time.time())%3==0): # in continuous, Every 3 seconds
...it will update the user variable for beacons that are sending at least every single second.

User avatar
darlomrh
Posts: 41
Joined: Monday 23 May 2016 8:03
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: UK
Contact:

Re: How to check presence of Beacon ?

Post by darlomrh » Friday 24 February 2017 8:39

I have a query around the NUT2 tag. It appears the tag is only broadcasting when I press the button on the device.

Do I have to pair it with something to start the device broadcasting.

I have used LightBlue Explorer on iPhone and sudo hcitool lescan and only when the button is pressed the nut will broadcast

Faulty device?

User avatar
emme
Posts: 508
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: How to check presence of Beacon ?

Post by emme » Friday 24 February 2017 9:35

My poor experience with NUT teches me that nut should be NEVER paired with anything, niether (especially) its app!
if you do that it will become unaccessible and hides from scan...
If you have previously paired... remove NUT from the device and switch off the BT radio.

try to:
sudo hciconfig hci0 down
sudo hciconfig hci0 up


sudo hcitool lescan
(chech if it becomes available)

sudo hcitool lecc --random <nut mac address>
(if the command is successful it will return something like: Conenction Handle xx
sudo hcitool ledc <handle from previous command>
gatttool -t random -b <nut mac address> --characteristics

to get information
gatttool -t random -b <nut mac address> --char-read --uuid 0x2a19
to read the battery

this works for my nut mini
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

User avatar
darlomrh
Posts: 41
Joined: Monday 23 May 2016 8:03
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: UK
Contact:

Re: How to check presence of Beacon ?

Post by darlomrh » Friday 24 February 2017 19:35

sudo hcitool lescan
NUT2 Only visible if I push the button into pair mode.
That scan reports and the script works with my fitbit One (perhaps should have just used that :? )
So i'm going to conclude broken device.

User avatar
emme
Posts: 508
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: How to check presence of Beacon ?

Post by emme » Friday 24 February 2017 19:39

what if you skip hcitool lescan and go straight to hcitool lecc <mac address> ?

you can either try to add the mac to the whitelist:
sudo hcitool lewladd <mac address> --random

and see if it start to be accessible
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

User avatar
darlomrh
Posts: 41
Joined: Monday 23 May 2016 8:03
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: UK
Contact:

Re: How to check presence of Beacon ?

Post by darlomrh » Friday 24 February 2017 19:49

emme wrote:what if you skip hcitool lescan and go straight to hcitool lecc <mac address> ?

you can either try to add the mac to the whitelist:
sudo hcitool lewladd <mac address> --random

and see if it start to be accessible
Did a sudo hcitool lewladd --random <mac address> and it returned nothing
Did a sudo hcitool lecc <mac address> ? and got Could not create connection: Connection timed out

User avatar
darlomrh
Posts: 41
Joined: Monday 23 May 2016 8:03
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: UK
Contact:

Re: How to check presence of Beacon ?

Post by darlomrh » Friday 24 February 2017 22:50

Yup definitely the NUT2. Ordered a NUT mini and worked straight off. Should have suspected as the NUT2 was a warehouse deal from Amazon.

:shock:

korniza
Posts: 184
Joined: Thursday 27 August 2015 18:12
Target OS: Raspberry Pi
Domoticz version: V3.6028
Location: Greece
Contact:

Re: How to check presence of Beacon ?

Post by korniza » Saturday 25 February 2017 7:54

My next concept is to read the press button on the NUT/ any ble device so to trigger an action. does anybody tried?
>>>> Google Home <<<<<
SBC: Odroid XU4 * Raspberry Pi2 * banana Pi v1
Peripherals: rfxtrx433E, aeon z-stick gen5, bluetooth dongles
Extended Software packages: Xeoma (video NVR), FHEM (extra home automation software)

User avatar
emme
Posts: 508
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: How to check presence of Beacon ?

Post by emme » Saturday 25 February 2017 15:29

I did...

I have 2 different services running handling beacons:

the first one is only presence detection (a simple lescan) using the script in this thread (switch mode) sets or unsets a user variable

than I have a cron job running once a day that check the battery status

Actually I have 3 beacons that arms alarm if away for more than 3 minutes...
if a NUT is seen in the network disarm the alarm ad switch on a light.

but you can do almost what you want... at the end they are variables and switches
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

User avatar
darlomrh
Posts: 41
Joined: Monday 23 May 2016 8:03
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: UK
Contact:

Re: How to check presence of Beacon ?

Post by darlomrh » Tuesday 28 February 2017 20:02

Hi,

I have had to abandon the use of nut minis. Not sure if bad luck or house construction or placement of pi. Over a couple of hours today I had 100+ variable updates on a tag that was sitting on a table in the room next door to my RPI. Not great when I plan to hang a number of commands off the Home and Away status changes.

Thanks to all that have helped me and keep up the good work. Back to IFTTT (IFTTT is currently down as I type this ho-hum) maker applets via geolocation I think.

User avatar
emme
Posts: 508
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: How to check presence of Beacon ?

Post by emme » Tuesday 28 February 2017 20:25

if you are running on a PI3 just give him the last chance in replacing the BT Radio with a CSR4.0 dongle

I've seen that it is much more fast and reliable than the onboard radio
of course it will create a new interface (hci1) but I think that for a couple of € you can eventfix your problems...

....try my battery script ;)
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

User avatar
darlomrh
Posts: 41
Joined: Monday 23 May 2016 8:03
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: UK
Contact:

Re: How to check presence of Beacon ?

Post by darlomrh » Tuesday 28 February 2017 21:11

Yeah.... never was able to get onboard BT to work so I purchased.
https://www.amazon.co.uk/gp/product/B00 ... UTF8&psc=1

Ran your battery script both 100%.

I am running RFXcom and uzb1 off the same PI so that may be it.

Thanks again great forum...

User avatar
Mooseknuckle
Posts: 68
Joined: Sunday 08 March 2015 10:24
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: How to check presence of Beacon ?

Post by Mooseknuckle » Wednesday 01 March 2017 15:01

Hi,

Thank you for the BT presence script. I had a Xiaomi mi band v1 lying around and tried it as a beacon with the presence script. After you make it discoverable in the Mi fit app the band is found with 'hcitool lescan' and can be used as a beacon.

Greetings,
RPI 3, Philips Hue,Toon Thermostat, Harmony Smart Control, Yeelights
Mysensors wifi gateway + sensors, RFXtrx433E, Kaku stuff, Xiaomi gateway + sensors
Domoticz controlled DIY ambilight, Selectplus chime, Mi-light led controller

Guiggess
Posts: 6
Joined: Tuesday 17 January 2017 21:50
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: How to check presence of Beacon ?

Post by Guiggess » Sunday 12 March 2017 11:01

Hi everyone,
I still have my service starting issue at reboot. It works fine when I start it manually, but stay as "Active (exited) when I reboot my Pi.

Any suggestion ?

User avatar
emme
Posts: 508
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: How to check presence of Beacon ?

Post by emme » Sunday 12 March 2017 19:19

in the .service file you created with the specification of the cmdline...
try to add:

Restart=always
RestartSec=3

in the [Service] section

this helped me with the telegram CLI service that fails at atartup, but runs if restarted
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

Guiggess
Posts: 6
Joined: Tuesday 17 January 2017 21:50
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: How to check presence of Beacon ?

Post by Guiggess » Sunday 12 March 2017 22:53

Hi emme,

Thanks for answer. I didn't wrote any part of that daemon, and I'm a total noob in service/daemon writing...

Anyway, I don't have a .service file nor a [Service] section in the daemon file...

Where should I add the lines you suggest ?

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests