This commit is contained in:
quackerd 2022-01-22 16:47:07 +08:00
parent ad09de77a0
commit 9ffd93c5fc

View File

@ -1,4 +1,6 @@
from warnings import catch_warnings
from influxdb import InfluxDBClient
import libptqs1005
import time
import logging
@ -12,6 +14,7 @@ DELAY_PER_MEASURE = 300
DB_TOKEN = 'sensor1234sensor'
DB_ORG = 'sensor'
DB_BUCKET = 'sensor'
g_exit = False
@ -37,7 +40,7 @@ COLUMN_MAP = {
"part100" : "10um particles"
}
def signal_handler():
def signal_handler(sig, frm):
global g_exit
logging.warn("SIGINT detected, exiting gracefully...")
g_exit = True
@ -65,14 +68,10 @@ def measure(drv : libptqs1005.PTQS1005Driver) -> libptqs1005.PTQS1005AC:
return ret
def data_to_line(data : libptqs1005.PTQS1005AC) -> str:
ret = "air "
def data_to_point(data : libptqs1005.PTQS1005AC) -> influxdb_client.Point:
ret = influxdb_client.Point("Air Quality")
for col in COLUMN_MAP:
desc = COLUMN_MAP[col].replace(" ", "\\ ")
attr = col
ret = ret + desc + "=" + str(getattr(data, attr)) + ","
ret = ret[:-1]
ret = ret.field(COLUMN_MAP[col], getattr(data, col))
return ret
def main():
@ -84,7 +83,7 @@ def main():
drv = libptqs1005.PTQS1005Driver(tty="/dev/ttyAMA0", reset_pin=23)
logging.info("Setting up InfluxDB connection...")
dbconn = influxdb_client.InfluxDBClient(url="http://nas:8086",token="sensor1234sensor", org="sensor")
dbconn = influxdb_client.InfluxDBClient(url="http://nas:8086",token=DB_TOKEN, org=DB_ORG)
logging.info("Begin monitoring...")
next_ms = current_ms()
@ -101,12 +100,10 @@ def main():
logging.error("Failed to read data from sensor. Error: " + str(ex.args))
logging.info("Uploading data to database...")
dbline = data_to_line(data)
logging.debug("DB line: " + dbline)
if not dbconn.write_api().write("sensor", "sensor", [dbline]):
dbpt = data_to_point(data)
logging.debug("DB point: " + str(dbpt))
if not dbconn.write_api().write(DB_BUCKET, DB_ORG, dbpt):
logging.error("Failed to update database.")
#print("PM2.5: " + str(data.pm25) + " PM10: " + str(data.pm100) + " PM1: " + str(data.pm10) + " Temp: " + str(data.temp) + " Humidity: " + str(data.hum) + " CO2: " + str(data.co2) + " HCHO: " + str(data.hcho) + " TVOC: " + str(data.tvoc) )
next_ms = cur_ms + DELAY_PER_MEASURE * 1000
logging.debug("Next measurement point: " + str(next_ms))
else: