Skip to content

Commit

Permalink
bme280 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
escomputers committed Mar 26, 2023
1 parent ab0e4b1 commit a7bd1df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
adafruit-circuitpython-bme280
requests
requests
board
28 changes: 19 additions & 9 deletions sensors/bme280/air-temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -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()

0 comments on commit a7bd1df

Please sign in to comment.