Deploy Mycroft AI voice assistant on Raspberry Pi using Ansible

Use these Ansible playbooks for an optimized Mycroft AI experience.
46 readers like this.
Looking at a map

opensource.com

Mycroft AI is a virtual assistant application that can respond to verbal requests and complete tasks such as searching the Internet for some information you need, or downloading your favorite podcast, and so on. It's a fine piece of open source software that, unlike similar software from companies in the business of harvesting personal data, provides privacy and platform flexibility.

Mycroft AI, written in Python, can install on many different hardware platforms. The famous Raspberry Pi board is a popular choice (but not the only one) to run the voice assistant. Conveniently, Mycroft provides an image for Raspberry Pi, which is called Picroft. Picroft is an excellent solution, but it does have some limitations, such as the lack of 64-bit support.

Raspberry Pi 4, the platform of choice and my target

The Raspberry Pi board is popular within the Mycroft community because it's inexpensive, has a lot of educational potential, and provides the possibility of interesting expansions thanks to Mycroft's skills and the Pi's easy access to General-Purpose Input/Output (GPIO) pins (for example, the wake word LED GPIO skill).

The model 4B has enough CPU power and memory to run Mycroft smoothly. I use the model 4B with 8GB of RAM, running Raspberry Pi OS Bullseye 64-bit Lite, which you can download from RaspberryPi.org.

Another automation story

Building your own Mycroft AI system does mean that you must pay attention to some details. According to my initial experience (from a year ago), here is a list of important technical details you must keep in mind:

  • Audio output (speaker configuration)
  • Audio input (microphone configuration)
  • Microphone quality (the actual hardware you buy)
  • Wake word response ("Hey Mycroft!")
  • Latency of response ("What's the weather like?")

These aren't issues of Mycroft AI, they're just the things you must keep in mind while choosing your hardware and configuring your operating system. The Pi itself is capable of running Mycroft AI, but there are adjustments that require special configuration. Specifically, these are:

  • CPU scheduler
  • SD card performances
  • PulseAudio configuration
  • Network latency

I had to do a lot of research and manual actions to solve the headaches from the list above, but I did achieve the "ultimate" goal—the smoothest experience!

Ansible to the rescue!

So now that I've created the smoothest experience, how do I ensure that every action done to achieve this goal gets captured and gets re-applied only if required on any Raspberry Pi 4 board?

Ansible to the rescue! Ansible is idempotent by design, which means that it applies the requested changes only if required. If everything is configured correctly, Ansible does not change a thing. This is the beauty of idempotency!

To achieve this ultimate goal, I use two Ansible roles:

  • One to configure and tweak the Raspberry Pi
  • One to install and configure Mycroft AI

Ansible prepi role

The Ansible prepi role applies some configurations to obtain better performances from a Raspberry Pi 4B board and prepare the path to Mycroft.

  • Update Raspberry Pi OS to the latest version
  • Add Debian backports repository
  • Update firmware using the next branch, which provides kernel 5.15 and edge firmware
  • Update EEPROM using the beta version, which provides edge features
  • Setup initial_turbo to speed up the boot process
  • Overclock the Raspberry Pi to 2Ghz
  • Mount /tmp on a RAMDisk
  • Optimize / partition mount options to improve SDcard read/write
  • Manage I2C, SPI & UART interfaces
  • Set CPU governor to performance to avoid context switching between the idle* kernel functions
  • Install and configure PulseAudio (non-system wide)
  • Reboot the Raspberry Pi when new firmware or EEPROM gets installed

Ansible mycroft role

This Ansible mycroft role installs and configures Mycroft AI Core from the GitHub repository based on the dev_setup.sh Bash script provided by the Mycroft core team.

  • Python 3 virtual environment
  • Systemd integration
  • Extra skills installation
  • Boto3, py_mplayer, and pyopenssl libraries install
  • RAMDisk support for IPC
  • File configuration support
  • PulseAudio optimizations
  • Secure Mycroft message bus websocket

I'll need an Ansible playbook to orchestrate the two roles.

Personal requirements

Here is a list of requirements for this project:

  • Raspberry Pi 4B board (or later) connected to your network
  • Raspberry Pi OS 64-bit
  • Ansible 2.9 (or later)
  • SSH, up and running

To burn the Raspberry Pi OS image to the SD card, you can use Etcher or the imaging tool of your choice.

I overclock my Pi to get a boost in performance, but this can be dangerous to your hardware. Before using my Ansible playbooks, read them carefully! You are responsible for the choices made with this role. You decide which firmware, which EEPROM you want to use. The same rule applies to the overclocking feature. Remember that overclocking requires a proper cooling system.

Execute the Ansible playbook

The first step is to retrieve the Ansible playbooks from GitHub. Use the following git command:

$ git clone https://github.com/smartgic/ansible-playbooks-mycroft.git

This repository contains an Ansible requirements.yml file which provides a list of Ansible roles required by this playbook, the roles must be retrieved from Ansible Galaxy.

$ cd ansible-playbooks-mycroft
$ ansible-galaxy install -r requirements.yml
Starting galaxy role install process
- downloading role 'mycroft', owned by smartgic
- downloading role from https://github.com/smartgic/ansible-role-mycroft/archive/main.tar.gz
- extracting smartgic.mycroft to /home/goldyfruit/.ansible/roles/smartgic.mycroft
- smartgic.mycroft (main) was installed successfully
- downloading role 'prepi', owned by smartgic
- downloading role from https://github.com/smartgic/ansible-role-prepi/archive/main.tar.gz
- extracting smartgic.prepi to /home/goldyfruit/.ansible/roles/smartgic.prepi
- smartgic.prepi (main) was installed successfully

The second step is to edit the Ansible inventory provided in the repository to reference the hosts I need to be managed by Ansible.

[rpi]
rpi4b01 ansible_host=192.168.1.97 ansible_user=pi

[rpi] is the group and should not be changed. This group has one host named rpi4b01, which has IP 192.168.1.97 and provides pi as Linux (default on Raspberry Pi OS) user for Ansible to connect with.

Now the tricky part: what value do you want for each option? It's up to you, but here's my go-to configuration:

# file: install-custom.yml
- hosts: rpi
  gather_facts: yes
  become: yes

  pre_tasks:
    - name: Install Python 3.x Ansible requirement
      raw: apt-get install -y python3
      changed_when: no
      tags:
        - always

  vars:
    # PREPI
    prepi_pi_user: pi
    prepi_hostname: mylovelypi
    prepi_firmware_update: yes
    prepi_overclock: yes
    prepi_force_turbo: yes
    prepi_cpu_freq: 2000
    prepi_pulseaudio_daemon: yes

    # MYCROFT
    mycroft_branch: dev
    mycroft_user: "{{ prepi_pi_user }}"
    mycroft_skills_update_interval: 2.0
    mycroft_recording_timeout_with_silence: 3.0
    mycroft_enclosure_name: picroft
    mycroft_extra_skills:
      - https://github.com/smartgic/mycroft-finished-booting-skill.git

  tasks:
    - import_role:
        name: smartgic.prepi

    - import_role:
        name: smartgic.mycroft

The content needs to be saved in a file (for example, install-custom.yml).

And now the moment of truth: run your freshly created playbook.

$ ansible-playbook -i inventory install-custom.yml -k

The -k option from ansible-playbook command is only required if not using the SSH key. During the playbook execution, the Raspberry Pi could reboot a couple of times. The playbook is smart enough to handle this.

Once Ansible is done, you see a congratulatory message, which prompts you for the next steps you need to take.

Ansible makes custom Mycroft easy

These playbooks are the lessons I learned since starting my journey with Mycroft AI. It allows me to build, re-build, customize, and replicate my install with the peace of mind of having consistency everywhere!

Do you have any comments, questions, or concerns? Leave a comment, visit me on Twitter @goldyfruit, or stop by Mycroft channels.

What to read next

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.