FRFAM.COM >> Famille >> Technologie &Innovation >> Informatique

Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Les assistants vocaux comme Google Home ou Amazon Alexa ont envahi nos foyers, contrôlant éclairages, médias et bien plus. Bonne nouvelle : vous pouvez reproduire cette technologie chez vous avec un Raspberry Pi ! Ce guide détaillé, basé sur le SDK officiel de Google, vous explique comment installer l'Assistant Google sur votre Pi et contrôler une LED via les broches GPIO par commandes vocales.

Ce tutoriel s'appuie sur des ressources officielles Google pour une mise en œuvre fiable et sécurisée. Assurez-vous de respecter les conditions d'utilisation de Google.

Matériel requis pour votre assistant Raspberry Pi

Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Liste du matériel :

  1. Raspberry Pi avec une installation fraîche de Raspberry Pi OS (ex-Raspbian) sur carte SD.
  2. Microphone USB ou webcam avec micro compatible.
  3. Haut-parleur externe.
  4. Circuit LED pour Pi (optionnel, pour le bonus GPIO).
  5. Navigateur connecté à votre compte Google.

Note : Utilisez des périphériques compatibles Raspberry Pi (listes disponibles en ligne). J'ai testé avec une webcam USB pour son micro et la sortie jack 3,5 mm. HDMI nécessite des ajustements audio.

Branchez le micro/haut-parleur et montez le circuit LED si souhaité.

Configuration audio

Travaillez directement sur le Pi ou via SSH. Dans le terminal, listez les périphériques :

arecord -l
aplay -l
Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Notez les numéros de carte/périphérique (ex. : carte 1, device 0 pour webcam).

Dans /home/pi, créez .asoundrc :

sudo nano .asoundrc

Adaptez selon vos périphériques (ex. priorisez HDMI si besoin). Exemple de contenu (ajustez les numéros) :

pcm.!default {
    type asym
    playback.pcm {
        type plug
        slave.pcm "hw:1,0"  # Haut-parleur
    }
    capture.pcm {
        type plug
        slave.pcm "hw:1,0"  # Micro
    }
}
ctl.!default {
    type hw
    card 1
}
Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Sauvegardez (Ctrl+X). Testez :

arecord -D plughw:1,0 -d 5 test.wav
a play test.wav
Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Optionnel : Ajustez le volume micro avec alsamixer (F6 pour sélectionner périphérique).

L'audio est prêt !

Création du projet Google

Dans un navigateur (sur Pi ou local), allez sur Actions Console. Créez un Nouveau projet.

Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Patientez, puis gardez l'onglet ouvert.

Activation de l'API Assistant Google

Sur API Assistant, cliquez Activer.

Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Activez dans votre Activité Google :

  • Activité Web & apps
  • Infos appareil
  • Activité vocale & audio
Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Enregistrement du Raspberry Pi

Dans Actions Console, sélectionnez Enregistrement appareil. Nommez Produit (ex. "MonPiAssistant"), Fabricant (n'importe quoi), Type : Autre.

Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Cliquez Enregistrer modèle, puis Télécharger credentials OAuth 2.0 (fichier JSON).

Déplacez-le vers /home/pi/. Pour SSH :

scp ~/Downloads/client_secret_*.json pi@IP_DU_PI:/home/pi/
Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Installation du SDK

Créez un environnement virtuel Python :

python3 -m venv env
source env/bin/activate

Installez dépendances :

sudo apt update
sudo apt install portaudio19-dev libffi-dev libssl-dev libmpg123-dev

SDK :

python -m pip install --upgrade pip setuptools wheel
google-assistant-library
python -m pip install --upgrade google-assistant-sdk[samples]
python -m pip install --upgrade google-auth-oauthlib[tool]

Authentification

google-oauthlib-tool \
  --scope https://www.googleapis.com/auth/assistant-sdk-prototype \
  --scope https://www.googleapis.com/auth/gcm \
  --save --headless --client-secrets /home/pi/client_secret_*.json

Copiez le lien, autorisez, collez le code. Credentials sauvées !

Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Test

googlesamples-assistant-hotword --project-id VOTRE_PROJECT_ID --device-model-id VOTRE_MODEL_ID

Dites "OK Google" ! Ajustez volume : "Hey Google, volume à 80%".

Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Bonus : Contrôle GPIO vocal

Dans Actions Console, pour votre appareil, activez trait OnOff.

Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Dans env :

git clone https://github.com/googlesamples/assistant-sdk-python
pip install RPi.GPIO
cd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/library

Éditez hotword.py :

nano hotword.py

Ajoutez :

import RPi.GPIO as GPIO

Dans process_event (ligne ~66), remplacez print par :

if command == "action.devices.commands.OnOff":
    if params["on"]:
        print("LED allumée")
        GPIO.output(18, GPIO.HIGH)
    else:
        print("LED éteinte")
        GPIO.output(18, GPIO.LOW)

Dans main() :

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT, initial=GPIO.LOW)
python hotword.py --device-model-id VOTRE_MODEL_ID
Tutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry PiTutoriel complet : Créez votre assistant vocal Google Home DIY avec Raspberry Pi

Dites "OK Google, allumez/éteignez" !

Votre assistant Google Home Raspberry Pi fait maison

Parfait pour débuter avec les API Google. Testez les meilleures commandes Google Home.

[]