Page 13 of 19
Re: How to check presence of Beacon ?
Posted: Tuesday 21 February 2017 16:55
by emme
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
and least but not last... usign nRF app you're able to see almost all the characteristics... so it's defenetly a software issue!

Re: How to check presence of Beacon ?
Posted: Tuesday 21 February 2017 19:23
by korniza
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).
Re: How to check presence of Beacon ?
Posted: Wednesday 22 February 2017 10:32
by emme
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!
$ 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
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
Re: How to check presence of Beacon ?
Posted: Wednesday 22 February 2017 16:21
by bigpea
You are a genius!
Thanks emme!
Re: How to check presence of Beacon ?
Posted: Thursday 23 February 2017 12:34
by kwintex
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.
Re: How to check presence of Beacon ?
Posted: Friday 24 February 2017 7:39
by darlomrh
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?
Re: How to check presence of Beacon ?
Posted: Friday 24 February 2017 8:35
by emme
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
Re: How to check presence of Beacon ?
Posted: Friday 24 February 2017 18:35
by darlomrh
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.
Re: How to check presence of Beacon ?
Posted: Friday 24 February 2017 18:39
by emme
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
Re: How to check presence of Beacon ?
Posted: Friday 24 February 2017 18:49
by darlomrh
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
Re: How to check presence of Beacon ?
Posted: Friday 24 February 2017 21:50
by darlomrh
Yup definitely the NUT2. Ordered a NUT mini and worked straight off. Should have suspected as the NUT2 was a warehouse deal from Amazon.

Re: How to check presence of Beacon ?
Posted: Saturday 25 February 2017 6:54
by korniza
My next concept is to read the press button on the NUT/ any ble device so to trigger an action. does anybody tried?
Re: How to check presence of Beacon ?
Posted: Saturday 25 February 2017 14:29
by emme
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
Re: How to check presence of Beacon ?
Posted: Tuesday 28 February 2017 19:02
by darlomrh
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.
Re: How to check presence of Beacon ?
Posted: Tuesday 28 February 2017 19:25
by emme
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

Re: How to check presence of Beacon ?
Posted: Tuesday 28 February 2017 20:11
by darlomrh
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...
Re: How to check presence of Beacon ?
Posted: Wednesday 01 March 2017 14:01
by Mooseknuckle
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,
Re: How to check presence of Beacon ?
Posted: Sunday 12 March 2017 10:01
by Guiggess
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 ?
Re: How to check presence of Beacon ?
Posted: Sunday 12 March 2017 18:19
by emme
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
Re: How to check presence of Beacon ?
Posted: Sunday 12 March 2017 21:53
by Guiggess
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 ?