diff --git a/html/lang/en.json b/html/lang/en.json index 8947b4c..8323d88 100644 --- a/html/lang/en.json +++ b/html/lang/en.json @@ -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", diff --git a/html/lang/fr.json b/html/lang/fr.json index 88a20de..4683d78 100644 --- a/html/lang/fr.json +++ b/html/lang/fr.json @@ -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", diff --git a/html/launcher.php b/html/launcher.php index 53f23c7..e4b63f4 100755 --- a/html/launcher.php +++ b/html/launcher.php @@ -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; } diff --git a/html/sensors.html b/html/sensors.html index 963b778..820444a 100755 --- a/html/sensors.html +++ b/html/sensors.html @@ -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; - $("#data-table-body_noise").append(` - - ${key} - ${value} DB - - `); - } + if (response.error) { + $("#data-table-body_noise").append(` + ${response.error} + `); + 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(` + + ${row.label} + ${row.value} + + `); }); }, 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 = `
-
- Port I2C +
+ Port USB
-
Decibel Meter
-

Capteur bruit sur le port I2C.

+
NSRT MK4
+

Sonomètre NSRT MK4 sur port USB.

-
- - @@ -519,7 +524,7 @@ `; - container.innerHTML += i2C_HTML; // Add the I2C card if condition is met + container.innerHTML += noiseHTML; } //creates MH-Z19 CO2 card diff --git a/sound_meter/read.py b/sound_meter/read.py new file mode 100644 index 0000000..3a02623 --- /dev/null +++ b/sound_meter/read.py @@ -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)}))