first commit
This commit is contained in:
BIN
sound_meter/sound_meter
Executable file
BIN
sound_meter/sound_meter
Executable file
Binary file not shown.
91
sound_meter/sound_meter.c
Executable file
91
sound_meter/sound_meter.c
Executable file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
Code pour lire la data du I2C decibel meter
|
||||
Pour compiler le code: gcc -o sound_meter sound_meter.c
|
||||
Connexion I2C:
|
||||
SDA to Pi Pico GPIO 2
|
||||
SCL to Pi Pico GPIO 3
|
||||
|
||||
Device connected on addr 0x48
|
||||
|
||||
to start the script:
|
||||
sudo /var/www/nebuleair_pro_4g/sound_meter/sound_meter
|
||||
|
||||
0x00 : VERSION register
|
||||
0x01 : Device ID (byte 1)
|
||||
0x02 : Device ID
|
||||
0x03 : Device ID
|
||||
0x04 : Device ID (byte 4)
|
||||
0x05 : SKRATCH register
|
||||
0x06 : CONTROL register
|
||||
0x07 : TAVG high register
|
||||
0x08 : TAVG low register
|
||||
0x09 : RESET register
|
||||
|
||||
0x0A : Decibel register (Latest sound intensity value in decibels, averaged over the last Tavg time period.)
|
||||
|
||||
0x0B : Min register (Minimum value of decibel reading captured since power-up or manual reset of MIN/MAX registers.)
|
||||
0x0C : Max register (Maximum value of decibel reading captured since power-up or manual reset of MIN/MAX registers.)
|
||||
|
||||
#Le Tavg est programmé sur 2 octets: le high byte et le low byte.
|
||||
0x07 #Averaging time (high byte) in milliseconds for calculating sound intensity levels. (default 0x03 -> 3)
|
||||
0x08 #Averaging time (low byte) in milliseconds for calculating sound intensity levels. (default 0xE8 -> 232)
|
||||
valeur = (Tavg_high x 256) + Tavg_low
|
||||
= (3 * 256) + 232
|
||||
= 1000 ms
|
||||
= 1 s
|
||||
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int file;
|
||||
char data;
|
||||
|
||||
// Open I2C1 for reading the sound meter module registers
|
||||
if ((file = open("/dev/i2c-1", O_RDWR)) < 0)
|
||||
{
|
||||
perror("Failed to open I2C1!");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// 0x48 is the decibel meter I2C address
|
||||
if (ioctl(file, I2C_SLAVE, 0x48) < 0)
|
||||
{
|
||||
perror("db Meter module is not connected/recognized at I2C addr = 0x48");
|
||||
close(file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Decibel value is stored in register 0x0A
|
||||
data = 0x0A;
|
||||
|
||||
// Write the register address (0x0A) to the device
|
||||
if (write(file, &data, 1) != 1)
|
||||
{
|
||||
perror("Failed to write register 0x0A");
|
||||
close(file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Read one byte from the device
|
||||
if (read(file, &data, 1) != 1)
|
||||
{
|
||||
perror("Failed to read register 0x0A");
|
||||
close(file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Display the sound level
|
||||
printf("%d\n", data);
|
||||
|
||||
// Close the I2C connection
|
||||
close(file);
|
||||
return 0;
|
||||
}
|
||||
BIN
sound_meter/sound_meter_moving_avg
Executable file
BIN
sound_meter/sound_meter_moving_avg
Executable file
Binary file not shown.
122
sound_meter/sound_meter_moving_avg.c
Executable file
122
sound_meter/sound_meter_moving_avg.c
Executable file
@@ -0,0 +1,122 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#define MAX_MEASURES 60 // To store 60 sound level measurements
|
||||
|
||||
int main()
|
||||
{
|
||||
int file;
|
||||
char data;
|
||||
int soundLevels[MAX_MEASURES];
|
||||
int index = 0;
|
||||
int sum = 0;
|
||||
int i;
|
||||
FILE *fileOutput; // File pointer for writing to file
|
||||
|
||||
|
||||
// Initialize the soundLevels array
|
||||
for (i = 0; i < MAX_MEASURES; i++) {
|
||||
soundLevels[i] = 0;
|
||||
}
|
||||
|
||||
// Open I2C1 for reading the sound meter module registers
|
||||
if ((file = open("/dev/i2c-1", O_RDWR)) < 0)
|
||||
{
|
||||
perror("Failed to open I2C1!");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// 0x48 is the decibel meter I2C address
|
||||
if (ioctl(file, I2C_SLAVE, 0x48) < 0)
|
||||
{
|
||||
perror("db Meter module is not connected/recognized at I2C addr = 0x48");
|
||||
close(file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
// Decibel value is stored in register 0x0A
|
||||
data = 0x0A;
|
||||
|
||||
// Write: send 1 byte from data (memory address pointer) to file
|
||||
if (write(file, &data, 1) != 1)
|
||||
{
|
||||
perror("Failed to write register 0x0A");
|
||||
close(file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Read the sound level from register 0x0A
|
||||
if (read(file, &data, 1) != 1)
|
||||
{
|
||||
perror("Failed to read register 0x0A");
|
||||
close(file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Insert the new reading into the array
|
||||
sum -= soundLevels[index]; // Subtract the old value to maintain the sum
|
||||
soundLevels[index] = data; // Store the new value
|
||||
sum += soundLevels[index]; // Add the new value to the sum
|
||||
|
||||
// Move to the next index, wrap around if needed
|
||||
index = (index + 1) % MAX_MEASURES;
|
||||
|
||||
// Calculate the moving average (sum of the last 60 values)
|
||||
int movingAverage = sum / MAX_MEASURES;
|
||||
|
||||
|
||||
|
||||
// Find the max and min values in the soundLevels array
|
||||
int max = soundLevels[0];
|
||||
int min = soundLevels[0];
|
||||
for (i = 1; i < MAX_MEASURES; i++)
|
||||
{
|
||||
if (soundLevels[i] > max) {
|
||||
max = soundLevels[i];
|
||||
}
|
||||
if (soundLevels[i] < min) {
|
||||
min = soundLevels[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Open the file to write the moving average (overwrite)
|
||||
fileOutput = fopen("/var/www/nebuleair_pro_4g/sound_meter/moving_avg_minute.txt", "w");
|
||||
|
||||
if (fileOutput == NULL)
|
||||
{
|
||||
perror("Failed to open file for writing");
|
||||
close(file);
|
||||
exit(1);
|
||||
}
|
||||
// Write the moving average to the file
|
||||
fprintf(fileOutput, "%d %d %d\n", movingAverage, max, min);
|
||||
// Close the file after writing
|
||||
fclose(fileOutput);
|
||||
|
||||
// Display the current sound level and the moving average
|
||||
printf("Current Sound level: %d\n", data);
|
||||
printf("Current index: %d\n", index);
|
||||
|
||||
printf("Sound levels array: ");
|
||||
for (i = 0; i < MAX_MEASURES; i++)
|
||||
{
|
||||
printf("%d ", soundLevels[i]);
|
||||
}
|
||||
printf("\nMax Sound level: %d\n", max);
|
||||
printf("Min Sound level: %d\n", min);
|
||||
printf("Moving Average (Last 60 seconds): %d\n\n", movingAverage);
|
||||
|
||||
|
||||
// Wait for 1 second before taking the next measurement
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
close(file);
|
||||
return 0;
|
||||
}
|
||||
BIN
sound_meter/sound_meter_nonStop
Executable file
BIN
sound_meter/sound_meter_nonStop
Executable file
Binary file not shown.
75
sound_meter/sound_meter_nonStop.c
Executable file
75
sound_meter/sound_meter_nonStop.c
Executable file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
Code pour lire la data du I2C decibel meter
|
||||
Pour compiler le code: gcc -o sound_meter sound_meter.c
|
||||
Connexion I2C:
|
||||
SDA to Pi Pico GPIO 2
|
||||
SCL to Pi Pico GPIO 3
|
||||
|
||||
Device connected on addr 0x48
|
||||
|
||||
0x0A : Decibel register (Latest sound intensity value in decibels, averaged over the last Tavg time period.)
|
||||
0x0B : Min register (Minimum value of decibel reading captured since power-up or manual reset of MIN/MAX registers.)
|
||||
0x0C : Max register (Maximum value of decibel reading captured since power-up or manual reset of MIN/MAX registers.)
|
||||
|
||||
#Le Tavg est programmé sur 2 octets: le high byte et le low byte.
|
||||
valeur = (Tavg_high x 256) + Tavg_low
|
||||
0x07 #Averaging time (high byte) in milliseconds for calculating sound intensity levels. (default 0x03)
|
||||
0x08 #Averaging time (low byte) in milliseconds for calculating sound intensity levels. (default 0xE8)
|
||||
|
||||
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int file;
|
||||
char data;
|
||||
|
||||
// Open I2C1 for reading the sound meter module registers
|
||||
if ((file = open("/dev/i2c-1", O_RDWR)) < 0)
|
||||
{
|
||||
perror("Failed to open I2C1!");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// 0x48 is the decibel meter I2C address
|
||||
if (ioctl (file, I2C_SLAVE, 0x48) < 0)
|
||||
{
|
||||
perror ("db Meter module is not connected/recognized at I2C addr = 0x48");
|
||||
close (file);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
// Decibel value is stored in register 0x0A
|
||||
data = 0x0A;
|
||||
|
||||
//write: send 1 byte from data (memory address pointer) to file
|
||||
if (write (file, &data, 1) != 1)
|
||||
{
|
||||
perror ("Failed to write register 0x0A");
|
||||
close (file);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
if (read (file, &data, 1) != 1)
|
||||
{
|
||||
perror ("Failed to read register 0x0A");
|
||||
close (file);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
printf ("Sound level: %d dB SPL\r\n\r\n", data);
|
||||
sleep (1);
|
||||
}
|
||||
|
||||
close (file);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user