oldjake
Posts: 87
Joined: Fri Aug 03, 2018 12:39 pm

Occupancy Detection

Sat Dec 01, 2018 2:14 pm

Hi all

Been chatting about this in another thread and someone suggested I put it in here as well.

I've been playing with the Hive API for a while to turn lights on, adjust the heating, etc. One of the things that Hive is missing is proper geofencing. i.e. If the house is empty it turns the heating down automatically. I've been messing around with this and have a fairly stable system in place where my Pi looks for the Bluetooth MAC addresses of our mobile phones. The problem is where people don't have a phone or, like me, have Bluetooth switched off when at home - on my phone, it KILLS the wifi speed.

So, I bought one of these Tile things and stuck it on my keyring. At first, I thought I had it working. But I think these devices are only visible for a short period every few seconds as it would variously flag me as in and out when I was always in. So far, this is my code - or the code I got from the Bluepy site plus some bits of my own:

Code: Select all

from bluepy.btle import Scanner, DefaultDelegate
import bluetooth
import time
import datetime

class ScanDelegate(DefaultDelegate):
        def __init__(self):
                DefaultDelegate.__init__(self)
        def handleDiscovery(self, dev, isNewDev):
                if isNewDev:
                        ts = time.time()
                        timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
                        score = 0
                        if "cc:1c:41:40:ec:11" in dev.addr:
                        #if "c1:ed:da:df:55:46" in dev.addr:
                                print "Andy in", timestamp
                                score = score + 1
                        else:
                                print "Andy out", timestamp
scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(15.0)
This is the result of 1 pass:

Code: Select all

Andy out 2018-12-01 13:41:10
Andy out 2018-12-01 13:41:14
Andy in 2018-12-01 13:41:20
15 seconds guarantees enough time to get 3 results back. 3 results seems to be the minimum to guarantee finding the device one time. If it finds the device then it increments the score. If the score for any set of 3 scans > 1 then I'm in, otherwise I'm out. The reason there's 2 MAC addresses is because this also works with my Fossil hybrid watch and I was testing that to see if I got the same sort of results as with the Tile.

After this, it writes the result to a MySQL table. Another script then reads the score and sets the heating temperature accordingly.

Anyway, I thought I'd put this in here to see if anyone had any better ideas for occupancy detection with multiple occupants. IFTTT is no good in this respect as it doesn't know who's still in if one person goes out, and it wouldn't work with other BLE devices.

Andyroo
Posts: 2057
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Occupancy Detection

Sat Dec 01, 2018 11:49 pm

How about facial recognition? This is #1 in my book at the moment but not sure on the exit compared to opening the door for the postman.

Other thoughts have included:

Sensor plates in the area where we drop our shoes off - OK till my wife is in her trainers and they never come off during the day!
Not keen on implanting RF-ID in my arm
The phone is no longer strapped to my body 25x7 :lol:
Weight sensors in the chairs?
Check power use in some plugs - would work 99% with my son and computer and us with the kettle...
Buttons by the door - no hope getting folk to lock it is a good win
Voice recognition - may be built on ‘I am home’ and ‘bye’
Key fob - fine till we just nip around the corner without the car
Need Pi spray - these things are breeding in my house...

morticiaskeeper
Posts: 101
Joined: Mon Aug 27, 2012 6:05 pm

Re: Occupancy Detection

Sun Dec 02, 2018 5:51 pm

I use ARP network scanning to let my cctv known when nobody is in. Most people carry phones with them these days.

wifi probe monitoring also works well.

PIR sensors in rooms?

NFC, i have a ring on my finger with an NFC tag that can open doors.

Face recognition sounds more fun, though!

User avatar
mikronauts
Posts: 2685
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Occupancy Detection

Sun Dec 02, 2018 7:01 pm

I have used PIR sensors for this.

When I detected motion, I flagged that area as occupied for half an hour.

When there was no further motion detected, area was flagged as unoccupied.

This worked fairly well for a smart house, back in 1984 :)

oldjake wrote:
Sat Dec 01, 2018 2:14 pm
Hi all

Been chatting about this in another thread and someone suggested I put it in here as well.

I've been playing with the Hive API for a while to turn lights on, adjust the heating, etc. One of the things that Hive is missing is proper geofencing. i.e. If the house is empty it turns the heating down automatically. I've been messing around with this and have a fairly stable system in place where my Pi looks for the Bluetooth MAC addresses of our mobile phones. The problem is where people don't have a phone or, like me, have Bluetooth switched off when at home - on my phone, it KILLS the wifi speed.

So, I bought one of these Tile things and stuck it on my keyring. At first, I thought I had it working. But I think these devices are only visible for a short period every few seconds as it would variously flag me as in and out when I was always in. So far, this is my code - or the code I got from the Bluepy site plus some bits of my own:

Code: Select all

from bluepy.btle import Scanner, DefaultDelegate
import bluetooth
import time
import datetime

class ScanDelegate(DefaultDelegate):
        def __init__(self):
                DefaultDelegate.__init__(self)
        def handleDiscovery(self, dev, isNewDev):
                if isNewDev:
                        ts = time.time()
                        timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
                        score = 0
                        if "cc:1c:41:40:ec:11" in dev.addr:
                        #if "c1:ed:da:df:55:46" in dev.addr:
                                print "Andy in", timestamp
                                score = score + 1
                        else:
                                print "Andy out", timestamp
scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(15.0)
This is the result of 1 pass:

Code: Select all

Andy out 2018-12-01 13:41:10
Andy out 2018-12-01 13:41:14
Andy in 2018-12-01 13:41:20
15 seconds guarantees enough time to get 3 results back. 3 results seems to be the minimum to guarantee finding the device one time. If it finds the device then it increments the score. If the score for any set of 3 scans > 1 then I'm in, otherwise I'm out. The reason there's 2 MAC addresses is because this also works with my Fossil hybrid watch and I was testing that to see if I got the same sort of results as with the Tile.

After this, it writes the result to a MySQL table. Another script then reads the score and sets the heating temperature accordingly.

Anyway, I thought I'd put this in here to see if anyone had any better ideas for occupancy detection with multiple occupants. IFTTT is no good in this respect as it doesn't know who's still in if one person goes out, and it wouldn't work with other BLE devices.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

oldjake
Posts: 87
Joined: Fri Aug 03, 2018 12:39 pm

Re: Occupancy Detection

Mon Dec 03, 2018 10:12 am

morticiaskeeper wrote:
Sun Dec 02, 2018 5:51 pm
I use ARP network scanning to let my cctv known when nobody is in. Most people carry phones with them these days.
This sounds interesting. One of the boys spends most of his time in his room gaming. His room is right on the limit for the BLE detection so he gets marked as out and in depending on whether his phone is on one side of his desk or the other - it's that close.

nmap might be a bit more robust.

Off to play...

Return to “Other projects”