Mar 102013
 

I’ve had a few queries regarding streaming audio as well as video on the Raspberry Pi, so tonight I set up my little Raspberry Pi with a Logitech C110. This webcam also has a microphone integrated which the Pi can use to record audio.
Everything has been setup as per part 3 of my ffmpeg streaming guide.

A little investigation reveals which hardware device the microphone is recognised as.

# arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: C110 [Webcam C110], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0

So the device is recognised as card 1. This comes in later when we are setting up the audio part of the streaming.

I’ve updated the webcam.sh script to reflect the fact that we are now recording sound –

ffserver -f /root/ff.conf & ffmpeg -vcodec mjpeg -v verbose -r 15 -s 176x128 -f video4linux2 -i /dev/video0 -f alsa -ac 1 -i hw:1 http://localhost:81/webcam.ffm

And I also need to update ff.conf with the new streaming settings
The Stream webcam section has now turned into this –
<Stream webcam.avi>
Feed webcam.ffm
Format avi
VideoSize 176x128
VideoFrameRate 15
VideoBufferSize 40
VideoBitRate 64
AudioBitRate 32
AudioChannels 1
AudioSampleRate 11025
VideoQMin 1
VideoQMax 20

With this configuration, I could start ffmpeg with audio and video streaming. However I could not get my laptop with vlc to connect to it for now, so I’ll have to keep investigating that.
Hopefully this helps though.


Please support the continued development of any useful scripts and tutorials that you have found useful !




Share
Feb 182013
 

I recently setup motion on my Raspberry Pi so I could stream a Logitech c110 to my Android phone. That was successfully setup in this post.

Next step was to start sending the snapshots to my email address when motion was detected. That way I could see if anything was being detected without having to log into the Raspberry Pi itself.

As I started off with RSE, there is no mail daemon installed, so that’s the first thing I need to do.
sudo apt-get install postfix

Configuration for postfix will depend on where you want email to be routed to. I will be sending it to GMail, and my ISP needs the mail to go through their mail server, so I selected Internet with smarthost
Next thing to setup is the local mail client. There are a few options, but in this post we will use heirloom-mailx.
sudo apt-get install heirloom-mailx

We can test out the Postfix install by attempting to send a test email with our newly installed mail client now !
echo 'hi' | mail -s Test <Your Email Address>
This will send an email to your email address with a body text of “hi” and the subject of “Test”.
If you receive it, congratulations ! Your Pi is now emailing out.
If not then you may need to check your junk mail to see if it got junked.
If there are any issues getting the mail out, running mail on the Pi will launch the mail client in which you will see any failed attempts to send an email.

Once Postfix and the local email client is setup, it’s time to configure motion.
If you’re only running one camera, in /etc/motion/motion.conf at approximately line 521, you should see this
; on_picture_save value
This is the line that configures what motion will do to a file once it is saved.

What we will do, is setup motion to attach the file to an email, and send it to your email address when motion is detected and a file is saved.
To do that, change line 521 to the following –
on_picture_save echo 'webcam alert' | mail -a %f -s "Webcam Alert" <Your Email Address>
Make sure you remove the ; at the beginning as that denotes a commented out line.
The ‘webcam alert’ text can be changed to whatever you want in your version.
Running mail with the -a command line argument will attach the file specified, in this case it is %f, which is motion’s variable for the saved file.

Once that is done, restart motion with /etc/init.d/motion restart and whenever you get motion detected, it will email you the picture as an attachment !

Share
Feb 122013
 

So I recently got my original 256MB Raspberry Pi operational again. I decided to try out Motion on it as I hadn’t tried it out on the Pi before.

Starting out with a fresh install of Raspbian Server Edition, I apt-getted motion with sudo apt-get install motion which installed all the relevant packages. I plugged in my Logitech C110 webcam, which got detected beautifully.

Afterwards, I modified /etc/motion/motion.conf to configure the settings
The following code block contains the important bit of the settings that I used

width 320
height 240
framerate 5
threshold 4500
quality 50
webcam_quality 25
webcam_maxrate 5
webcam_localhost off
control_localhost off

After modifying /etc/motion/motion.conf, I modified /etc/default/motion to enable the daemon to start, and started motion via the /etc/init.d/motion start command.

Trying to view the feed on the Pi via the web interface was a success !
To view it, simply visit http://pi ip address:8081 and you should get a stream.
With those settings, I get a slight delay in the webcam feed from motion, about 1 second, but nothing major.
CPU is sitting on around 15% with just the feed.
I will trial out some motion detection later on, as well as trying motion out via WiFi as it is currently plugged in to ethernet.

Share
Feb 102013
 

As a follow up this this post, the script that I used had a few issues running on the server that I was using it on.
For some reason, on my server, the http interface would randomly die on me and then the script would not be able to stop or start motion.

So I modified the old one to this one, but it necessitated some modifications to the startup script for motion as trying to start motion via a shell script did not work due to the paths used within the startup script.

In the startup script /etc/init.d/motion, the full path is not used for start-stop-daemon. This means that when motion is started from a shell script, the command start-stop-daemon is not found. To rectify this, add /sbin/ to all instances of start-stop-daemon. The following example shows one of the instances of start-stop-daemon. The example begins on line 50, and the start-stop-daemon command is the last line of the example.

case "$1" in
start)
if check_daemon_enabled ; then
if ! [ -d /var/run/motion ]; then
mkdir /var/run/motion
fi
chown motion:motion /var/run/motion

log_daemon_msg "Starting $DESC" "$NAME"
if /sbin/start-stop-daemon --start --oknodo --exec $DAEMON -b --chuid motion ; then

In the updated script, I have added a few checks to ensure that the http interface is still running, and if it isn’t the script will kill motion altogether, and then restart motion to get everything up and running again.

The updated script is below –

#!/bin/bash
PeopleAround=0
people=( 192.168.2.72 192.168.2.117 192.168.2.129 )

wget -o getlog -O /usr/scripts/status http://localhost:8080/0/detection/status
if [ -f /usr/scripts/status ]; then
echo "SettingMotion"
MotionStarted=`cat /usr/scripts/status | sed -e 's/<[^>]*>//g' | tail -n 3 | head -n -2`
else
echo "Status file not available"
echo "Attempting to start motion"
/etc/init.d/motion start
exit
fi

sleep 5

if [ -f /usr/scripts/status ]; then
echo Checking Size
StatusSize=$(stat -c%s /usr/scripts/status)
if [[ $StatusSize -eq 0 ]]; then
echo Size is zero
MotionStarted=`pidof motion | wc -l`
if [[ $MotionStarted -eq 1 ]]; then
echo Motion web interface failed. Restarting motion
killall -9 motion
echo "HTTP interface has failed. killing motion and restarting" | mail -s 'Motion killed'
/etc/init.d/motion restart
else
echo Motion not started.
echo Starting Motion
/etc/init.d/motion start
fi
fi
else
echo "Status file not available"
exit
fi

for ip in "${people[@]}"; do
PersonAround=`ping -c 1 $ip | grep '64 bytes' | wc -l`
if [[ PersonAround -eq 1 ]]; then
PeopleAround=1
break
fi
done

if [[ $PeopleAround -eq 1 ]]; then
if [[ "$MotionStarted" =~ "ACTIVE" ]]; then
wget -O /usr/scripts/pause http://127.0.0.1:8080/0/detection/pause
echo "People around, stopping motion" | mail -s 'Motion stopped'
echo "People around, stopping motion"
fi
else
if [[ "$MotionStarted" =~ "PAUSE" ]]; then
wget -O /usr/scripts/start http://127.0.0.1:8080/0/detection/start
echo "No One around, Starting motion" | mail -s 'Motion started'
echo "No One around, Starting motion"
fi
fi
if [ -e /usr/scripts/status ]; then
rm /usr/scripts/status
fi

Share
Jan 202013
 

I have been playing around with Motion on one of my Linux boxes, and got annoyed at the constant stream of emails that I am receiving as I have setup Motion to send an email to myself when it detects motion, so I wrote up a short script to activate or deactivate motion if it can pick up certain devices by pinging them.

It’s not foolproof as my wifi is playing up a bit and some devices do not respond by ping sometimes, but it’s useful for deactivating something when you approach and reactivating it when you leave, or vice versa depending on your use of this script.
Setting this script up as a cronjob will allow it to run every say 5 minutes for automation.


#!/bin/bash
PeopleAround=0
people=( 192.168.2.72 192.168.2.117 )
MotionStarted=`wget -o getlog -O /usr/scripts/status http://localhost:8080/0/detection/status | cat /usr/scripts/status | sed -e 's/<[^>]*>//g' | tail -n 3 | head -n -2`

for ip in "${people[@]}"; do
PersonAround=`ping -c 1 $ip | grep '64 bytes' | wc -l`
if [[ PersonAround -eq 1 ]]; then
PeopleAround=1
break
fi
done

if [[ $PeopleAround -eq 1 ]]; then
if [[ "$MotionStarted" =~ "ACTIVE" ]]; then
wget -O /usr/scripts/status http://127.0.0.1:8080/0/detection/pause
echo "People around, stopping motion" $run | mail -s 'Motion stopped' YOUR@EMAIL.COM
fi
else
if [[ "$MotionStarted" =~ "PAUSE" ]]; then
wget -O /usr/scripts/status http://127.0.0.1:8080/0/detection/start
echo "No One around, Starting motion" $run | mail -s 'Motion started' YOUR@EMAIL.com
fi
fi

Share