replace sds011 module with serial (#12)

* index: remove hardcoded IP api route

* app: remove whitespcce

* aqm: replace sds011 module with serial

* add AQI to plot

* timestamps to the second, not microseconds

---------

Co-authored-by: David <dhfriedman@gmail.com>
This commit is contained in:
Abiel Zulio M 2023-10-01 08:32:09 +07:00 committed by GitHub
parent 75b5109cf3
commit 0683ce7b58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 9 deletions

View file

@ -1,8 +1,10 @@
import json
import os
import time
from sds011 import SDS011
import datetime
import serial
import redis
import aqi
redis_client = redis.StrictRedis(host=os.environ.get('REDIS_HOST'), port=6379, db=0)
@ -10,13 +12,28 @@ redis_client = redis.StrictRedis(host=os.environ.get('REDIS_HOST'), port=6379, d
class AirQualityMonitor():
def __init__(self):
self.sds = SDS011(port='/dev/ttyUSB0')
self.sds.set_working_period(rate=1)
self.ser = serial.Serial('/dev/ttyUSB0')
def get_measurement(self):
self.data = []
for index in range(0,10):
datum = self.ser.read()
self.data.append(datum)
self.pmtwo = int.from_bytes(b''.join(self.data[2:4]), byteorder='little') / 10
self.pmten = int.from_bytes(b''.join(self.data[4:6]), byteorder='little') / 10
myaqi = aqi.to_aqi([(aqi.POLLUTANT_PM25, str(self.pmtwo)),
(aqi.POLLUTANT_PM10, str(self.pmten))])
self.aqi = float(myaqi)
self.meas = {
"timestamp": datetime.datetime.now(),
"pm2.5": self.pmtwo,
"pm10": self.pmten,
"aqi": self.aqi,
}
return {
'time': int(time.time()),
'measurement': self.sds.read_measurement(),
'measurement': self.meas
}
def save_measurement_to_redis(self):

View file

@ -7,8 +7,6 @@ import redis
import atexit
from flask_cors import CORS, cross_origin
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
@ -19,6 +17,12 @@ scheduler.add_job(func=aqm.save_measurement_to_redis, trigger="interval", second
scheduler.start()
atexit.register(lambda: scheduler.shutdown())
def pretty_timestamps(measurement):
timestamps = []
for x in measurement:
timestamp = x['measurement']['timestamp']
timestamps += [timestamp.split('.')[0]]
return timestamps
def reconfigure_data(measurement):
"""Reconfigures data for chart.js"""
@ -26,7 +30,14 @@ def reconfigure_data(measurement):
measurement = measurement[:30]
measurement.reverse()
return {
'labels': [x['measurement']['timestamp'] for x in measurement],
'labels': pretty_timestamps(measurement),
'aqi': {
'label': 'aqi',
'data': [x['measurement']['aqi'] for x in measurement],
'backgroundColor': '#181d27',
'borderColor': '#181d27',
'borderWidth': 3,
},
'pm10': {
'label': 'pm10',
'data': [x['measurement']['pm10'] for x in measurement],

View file

@ -3,4 +3,6 @@ ipython
flask
redis
APScheduler==3.7.0
flask-cors
flask-cors
python-aqi==0.6.1

View file

@ -38,7 +38,7 @@
type: 'line',
data: {
labels: data.historical.labels,
datasets: [data.historical.pm10, data.historical.pm2]
datasets: [data.historical.aqi, data.historical.pm10, data.historical.pm2]
},
options: {
scales: {