/BlueSniffer

Primary LanguagePython

What is this thing?

This is my bluetooth sniffer.
The file that matters is blue_lib.py; Also 2 scripts are provided, but they are merely examples, unless they do exactly what you want them to do
blue_explorer.py: sniffs bluetooth packets for a minute, detects if any of them are from thermometers it is familiar with, and in the end prints the list of detected thermometers
blue_worker.py: sniffs bluetooth packets for a minute, finds if any of them come from thermometers listed in ~/.blue/devices config file, afterwards writes the readings into a file in S3, and triggers alerts in PagerDuty in case temperature/humidity/battery charge are outside allowed ranges

What thermometers does it work with?
At this moment, it works with 2 kinds of thermometers:

  • Govee 5075 - a simple and reliable cheap thermometer
  • Inkbird IBS-TH1 - a slightly more expensive thermometer that has an external probe; handy when you brew beer or wine and need to measure liquid temperature inside the fermenter. Also must be handy if you have an aquarium.

What are the prerequisites?

  • I made it for a raspberry pi device, but it will probably run on any linux device that has bluetooth. Running it on windows might require slight modifications
  • Tested with Python 2.7 and 3.9
  • You must have hcitool and hcidump utilities installed on your device
    This is what you need to do in order to install the prerequisites on a fresh Raspberry PI device:
Python stuff
sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip

These libraries are required for blue_worker.py, but if you aren't planning to run it, you don't have to install them
pip install boto3
pip install pdpyras

Check out the code
sudo apt install git -y
git clone https://github.com/SergeNov/BlueSniffer.git

Create config files
cd BlueSniffer/
mkdir ~/.blue
cp config_sample/* ~/.blue/

Install bluetooth cli utility
(Unfortunately, there's no other way i could find for retrieving raw bluetooth packets at the moment i wrote this tool)
sudo apt-get install bluez-hcidump

Make sure it works under non-root users
sudo apt-get install libcap2-bin
sudo setcap 'cap_net_raw,cap_net_admin+eip' `which hcitool`
sudo setcap 'cap_net_raw,cap_net_admin+eip' `which hcidump`

Functions that might be useful to you:

blue_lib.open_sniffer(): Start the HCI subprocesses that allow listening to Bluetooth
blue_lib.close_sniffer(): Kill the HCI subprocesses
blue_lib.get_batch(seconds): Sniff packets for X seconds; returns an array of strings, each string a raw packet
blue_lib.parse_raw_message(data): Input is a raw packet in a string; returns a dictionary containing sender's mac address, and, if the packet has been recognized, thermometer model name, temperature, and other info extracted from the packet

Acknowledgements

I relied heavily on code by user Thrilleratplay located here:
https://github.com/Home-Is-Where-You-Hang-Your-Hack/sensor.goveetemp_bt_hci
It helped me understand how to make a bluetooth sniffer and how to parse packets (which i had no previous experience with). I needed something completely different from a Home Assistant component, but i used bluetooth related code from that repository