Skip to content

Commit

Permalink
Fix bugs in current readings
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekl1 committed Jan 13, 2024
1 parent 8226aa9 commit fdb0f61
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 134 deletions.
60 changes: 36 additions & 24 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,32 @@ def load_configs(c):
w1_sensor = None
pms5003 = None

try:
pms5003 = PMS5003(**configs["pms5003"])
except Exception:
pass


def load_sensors():
global bme280_bus, w1_sensor, pms5003

bme280_bus = smbus2.SMBus(bme280_port)

bme280.load_calibration_params(bme280_bus, bme280_address)
try:
bme280.sample(bme280_bus, bme280_address)
except Exception:
try:
bme280_bus = smbus2.SMBus(bme280_port)

w1_sensor = W1ThermSensor()
bme280.load_calibration_params(bme280_bus, bme280_address)
except Exception:
pass

try:
pms5003.read()
w1_sensor.get_temperature()
except Exception:
pms5003 = PMS5003(**configs["pms5003"])
try:
w1_sensor = W1ThermSensor()
except Exception:
pass


try:
Expand Down Expand Up @@ -88,18 +100,22 @@ def index():

@app.route('/api/current_reading')
def current_reading_api():
try:
load_sensors()
except Exception:
pass

try:
bme280_data = bme280.sample(bme280_bus, bme280_address)
bme280_humidity = bme280_data.humidity
bme280_pressure = bme280_data.pressure
bme280_temperature = bme280_data.temperature
except Exception:
bme280_dict = {}

try:
load_sensors()
except Exception:
pass
bme280_dict = {
"temperature": None,
"humidity": None,
"pressure": None
}
else:
bme280_dict = {
"temperature": bme280_temperature,
Expand All @@ -110,12 +126,9 @@ def current_reading_api():
try:
w1_temperature = w1_sensor.get_temperature()
except Exception:
ds18b20_dict = {}

try:
load_sensors()
except Exception:
pass
ds18b20_dict = {
"temperature": None
}
else:
ds18b20_dict = {
"temperature": w1_temperature
Expand All @@ -127,12 +140,11 @@ def current_reading_api():
pms5003_2_5 = pms5003_data.pm_ug_per_m3(2.5)
pms5003_10 = pms5003_data.pm_ug_per_m3(10)
except Exception:
pms5003_dict = {}

try:
load_sensors()
except Exception:
pass
pms5003_dict = {
"pm1.0": None,
"pm2.5": None,
"pm10": None
}
else:
pms5003_dict = {
"pm1.0": pms5003_1_0,
Expand Down
141 changes: 42 additions & 99 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,97 +5,38 @@ function loadCurrentReadings() {
})
.then((json) => {
if (json.status === "ok") {
document.querySelector("#bme280-temperature").innerHTML =
json.bme280.temperature.toFixed(2);
document.querySelector("#bme280-humidity").innerHTML =
json.bme280.humidity.toFixed(2);
document.querySelector("#bme280-pressure").innerHTML =
json.bme280.pressure.toFixed(2);
document.querySelector("#ds18b20-temperature").innerHTML =
json.ds18b20.temperature.toFixed(2);
document.querySelector("#pms5003-pm-1-0").innerHTML =
json.pms5003["pm1.0"];
document.querySelector("#pms5003-pm-2-5").innerHTML =
json.pms5003["pm2.5"];
document.querySelector("#pms5003-pm-10").innerHTML =
json.pms5003["pm10"];

document.querySelector("#sensor-error").classList.add("d-none");
document
.querySelector("#current-readings")
.classList.remove("d-none");
} else if (
json.status === "error" &&
json.error === "sensor_error"
) {
document
.querySelector("#current-readings")
.classList.add("d-none");
document
.querySelector("#sensor-error")
.classList.remove("d-none");
document.querySelector("#ds18b20-temperature").innerHTML = json.ds18b20.temperature !== null ? `${json.ds18b20.temperature.toFixed(2)}℃` : "-";
document.querySelector("#bme280-temperature").innerHTML = json.bme280.temperature !== null ? `${json.bme280.temperature.toFixed(2)}℃` : "-";
document.querySelector("#bme280-humidity").innerHTML = json.bme280.humidity !== null ? `${json.bme280.humidity.toFixed(2)}%` : "-";
document.querySelector("#bme280-pressure").innerHTML = json.bme280.pressure !== null ? `${json.bme280.pressure.toFixed(2)} hPa` : "-";
document.querySelector("#pms5003-pm-1-0").innerHTML = json.pms5003["pm1.0"] !== null ? `${json.pms5003["pm1.0"]} μg/m³` : "-";
document.querySelector("#pms5003-pm-2-5").innerHTML = json.pms5003["pm2.5"] !== null ? `${json.pms5003["pm2.5"]} μg/m³` : "-";
document.querySelector("#pms5003-pm-10").innerHTML = json.pms5003["pm10"] !== null ? `${json.pms5003["pm10"]} μg/m³` : "-";

document.querySelector("#current-readings-error").classList.add("d-none");
document.querySelector("#current-readings").classList.remove("d-none");
} else if (json.status === "error" && json.error === "sensor_error") {
document.querySelector("#current-readings").classList.add("d-none");
document.querySelector("#current-readings-error").classList.remove("d-none");
}

document
.querySelector("#loading-current-readings")
.classList.add("d-none");
document
.querySelector("#refresh-current-readings-div")
.classList.remove("d-none");

setInterval(refreshCurrentReadings, 30_000);
document.querySelector("#loading-current-readings").classList.add("d-none");
document.querySelector("#refresh-current-readings-div").classList.remove("d-none");
document.querySelector("#refresh-current-readings-loading").classList.add("d-none");
document.querySelector("#refresh-current-readings").classList.remove("d-none");
}).catch(() => {
document.querySelector("#current-readings").classList.add("d-none");
document.querySelector("#current-readings-error").classList.remove("d-none");
document.querySelector("#refresh-current-readings-loading").classList.add("d-none");
document.querySelector("#refresh-current-readings").classList.remove("d-none");
});
}

function refreshCurrentReadings() {
document.querySelector("#refresh-current-readings").classList.add("d-none");
document
.querySelector("#refresh-current-readings-loading")
.classList.remove("d-none");
fetch("/api/current_reading")
.then((response) => {
return response.json();
})
.then((json) => {
if (json.status === "ok") {
document.querySelector("#bme280-temperature").innerHTML =
json.bme280.temperature.toFixed(2);
document.querySelector("#bme280-humidity").innerHTML =
json.bme280.humidity.toFixed(2);
document.querySelector("#bme280-pressure").innerHTML =
json.bme280.pressure.toFixed(2);
document.querySelector("#ds18b20-temperature").innerHTML =
json.ds18b20.temperature.toFixed(2);
document.querySelector("#pms5003-pm-1-0").innerHTML =
json.pms5003["pm1.0"];
document.querySelector("#pms5003-pm-2-5").innerHTML =
json.pms5003["pm2.5"];
document.querySelector("#pms5003-pm-10").innerHTML =
json.pms5003["pm10"];

document.querySelector("#sensor-error").classList.add("d-none");
document
.querySelector("#current-readings")
.classList.remove("d-none");
} else if (
json.status === "error" &&
json.error === "sensor_error"
) {
document
.querySelector("#current-readings")
.classList.add("d-none");
document
.querySelector("#sensor-error")
.classList.remove("d-none");
}

document
.querySelector("#refresh-current-readings-loading")
.classList.add("d-none");
document
.querySelector("#refresh-current-readings")
.classList.remove("d-none");
});
document.querySelector("#refresh-current-readings-loading").classList.remove("d-none");

loadCurrentReadings();
}

function loadArchiveReadings(options) {
Expand Down Expand Up @@ -137,17 +78,17 @@ function loadArchiveReadings(options) {
var ds18b20Temperature = document.createElement("td");
ds18b20Temperature.innerHTML = (e.ds18b20_temperature !== null) ? `${e.ds18b20_temperature.toFixed(
2
)} ℃` : "-";
)}℃` : "-";
rowElement.append(ds18b20Temperature);

var bme280Temperature = document.createElement("td");
bme280Temperature.innerHTML = (e.bme280_temperature !== null) ? `${e.bme280_temperature.toFixed(
2
)} ℃` : "-";
)}℃` : "-";
rowElement.append(bme280Temperature);

var bme280Humidity = document.createElement("td");
bme280Humidity.innerHTML = (e.bme280_humidity !== null) ? `${e.bme280_humidity.toFixed(2)} %` : "-";
bme280Humidity.innerHTML = (e.bme280_humidity !== null) ? `${e.bme280_humidity.toFixed(2)}%` : "-";
rowElement.append(bme280Humidity);

var bme280Pressure = document.createElement("td");
Expand Down Expand Up @@ -218,14 +159,14 @@ function loadStats() {
"#min-bme280-temperature"
).innerHTML = `${json.bme280.temperature.min.value.toFixed(
2
)} ℃ <br> ${new Date(
)}℃ <br> ${new Date(
json.bme280.temperature.min.read_time
).toLocaleString()} <br> #${json.bme280.temperature.min.id}`;
document.querySelector(
"#min-bme280-humidity"
).innerHTML = `${json.bme280.humidity.min.value.toFixed(
2
)} % <br> ${new Date(
)}% <br> ${new Date(
json.bme280.humidity.min.read_time
).toLocaleString()} <br> #${json.bme280.humidity.min.id}`;
document.querySelector(
Expand All @@ -239,7 +180,7 @@ function loadStats() {
"#min-ds18b20-temperature"
).innerHTML = `${json.ds18b20.temperature.min.value.toFixed(
2
)} ℃ <br> ${new Date(
)}℃ <br> ${new Date(
json.ds18b20.temperature.min.read_time
).toLocaleString()} <br> #${json.ds18b20.temperature.min.id}`;
document.querySelector(
Expand All @@ -262,14 +203,14 @@ function loadStats() {
"#max-bme280-temperature"
).innerHTML = `${json.bme280.temperature.max.value.toFixed(
2
)} ℃ <br> ${new Date(
)}℃ <br> ${new Date(
json.bme280.temperature.max.read_time
).toLocaleString()} <br> #${json.bme280.temperature.max.id}`;
document.querySelector(
"#max-bme280-humidity"
).innerHTML = `${json.bme280.humidity.max.value.toFixed(
2
)} % <br> ${new Date(
)}% <br> ${new Date(
json.bme280.humidity.max.read_time
).toLocaleString()} <br> #${json.bme280.humidity.max.id}`;
document.querySelector(
Expand All @@ -283,7 +224,7 @@ function loadStats() {
"#max-ds18b20-temperature"
).innerHTML = `${json.ds18b20.temperature.max.value.toFixed(
2
)} ℃ <br> ${new Date(
)}℃ <br> ${new Date(
json.ds18b20.temperature.max.read_time
).toLocaleString()} <br> #${json.ds18b20.temperature.max.id}`;
document.querySelector(
Expand All @@ -304,16 +245,16 @@ function loadStats() {

document.querySelector(
"#avg-bme280-temperature"
).innerHTML = `${json.bme280.temperature.avg.value.toFixed(2)} ℃`;
).innerHTML = `${json.bme280.temperature.avg.value.toFixed(2)}℃`;
document.querySelector(
"#avg-bme280-humidity"
).innerHTML = `${json.bme280.humidity.avg.value.toFixed(2)} %`;
).innerHTML = `${json.bme280.humidity.avg.value.toFixed(2)}%`;
document.querySelector(
"#avg-bme280-pressure"
).innerHTML = `${json.bme280.pressure.avg.value.toFixed(2)} hPa`;
document.querySelector(
"#avg-ds18b20-temperature"
).innerHTML = `${json.ds18b20.temperature.avg.value.toFixed(2)} ℃`;
).innerHTML = `${json.ds18b20.temperature.avg.value.toFixed(2)}℃`;
document.querySelector(
"#avg-pms5003-pm1-0"
).innerHTML = `${json.pms5003["pm1.0"].avg.value.toFixed(2)} μg/m³`;
Expand All @@ -326,16 +267,16 @@ function loadStats() {

document.querySelector(
"#amp-bme280-temperature"
).innerHTML = `${json.bme280.temperature.amp.value.toFixed(2)} ℃`;
).innerHTML = `${json.bme280.temperature.amp.value.toFixed(2)}℃`;
document.querySelector(
"#amp-bme280-humidity"
).innerHTML = `${json.bme280.humidity.amp.value.toFixed(2)} %`;
).innerHTML = `${json.bme280.humidity.amp.value.toFixed(2)}%`;
document.querySelector(
"#amp-bme280-pressure"
).innerHTML = `${json.bme280.pressure.amp.value.toFixed(2)} hPa`;
document.querySelector(
"#amp-ds18b20-temperature"
).innerHTML = `${json.ds18b20.temperature.amp.value.toFixed(2)} ℃`;
).innerHTML = `${json.ds18b20.temperature.amp.value.toFixed(2)}℃`;
document.querySelector(
"#amp-pms5003-pm1-0"
).innerHTML = `${json.pms5003["pm1.0"].amp.value} μg/m³`;
Expand Down Expand Up @@ -406,3 +347,5 @@ loadStats();
document
.querySelector("#refresh-statistics")
.addEventListener("click", loadStats);

setInterval(refreshCurrentReadings, 30_000);
7 changes: 5 additions & 2 deletions static/js/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const textTranslations = [
"Średnia",
"Amplituda",
"Podziękowania",
"Wystąpił błąd",
];

const titleTranslations = {
Expand Down Expand Up @@ -61,6 +62,8 @@ if (window.navigator.language.split("-")[0] == "pl") {
e.title = elementsTitlesTranslations[e.title];
});

document.querySelector("#acknowledgements-link").href = "https://github.com/bartekl1/meteo/blob/main/ACKNOWLEDGEMENTS_PL.md";
document.querySelector("#changelog-link").href = "https://github.com/bartekl1/meteo/blob/main/CHANGELOG_PL.md";
document.querySelector("#acknowledgements-link").href =
"https://github.com/bartekl1/meteo/blob/main/ACKNOWLEDGEMENTS_PL.md";
document.querySelector("#changelog-link").href =
"https://github.com/bartekl1/meteo/blob/main/CHANGELOG_PL.md";
}
Loading

0 comments on commit fdb0f61

Please sign in to comment.