LoginSignup
6
8

More than 5 years have passed since last update.

Twitterを使って外出先から家のラズパイをしゃべらせる

Posted at

はじめに

外出先から、自分の存在感を示すべく、家の中に家内放送したくなりました。
スマホTwitterアプリで投稿して、その内容を家のラズパイにしゃべってもらおうと思います。

準備

  • Raspbian Stretchインストール済みRaspberry Pi 3
  • Raspberry Pi3 から音が出ること

OpenJTalkのインストール

音声合成にOpenJTalkを使いました。
OpenJTalkのインストールと女性の音声への変更と、読み上げスクリプト

$ sudo apt-get install open-jtalk
$ sudo apt-get install open-jtalk-mecab-naist-jdic hts-voice-nitech-jp-atr503-m001
$ wget http://downloads.sourceforge.net/project/mmdagent/MMDAgent_Example/MMDAgent_Example-1.7/MMDAgent_Example-1.7.zip
$ unzip MMDAgent_Example-1.7.zip
$ sudo cp -R ./MMDAgent_Example-1.7/Voice/mei /usr/share/hts-voice/
$ nano speak.sh
#!/bin/sh
TMP=/tmp/voice.wav
echo "$1" | open_jtalk -m /usr/share/hts-voice/mei/mei_happy.htsvoice -x /var/lib/mecab/dic/open-jtalk/naist-jdic -ow $TMP && aplay --quiet $TMP
rm -f $TMP
$ chmod 755 speak.sh

読み上げテスト

$ ./speak.sh 俺は、海賊王になる!

ちゃんと読み上げられれば成功です。

TwitterAPI

Twitter
https://apps.twitter.com/
CONSUMER_KEY、CONSUMER_SECRET、ACCESS_TOKEN、ACCESS_TOKEN_SECRETをメモします。

PythonのOAuth認証ライブラリrequests-oauthlibをインストール

$ pip install requests requests_oauthlib

Pythonプログラムを書きます

5秒に一度、投稿をチェックして、前回の投稿日時と比較して新しかったら、音声合成でしゃべらせるプログラムとなります。

config.py
CONSUMER_KEY = "xxxxxxxxx"
CONSUMER_SECRET = "xxxxxxxx"
ACCESS_TOKEN = "xxxxxxxxxxx"
ACCESS_TOKEN_SECRET = "xxxxxxxxxxxx"
timeline.py
# -*- coding:utf-8 -*-
import json, config, time
import datetime
from datetime import datetime as dt
from requests_oauthlib import OAuth1Session
import subprocess

CK = config.CONSUMER_KEY
CS = config.CONSUMER_SECRET
AT = config.ACCESS_TOKEN
ATS = config.ACCESS_TOKEN_SECRET
twitter = OAuth1Session(CK, CS, AT, ATS)

url = "https://api.twitter.com/1.1/statuses/user_timeline.json"

params ={'count' : 1}
check_time = dt.now()
while(1):
    req = twitter.get(url, params = params)

    if req.status_code == 200:
        timeline = json.loads(req.text)
        for tweet in timeline:
            tstr = tweet['created_at']
            _post_time = datetime.datetime.strptime(tstr, '%a %b %d %H:%M:%S %z %Y')
            post_time = datetime.datetime(_post_time.year, _post_time.month, _post_time.day,_post_time.hour,_post_time.minute,_post_time.second)
            post_time = post_time + datetime.timedelta(hours=9)
            tstr = post_time.strftime('%Y/%m/%d %H:%M:%S')
            print(tweet['user']['name']+'::'+tweet['text'])
            print(tstr)
            cstr = check_time.strftime('%Y/%m/%d %H:%M:%S')
            print(cstr)
            if post_time > check_time:
                print(tweet['user']['name']+'::'+tweet['text'])
                msg = '/home/pi/speak.sh '+tweet['text']
                subprocess.call(msg, shell=True)    
                check_time = post_time
    else:
        print("ERROR: %d" % req.status_code)
    time.sleep(5)

実行

$ python3 timeline.py

まとめ

これで、家にいなくとも、私の存在をアピールできます。

6
8
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
6
8