Skip to content

Instantly share code, notes, and snippets.

@danricho
Created January 19, 2022 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danricho/8cb2212cf8ed2d1e49903d118ec081b3 to your computer and use it in GitHub Desktop.
Save danricho/8cb2212cf8ed2d1e49903d118ec081b3 to your computer and use it in GitHub Desktop.
Govee Receive
# needs to run as sudo (probably to lock the use of the RPi bluetooth).
# based on gist by tchen @ https://gist.github.com/tchen/65d6b29a20dd1ef01b210538143c0bf4
# changed to look for H5074 and decode its data messages
from datetime import datetime
from time import sleep
from bleson import get_provider, Observer
# pip install bleson
govees = {}
def on_advertisement(advertisement):
global govees
mfg_data = advertisement.mfg_data
address = str(advertisement.address).replace("BDAddress('","").replace("')","")
if mfg_data is not None:
if b"INTELLI_ROCKS_HWP" in advertisement.mfg_data: # matches one of the packets out of a govee H5074
if address not in govees: # add the address to the govees of interest!
govees[address] = {"battery":100,"rssi":0,"data":{"temp":0, "humidity":0, "buffer":[]}}
elif address in govees:
# print(advertisement)
# print(mfg_data.hex())
govees[address]['rssi'] = advertisement.rssi
govees[address]['battery'] = mfg_data[7]
humidity = ((mfg_data[5] + (mfg_data[6] << 8)) / 100.0 )
temp = ((mfg_data[3] + (mfg_data[4] << 8)) / 100.0 )
now = datetime.now()
govees[address]['data']['buffer'].append((now,temp,humidity))
govees[address]['data']['buffer'] = [ entry for entry in govees[address]['data']['buffer'] if (now-entry[0]).seconds < 15 ] # buffer of last 15 seconds of data
govees[address]['data']['temp'] = round(sum( [entry[1] for entry in govees[address]['data']['buffer']] ) / len( [entry[1] for entry in govees[address]['data']['buffer']] ), 2)
govees[address]['data']['humidity'] = round(sum( [entry[2] for entry in govees[address]['data']['buffer']] ) / len( [entry[2] for entry in govees[address]['data']['buffer']] ), 2)
print("{} | TEMP: {:>6}*C | HUMIDITY: {:>6}% | BATTERY: {:>3}% | RSSI: {:>3} | ADDRESS: {}".format(now.isoformat(), govees[address]['data']['temp'], govees[address]['data']['humidity'], govees[address]['battery'], govees[address]['rssi'], address))
"""
2022-01-19T16:24:55.075598 | TEMP: 2.87*C | HUMIDITY: 65.18% | BATTERY: 100% | RSSI: -72
2022-01-19T16:24:57.075658 | TEMP: 2.87*C | HUMIDITY: 65.19% | BATTERY: 100% | RSSI: -80
2022-01-19T16:24:59.076879 | TEMP: 2.87*C | HUMIDITY: 65.2% | BATTERY: 100% | RSSI: -73
2022-01-19T16:25:01.077004 | TEMP: 2.87*C | HUMIDITY: 65.21% | BATTERY: 100% | RSSI: -71
2022-01-19T16:25:03.073569 | TEMP: 2.86*C | HUMIDITY: 65.22% | BATTERY: 100% | RSSI: -83
2022-01-19T16:25:05.077955 | TEMP: 2.86*C | HUMIDITY: 65.23% | BATTERY: 100% | RSSI: -72
2022-01-19T16:25:07.074760 | TEMP: 2.87*C | HUMIDITY: 65.24% | BATTERY: 100% | RSSI: -71
2022-01-19T16:25:09.073112 | TEMP: 2.87*C | HUMIDITY: 65.25% | BATTERY: 100% | RSSI: -80
2022-01-19T16:25:11.075892 | TEMP: 2.87*C | HUMIDITY: 65.27% | BATTERY: 100% | RSSI: -73
"""
adapter = get_provider().get_adapter()
observer = Observer(adapter)
observer.on_advertising_data = on_advertisement
observer.start()
while True:
sleep(60)
observer.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment