Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
New worker: ble devices scanner (#46)
Browse files Browse the repository at this point in the history
* Worker for ble devices scanning

Added a worker that scan for ble devices and notify, by mqtt, presence of mac addresses and rssi.

Configuration section example:

....
workers:
    blescanmulti:
      args:
        devices:
          nut: xx:xx:xx:xx:xx:xx
          ionic: xx:xx:xx:xx:xx:xx
        topic_prefix: blescan
      update_interval: 60
...

* Update README.md 

blescanmulti worker added

* Update config.yaml.example

Added configuration section for blescanmulti worker

* Converted blescanmulti.py indentation to 2 spaces

* changed mqtt message syntax

* Update README.md

Added contributor repos url

* Update .gitignore

Removed .vscode/

* Delete settings.json

* Changes by pull request review 11/01/2019
 On branch master
	modified:   workers/blescanmulti.py
  • Loading branch information
elviosebastianelli authored and zewelor committed Jan 11, 2019
1 parent 2e27c9a commit 345b200
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
config.yaml
__pycache__/
.idea/
.vscode/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ See [Wiki](https://github.com/zewelor/bt-mqtt-gateway/wiki) for more information
* [MySensors](https://www.mysensors.org/)
* [Xiaomi Mi Flora plant sensor](https://xiaomi-mi.com/sockets-and-sensors/xiaomi-huahuacaocao-flower-care-smart-monitor/) via [miflora](https://github.com/open-homeautomation/miflora)
* Xiaomi Aqara thermometer via [mithermometer](https://github.com/hobbypunk90/mithermometer)
* Bluetooth Low Power devices (BLE)

## Getting Started

Expand Down Expand Up @@ -104,6 +105,8 @@ You need to define the absolute path of `gateway.py` in `bt-mqtt-gateway.service

* [**zewelor**](https://github.com/zewelor) - *Initial work*
* [**bbbenji**](https://github.com/bbbenji) - *Minor contributions*
* [**elviosebastianelli**](https://github.com/elviosebastianelli) - *BLEscanmulti*
* [**jumping2000**](https://github.com/jumping2000) - *BLEscan*

## License

Expand Down
7 changes: 7 additions & 0 deletions config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ manager:
living_room: 00:11:22:33:44:55
topic_prefix: mithermometer
update_interval: 300
blescanmulti:
args:
devices:
beacon: 00:11:22:33:44:55
smartwath: 00:11:22:33:44:55
topic_prefix: blescan
update_interval: 60
39 changes: 39 additions & 0 deletions workers/blescanmulti.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import time
from interruptingcow import timeout
from bluepy.btle import Scanner, DefaultDelegate
from mqtt import MqttMessage
from workers.base import BaseWorker
from logger import _LOGGER

REQUIREMENTS = ['bluepy']

class ScanDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)

def handleDiscovery(self, dev, isNewDev, isNewData):
if isNewDev:
_LOGGER.debug("Discovered new device: %s" % dev.addr)

class BlescanmultiWorker(BaseWorker):
def searchmac(self, devices, mac):
for dev in devices:
if dev.addr == mac.lower():
return dev

return None

def status_update(self):
scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(10.0)
ret = []

for name, mac in self.devices.items():
device = self.searchmac(devices, mac)
if device is None:
ret.append(MqttMessage(topic=self.format_topic('presence/'+name), payload="0"))
else:
ret.append(MqttMessage(topic=self.format_topic('presence/'+name+'/rssi'), payload=device.rssi))
ret.append(MqttMessage(topic=self.format_topic('presence/'+name), payload="1"))

return ret

0 comments on commit 345b200

Please sign in to comment.