Last active
December 31, 2022 04:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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