We have identified all the software requirements and we have ordered the goodies. Now it is time to start installing the different components and start the configuration of all different packages. After following this installation instruction we should have a basic system up and running with the latest MyCroft version using the ReSpeaker microphone array and any connected speaker.
The base system – OS
Raspbian stretch – Lite
We had decided that we would like to use a Debian based OS on our Raspberry Pi, so Raspbian was our choice. So we start by setting up our SD card. This step is proper documented on so many other websites, including the official one at Raspberry.org. So let us head over to their download page and download the “RASPBIAN STRETCH LITE – Minimal image based on Debian Stretch” version onto our computer. Head over to the installation guide at raspberry.org to get it installed onto your SD-card by choosing your preferred method of flashing the image.
Now, before we slide our SD-card into the Raspberry Pi there is something that I would like to do before we do that. I don’t really like to attach a screen and keyboard to the Raspberry Pi just for the first run so we can configure stuff. I am more a fan of a completely headless installation. For me that means, powering on the device, wait for it to come online on the network, SSH to it and go from there. So that is what we are going to do.
When the image is flashed to your SD card. Eject it and slide it back in. Depending on which operating system you are on you could have one or two new drives. It is the BOOT named drive that we are interested in. Mount it / open it and create a new file onto it named; “ssh”. Don’t give it a file extension or put anything in it. Just an empty file named ssh without extension.
On Linux we can do this very simple with the following command.
[bash] touch ssh[/bash]
Next we want to do is pre-configuring our WiFi settings. If you are planning on using the LAN connection, you can of course skip this step. Again on that same drive / partition on which we just created the ssh file, we need to create another one. This time we need to create; “wpa_supplicant.conf”. Then we put the following content into that file using a (proper) text editor. I use VIM on linux. If you are on Windows, I would like to recommend Notepad++. Put the following content into the wpa_supplican.conf file;
[bash] ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdevupdate_config=1
country=NL
network={
ssid="YOUR_WIFI_SSID_NAME"
psk="YOUR_WIFI_PASSWORD"
}
[/bash]
Of course you need to change the parameters to fit your system. Change the country= setting to wherever you are based. Needless to say what to fill in at “ssid” and “psk” section. When done, save it and we are ready to fire up our Raspberry Pi for the first time. So unmount / eject the SD card. Slide it into the Raspberry Pi and power it on. Now the first boot of Raspbian can take a little while as it extends the filesystem to use the full size of the SD card. Give it some time, you would eventually see the device coming online onto your network. Have a look at your router admin page, or use any other method to see which devices are on your network. Get the IP address of the Raspberry Pi, it’s what we need to remotely log in to it.
When the system is fully booted and ready, we can log in with our SSH client from our workstation. If you are on Windows I recommend to use PuTTY, on Linux you can use the following command from the CLI;
[bash] ssh -l pi 10.0.0.110[/bash]
Again change the IP address to whatever your Pi is using. Default user and password for a clean Raspbian install is; pi / raspberry. Log in using those credentials and then we can further configure the base system by using the raspi-config tool, but first things first. We NEED to change the password !!! So let’s do that first by;
[bash] passwd[/bash]
You will be prompted to once insert the current password (again: raspberry) and than insert your new password, followed by confirming it by typing it again. You should see a message saying “passwd: password updated successfully”.
Temporarily files and log files are a burden for SD cards. So to prevent unnecessary write cycles to our SD card we are going to move temp at each boot into a temporarily memory space. While running, we can still check them if needed. However we loose the backlog content when we reboot. However I do care more about my SD card then I do to have a look back in time at the log files etc. So we are going to add a couple of lines into a system file on our Raspberry Pi. Run the command and add the following lines.
[bash] sudo nano /etc/fstab[bash] [bash] tmpfs /var/log tmpfs defaults 0 0
tmpfs /tmp tmpfs defaults 0 0
tmpfs /var/tmp tmpfs defaults 0 0
[/bash]
Exit the editor by CRTL+X and press Y for it to save. Now we are ready for some other configuration tweaks. Run the following command to open the raspi-config tool;
[bash] sudo raspi-config[/bash]
You will see the following screen popping up.
Within this tool we need to change certain settings. We will get back to this tool during the ReSpeaker driver setup, but for now let’s change the following parameters;
[bash] – ‘2 Hostname’— Enter: `GIVE-IT-A-NAME`
– ‘3 Boot Options’
— ‘B1 Desktop / CLI’
— ‘B2 Console Autologin’
– ‘3 Boot Options’
— ‘B2 Wait for Network at Boot’
— ‘No’
— ‘Ok’
[/bash]
Finish, and reboot the Raspberry Pi. If it doesn’t do it automatically, use the following command;
[bash] sudo reboot[/bash]
Now we are ready to move onto the next step. Installing the ReSpeaker 4-Mic array.
ReSpeaker 4-Mic Array
Installing the ReSpeaker microphone array is very much properly explained on seeedstudio it’s wiki page, however for your ease of use I will list the different steps needed here as well. So let us start with the warning stated on their site;
Hot-plugging ReSpeaker 4-Mic Array is not allowed.It will damage the respeaker.
So power down your Raspberry Pi and mount the ReSpeaker 4-Mic Array on Raspberry Pi, make sure that the pins are properly aligned when stacking the ReSpeaker 4-Mic Array for Raspberry Pi. Next is to install the drivers. First we need to install some requirements. Run the following commands into your terminal. (remember to ssh into your raspberry Pi / using PuTTY);
[bash] sudo apt-get updatesudo apt-get upgrade
sudo apt-get install git
git clone https://github.com/respeaker/seeed-voicecard.git
cd seeed-voicecard
sudo ./install.sh
sudo reboot
[/bash]
Allow me to explain a bit more what we do with above commands. First we update the packaging-list of the Raspbian OS. Secondly we then upgrade our base system. Just type “y” to install the propossed packages. Then we install a new package called “git” which we need for the next command after that. The “git clone” command then downloads all the files needed for the ReSpeaker into the folder “seeed-voicecard” where me need to get into by the “cd” command. Next we install all drivers needed via an automated script “install.sh” and last command is to once again reboot the Raspberry Pi.
Wait for the device to come up again, log in and then like i said earlier, we need to change some setting again within the “raspi-config” tool. So again run the “sudo raspi-config” command and change the following parameters;
[bash] # Select 7 Advanced Options# Select A4 Audio
# Select 1 Force 3.5mm (‘headphone’) jack
# Select Finish
[/bash]
Now we are ready for some sound testing. First we are going to check if the ReSpeaker sound card name is correct. Run the following command and check if the output is the same as below;
[bash] pi@raspberrypi:~/seeed-voicecard $ arecord -Lnull
Discard all samples (playback) or generate zero samples (capture)
playback
capture
dmixed
array
ac108
default:CARD=seeed4micvoicec
seeed-4mic-voicecard,
Default Audio Device
sysdefault:CARD=seeed4micvoicec
seeed-4mic-voicecard,
Default Audio Device
dmix:CARD=seeed4micvoicec,DEV=0
seeed-4mic-voicecard,
Direct sample mixing device
dsnoop:CARD=seeed4micvoicec,DEV=0
seeed-4mic-voicecard,
Direct sample snooping device
hw:CARD=seeed4micvoicec,DEV=0
seeed-4mic-voicecard,
Direct hardware device without any conversions
plughw:CARD=seeed4micvoicec,DEV=0
seeed-4mic-voicecard,
Hardware device with all software conversions
[/bash]
If all correct we can test if everything works by recording a bit and playing it back. Run the next command. Now talk a bit, make some noise, whatever and stop the recording by CRTL-C. We can then play it back via the second command
[bash] arecord -Dac108 -f S32_LE -r 16000 -c 4 hello.wav[/bash] [bash] aplay hello.wav
[/bash]
Listen if we hear ourself, the noise, whatever…. If all systems are a go, we can get to the fun stuff!
Installing MyCroft A.I.
The next section is based upon different information sources. However combined to better fit our need as stand alone embedded device with only one purpose: A smart Personal AI Assistant. The PiCroft image for instance uses a dedicated user ” mycroft” (I believe) to run the software. Now I am a huge fan of this approach for “normal” installations, however for such an embedded device as we are creating with this blog post series, I found it a bit overkill. The Mycroft documentation does have a section dedicated to create your own Picroft image and we are using snippets of it. The main difference I believe is that the original documentation is using debian packages to install MyCroft. We however are going to install the latest and greatest version from Github, compiling and installing it from source code.
So let us start to login into our Raspberry Pi if you are not already logged in and run the following series of command. Again I will explain the different commands afterwards.
[bash] cd ~/git clone https://github.com/MycroftAI/mycroft-core.git
cd mycroft-core
./dev_setup.sh
[/bash]
OK, here we go! The first command is to get into the so called home directory of the pi user. Then we pull all sourcecode from their online repository into a newly created directory “mycroft-core” followed by the cd command to get into that new directory. The last command basically again using an automated script to install everything at the right place. The very first time that you run that script (which we are), compilation and installing can take a while. Mimic is compiled from source which on a Raspberry Pi can take some time (hours, not minutes). So grab a cup of coffee, do some other things etc.
After a while if everything went fine, all should be installed. Before we get any further let’s cleanup a little bit by;
[bash] cd mimicmake clean
[/bash]
By running our own instance of MyCroft from sourcecode we need to create an automated way to start MyCroft when we boot the system. On raspbian Stretch deamon services are handled by “systemd”. So we are going to create a script that runs MyCroft after boot. Keeps it running in case of a crash and can be used to easily start/stop/restart the program. To create a systemd startup unit script, run the following command and insert the lines below the command into that file;
[bash] nano /etc/systemd/system/mycroft.service [Unit] Description=Mycroft personal AIAfter=pulseaudio.service [Service] User=pi
WorkingDirectory=/home/pi/mycroft-core
ExecStart=/home/pi/mycroft-core/mycroft.sh start
ExecStop=/home/pi/mycroft-core/mycroft.sh stop
Type=forking
Restart=always
RestartSec=3 [Install] WantedBy=multi-user.target
[/bash]
Again, exit and save by CRTL-X followed by “Y”. Now we need to enable this script so it is being ran at boot time.
[bash] sudo systemctl enable mycroft.service[/bash]
We are not going to start it as of yet. First we are going to do some extra system tweaks. First create the system wide configuration folder.
[bash] sudo mkdir /etc/mycroft[/bash]
Then we are going to create a RAM disk as stated on the PiCroft creating page. Like said before, small and continous write cycles to a SD card is always a bad idea. The RAM disk is used for inter-process communication (what the ….. that may be). Run these commands at the command prompt:
[bash] sudo mkdir /ramdisk[/bash] [bash] sudo nano /etc/fstab
[/bash]
And again add the following line to the bottom of the file.
[bash] tmpfs /ramdisk tmpfs rw,nodev,nosuid,size=20M 0 0[/bash]
Next in line is to tweak the audio system a little bit. At this point everything should be fine and working. However in the case we are going to add more audio hardware such as webcams with microphones, USB soundcards and / or perhaps Bluetooth speakers in the near future, we might run into problems with the audio system. Loosing the audio or microphone input, etc.
PulseAudio
If you are on a desktop type of Linux you normally should not run into (pulse)audio issues. However if you are on a headless setup and/or have connected multiple sound cards or microphones, you easily run into issues. One of the issues is that PulseAudio is started on a per user basis, which happens at log in. On our system we do not log in (I know, not completely true as we used raspi-config to auto log in, but we are going to start mycroft as systemd service) so a PulseAudio deamon is not spawn. To add to that, the latest Raspbian Stretch doesn’t even come default with pulseaudio. So first we need to install it by;
[bash] sudo apt-get install pulseaudio[/bash]
Next is to configure pulseaudio to be started as system wide service. Now, normally doing this is NOT recommended for multiple reasons, however in our case, running an one purpose only embedded device it should be fine. So again we first need to create a systemd startup script for it. Run the command and add the lines below it into the file.
[bash] sudo nano /etc/systemd/system/pulseaudio.service [Unit] Description=PulseAudio Daemon [Install] WantedBy=multi-user.target [Service] Type=simplePrivateTmp=true
ExecStart=/usr/bin/pulseaudio –system –realtime –disallow-exit –no-cpu-limit
[/bash]
CTRL+X and Y. With above system service, it will be running the deamon as root. For a normal user to be able to communicate with that deamon we need to change some configuration files. So again run the command and put the lines below it at the bottom of the file;
[bash] sudo nano /etc/pulse/system.pa#allow localhost connections
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1
[/bash]
Before we start this pulseaudio system wide service, we first going to spawn a pulseaudio deamon under our pi user and use it to grab the right named sink (sound card) and source (microphone). We will do this by;
[bash] pulseaudio -D[/bash]
Then to show the different sinks
[bash] pacmd list-sinks | egrep “index|name:” index: 0
name: <alsa_output.usb-0d8c_C-Media_USB_Headphone_Set-00.analog-stereo>
* index: 1
name: <alsa_output.platform-soc_audio.analog-stereo>
[/bash]
NOTE: The output you should see if you only have a 3.5 jack speaker (like us) is NOT like above, where you should have only the “* index: 0” as the “alsa_output.platform-soc_audio.analog-stereo” entry.
Above example is for the users that would like to use other speakers and have problems getting sound from MyCroft and did come here via Google or whatever link.
We need to tell MyCroft which sound card to use. We do that by using a custom type of setting that we save in one of the local configuration files. I opted for the /home/pi/.mycroft/mycroft.conf but you could also use /etc/mycroft/mycroft.conf The first is for specific user configs, the second for system wide configs. For our particular use, there is no difference. So let’s edit/create the file and add the following content.
[bash] nano /home/pi/.mycroft/mycroft.conf{
"play_wav_cmdline": "paplay -d alsa_output.platform-soc_audio.analog-stereo %1",
"play_mp3_cmdline": "mplayer -ao pulse::alsa_output.platform-soc_audio.analog-stereo %1"
}
[/bash]
Have a good look on what we are doing here in combination with the output from the sink list earlier. For our Raspberry Pi project this is what it should look like. But you can tell it to use any (USB) soundcard like this. Now you may notice that the “play_mp3_cmdline” changed from “mpg123” to “mplayer”. This is needed because mpg123 does not allow it to switch / select the named sink like we do above. So for that reason we are using mplayer instead. However, it is not yet installed onto our system. So to install it;
[bash] sudo apt-get install mplayer[/bash]
We are onto the last few steps before it is all done. Hang in there. Next, we need to do a similar thing with the microphone. So let’s get a view on which sources we have;
[bash] pacmd list-sources | egrep “index|name:” index: 1
name: <alsa_output.usb-0d8c_C-Media_USB_Headphone_Set-00.analog-stereo.monitor>
* index: 2
name: <alsa_input.usb-0d8c_C-Media_USB_Headphone_Set-00.analog-mono>
index: 3
name: <alsa_input.platform-soc_sound.multichannel-input>
[/bash]
Again, I took an example from the internet here just in case you want to use this information for other microphones. In our case we have only one. The ReSpeaker with named source of; “<alsa_input.platform-soc_sound.multichannel-input>”
So now we need to tell the pulseaudio system which microphone we would like to use;
[bash] sudo nano /etc/pulse/system.pa[/bash]
Add the following section at the bottom of that file;
[bash] ### Set default microphoneset-default-source alsa_input.platform-soc_sound.multichannel-input
[/bash]
If you use another microphone, change the name to the one you want to use as shown above by the “pacmd list source” command.
Exit and save the file. Now we can finally enable and start the system wide service. Run the following commands;
[bash] sudo systemctl enable pulseaudiokillall pulseaudio
sudo systemctl start pulseaudio
[/bash]
Almost there (I know I said that already before). With pulseaudio running as service, it is finally time to start MyCroft for the very first time. We are not going to use the systemd service as of yet. We are going to manually start it.
[bash] cd ~/mycroft-core./start-mycroft.sh debug
[/bash]
This will start all the different sub systems of MyCroft and finally starts the CLI interface of MyCroft A.I. You will end up with a similar screen as below.
Don’t pay attention on the actual output as I just grabbed this example from the web. Now, with all the tweaks we did, it might be that you don’t YET have sound and / or microphone input, but don’t worry if that happens, the reboot which we will do shorty will most likely fix those last quirks.
Give MyCroft a few minutes to do it’s thing. In the end it will ask you to pair the device and give you a code. If sound is not there yet. Not a big deal as you can grab it from the CLI screen as well. As soon as it gives you the code head over to home.mycroft.ai and login or create an account if this is your first time.
Click the menu on the top right, Go to the “Devices” section and click “Add device”. All fields are self explanatory. The very first “Registration Code” input field is the code received above from you Mycroft instance.
When the device is paired, we are ready to wrap it all up.
Exit the CLI environment by; CTRL+C to return to the normal linux command line. Before we reboot and see if everything is working as it should. Remember we created the RAM disk at the beginning of the MyCroft A.I. installation? Right, we haven’t used that yet which I did on purpose such that we don’t need to bother with mounts (sudo mount -a). Anyhow, to make sure the mycroft.service is using the /ramdisk we need to change the /home/pi/.mycroft/mycroft.conf once more to add one more line to it.
[bash] {"play_wav_cmdline": "paplay -d alsa_output.platform-soc_audio.analog-stereo %1",
"play_mp3_cmdline": "mplayer -ao pulse::alsa_output.platform-soc_audio.analog-stereo %1",
"ipc_path": "/ramdisk/mycroft/ipc/"
}
[/bash]
Add that last ipc_path line before the closing } character. IMPORTANT! Note the comma at the end of the line above the ipc_path line. That ” , ” is needed !!!
Now, this should be it. Reboot the device. Wait a bit for the whole boot process and all should work. Speak into the microphone and ask MyCroft some questions.
Hey Mycroft, what’s the time?
Hey Mycroft, what day is today?
Hey Mycroft, wiki European Union.
Hey Mycroft, tell me about Abraham Lincoln.
Hey Mycroft, tell me a joke.
Hey Mycroft, why is 6 afraid of 7?
Hey Mycroft, sing me a song!
Hey Mycroft, what is 2+5?
If something is wrong or you want to see what is going on, just SSH into your device and run the following commands to get back into the MyCroft CLI interface.
[bash] cd ~/mycroft-core./start-mycroft.sh cli
[/bash]
Disclaimer: Above information is based on the crappy notes I made during my research and development, try and error process to get it all working. By all means, if you followed all the steps above and run into problems somewhere please let me know in the comments below and I will do my best to help you out. Will of course change the above information accordingly if required.
If you followed all the steps succesfully without issues, again please let me know in the comments below.
Do you like what you just read? Keep me going by donating on Paypal or become a Patron.
hi Peter,
thanks a lot for the clear explanation of how to setup this system. I’ve got it al working now, except for two problems.
I´m not that experienced with Linux and python, so i´m sorry if these are really simple problems, but i hope you can help me.
1. There is no audio output. (the microphone and audio input works fine on the other hand)
I use some simple headphones via the 3.5 jack output.
In the voice.log file I found the following error:
ALSA lib pulse.c:243:(pulse_connect) PulseAudio: unable to connect: Acces denied
2. secondly, after I reboot the rPi mycroft stops working, if I try to start the CLI interface it shows connection errors.
I found a solution for this, but I don’t think this is how it should work.The problem is gone by running the following commands after reboot:
cd mycroft-core
./dev_setup.sh
./start-mycroft.sh debug
thanks a lot in advance!
Philip
Hi Philip,
Great to hear you got it working in some extend. Perhaps both errors have to do with the same error. Let’s see if I can help out.
1) Access denied. Could you try to replace the following line in /etc/system.pa
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1
into
load-module module-native-protocol-tcp auth-anonymous=1
2) It looks like the Mycroft services are not started at boot time. This could be because pulseaudio doesn’t start completely as in the systemd we created for Mycroft we said;
[Unit]
Description=Mycroft personal AI
After=pulseaudio.service
Which means, Mycroft get’s started after starting Pulseaudio. Now if pulseaudio is not started properly, this might mean we can’t start mycroft properly. So in short, let us try to fix 1 first, perhaps it also fixes 2
Thank you for your fast response.
However unfortunately both problems are still there.
But it did change something, before I was also not able to use commands like: aplay and record, it would also say “acces denied”. But these are accessible now.
However mycroft still gives the same errors as before.
correction: after another reboot and changing nothing else, aplay and arecord also give an error again…
for completion, the error states:
ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Acces denied
aplay: main:788: audio open error: Connection refused
Philip, on a short holiday till this weekend. Will pick it up again later this week if you do not mind.
hi Peter,
off course that is no problem at all. I can happily report that I have managed to fix the audio problem over the weekend. By starting from scratch again, but first installing all off Pulseaudio and after this I installed Mycroft. I probably made a typo somewhere, the first time I installed everything.
However I am unfortunately still left with the second problem. Mycroft does not start at boot. If I then open cli it gives connection errors to the websocket. And if I then look at: “systemctl status mycroft.service” it says failed to start.
I tried to solve this by changing the line:
ExecStart=/home/pi/mycroft-core/mycroft.sh start
to :
ExecStart=/home/pi/mycroft-core/start-mycroft.sh all
this removed the error in “systemctl status mycroft.service”, but mycroft is still not working.
it only works properly if I run:
./dev_setup.sh
./start-mycroft.sh debug
If I then however close the “cli” mycroft stops working again. So I can only use it at the moment with “cli” open.
I hope you know a way to solve this problem.
again, thanks a lot in advance for looking into it.
Philip, apologies! Is this still valid?
no problem, It is not needed anymore. I’ve got it all up and running now with the standard pycroft image from mycroft.ai, which installed flawlessly without any problems.
Hi,
On the section pertaining to getting Mycroft to run at boot, I have used the systemd file you have written and for some reason on ubuntu 18.04, this file kills my system audio. Mycroft’s audio works fine, but every other sound from the system doesn’t work. Can you help me debug the file so that it doesnt kill my audio?
Hi there, apologies for not noticing this post in time. I believe you already figured it out right? (forking/simple)