Mise à jour capteur bruit: ancien I2C → NSRT MK4 USB

- Nouveau script sound_meter/read.py pour lecture à la demande (JSON)
- launcher.php: appel du script Python au lieu de l'ancien binaire C
- sensors.html: carte USB, suppression boutons start/stop, affichage JSON
- Traductions fr/en: I2C → USB, NSRT MK4

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
PaulVua
2026-03-12 15:23:54 +01:00
parent 98b5b43190
commit 7479344df7
5 changed files with 72 additions and 33 deletions

View File

@@ -23,9 +23,9 @@
"press": "Pressure"
},
"noise": {
"title": "Decibel Meter",
"description": "Noise sensor on I2C port.",
"headerI2c": "I2C Port"
"title": "NSRT MK4",
"description": "NSRT MK4 sound level meter on USB port.",
"headerUsb": "USB Port"
},
"envea": {
"title": "Envea Probe",

View File

@@ -23,9 +23,9 @@
"press": "Pression"
},
"noise": {
"title": "Sonomètre",
"description": "Capteur bruit sur le port I2C.",
"headerI2c": "Port I2C"
"title": "NSRT MK4",
"description": "Sonomètre NSRT MK4 sur port USB.",
"headerUsb": "Port USB"
},
"envea": {
"title": "Sonde Envea",

View File

@@ -832,7 +832,7 @@ if ($type == "envea_debug") {
}
if ($type == "noise") {
$command = '/var/www/nebuleair_pro_4g/sound_meter/sound_meter';
$command = 'sudo /usr/bin/python3 /var/www/nebuleair_pro_4g/sound_meter/read.py';
$output = shell_exec($command);
echo $output;
}

View File

@@ -265,35 +265,43 @@
function getNoise_values() {
console.log("Data from I2C Noise Sensor:");
console.log("Data from NSRT MK4 Noise Sensor:");
$("#loading_noise").show();
$.ajax({
url: 'launcher.php?type=noise',
dataType: 'text',
method: 'GET', // Use GET or POST depending on your needs
dataType: 'json',
method: 'GET',
success: function (response) {
console.log(response);
const tableBody = document.getElementById("data-table-body_noise");
tableBody.innerHTML = "";
$("#loading_noise").hide();
// Create an array of the desired keys
const keysToShow = ["Noise"];
// Add only the specified elements to the table
keysToShow.forEach(key => {
if (response !== undefined) { // Check if the key exists in the response
const value = response;
if (response.error) {
$("#data-table-body_noise").append(`
<tr><td colspan="2" class="text-danger">${response.error}</td></tr>
`);
return;
}
const rows = [
{ label: "LEQ", value: response.LEQ + " dB" },
{ label: "dB(A)", value: response.dBA + " dBA" },
{ label: "Weighting", value: response.weighting },
{ label: "Tau", value: response.tau + " s" }
];
rows.forEach(row => {
$("#data-table-body_noise").append(`
<tr>
<td>${key}</td>
<td>${value} DB</td>
<td>${row.label}</td>
<td>${row.value}</td>
</tr>
`);
}
});
},
error: function (xhr, status, error) {
$("#loading_noise").hide();
console.error('AJAX request failed:', status, error);
}
});
@@ -496,21 +504,18 @@
container.innerHTML += i2C_BME_HTML; // Add the I2C card if condition is met
}
//creates i2c sound card
//creates NSRT MK4 noise sensor card (USB)
if (config.NOISE) {
const i2C_HTML = `
const noiseHTML = `
<div class="col-sm-3">
<div class="card">
<div class="card-header" data-i18n="sensors.noise.headerI2c">
Port I2C
<div class="card-header" data-i18n="sensors.noise.headerUsb">
Port USB
</div>
<div class="card-body">
<h5 class="card-title" data-i18n="sensors.noise.title">Decibel Meter</h5>
<p class="card-text" data-i18n="sensors.noise.description">Capteur bruit sur le port I2C.</p>
<h5 class="card-title" data-i18n="sensors.noise.title">NSRT MK4</h5>
<p class="card-text" data-i18n="sensors.noise.description">Sonomètre NSRT MK4 sur port USB.</p>
<button class="btn btn-primary mb-1" onclick="getNoise_values()" data-i18n="common.getData">Get Data</button>
<br>
<button class="btn btn-success" onclick="startNoise()" data-i18n="common.startRecording">Start recording</button>
<button class="btn btn-danger" onclick="stopNoise()" data-i18n="common.stopRecording">Stop recording</button>
<div id="loading_noise" class="spinner-border spinner-border-sm" style="display: none;" role="status"></div>
<table class="table table-striped-columns">
<tbody id="data-table-body_noise"></tbody>
@@ -519,7 +524,7 @@
</div>
</div>`;
container.innerHTML += i2C_HTML; // Add the I2C card if condition is met
container.innerHTML += noiseHTML;
}
//creates MH-Z19 CO2 card

34
sound_meter/read.py Normal file
View File

@@ -0,0 +1,34 @@
'''
____ ___ _ _ _ _ ____
/ ___| / _ \| | | | \ | | _ \
\___ \| | | | | | | \| | | | |
___) | |_| | |_| | |\ | |_| |
|____/ \___/ \___/|_| \_|____/
python3 /var/www/nebuleair_pro_4g/sound_meter/read.py
Read current values from NSRT MK4 Sound Level Meter (USB /dev/ttyACM0)
Used by the web interface "Get Data" button via launcher.php
'''
import json
import nsrt_mk3_dev
try:
nsrt = nsrt_mk3_dev.NsrtMk3Dev('/dev/ttyACM0')
leq_level = nsrt.read_leq()
weighted_level = nsrt.read_level()
weighting = nsrt.read_weighting()
time_constant = nsrt.read_tau()
data = {
"LEQ": round(leq_level, 2),
"dBA": round(weighted_level, 2),
"weighting": str(weighting),
"tau": time_constant
}
print(json.dumps(data))
except Exception as e:
print(json.dumps({"error": str(e)}))