Have you ever wanted to make an object speak phrases on demand? This sort of high-tech ventriloquism can be huge fun when greeting people in an office, scaring visitors at Halloween, or, as Master Yoda shows in this example, providing surprise and delight on Star Wars Day (“May the 4th be with you”).
What You’ll Need For These Tutorials:
- Raspberry Pi with WiFi Capability the Raspberry Pi 3A+ is a good choice – inexpensive, but has both USB and round audio jacks, so it would work with lots of speaker options (the cheaper Pi Zero W doesn’t have these jacks, although you can easily add RCA audio with an HDMI adapter).
- A 5v/2A DC Power Supply. If you have an extra iPhone USB Power Adapter, you can buy a 2.1mm to USB cable and use this. Your computer’s USB port won’t provide enough power by itself.
- A microSD card (8GB or more) for the Raspberry Pi.
- A microSD card reader if your Mac doesn’t have one (and late model Macs with USB-C don’t).
- A small speaker that plugs into the round RCA jack or USB port of the Raspberry Pi 3. Inexpensive hamburger speakers that use the RCA jack can be purchased for less than $5. A good USB option would be this mini speaker.
- Some object that you want to “speak”. Yoda works for me, but I’ve used the same technique to give “voice” to a Raspberry Pi robot, among other examples.
- Contains all files used in this project:
Step 1: Setup the Pi Without a Keyboard or Mouse (assumes you’re using a Mac)
Note: The tutorial below was originally made as part of the “Let’s Build a Robot” series on controlling a Raspberry Pi-powered Robot via iOS app, but the first steps are the same.
- Video: Headless Raspberry Pi setup for Mac (in playlist)
- Web Page Instructions: Setup Raspberry Pi Without Keyboard or Mouse.
Step 2: Installing CircuitPython on the Raspberry Pi
Note: These tutorials are also part of the “Let’s Build a Robot” series, but the steps needed are the same. While technically you don’t need the CircuitPython variety of python, it’s my preferred python version for physical computing projects. I also plan to expand the project at a future date by letting Yoda “speak” when touched, adding an Adafruit capacitive touch board (the Adafruit Crickit HAT for Raspberry Pi) to my Pi, so I’ve included these steps, below. There’s no harm if you perform the steps, but don’t end up using some of the capabilities offered.
- Video: Installing CircuitPython on the Raspberry Pi on a MacBook (in playlist)
- Web Page: Installing CircuitPython on a Raspberry Pi
Step 3: Install the Pygame Python library to support audio playback and volume control
- Enter the command below from the terminal prompt and press the return key.
sudo apt-get install python3-pygamePress “Y” if asked to confirm the use of additional disks space for the installation
Step 4: Install and test the mosquitto MQTT Broker on a Raspberry Pi
Mosquitto is the software tool that will allow the Pi to accept commands sent to it over the Internet using the MQTT protocol, popular for messaging among Internet of Things. MQTT is very practical for maker projects, and I also use it as the way to communicate from iOS app to Raspberry Pi robot in my Let’s Build a Robot tutorial series. The beginning of the video on mqtt gives a nice introduction on how mqtt works.
- From the command prompt type the command below and press the return key.
sudo apt-get install mosquitto mosquitto-clients
Enter “Y” if asked to confirm installation.
Install the libraries for python support for MQTT by entering the command below and pressing the return key.
sudo pip3 install paho-mqtt
Verify that the mosquitto broker software is now running by typing the command below and pressing the return key.
systemctl is-active mosquitto
You should see a message stating “active” if everything is working properly. If, for some reason, you don’t receive an “active” message, you can try rebooting your Pi, waiting about 30 seconds, login, then re-enter the command above.
Enter the command below and press the return key to ensure that the mosquitto broker runs automatically every time you boot your Pi (no need to ssh in or type additional commands). This is exactly what we’ll want if this Pi is dedicated to our robotics project – ideally just turn on the Pi and the software we need will begin running.
sudo systemctl enable mosquitto.service
Step 5: Prepare Sounds
This tutorial assumes that you are using MP3 files. You can download the large # of “Yoda-ish” files I’ve used in my own project at the GitHub repo for this project: https://github.com/gallaugher/MakeItTalk. To download these sound files:
- Use a web browser to visit: https://github.com/gallaugher/MakeItTalk
- Click the green “Clone or Download” button in the upper left-hand corner.
- Press “Download Zip” from the dropdown dialog that appears and save it to your Mac.
- When the file is done downloading, you can double click the .zip file to uncompress it. A folder named “MakeItTalk-master” will appear. You can throw the .zip file in the trash.
- Double click the “MakeItTalk-master” folder, then double click the “sounds” folder. Apologies if you don’t like my Yoda impression.
If you have a good Yoda voice and want to record your own phrases, the Mac’s QuickTime program is one of many ways to record from your Mac’s microphone. Simply:
- Open QuickTime by pressing command-space on your Mac to open Spotlight, then type Quicktime and press the return key.
- From within Quicktime, select the File menu, then New Audio Recording. The Audio Recording box will appear.
- Press the red “record” button to begin recording.
- Press the “stop” square to stop recording.
- From the File menu select Export As… > Audio Only. This will save the file as an .m4a file. Unfortunately QuickTime cannot save or convert files to the .mp3 file format.
After you’ve prepared a bunch of .m4a files, you can convert them to .mp3 files in a batch, by using the Online Audio Converter website.
- Prepare a folder of all of the files you’d like to convert
- Using a browser, visit: https://online-audio-converter.com
- Press Open
- Command-click to select multiple files, and click “Open” to upload these files for conversion.
- After files have been uploaded, scroll to the end of the webpage and click the “Convert” button.
- Save a .zip of the converted files by clicking the “Download” link when the conversion is done. Close the browser.
- Return to the Finder and double-click the zip file to uncompress it.
- Rename the unzipped folder to “sounds”
You can also browse the web for clever sound clips and utilities. The Acapela Group has a computer-generated voice named “WillLittleCreature” that sounds like “The Small Green Master” (this voice is also used in the Cognitoys Watson-powered kids toy, which my daughter also owns). Acapela has a demo service that will play the sound over an audio mark tune, and an online application where individuals working on art or educational projects can apply to use their service.
I have also used the commercial program ScreenFlow by Telestream to capture sounds playing on my Mac, then saved them as AAC Audio files, and repeated the iTunes .mp3 conversion steps, mentioned above.
Step 6: Set up your Raspberry Pi so that you can use it with the Mac Finder’s File System, then Copy Sounds to Your Pi
- Follow the steps in the tutorial: Set up your Raspberry Pi so that you can use it with the Mac Finder’s File System
- Open a Finder window, select your Raspberry Pi’s name in the “”Locations” area in the list on the left.
- Select “Home Directory” in the main Finder window to get to the root directory on your Pi.
- On your Mac, if you haven’t already, create a folder called “sounds” and fill it with the .mp3 files of all of the sounds that you’d to access from your App.
- Drag the “sounds” folder from the Finder window of your Mac into the Finder window holding the Home Directory of your Raspbery Pi. This will make a copy of the “sounds” directory and all enclosed MP3 files on your Pi.Step 6: Add the talk.py file to your Raspberry Pi
Step 7: Download the “MakeItTalk” Project from GitHub
The GitHub repo, below, includes the talk.py python file for your Raspberry Pi, plus all of the files to build and run the MakeItTalk app in the Xcode iPhone simulator, as well as to install it on an iOS device.
- Visit the GitHub repo at:
- Select the green “Clone or Download” button in the upper right-hand corner.
- Select the “Download Zip” option at the bottom of the drop-down box that appears.
- Save the file to your Mac.
- Once it has completely downloaded, double click the downloaded .zip file to decompress the project. You can delete the .zip file at this point.
- Open the folder named “MakeItTalk-master”
- Find the file named talk.py and drag into into the Home Directory of your Raspberry Pi. This will make a copy of the file on your Pi.
- Return to the terminal and type the following command into the terminal prompt, followed by the return key. This will open the talk.py file on your Raspberry Pi so that it can be edited.
- Modify any lines in this code so that they will work with your project. In particular, if you’re going to change your client name or server name from something other than “talkpi”, you’ll want to find these lines and enter the names that are right for your project:
clientName = "talk"as well as the line:
serverAddress = "talkpi"
- The line below is used to play a file named “yes.mp3” whenever this script starts. It’s a nice cue for to know if things are working. If you don’t have “yes.mp3”, then replace it with a file of your own.
pygame.mixer.music.load(fileLocation + "yes.mp3")
- You likely didn’t change the “sounds” folder or its location on the Pi, however if you change either of these, then you’ll need to update this line with the correct path and folder name:
fileLocation = "/home/pi/sounds/"
- Save the changes you just made in nano, by pressing:
Control-X, Y, and return
- Now run the talk.py program by typing the following into the command prompt, followed by the return key:
- You should “yes.mp3” or your replacement sound when this program begins to run.
We can also enter a command to send a “publish” message to the mosquitto server and test that everything is working. With our talk.py program running, we should hear the sound played out of our speaker. To do this, open a second window in the Terminal program as follows:
- From within Terminal program, select the “Shell” menu, then New Window > New Window with Profile – Basic (or type Command-N).
A new Terminal window will open.
The talk.py program is written to subscribe to (listen and respond to) messages published as “talkpi/talk”.
- Copy and paste the line below into the terminal prompt of this new Terminal window, then press enter. This line assumes that it’s being sent to a Raspberry Pi with the hostname talkpi.local, and that the Pi contains a file named “yes”. Be sure to change the details for your own project:
mosquitto_pub -h yoda.local -t "yoda/talk" -m "no_i_fear.mp3"
- You can now close this second terminal window and return to the Terminal window running talk.py.
- Cancel the running talk.py program by typing Control-c. You’ll some text complaining that the program was stopped and is no longer running, but that’s fine, since this is what we wanted to do.
Step 8: Update the rc.local file on your Pi so that talk.py runs automatically whenever you turn on your Pi
By performing the steps below, your Pi will automatically begin to listen for commands from your app whenever it starts.
- From the command prompt in the Terminal, enter the line below, then press the return key. This will allow you to edit the rc.local file on your Raspberry Pi,
sudo nano /etc/rc.local
- Press the down arrow to navigate through the file in nano. Just before the last exit 0, press the return key and add the line:
sudo python3 /home/pi/talk.py &
- Exit nano and save your work by typing: Control-X, Y, and the return key
Reboot your Pi by typing the command below, then press enter
sudo rebootIf you modified “talk.py” to play one of your own sounds on startup, you should hear that. If you run into problems, try rebooting your Raspberry Pi, again.
Step 9: Configure your Pi to use python3 and pip3 by default (Optional):
While this step is technically not required, it’s a good idea, since python2 has been deprecated. It’ll allow you to use python3 and pip3 whenever typing this in without the “3” at the end of their names:
- From the command prompt, edit the bashrc file by entering the following command and pressing the return key:
Scroll to the bottom and add the following commands:
- Save the changes you just made in nano, by pressing:
Control-X, Y, and return
These changes will take effect the next time you reboot your Pi.
Step 10: Set up your iOS Development Environment If You Need To
You’ll need a free Apple Developer Account and you’ll need to download and configure Xcode in order to build the App on your Mac, and install it on your iPhone. For those new to iOS, the first part of these two guides (video and written tutorials) describe how to do this:
- Web Page: Control your Pi Bot with an iOS App – Quick Download & Run (without explaining how to create the app from scratch). Following steps 1-4 should get you set up.
- This information is also covered in the video of full Code-Along iOS App Instructions: Build a Swift iOS app to control a Raspberry Pi robot (in playlist)
Step 11: Make any Necessary Changes for Your Pi and Sound Files
With Xcode installed and configured on your Mac:
- Find the file named “MakeItTalk.xcworkspace“. Double click to launch this file in Xcode.
- Click the expand triangle in the upper left under “MakeItTalk” to show files and folders associated with this project. The file you may want to is called “ViewController.swift”
- Click the “ViewController.swift file. This will show most of the code that makes your app work.
If your app is using .mp3 files different than the ones included in the “sounds” folder in the project that you just downloaded, you’ll need to enter the names of these files EXACTLY as they are stored in the “sounds” folder on your Raspberry Pi. To do this, we’ll update the variable named “soundFiles” to contain a list of your own files stored on the Pi:
- Find the line that begins:
var soundFiles: [String] = [
- Delete all of the file names between the square brackets (they are all in quotes, separated by commas).
- Enter the names of any files in your Pi’s “sounds” folder. Each name should be inside double quotes, each double-quoted name should be separated by a comma.
- Make sure there is no comma after your lsat sound file name, and that you close the list of sound files with a close bracket symbol:
You might also need to change the name of the Pi if it isn’t named talkpi.
- Find the line:
let mqttclient = CocoaMQTT(clientID: "TalkApp", host: "talkpi.local", port: 1883)and replace “talkpi.local” with your Pi’s host name.
You don’t need to change “TalkApp” on this line. It doesn’t have any formal role in our application. You also don’t need to change any references to publish messages named “talkpi/talk”. This is the same message that your talk.py file will subscribe to, so keep this unchanged.
Step 12: Run the App in the iOS Simulator on your Mac
The Xcode toolbar will show a Play arrow icon, a Stop square, then project’s name MakeItTalk (and the app’s icon) followed by > and an iOS device name (it may be iPhone 8, Generic iOS Device, or other). First let’s run the app in the simulator to make sure everything is working. The steps to build and run the project in the simulator are also shown in the animation at the right:
Select the the iOS device named at the right of “MakeItTalk”. This is known as the “Scheme” menu.
- Scroll to select the device you’d like to simulate in the Xcode simulator. The example to the right shows iPhone X being selected.
- Click the Play arrow button to build and run your app. The build may take a few minutes, especially the first time this is run.
If everything works fine you should see a “Build Succeeded” message fade into the center of the screen, then the simulator will launch. There is a chance that a red error ball shows up stating that CocoaMQTT hasn’t bee installed, but this can be ignored as long as the progress bar continues to build the app (this is a weird quirk with Xcode and cocoapods and often goes away after the first build/run).
- Once the simulator is running the app, click the “Connect to Device” button.
- You can scroll through the list of sound file names. Clicking a file name should briefly highlight it, then play the sound on your Raspberry Pi. You may want to pause briefly between tapping file names so that you don’t overwhelm your Pi with repeated requests that sound like a chorus of Yodas.
Step 13: Install the App on your Device!
Using Xcode, set up a provisioning profile for your device – you’ll only need to do this once:
- Select the menu option: Xcode > Preferences…
- Click the “Accounts” tab.
- Click the “Manage Certificates” button in the lower-right of Accounts dialog.
- If you don’t have an iOS Development Certificate listed, click the “+” button in the lower left of the dialog box that appears next and select “iOS Development“. You should see a listing under iOS Development Certificates listing today’s date.
Make sure you have a valid “Team” selected for this project:
- Click the blue MakeItTalk icon at the top of the Project Navigator list. You should see Project Targets details in the middle of Xcode.
Make sure “Targets” is selected on the left, and the “General” is selected at the top.
Under “Deployment Info” select “Deployment Target” to the iOS version number appropriate for the device you’re installing on and features of your app. You will not be able to deploy this app on devices running a lower operating system number than is listed here.
Under “Signing” select your “Team.” In the drop-down menu, select “yourName (Personal Team)”, which will signify your Apple ID. If you’re paying for a non-personal developer account, feel free to select that. The free Apple ID will let you install on a number of devices; however, the installations will expire in 7 days, so you’ll likely have to re-install.
Now select the Device to Deploy to:
Use a cable to connect your device to your Mac.
Select the scheme menu with the device name at the Xcode toolbar. Scroll all the way to the top. There will be a Device subsection, which should list the device that you’ve just plugged in.
Press the “Play” arrow icon in the Xcode toolbar to Build and Run.
NOTE: The first time that you load Xcode apps on your device, you might see a window saying something like “Could not launch” and asking if you can trust this device. If you see this, follow the instructions in the box to prepare your device to accept apps. If needed, Build and Run again.
Your app should now be installed and launched on your device. Xcode will remain active and you can use it to debug the app as it runs on the device, including setting breakpoints, reading console output, and more.
If your Mac and device are on the same WiFi network, and your iOS device is running iOS 11 or greater, then you can take advantage of wireless debugging. In Xcode, simply use the Window menu, select Devices and Simulators, you should see your plugged-in device, and there should be a checkbox for “Connect to Network” in the upper right. Click that box and you should be able to unplug your cable. Note: you only really need wireless debugging if you are working on something that’d require you to move around longer than the cable (e.g., a location app, fitness app, etc.).
Build and Run.
Once installed, you should be able to run the app without cable, network, or Xcode for 7 days on free accounts (then just do a re-install), or forever if you’ve got a paid-developer account. Congratulations, you Physical-Computing-Savvy iOS app developer!
❖ ❖ ❖ ❖ ❖
Ready for more? Check out tutorials on Robotics, Wearables, App Development and more at: http://bit.ly/GallaugherYouTube. Please consider subscribing and do share with others who may be interested!
Want to learn to build apps? The same content I use in my semester-long online course (videos and tutorial content) is available for less than $25 via links you’ll find at https://gallaugher.com/swift.