Monday, 18 April 2016

An Audio/Video Raspberry Pi Beehive Monitoring System

This winter's project (2016) was to get audio and video back from the hives, to compliment the BeeBug system based on an Arduino. The Arduino used was a Sweetpea from Sweden, but I ran out of memory on that because the WiFi software module took so much space. So adding more functionality to this hardware base was not an option. I was aware of the Raspberry Pi but never used one and had no feel for what it could do. So I decided on a two pronged attack. I ordered a WiFi Bridge module (£18 on eBay) for a basic Arduino Uno I had in stock and I ordered a Raspberry Pi (£35). I'd thought I'd see which was best suited.

Arduino Uno with Bridge
--------------------------------
I won't spend much time on this because I rapidly came to the conclusion that there was no easy way to transfer video or audio, at least none that I could find. However it should be noted that the Uno + Bridge (equivalent to a Yun) is a really powerful combination and if I want to do another BeeBug this is the way to go. There is a second computer running Linux and dedicated to the WiFi control which frees off the Arduino cpu and provides loads of memory for the user. In addition it provides remote programming - a major step forward. I may get back to this system for a Beebug update, the existing shield should plug on top of the bridge.

Raspberry Pi
----------------
To get this off the ground requires
- a Raspberry Pi with a Pi box
- a 1A power supply to provide power via the micro USB socket
- a USB keyboard
- a USB mouse
- a screen with HDMI input
- an ethernet link to the wifi hub
and an 8Gb micro SD card with a Linux OS burned in, I downloaded Raspbian as this seems the favourite.

Having got that the Pi up and running it then has to be customised to do the job in hand. For this  application it needs two extra items
- a USB microphone
- a Raspberry Pi camera module
- a suitable box for the Pi
- a WiFi dongle

Once the camera is running it doesn't need the mouse, keyboard or screen, it's operated in 'headless mode'.
The software has to be constructed by downloading and installing various support modules and the sequence for this is outlined in the Appendix. This will be intelligible to Pi aficionados but not to anyone else. It can be used as a template but is not detailed enough for a newcomer to work from. The detail is however soon gleaned from the mass of Raspberry Pi support on the internet.

All the special software routines were written for the Bourne shell so are initiated via the terminal window. Once the wifi is running the terminal window can be opened on a Windows Pc by using a utility called Putty. Find the Pi's IP address from the top of the screen, or in headless mode use a browser to display the wifi router page (see the router manual to get the IP address of the router, or open a Windows command box and run ipconfig).

To test the camera run picamstill.sh as below. In the terminal window, type './picamstill.sh' and a time and date stamped picture should appear in the camera directory. (create a camera directory with 'mkdir camera'

################
#take a picture
#picamstill.sh
################
#!/bin/bash
DATE=$(date +"%Y-%m-%d_%H%M")
raspistill -vf -hf -o /home/pi/camera/$DATE.jpg
################

At the PC start a utility called WinSCP, log in to the pi, and transfer the still to view it a photo viewer.


First I produced a simple video streaming software called getvid.sh

###############
#stream video to VLC
#picam.sh
###############
raspivid -o - -t 0 -n -w 1200 -h 800 -fps 16 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' :demux=h264
###############
[^C to stop]

This starts a video transfer until I press Ctrl C. To see the video stream on the PC I used a media player called VLC. From the VLC menu select Open Network Stream an enter rtsp://192.168.1.196:8554/ where 192.168.1.196 is the Pi IP, and click Play.

Here is a similar program for audio streaming. Use VLC again to listen.

###############
#stream audio to VLC
#listen.sh
###############
arecord -D plughw:1,0 -f dat | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' dat
###############
[^C to stop]

It's a bit hit and miss whether you catch the stream or not on VLC but it works most of the time. The problem is that on my own wifi, linking via a repeater down the garden, the video breaks up quite a bit. And the hit and miss aspect soon drives you mad. So I decided to try another approach. I generated a program that will upload a preset length of video, on demand, into my Dropbox. This is shown below for a 10 sec test clip. Change the 10000 to a different number of mS as required.

##############
#record predefined no of mS of video and upload to Dropbox
#apps folder - 1Mb per second @ mode 4
#getdbvid.sh
##############
DATE=$(date +"%Y-%m-%d_%Hh%Mm")
raspivid -t 10000 -vf -hf -md 4 -fps 30 -o /home/pi/Dropbox-Uploader/$DATE.h264
cd Dropbox-Uploader
./dropbox_uploader.sh upload $DATE.h264 $DATE.h264
rm $DATE.h264
cd ..
##############

This requires Dropbox support software at the Pi and an App on your Dropbox. Instructions to link it to your Dropbox are at the following website.
https://github.com/andreafabrizi/Dropbox-Uploader

A similar routine to get a piece of audio is getdbmic.sh shown below for a 30sec sequence. Modify it with
sudo nano getdbmic.sh

##############
#!/bin/sh
#record predefined no of Secs sound and upload to Dropbox as wav
#getdbmic.sh
##############
DATE=$(date +"%Y-%m-%d_%Hh%Mm")
arecord -D plughw:1 -f dat --duration=30 /home/pi/Dropbox-Uploader/$DATE.wav
cd Dropbox-Uploader
./dropbox_uploader.sh upload $DATE.wav $DATE.wav
rm $DATE.wav
cd ..
##############

And yet another routine will put 2592 x 1944 stills in the Dropbox folder.

##############
#!/bin/sh
#record a still and upload to Dropbox
#getdbstill.sh
##############
#!/bin/sh
DATE=$(date +"%Y-%m-%d_%Hh%Mm")
raspistill -vf -hf -q 90 -o /home/pi/Dropbox-Uploader/$DATE.jpg
cd Dropbox-Uploader
./dropbox_uploader.sh upload $DATE.jpg $DATE.jpg
rm $DATE.jpg
cd ..
##############


Using the Pi Bee Monitor
=========================
The camera has been mounted in front of the hive as shown, using some bits I found in my garage. The lead to the mic can be seen at the entrance. A protective cover, made from an ice cream container, was added - retained by self-adhesive Velcro pads. Velcro was also used to secure the camera to the support assembly to facilitate rapid removal.






Once set up it's easy to use.
-log on to the terminal window (pi and raspberry are the default user and password)
-type ./getdbstill.sh or ./getdbvid.sh or ./getdbvid.sh and wait for them to appear in your Dropbox!

It's interesting to hear the difference in bee-noise at different times. Late at night it's a gentle buzz and a lot loader during the day - as you'd expect. I'm hoping that after a while I'll recognise different 'moods'. I'll also be doing some frequency analysis on the audio clips to see if that reveals anything. Certainly when they are in swarming mode it should be interesting. Being able to predict a swarm by analysing the audio is of course the holy grail.

On the video side it's nice to see if they are out without having to leave my armchair! For the future I'll be looking at some more complex software that does frame comparisons so that I can get it to detect rapid build-up of bees on the hive face. A message to my mobile would then alert me to a possible swarm situation. All interesting work for the future.

To get information from outside the local WiFi network I found setting up a free Weaved account does the job nicely. You can then see/hear your bees from anywhere in the world!

I tend to leave only a few videos, stills and sound clips in my Dropbox to avoid too much clutter. If anyone wants to examine the contents here's the link
https://www.dropbox.com/sh/3vonvtyt66v7udj/AAAd9SZyRI0s8D3oEhepfuBqa?dl=0



Appendix - Software Installation for Pi Beehive Audio/Video Monitor
====================================================================
burn Raspbian-jessie image to 8Gb+ SD card [use Etcher s/w]
Connect mouse, keybd, ethernet, hdmi screen - boot up
sudo raspi-config [option 1, change password, enable camera]
df -h [check memory space looks ok]
sudo dpkg-reconfigure tzdata [config time zone]
sudo date -s "10 JAN 2016 20:37:00"  [set time & date]
sudo apt-get update
sudo apt-get upgrade [get latest changes - takes an hour or two]
sudo nano /etc/hosts  [change network name from raspberrypi if reqd]
sudo nano /etc/hostname [change hostname]  sudo /etc/init.d/hostname.sh [reboot]
sudo modprobe snd_bcm2835 [add support for USB microphone]

raspistill -v -o test.jpg [test camera]
 
[add .sh files to /home/pi from backup on PC using WinSCP]
sudo chmod +x all .sh files

sudo apt-get install vlc [for streaming to XP PC]

Setup wifi:-
If required, disable onboard WiFI on Pi3 by adding
dtoverlay=pi3-disable-wifi
to   /boot/config.txt

lsusb [check WiFi dongle recognised]
sudo iwgetid [see which network it's on]
sudo iwlist wlan0 scan [report back on all wifi seen]

sudo nano /etc/network/interfaces [no change]

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
  add
network={ssid="myWiFi-1" psk="myPassword-1"}
network={ssid="myWiFi-2" psk="myPassword-2"}
  add as many as you like, useful if you intend moving the Pi to other locations.

If you have problems with hang-ups on WiFi - as I have had - you can force a reboot every hour by adding a line to crontab
type 'crontab-e' and add
59 * * * * ./myreboot.sh
where myreboot.sh is a script containing
sudo reboot
This is a bit crude but it works.

Set up a WEAVED account for remote access over web. When installed it will add an autostart to the root crontab (accessed by sudo crontab -e ). It should look like '@reboot /usr/bin/weavedstart.sh'

========================================================================



No comments:

Post a Comment