Skip to content

Instantly share code, notes, and snippets.

@kc1awv
Last active July 6, 2022 17:06
Show Gist options
  • Save kc1awv/a5c8b067c74364c2fae67c253ff6164c to your computer and use it in GitHub Desktop.
Save kc1awv/a5c8b067c74364c2fae67c253ff6164c to your computer and use it in GitHub Desktop.
enviropi+ influx logger
#!/usr/bin/env python3
import time
import datetime
from bme280 import BME280
from ltr559 import LTR559
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
try:
from smbus2 import SMBus
except ImportError:
from smbus import SMBus
# Corrects the relative humidity given a raw and corrected temperature reading
def correct_humidity(humidity, temperature, corr_temperature):
dewpoint = temperature - ((100 - humidity) / 5)
corr_humidity = 100 - (5 * (corr_temperature - dewpoint)) - 20
return min(100, max(0, corr_humidity))
# Set up the BME280 weather sensor
bus = SMBus(1)
bme280 = BME280(i2c_dev=bus)
bme280.setup(mode="forced")
time.sleep(5)
# Set up the light sensor
ltr559 = LTR559()
# InfluxDB client to write to
token = "****"
org = "****"
bucket = "****"
while True:
with InfluxDBClient(url="INFLUXDB_IP:8086", token=token, org=org) as client:
write_api = client.write_api(write_options=SYNCHRONOUS)
measurement = "indoors"
location = "bedroom"
# Read temperature (read twice, as the first reading can be artificially high)
temperature = bme280.get_temperature()
time.sleep(1)
temperature = bme280.get_temperature()
# Calculate corrected temperature
offset = 7.5
corr_temperature = temperature - offset
w_temperature = measurement + ",location=" + location + " temperature=" + str(corr_temperature)
# Read humidity and correct it with the corrected temperature
humidity = bme280.get_humidity()
corr_humidity = correct_humidity(humidity, temperature, corr_temperature)
w_humidity = measurement + ",location=" + location + " humidity=" + str(corr_humidity)
# Read pressure
pressure = bme280.get_pressure()
w_pressure = measurement + ",location=" + location + " pressure=" + str(pressure)
# Read light
light = ltr559.get_lux()
w_light = measurement + ",location=" + location + " lux=" + str(light)
# Log the data
write_api.write(bucket, org, [w_temperature, w_humidity, w_pressure, w_light])
client.close()
time.sleep(15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment