Skip to content

Instantly share code, notes, and snippets.

@TheFlyingCorpse
Last active April 6, 2019 22:47
Show Gist options
  • Save TheFlyingCorpse/8904638a9664b98a5fdedeb87d27e96e to your computer and use it in GitHub Desktop.
Save TheFlyingCorpse/8904638a9664b98a5fdedeb87d27e96e to your computer and use it in GitHub Desktop.
HumiTemp Sensor Tag
#!/usr/bin/env python3
# Script is based on https://www.silabs.com/community/wireless/bluetooth/forum.topic.html/python_code_to_useb-RswG
import time
import struct
from meteocalc import Temp, dew_point, heat_index
from influxdb import InfluxDBClient
from bluepy.btle import *
# Product ID (Google / Amazon): B06XQWWPNT
# The decimal reading is only valid to the 10th, it doesnt do 100th, so 34.7c vs 34.75c and 30.5% vs 30.55%
SENSORTAG_NAME = "HT-2"
SENSORTAG_ADDRESS = "00:1B:35:12:5D:A7"
SENSORTAG_ALIAS = "Bøttekott"
global battery
# callback class
class MyDelegate(DefaultDelegate):
global battery
def __init__(self):
DefaultDelegate.__init__(self)
def handleNotification(self, cHandle, data):
negative,rawTint,rawTdec,reserved,rawHint,rawHdec = data
temp = float(rawTint) + float(rawTdec) / 10
RH = float(rawHint) + float(rawHdec) / 10
if negative > 0:
temp = temp - temp - temp
t = Temp(temp, 'c')
dp = dew_point(temperature=t, humidity=RH)
hi = heat_index(temperature=t, humidity=RH)
global battery
json_body = [
{
"measurement": "humitemp",
"tags": {
"sensor_address": SENSORTAG_ADDRESS,
"sensor_name": SENSORTAG_NAME,
"sensor_alias": SENSORTAG_ALIAS,
},
"fields": {
"humidity_temp": temp,
"humidity": RH,
"battery": battery,
"dew_point": dp.c,
"heat_index": hi.c,
}
}
]
client = InfluxDBClient('localhost', 8086, database='sensortag')
client.write_points(json_body)
#print(json_body)
enable_notify = b"\x01\x00"
disable_notify = b"\x00\x00"
enable_sensor = b'\x01'
disable_sensor = b'\x00'
# connect to device
per = None #Peripheral(SENSORTAG_ADDRESS, "public")
while True:
try:
if per is None:
per = Peripheral()
while True:
try:
if "status" in dir(per):
status = per.status()
if status['state'][0] == 'conn':
break
except:
per.connect(SENSORTAG_ADDRESS)
time.sleep(1.0)
# Sensor control
for chara in per.getCharacteristics():
if chara.uuid == '0000aa22-0000-1000-8000-00805f9b34fb':
control_uuid = chara.uuid
control_handle = chara.handle
if chara.uuid == '00002a19-0000-1000-8000-00805f9b34fb':
batteryRaw = chara.read()
battery = float(struct.unpack('<b',chara.read())[0])
print("Delegate")
# set callback for notifications
per.setDelegate(MyDelegate())
# enable sensor
per.writeCharacteristic(control_handle, enable_sensor)
# enable notification
notify = per.getCharacteristics(uuid='0000aa21-0000-1000-8000-00805f9b34fb')[0]
notify_handle = notify.getHandle() + 1
per.writeCharacteristic(notify_handle, enable_notify, withResponse=True)
# wait for answer
while True:
if per.waitForNotifications(5.0):
# Disable notitication, we've got data already.
per.writeCharacteristic(notify_handle, disable_notify, withResponse=True)
# Disable sensor to save battery
per.writeCharacteristic(control_handle, disable_sensor)
time.sleep(50.0)
break
except BTLEException as e:
continue
@tazmad
Copy link

tazmad commented Apr 6, 2019

Do you have any issues with getting the data from your humitemp sensor? I'm a python noob and struggling with this somewhat.....any tips you could give?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment