Skip to content

Commit

Permalink
Merge pull request #5 from vondraussen/feature/battery-correction
Browse files Browse the repository at this point in the history
fix: Battery calibration
  • Loading branch information
jshridha authored Aug 4, 2020
2 parents ba11d8e + dfde0c4 commit 30dfa0f
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
14 changes: 13 additions & 1 deletion inkbird/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,23 @@ def handleTemperature(self, data):
for probe, t in enumerate(temp):
self.probes[probe + 1].temperature = t

def __batteryPercentage(self, current, max):
factor = max / 6550.0
current /= factor
if current > const.BATTERY_CORRECTION[-1]:
return 100
if current <= const.BATTERY_CORRECTION[0]:
return 0
for idx, voltage in enumerate(const.BATTERY_CORRECTION, start=0):
if (current > voltage) and (current <= (const.BATTERY_CORRECTION[idx + 1])):
return idx + 1
return 100

def handleBattery(self, data):
if data[0] != 36:
return
battery, maxBattery = struct.unpack("<HH", data[1:5])
battery = int(battery / maxBattery * 100)
battery = self.__batteryPercentage(battery, maxBattery)
for probe, sensor in self.probes.items():
sensor.battery = battery
self.battery.value = battery
Expand Down
103 changes: 103 additions & 0 deletions inkbird/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,106 @@
UNITS_F_MESSAGE = bytes([0x02, 0x01, 0x00, 0x00, 0x00, 0x00])
UNITS_C_MESSAGE = bytes([0x02, 0x00, 0x00, 0x00, 0x00, 0x00])
REQ_BATTERY_MESSAGE = bytes([0x08, 0x24, 0x00, 0x00, 0x00, 0x00])

BATTERY_CORRECTION = [
5580,
5595,
5609,
5624,
5639,
5644,
5649,
5654,
5661,
5668,
5676,
5683,
5698,
5712,
5727,
5733,
5739,
5744,
5750,
5756,
5759,
5762,
5765,
5768,
5771,
5774,
5777,
5780,
5783,
5786,
5789,
5792,
5795,
5798,
5801,
5807,
5813,
5818,
5824,
5830,
5830,
5830,
5835,
5840,
5845,
5851,
5857,
5864,
5870,
5876,
5882,
5888,
5894,
5900,
5906,
5915,
5924,
5934,
5943,
5952,
5961,
5970,
5980,
5989,
5998,
6007,
6016,
6026,
6035,
6044,
6052,
6062,
6072,
6081,
6090,
6103,
6115,
6128,
6140,
6153,
6172,
6191,
6211,
6230,
6249,
6265,
6280,
6296,
6312,
6328,
6344,
6360,
6370,
6381,
6391,
6407,
6423,
6431,
6439,
6455,
]

0 comments on commit 30dfa0f

Please sign in to comment.