13
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MicroPythonでGoogle Homeを喋らせる【Raspberry Pi Pico W】

Last updated at Posted at 2022-09-04

PyChromecastが動かなかったので作ってみました。

使い方

from pichromecast import play_url

play_url('https://nyanpass.com/nyanpass.mp3', '192.168.10.101')

音声合成

from pichromecast import play_url, create_url

play_url(create_url('hello world', 'en'), '192.168.10.101')
# https://gist.github.com/SpotlightKid/eca9b00239104e8c599b86635f62ab73#file-urlencode-py
from urlencode import urlencode
from pichromecast import play_url

url = 'https://translate.google.com/translate_tts?client=tw-ob&' + urlencode({'q': 'Hello, 世界', 'tl': 'ja'})
play_url(url, '192.168.10.101')

インストール

Tools > Manage package...からインストールできます。

image.png

ソースコードコピペでも大丈夫です。
https://github.com/GitHub30/pichromecast/blob/main/pichromecast.py

デモ

BOOTSELボタンを押したときに喋るようにしてみました
Watch the video

import network
import time

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("aterm-SSID-g", "YOUR_PASSWORD")
while not wlan.isconnected() and wlan.status() >= 0:
    time.sleep(1)

from pichromecast import play_url


playing = False
while True:
    if rp2.bootsel_button() == 1 and not playing:
        playing = True
        machine.Pin('LED', machine.Pin.OUT).on()
        play_url('https://nyanpass.com/nyanpass.mp3', '192.168.10.101')
        playing = False
        machine.Pin('LED', machine.Pin.OUT).off()
    time.sleep(0.1)

IPの見つけ方

MDNSでIPを見つけられます。

#pip install pychromecast
import pychromecast

services, browser = pychromecast.discovery.discover_chromecasts()
pychromecast.discovery.stop_discovery(browser)
print(services)
[CastInfo(services={ServiceInfo(type='mdns', data='Google-Home-Mini-3b0a32dc5803130351919f8a286e406f._googlecast._tcp.local.')}, uuid=UUID('3b0a32dc-5803-1303-5191-9f8a286e406f'), model_name='Google Home Mini', friendly_name='書斎', host='192.168.10.101', port=8009, cast_type='audio', manufacturer='Google Inc.')]
13
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?