Hi, I made this as a component for my elephant detection system. The aim was to use it to play sounds of bees in order to scare away elephants when they try to steal crops! But, the general idea might be useful to adapt to many other applications!
You can see the entire thing working in the video above!
My elephant detection and deter project is here: https://hackaday.io/project/20448-elephant-ai
* please let me know if you get stuck, I've dealt with various errors, so should be able to help!
Step 1: Parts Required
The parts required are:
- Raspberry Pi 3
- JustBoom Amp Zero pHAT for the Raspberry Pi Zero (£24)
- Raspberry Pi Zero W with Soldered Header (£14)
- of course you can solder them on yourself, which would mean a cost of £9 + headers e.g. £1
- micro SD card with operating system installed. I've used 16GB with NOOBS pre-installed (£9.99)
- Power supply for Amp Zero
- Speakers x2, I'm using two waterproof 5V 4Ohm speakers from ebay (£11 each)
Step 2: Attach the Pi Zero and the Amp Zero Together
First we need to attach the amp zero HAT to the pi zero! This is really easy and you just need a tiny phillips screwdriver to do it. There are 4x metal spacers (well they look like nuts really), and 8x metal bolts.
1. Put you SD card into the pi zero card reader slot!
2. Fit your nuts/spacers and secure them to the pi zero with bolts
3. Gently attach the Amp Zero female header to the Pi Zero male header. Push down to do this.
4. Screw in your last 4x nuts to the holes in amp zero - so securing the two devices together
* The power connector on the Amp Zero is really delicate!
Step 3: Configure Raspian on the Pi Zero to Use the Amp Zero for Audio
Now we need to configure Raspian OS to use the Amp Zero for sound output. First open the config.txt file with the command on your raspberry pi:
sudo nano /boot/config.txt
Now scroll down the file until you reach the line:
dtparam=audio=on You should comment this out with #, so you will have:
#dtparam=audio=on Now, go ahead and add these new config instructions:
Ok, all done!
Step 4: Connect Your Speakers to the AmpZero
I just purchased some cheap waterproof speakers from ebay for £11 each. My speakers are 5V 4Ohm 'Weatherproof Communication Extension Speaker B185' .
First of all I cut off the 3.5mm jack that came on the end of the speaker input cables, and stripped the wires to attach to the speaker output terminals on the Amp Zero.
Then I attached the wires to the speaker output terminals of the Amp Zero. There are four output blocks. From the left: outputs 1 and 2 for the first speaker, outputs 3 and 4 for the second speaker. The -ve output is first, then the +ve output, same again for the second speaker.
Step 5: Test Out Playing Some Audio!
I downloaded wav files from freesound.org. The audio files here are licensed under Creative Commons Attribution 3.0 Unported (CC BY 3.0) https://creativecommons.org/licenses/by/3.0/
Probably you won't want to download the sounds of bees like I did! Anyway, I saved them in a directory called deter_elephants on the PiZero. That's what the code later will reference.
Now, we can play them with aplay. That's a linux command line player for the ALSA soundcard driver http://manpages.ubuntu.com/manpages/trusty/man1/a...
The following python code can be used to play the wav files you downloaded:
import time<br>import randomimport os scare_sounds = ['aplay bees1.wav', 'aplay bees2.wav', 'aplay bees3.wav'] i = 0 while i < 4: i = i+1 to_play = random.choice(scare_sounds) print(to_play) #debug os.system(to_play) time.sleep(1)
We import time, random and os. We execute aplay use os.system(aplay filename). The command to execute on command line (i.e. "aplay") and the file to pass to aplay (e.g. "bees1.wav") are all stored in the scare_sounds list.
So random.choice will pseudo-randomly pick a list item and put it into to_play variable (e.g. "aplay bees3.wav") and then os.system will execute this.
The while loop is set to run while i is less than 4. You can adjust this to how long you want to continue playing the scare_sounds. It depends on the length of your scare_sound audio files and how long you think it will take to deter the elephants! So that needs some field research to determine!
So we've got that python code saved as scare_sounds.py into deter_elephants directory, and the bee%.wav files saved into the deter_elephants directory!
* yes I know it's unlikely you want to scare elephants as I did! But it can easily be adapted for something else!
Step 6: Getting Started to Communicate From Our Raspberry Pi 3 to the Pi Zero and Amp Zero Via Bluetooth
First let's make sure serial profile is loaded on both Raspberry Pi 3 and Pi Zero:
sudo sdptool add SP
Then we need to get a MAC address for the Bluetooth adaptors on the Pi Zero and the Pi 3.
We can use the following command to get the MAC address:
And we should get back something like 43:43:A1:12:1F:AC (which is what I got). You shouldn't get something like AA:AA:AA:AA:AA:AA. That is not good!
Step 7: Pair the Raspberry Pi 3 and Pi Zero
Now is the time to pair your raspberry pi 3 with the pi zero so they can communicate via Bluetooth! It's easy in the GUI. Just head to the Bluetooth icon on the top right of screen for your pi zero and select add device (the other device i.e. raspberry pi 3 should be set via its Bluetooth GUI menu to discoverable when you do this).
Step 8: Let's Deploy Code for the Pi Zero (and Amp Zero)
Now, the Pi Zero is going to act as the server in this communications scenario. It is waiting for a Bluetooth message from the Raspberry Pi 3 (client) and when it gets one, it will play audio! N.B. This is Python 3 code, because socket library doesn't allow Bluetooth in Python 2.7.x. You can get this on my GitHub here: https://github.com/nksheridan/elephantAI/blob/mast...
import socket import time import os import random
hostMACaddress = 'xxx' port = 9 backlog = 1 size = 1024 s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) s.bind((hostMACaddress, port)) s.listen(backlog)
print("We are waiting for a message from the Raspberry Pi 3 to arrive via bluetooth!")
try: client, address = s.accept() data = client.recv(size) if data: print(data) client.send(data) #echo back except: print("closing the socket") client.close() s.close()
message = str(data) #convert the data received to a string print(message)
if message == "b'yes_audio'": print("play scare sounds now") time.sleep(3) scare_sounds = ['aplay bees1.wav', 'aplay bees2.wav', aplay bees3.wav'] i = 0 while i <10: i = i+1 to_play = random.choice(scare_sounds) print(to_play) os.system(to_play)
print("Finished playing audio. ")
Step 9: Let's Deploy Code for the Raspberry Pi 3
Now, the Raspberry Pi 3 is going to act as client in this communications scenario. So it will send a message to the server (Pi Zero) via Bluetooth! N.B. It is Python 3 code.
import socket<br>message = "yes_audio" serverMACAddress = '00:1f:e1:dd:08:3d' port = 9 s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) s.connect((serverMACAddress,port)) while 1: s.send(bytes(message, 'UTF-8')) s.close()