A.I has become the norm in technology, so why not add your own Iron-Man-inspired Jarvis A.I. assistant to your raspberry pi. Here we’ll cover everything you need to know about adding a cool A.I. assistant to a raspberry pi.
In this tutorial, we will be using the programming language Python to program our A.I. assistant onto the raspberry pi. Before beginning this project, make sure to have a raspberry pi already set up with an internet connection and properly configured. The initial setup and software flashing for the raspberry pi will not be covered in this tutorial. There are plenty of available online sources that can help you with that process.
The main items that you will need to complete this project are the following:
The first thing that you will want to do before anything else is to update your raspberry pi with the latest packages by running the following command in the terminal:
sudo apt update && sudo apt upgrade
After you’ve updated your pi you’re ready to start adding some of the required dependencies. Open your text editor and create a file named Jarvis_1.0.py and save it.
The first package we will add will be PYTTSX3. This package is responsible for handling text to speech. This package will allow our program to communicate with us. We install this by running the following command in the terminal:
pip install pyttsx3
Once installed we will test to make sure everything is working properly by adding the following lines of code:
If you run this code, you should hear your computer say, “Hello World! This is Jarvis”. If you do not hear anything, you may want to ensure that your audio settings are correct. You also want to ensure that you have the package espeak installed on your pi if you get an error trying to run the above code, if it’s not installed just run:
sudo apt-get install espeak
Personally, I have configured my raspberry pi to run audio through the 3.5 audio jack instead of the HDMI port. This is helpful if you plan on using Jarvis without a monitor in the future.
Once you have everything working we will move two of those lines into a function that we will later call to allow Jarvis to speak to us.
Now we can run our speak function and just pass it the text to speak like
text = "This is a text string" speak(text)
Now that we have given Jarvis a voice, let’s have him find some information for us. For starters let’s have Jarvis tell us what the current date and time is.
There are a few different ways that this can be done, however, for now, we’ll import python’s datetime module at the top of your code:
import pyttsx3 import datetime
We can now write a function that can call to get the current time.
If we call the getTime function we now have Jarvis giving us the current time. Similarly, we can get the current date with another function.
When we execute the getDate function, Jarvis will tell us what today’s date is. Now we can try to put a simple greeting function together using both functions.
So now if we run the greetMe function we will get a nice welcome message along with the current time and date.
As you can see, it’s not very hard to get Jarvis talking, but what about asking questions or even giving commands? Well, we can easily take care of that with the help of another library.
So that we can start giving commands to Jarvis we will use Google’s speech recognition package and add it with the following command:
pip install speechrecognition
The speech recognition package relies on PyAudio so be sure to have that already installed. Otherwise, you can run the following code in your terminal:
pip install pyaudio
In some cases, you may be required to also install a dependency of PyAudio called flac. you can install that by running the command:
sudo apt-get install flac
With all the required dependencies installed we call now import speech recognition and build a function that allows us to speak to Jarvis.
With the above code entered you can test if the function is working properly by calling the TakeCommand function and speaking. You should see whatever you said printed in the terminal window.
Now that we have working code we can add our main function with the following lines:
When we run our program what’s happening in the main function is Jarvis first greets you then waits for a command. If Jarvis hears the word time in your statement or question, he will give you the current time. The same is true for the word date or any other words or phrases you tell Jarvis to listen for. When a specified word or phrase is recognized, you can enable Jarvis to do any number of tasks. At this point your code should look like this:
As you can see with just a bit of python code, it’s fairly easy to create a Jarvis A.I. assistant for your raspberry pi. What we’ve done is pretty amazing, but it only just scratches the surface of its true potential. In the next part of this tutorial, we will be looking at how to add more capabilities like web searching and even sending emails. So if you’re looking for something cool to do with your raspberry pi, make your life simpler by turning it into your personal A.I. assistant.