pimoroni / enviroplus-python

Python library for the Enviro+ environmental monitoring board
https://shop.pimoroni.com/products/enviro-plus
MIT License
399 stars 184 forks source link

Support MEMS microphone #11

Open jk opened 5 years ago

jk commented 5 years ago

I received my enviro+ last week and over the weekend I managed to install the examples and build my own InfluxDB bridge to show all those measurements via Grafana. Had a lot of tinkering fun, thanks :)

But I'm missing a way to interface the MEMS microphone to read out noise levels. I suppose that support is under way, can someone give an ETA or directions where to read further.

ChristophDerndorfer commented 3 years ago
  1. Okay, that then seems to confirm what I thought that there's some sort of difference depending on whether alsamixer is started local or via SSH.
  2. Thanks, I think that will be useful.
  3. Thanks for the confirmation. I ran so many different test and library versions on that Pi Zero that I thought that the long startup might have been caused by some incompatibility issues or whatnot.
ftosteve commented 3 years ago

Not sure if will help anyone, but i've been scratching my head on this for ages. If I followed all the help and did everything through console logged on a pi then everything worked fine. However, every time I tried to sudo the code or remote debug(sudo) then my sound class kept giving an error about unable to find device "dmic_sv"...

After many obscure searches I stumbled upon https://www.alsa-project.org/main/index.php/Asoundrc. In there it suggests you can create the devices in the ~/.asoundrc is AdaFruit instruct, but also in /etc/asound.conf for system wide configuration. I did this and now I can see the "dmic_sv" running under sudo.

Reason, I need this, is my pi has no desktop and my code will run as a Service on boot, so ordinarily not logged in to console. Hope this helps soem others too.

dguk25 commented 3 years ago

hi all . great to see this continually progressing. appreciate the work @roscoe81

I'm wondering about a couple of things:

1) is it possible to have some kind of 'smoothing' to the noise detection, such that there is less fluctuation? even when sound levels seem steady, there is oscillation of around 5 dB. is that potentially due to air fluctuations?.... I've noticed the device is very sensitive to this (just waving hand in front of it registers as quite loud, presumably due to pressure fluctuation at the sensor). has anyone tried to reduce this with a microphone cover etc?

2) how would you recommend streaming the data to a central server if I was to use a number of devices? is installing MQTT the best route and could this be incorporated into your code?

roscoe81 commented 3 years ago

Thanks for your feedback @dguk25.

  1. It would be a simple exercise to smooth the output so it only responded to spl changes beyond a set threshold. However, two issues would need to be considered. Firstly, it’s counter to the desire to provide as accurate noise level readings as possible. Secondly, fluctuations of 5dB are not large. It’s generally accepted that the minimum change in sound level that can be detected by most people is 3dB, so I would expect that the current sensitivity of the noise sensor is consistent with what’s required to monitor noise level changes that a listener would experience .
  2. My Enviro Monitor code sends maximum noise levels every 5 minutes as mqtt messages. That would allow you to send noise level data to a central system, whilst having manageable levels of mqtt traffic. It would be relatively easy to also send minimum and mean levels via mqtt, like I’ve done with the Luftdaten and Adafruit IO noise level capture options in that code. I haven’t done that yet, because I’ve been happy to just use maximum noise level data in my own mqtt data capture system. I expect that streaming the raw microphone data or the measured dB level on every loop via mqtt message would result in heavy mqtt traffic levels and it’s not something that I would recommend. I think it’s much better to do the processing locally and send summarised data via mqtt.
jpiper commented 3 years ago

I followed the instructions here and then the noise-amps-at-freqs.py and noise-profile.py scripts worked

dguk25 commented 3 years ago

I find I'm having to do this step each time I reboot the pi:

nano ~/.asoundrc

copying specific text, then running

arecord -D plughw:0 -c1 -r 48000 -f S32_LE -t wav -V mono -v file.wav

Otherwise it doesn't recognise the i2smic. Is there a way round this @roscoe81 ? Have I missed something in the setup.

roscoe81 commented 3 years ago

I haven't come across that issue @dguk25 and the i2smic is recognised on my unit after each reboot, so I'm not sure that I can help you. The only thing I can suggest is to ask if you set up the auto load at boot in "Auto Load at Boot" in these instructions.

dguk25 commented 3 years ago

Thanks. I have done that (on multiple occasions!). Is ~/.asoundrc meant to be a permanent doc? It seems to need creating after each boot. I'll hopefully be setting up a couple of other devices soon, so will see if I get the same behaviour.

On Thu, 6 May 2021 at 07:06, Ross Fowler @.***> wrote:

I haven't come across that issue @dguk25 https://github.com/dguk25 and the i2smic is recognised on my unit after each reboot, so I'm not sure that I can help you. The only thing I can suggest is to ask if you set up the auto load at boot in "Auto Load at Boot" in these instructions https://learn.adafruit.com/adafruit-i2s-mems-microphone-breakout/raspberry-pi-wiring-test .

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/pimoroni/enviroplus-python/issues/11#issuecomment-833252480, or unsubscribe https://github.com/notifications/unsubscribe-auth/AETBUPWWTGAVXMXSYKGIRM3TMIWWLANCNFSM4H62SO7A .

--

-- http://youngrobot.com/ Young Robot | App Development Specialists Registered in England and Wales. Company No 06076173. Registered Office: International House, 24 Holborn Viaduct, London, EC1A 2BN

Technical Support UK: +44 7872 825 008 |  @. @.>

IMPORTANT NOTICE This e-mail and the information it contains may be privileged and/or confidential. The unauthorized use, disclosure or copying of this e-mail or any information it contains, is prohibited and could, in certain circumstances be a criminal offence. If you are not the intended recipient please notify us immediately and delete this message from your system.

jeolives commented 2 years ago

For anyone in the future who is also trying to make noise-amps-at-freqs.py and noise-profile.py work on their Enviro/+, and it spits out the "adau7002" device not found error - but doesn't want to modify FFsocket as per https://github.com/pimoroni/enviroplus-python/issues/11#issuecomment-524489500 :

  1. Follow the Adafruit instructions from https://learn.adafruit.com/adafruit-i2s-mems-microphone-breakout/raspberry-pi-wiring-test up to and including the i2scmic.py script - set it up so that it loads automatically, but when presented with the option to reboot, don't do that yet with n.
  2. After running the installer script, change directory by cd Raspberry-Pi-Installer-Scripts. Then cd i2s_mic_module
  3. In your text editor of choice, edit the file snd-i2smic-rpi.c. Edit the following line: .card = "snd_rpi_i2s_card", // -> snd_soc_card.name to .card = "adau7002", // -> snd_soc_card.name
  4. Run the following commands in order make clean, make, make install
  5. If this hasn't been done automatically by the script, modify your /boot/config.txt with dtparam=i2s=on (some distros like DietPi)
  6. Now you can reboot
  7. ssh back into your pi, create a new config file in your home directory ~ called .asoundrc with your text editor of choice. Copy and paste the following from @roscoe81:
#This section makes a reference to your I2S hardware, adjust the card name
#to what is shown in arecord -l after card x: before the name in []
#You may have to adjust channel count also but stick with default first
pcm.dmic_hw {
type hw
card adau7002
channels 2
format S32_LE
}

#This is the software volume control, it links to the hardware above and after
#saving the .asoundrc file you can type alsamixer, press F6 to select
#your I2S mic then F4 to set the recording volume and arrow up and down
#to adjust the volume
#After adjusting the volume - go for 50 percent at first, you can do
#something like
#arecord -D dmic_sv -c2 -r 48000 -f S32_LE -t wav -V mono -v myfile.wav
pcm.dmic_sv {
type softvol
slave.pcm dmic_hw
control {
name "Master Capture Volume"
card adau7002
}
min_dB -3.0
max_dB 30.0
}

Format it like so: image

After running these steps, test if everything works through:

Dashbrook commented 7 months ago

Thank you to everyone who has contributed to this awesome thread! Thanks especially to @roscoe81 I can now record from the mic. What is going over my head through is just getting a simple output to a csv e.g. a csv with the noise level every minute or the average noise level in the past minute.

E.g. similar to the other outputs here https://angelaambroz.com/blog/posts/2021/Mar/08/pi_project_2_indoor_environment_monitor/

r-carroll commented 6 months ago

Also want to chime in and mention that with the help of @roscoe81's repo here I was able to successfully get accurate dbA readings. It will be my goal in the next few weeks to write up a script that saves readings to a database every few seconds to get a historical sound pressure record.

IMG_1152

HOD42 commented 6 months ago

That really is EXCELLENT news! Thank you. I'll be experimenting more with this functionality and hopefully improve my Grafana dashboard.

Gadgetoid commented 6 months ago

Sorry for the lack of response here. I don't have a good answer for why it slipped me by, other than: ARGH.

I think it goes without saying that the effort of @roscoe81 is greatly, greatly appreciated. I am not in possession of the time, expertise and headspace to produce as well-rounded a solution. Our software always straddles some awkward line between "feature complete, usable example" and "very basic starter for makers to take and run with" and it doesn't always work out.

Should this library try to implement @roscoe81's code? I believe we should probably have a basic setup in the Enviro library, but also link to @roscoe81's excellent project as a more complete solution. This is so we can direct credit where credit is due, lend some exposure to a good project, but also not simply kick the can for proper noise monitoring support completely down the road.