From a7bd1df45caad34ffbbc8fe54e2c6bcb30f49f15 Mon Sep 17 00:00:00 2001 From: escomputers Date: Sun, 26 Mar 2023 03:04:53 +0200 Subject: [PATCH] bme280 fixes --- requirements.txt | 3 ++- sensors/bme280/air-temperature.py | 28 +++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/requirements.txt b/requirements.txt index a99df33..26031b3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ adafruit-circuitpython-bme280 -requests \ No newline at end of file +requests +board \ No newline at end of file diff --git a/sensors/bme280/air-temperature.py b/sensors/bme280/air-temperature.py index e3f44a8..85e4018 100644 --- a/sensors/bme280/air-temperature.py +++ b/sensors/bme280/air-temperature.py @@ -3,6 +3,10 @@ import threading import board from adafruit_bme280 import basic as adafruit_bme280 +import syslog +import datetime + +syslog.openlog(facility=syslog.LOG_LOCAL0) LLA_TOKEN = os.environ["TOKEN"] @@ -14,12 +18,18 @@ def sensing(): # GET & SEND AIR TEMPERATURE air_temperature = str(("%.2f" % round(bme280.temperature, 2))) - requests.post('http://localhost:8123/api/states/sensor.air_temperature', headers={ - 'Authorization': 'Bearer ' + LLA_TOKEN, - 'Content-Type': 'application/json' - }, json={ - 'state': air_temperature, - 'attributes': { - 'unit_of_measurement': '°C' - } - }) + try: + requests.post('http://localhost:8123/api/states/sensor.air_temperature', headers={ + 'Authorization': 'Bearer ' + LLA_TOKEN, + 'Content-Type': 'application/json' + }, json={ + 'state': air_temperature, + 'attributes': { + 'unit_of_measurement': '°C' + } + }) + except ConnectionError: + timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + syslog.syslog(syslog.LOG_WARNING, '[' + timestamp + ']' + '[WARNING HASSIO REST API for BME280-AIRTEMP] Cannot send sensor data to Home Assistant REST endpoint.') + +sensing()