Skip to content

Instantly share code, notes, and snippets.

@takuan-osho
Last active December 31, 2022 04:56
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 takuan-osho/de03f755bb55601337f2b7f86049e348 to your computer and use it in GitHub Desktop.
Save takuan-osho/de03f755bb55601337f2b7f86049e348 to your computer and use it in GitHub Desktop.
Fetch the data of CO2 concentration and temparature and send them to influxDB. refs: https://github.com/heinemml/CO2Meter
import time
from datetime import datetime
from CO2Meter import CO2Meter
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
token = "your token"
org = "your org"
bucket = "your bucket"
client = InfluxDBClient(
url="your dbs url",
token=token,
)
write_api = client.write_api(write_options=SYNCHRONOUS)
interval = 5
def main():
sensor = CO2Meter("/dev/hidraw0")
while True:
data = sensor.get_data()
now = datetime.utcnow()
co2 = data.get("co2")
temperature = data.get("temperature")
if not (co2 and temperature):
continue
point = (
Point("data")
.tag("location", "my-room")
.field("co2", co2)
.field("temperature", temperature)
.time(now, WritePrecision.NS)
)
write_api.write(bucket, org, point)
time.sleep(interval)
if __name__ == "__main__":
main()
[Unit]
Description=Send CO2 and temparature data to influx DB
After=network.target
[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/co2meter
Environment=PYTHONPATH=/home/pi/co2meter
ExecStart=/home/pi/co2meter/.venv/bin/python3 /home/pi/co2meter/main.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment