Hawkes Blog
Windows reboot | Linux be root
Hawkes Blog

a raspberry pi powered baby monitor (1/2)

Short How-To: a raspberry pi powered baby monitor Part 1 (Audio)

prerequisite:
  • raspberry pi running raspbian
  • a webcam (i've used a Logitech HD Webcam C910)
  • a pc with linux as the receiver with speakers (in my case it´s another raspberry)

Prolog:

I was looking for a working solution to turn one of my raspberry pis in a baby monitor for my unborn baby.
The "common" way to set it up is to use Darkice with Icecast, FFMpeg.
All these tutorials are fine and the solutions are working but the audio always have a >5 sec delay which was to much for me.

After some googleing i´d found a nice little tutorial.
Hmm netcat... lets go :)

Setup baby monitor Part 1 (Audio):

1. install netcat

apt-get update
apt-get install netcat

2. prepare Webcam (Audio)

lsusb
>Bus 001 Device 004: ID 046d:0821 Logitech, Inc. HD Webcam C910

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

Webcam is connected and alsa found the mic

amixer -c 1 sset 'Mic' 95

set recording level to 95%

The base audio setup on the baby monitor is finished.


Setup the receiver:

1. install netcat

apt-get update
apt-get install netcat

2. enable the analog output on the receiver (for a raspberry in my case)

amixer cset numid=3 1

3. Test audio

Switch your speakers on, check the alsamixer and try to play some audio on the receiver

alsamixer
aplay /usr/share/sounds/alsa/*

receiver base setup done if you hear the alsa samples :)


Bring it together:

1. Start netcat in listen mode on the babyfone with this small script

#!/bin/bash
DEVICE="plughw:1,0"
PORT=5000
echo "starting netcat for $DEVICE Port $PORT"
while true
do
    echo "Press and hold [CTRL+C] to stop.."
    echo ""
    arecord -f cd -D $DEVICE | netcat -v -l -p $PORT
done
the arecord output is piped to netcat
netcat make the output available on port 5000
the endless loop is needed because netcat will stop working if no data is requested by any client
Please change DEVICE="plughw:1,0" according to your webcam
starting netcat for plughw:1,0 Port 5000
Press and hold [CTRL+C] to stop..

Listening on [0.0.0.0] (family 0, port 5000)
Recording WAVE 'stdin' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
netstat -tulpen | grep 5000
tcp        0      0 0.0.0.0:5000            0.0.0.0:*               LISTEN      0          27251       15497/netcat

script is running and netcat is listening on port 5000

2. get the audio to our receiver

#!/bin/bash
pipe="/var/www/babyfone/pifone.mp3"

if [[ ! -p $pipe ]]; then
    mkfifo $pipe
fi

while true
do
    echo "Press and hold [CTRL+C] to stop.."

    netcat -v <<babyfone ip>> 5000 > $pipe
done
same like on the babyfone
the pipe is used to act as a local "playback file"
netcat connects to the raspberry on port 5000 and pipes the output to the fifo
Please change pipe="/var/www/babyfone/pifone.mp3" accordingly
Press and hold [CTRL+C] to stop..
nothing fancy but the script is started and the system is read to use.

Playback

Starting the audio playback

excecute on the receiver

aplay pifone.mp3
Playing WAVE 'pifone.mp3' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo

you should hear the webcam recording on your receiver with a really small delay <1sec if you set it up correctly | | server output

Recording WAVE 'stdin' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
Connection from [192.168.178.200] port 5000 [tcp/*] accepted (family 2, sport 59223)

and the netcat receiver output

Press and hold [CTRL+C] to stop..
Connection to pifone.home 5000 port [tcp/*] succeeded!

Video Part follows

MfG Hawkes


comments powered by Disqus