mirror of
https://github.com/NotAShelf/air-quality-monitor.git
synced 2024-11-26 15:16:49 +00:00
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:
parent
75b5109cf3
commit
0683ce7b58
4 changed files with 39 additions and 9 deletions
|
@ -1,8 +1,10 @@
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
from sds011 import SDS011
|
import datetime
|
||||||
|
import serial
|
||||||
import redis
|
import redis
|
||||||
|
import aqi
|
||||||
|
|
||||||
redis_client = redis.StrictRedis(host=os.environ.get('REDIS_HOST'), port=6379, db=0)
|
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():
|
class AirQualityMonitor():
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.sds = SDS011(port='/dev/ttyUSB0')
|
self.ser = serial.Serial('/dev/ttyUSB0')
|
||||||
self.sds.set_working_period(rate=1)
|
|
||||||
|
|
||||||
def get_measurement(self):
|
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 {
|
return {
|
||||||
'time': int(time.time()),
|
'time': int(time.time()),
|
||||||
'measurement': self.sds.read_measurement(),
|
'measurement': self.meas
|
||||||
}
|
}
|
||||||
|
|
||||||
def save_measurement_to_redis(self):
|
def save_measurement_to_redis(self):
|
||||||
|
|
17
src/app.py
17
src/app.py
|
@ -7,8 +7,6 @@ import redis
|
||||||
import atexit
|
import atexit
|
||||||
from flask_cors import CORS, cross_origin
|
from flask_cors import CORS, cross_origin
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
cors = CORS(app)
|
cors = CORS(app)
|
||||||
app.config['CORS_HEADERS'] = 'Content-Type'
|
app.config['CORS_HEADERS'] = 'Content-Type'
|
||||||
|
@ -19,6 +17,12 @@ scheduler.add_job(func=aqm.save_measurement_to_redis, trigger="interval", second
|
||||||
scheduler.start()
|
scheduler.start()
|
||||||
atexit.register(lambda: scheduler.shutdown())
|
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):
|
def reconfigure_data(measurement):
|
||||||
"""Reconfigures data for chart.js"""
|
"""Reconfigures data for chart.js"""
|
||||||
|
@ -26,7 +30,14 @@ def reconfigure_data(measurement):
|
||||||
measurement = measurement[:30]
|
measurement = measurement[:30]
|
||||||
measurement.reverse()
|
measurement.reverse()
|
||||||
return {
|
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': {
|
'pm10': {
|
||||||
'label': 'pm10',
|
'label': 'pm10',
|
||||||
'data': [x['measurement']['pm10'] for x in measurement],
|
'data': [x['measurement']['pm10'] for x in measurement],
|
||||||
|
|
|
@ -4,3 +4,5 @@ flask
|
||||||
redis
|
redis
|
||||||
APScheduler==3.7.0
|
APScheduler==3.7.0
|
||||||
flask-cors
|
flask-cors
|
||||||
|
python-aqi==0.6.1
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
type: 'line',
|
type: 'line',
|
||||||
data: {
|
data: {
|
||||||
labels: data.historical.labels,
|
labels: data.historical.labels,
|
||||||
datasets: [data.historical.pm10, data.historical.pm2]
|
datasets: [data.historical.aqi, data.historical.pm10, data.historical.pm2]
|
||||||
},
|
},
|
||||||
options: {
|
options: {
|
||||||
scales: {
|
scales: {
|
||||||
|
|
Loading…
Reference in a new issue