Henter...
Inkbird oversigt
Inkbird enheder til styring/overvågning af temperatur og fugtighed.
Er du i tvivl om, hvilken enhed du skal vælge, så ring til os på 23 24 48 00. Du er naturligvis også velkommen forbi vores butik, og se enhederne og få vejledning.
Finder du enhederne billigere andre steder, så ring til os på 23 24 48 00.
Raspberry Pi, Bluetooth
Herunder er et forslag til Python kode, som frit kan bruges som udgangspunkt i forbindelse med aflæsning af IBS-THx enhederne via Bluetooth på en Raspberry Pi. Kontakt Hjemmeriet hvis du skal have hjælp.
Vil du læse mere om brugen af Raspberry Pi - kan du kigge her...
- Klik for at se/skjule ibsthx.py | | Klik for at hente filen ibsthx.py Version: 31-Jan-2023 16:08 |
|
#!/usr/bin/python3
# -*- coding: iso-8859-1 -*-
'''
Example code written in python, to read bluetooth Inkbird IBS-THx
temperature/humidity devices within reach.
Developed on Raspberry Pi
Author - Jan Jochimsen / Hjemmeriet
Version Date: 29. January 2023
Disclaimer: No warranties or guaranties.
Use and modify at your own risk.
To be started from a terminal window using the command:
sudo python3 IBS-TH.py
'''
# Standard
from datetime import datetime
# Bluetooth
from bluepy.btle import Scanner, DefaultDelegate
from bluepy import btle
dateFormat = "%Y-%m-%d" # Gives f.ex. "2022-06-23"
timeFormat = "%H:%M:%S" # Gives f.ex. "17:23:15"
datetimeFormat = dateFormat + " " + timeFormat
unpackstring = "<" + "hhBBB"
class ScanDelegate( DefaultDelegate ):
def __init__(self):
DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
print( "** handleDiscovery ** isNewDev:", isNewDev, "- isNewData:", isNewData )
if isNewDev or isNewData:
bFound_sps = False
ManuData = ""
LocalName = ""
for( devadtype, devdesc, devvalue ) in dev.getScanData():
if devdesc == "Manufacturer":
ManuData = devvalue
if devdesc == "Complete Local Name":
LocalName = devvalue
if devvalue == "sps" or devvalue == "tps":
bFound_sps = True
sTime = datetime.now().strftime( datetimeFormat )
if bFound_sps and len( ManuData ) == 18:
# This is a IBS TH
sText = "IBS-TH"
else:
# This is NOT a IBS TH1...
sText = "NOT IBS-TH"
if isNewDev:
sText = "Discovered"
else: # isNewData:
sText = "New Data"
print( sTime, "-", sText, "-", dev.addr, "- [", ManuData, "] - <" + LocalName + ">" )
# Alternative read data from bluetooth device ( IBS-THx ) - not in use...
def get_ibsth1_data( macaddr ):
try:
peripheral = btle.Peripheral( macaddr )
handle = 0x002d
characteristic = peripheral.readCharacteristic( handle )
# unpack format: h - short, l - long, i - int, ? - BOOL, q - long long int, f - float
# 1st char in format is byte-order < - little-endian
(temp, humid, unknown1, unknown2, unknown3) = struct.unpack( unpackstring, characteristic )
sensorValue = { 'Temperature': temp / 100, 'Humidity': humid / 100, 'unknown1': unknown1, 'unknown2': unknown2, 'unknown3': unknown3 }
except Exception as e:
print( "*** Exception ***\n{}".format(e))
print( "Failure - get data for... " + macaddr )
sensorValue = { 'Temperature':-999.999, 'Humidity':-999.999, 'unknown1': 0, 'unknown2': 0, 'unknown3': 0 }
return sensorValue
def twosComplementHex( hexval ):
bits = 16
val = int( hexval, bits )
if val & ( 1 << ( bits-1) ):
val -= 1 << bits
return val
## =====================================================================
print( "Start scanning for bluetooth devices..." )
scanner = Scanner().withDelegate( ScanDelegate() ) ## Alternative: scanner = Scanner()
devices = scanner.scan( 5.0 )
print( "... Scan done ... Found: " + str( len( devices ) ) )
for dev in devices:
ManuData = ""
sDev = "\nDev: " + dev.addr + ", RSSI: %d dB\n" % dev.rssi
bFound_sps = False # Temperature and Humidity device
bFound_tps = False # Temperature device only
sData = ""
for( devadtype, devdesc, devvalue ) in dev.getScanData():
if sData != "":
sData = sData + "\n"
sData = sData + " %s %s = %s" % ( devadtype, devdesc, devvalue )
if devdesc == "Manufacturer":
ManuData = devvalue
if devdesc == "Complete Local Name" and devvalue == "sps": # Temp AND Humi device
bFound_sps = True
if devdesc == "Complete Local Name" and devvalue == "tps": # Temp ONLY device
bFound_tps = True
if ( bFound_sps or bFound_tps ) and len( ManuData ) == 18:
dTempStr = ManuData[ 2 ] + ManuData[ 3 ] + ManuData[ 0 ] + ManuData[ 1 ]
dTemp = twosComplementHex( dTempStr ) / 100
dHumiStr = ManuData[ 6 ] + ManuData[ 7 ] + ManuData[ 4 ] + ManuData[ 5 ]
dHumi = twosComplementHex( dHumiStr ) / 100
dBattStr = "00" + ManuData[ 14 ] + ManuData[ 15 ]
dBatt = twosComplementHex( dBattStr )
# Signal strength
dSign = dev.rssi
if bFound_sps:
print( sDev + "=== sps, Temperature: " + str( dTemp ) + chr(176) + "C"+ ", Humidity: " + str( dHumi ) + " %RH, Battery: " + str( dBatt ) +"%" )
else: # tps
print( sDev + "=== tps, Temperature: " + str( dTemp ) + chr(176) + "C"+ ", Battery: " + str( dBatt ) + "%" )
else:
print( sDev + "=== Unknown" )
print( sData )
''' Alternative...
sensorValue = get_ibsth1_data( saddr, DevType )
dTemp = sensorValue[ 'Temperature' ]
dHumi = sensorValue[ 'Humidity' ]
'''
|
|