Mastodon
Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Sunday, 28 March 2021

PiCorder

I enjoy getting out and about when I can, nature reserves are a great place to go and I've thought a while about recording birdsong and other noises such as rivers and so on. I could use a phone but where's the fun in that and then once you get the phone out there's the distractions of social media. It's nice to leave those things behind.

I decided to knock up a sound recorder using a Raspberry Pi Zero W, it's never going to be brilliant, there's no display or controls on it so the sound is a fixed level but it had to be tried.



I got hold of one of these from Amazon, no drivers needed, just plug it in and it will work is what was said everywhere. Not quite really but in the end it wasn't too hard to get sorted.

I followed all the initial instructions and nothing happened, just an error so more searching on the Google thing found me this.


sudo nano /usr/share/alsa/alsa.conf

defaults.ctl.card 0

defaults.pcm.card 0

Change these to a 1 as shown below.

defaults.ctl.card 1

defaults.pcm.card 1

Save the file and it, sort of. Loads of noise on the recordings and suggestions of adding capacitors across the USB ports to dampen it down seemed drastic. Not a lot seemed to work, looked like it was going to be a complete failure.

So I had a poo and it came to me, first thing was get the recording level
up, I'm running everything here as root, there's nothing on here that is precious.

Run alsamixer and you should see this:

Press F4 to select the capture device and using the arrow keys increase the gain to the top as shown below.


Press escape to leave it.

As mentioned before, I was getting loads of noise when I tried this earlier, I decided to hard wire power and a record button onto the Pi, this meant soldering the power wires to the board on pins 2 and 6, you can see the complete list of pins here.

And the stop start button soldered to pins 36 and 34 (GPIO 16 and Ground)  Don't blame me if you break your Pi while doing this. 

I got hold of one of these so I could connect the mic into the Pi, no cables needed, again from them Amazon people.

Then I decided that as I wanted to keep power usage down I would disable the bluetooth connection and remove the bluetooth software.



Open the config.txt file with

sudo nano /boot/config.txt and then add this to the bottom of the file:


# Disable Bluetooth

dtoverlay=disable-bt

Save and close it then run the following to remove all the bluetooth stuff.

sudo systemctl disable hciuart.service

sudo systemctl disable bluealsa.service

sudo systemctl disable bluetooth.service

 sudo apt-get purge bluez -y

sudo apt-get autoremove -y

I rebooted and carried out a test recording and there was no electrical noise apart from the odd regular buzz, I disabled the wifi and retested and this time no noise at all.

It was now time to stick it in a box and make it work.

I needed a way of getting the files off afterwards, easiest option was install samba with 

apt install samba

Create a directory with 

mkdir /share

Open the samba config with

nano /etc/samba/smb.conf

Add the following at the bottom

[Sounds]

path = /share

public = yes

writeable = yes

browseable = yes

Restart Samba with service smbd restart


I decided to create a sound recording service with:

nano /etc/systemd/system/soundrec.service


[Unit]

Description=Sound Recording

[Service]

ExecStart=/bin/mtcapture

WorkingDirectory=/share

StandardOutput=inherit

StandardError=inherit

Restart=always

User=root

[Install]

WantedBy=multi-user.target


I'm not enabling the service to auto start

Create some files in the bin folder:

nano /bin/mtstart

#!/bin/bash

# shuts down the wifi

ifconfig wlan0 down

service soundrec start

# sleep 1

echo 0 >/sys/class/leds/led0/brightness

# Makes the green LED flash so we know it's recording

echo heartbeat >/sys/class/leds/led0/trigger


nano /bin/mtstop

#!/bin/bash

service soundrec stop

# Starts up the wifi

ifconfig wlan0 up

# sleep 1

echo 0 >/sys/class/leds/led0/brightness

sleep 3

# Leaves the green LED on so we know it's finished

echo 1 >/sys/class/leds/led0/brightness


nano /bin/mtcapture

#!/bin/bash

for (( ; ; ))

do

FILE=/share/filenum

if [ -f "$FILE" ]; then

    echo "$FILE exists."

else

    echo "$FILE does not exist."

echo "0"> /share/filenum

fi

read newfile < /share/filenum

echo $newfile

newfile=$((newfile+1))

echo $newfile > /share/filenum

arecord -f cd /share/AudioTux1-$newfile.wav

done

This increments the file number each time it's run so we can have as many files as we want until we run out of space.



You need to make all these executable with chmod +x filename


Then a little bit of python to get it working

nano record.py


# !/bin/python

import RPi.GPIO as GPIO

import time

from subprocess import call

import os

GPIO.setmode(GPIO.BCM)

GPIO.setup(16,GPIO.IN,pull_up_down=GPIO.PUD_UP)


flag = False

while True:

                input = GPIO.input(16)

                if(input == False):

                        if(flag == False):

                                print "Start"

                                os.system("/bin/mtstart")

                                flag = True

                                time.sleep(0.4)

                        else:

                                print "Stop"

                                os.system("/bin/mtstop")

                                flag = False

                                time.sleep(0.4)



Open up the rc.local file with

nano /etc/rc.local

And add the following just before the exit 0 at the bottom

python2 /root/record.py &


Then restart it, once it's up and running, pressing the button connected to the GPIO pins should start it recording. A second press will stop if, while it's recording the wifi will be disabled. If you want you can add a second button connected to pins 5 and 6 (GPIO 3 and Ground), add the following to the bottom of the /boot/config.txt file.

dtoverlay=gpio-shutdown

Reboot and on press on the button will shut the pi down, a second press will restart it.


Here's the finished item, it's not overly pretty as I got the wrong case but it works, the USB mic at the top can be unplugged and put on an extension cable and possibly fitted into a parabolic dish, but that's for another time.


It does work, seems to take ages to start recording as the service has to start and once started it then takes ages to stop the service.

It needed a rethink and so was born "PiCorder, the Next Generation"

Instructions are here.



Friday, 12 May 2017

Shell Script stuff in Linux

I've wanted to make up a simple script that runs an update on my desktop and then either shuts down, reboots or continues depending on what parameters I add to the end.

Something like;

update reboot

update shutdown

or just update to install the updates and do nothing else.

I managed this with good old DOS many years ago and started to look up how to do this with Linux, I have to say that the vast majority of instructions are based up using foo and bar and are not the easiest things in the world to understand, here's one of the easier examples:

 $ mytest foo bar quux
   There are 3 arguments to mytest: foo bar quux
   first argument: foo
   second argument: bar
   third argument: quux
   here they are again: foo bar quux

I really can't work out how that helps me but it gave me a starting point.

So in plain language here's what I want to do, I've created a script with:

sudo nano /bin/mtupdate

The main part of the script checks for updates and installs the updates:

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get autoremove -y

It then goes onto check what additional parameters have been passed and this is the bit that caused me heartache.

So if I run mtupdate shutdown it does the above and then this bit

# checking for shutdown parameter
if [ "$1" = "shutdown" ]; then
echo "Shutting Down in 10 seconds"
# gives you a chance to change your mind
echo "Press CTRl+C to abort"
sleep 10
echo "Shutting Down"
# shuts down
shutdown -h now
else
if

Then it became harder as I wanted to check for a reboot parameter, to get it all in I had to do the following;

# checks for reboot parameter
if [ "$1" = "reboot" ]; then
echo "Rebooting in 10 seconds"
echo "Press CTRl+C to abort"
sleep 10
echo "Rebooting"
shutdown -r now
else

# checks for shut down parameter
if [ "$1" = "shutdown" ]; then
echo "Shutting Down in 10 seconds"
echo "Press CTRl+C to abort"
sleep 10
echo "Shutting Down"
shutdown -h now

else
# no parameters
clear
echo .
echo.

echo "Updates Completed, please reboot as soon as you can"

fi fi

You have to have a fi at the end for every if in the list


The end product looks like this:

# mtupdate reboot or shutdown
echo "Running Update"
apt-get update
echo "Running Upgrade"
apt-get upgrade -y
echo "Running Dist Upgrade"
apt-get dist-upgrade -y
echo "Running Cleanup"
apt-get autoremove -y
apt-get install -fy

# checking for reboot parameter
if [ "$1" = "reboot" ]; then
echo "Rebooting in 10 seconds"
echo "Press CTRl+C to abort"
sleep 10
echo "Rebooting"
shutdown -r now
else

# checking for shutdown parameter
if [ "$1" = "shutdown" ]; then
echo "Shutting Down in 10 seconds"
echo "Press CTRl+C to abort"
sleep 10
echo "Shutting Down"
shutdown -h now

else
# no parameters entered
clear
echo .
echo .
echo "Updates Completed, please reboot as soon as you can"
fi fi

To finish it off I ran 

sudo chmod +x /bin/mtupdate 

To make it executable

My suggestion to the people that write help stuff is to use a proper example, don't use foo bar quux as this means bugger all to people like me.

My next bit of scripting is going to revolve around taking two parameters and running something based on them, such as

mtbackup source destination

Where mtbackup runs something like rsync or cp or mv

I'll come to that next time round