用意するもの
- Raspberry Pi 2 Model B
- スピーカー
- Netatmo Weather Station
動作確認環境
- Raspbian GNU/Linux 8.0 Lite (Jessie)
- Open JTalk 1.07 ※今回導入します
Open JTalkで言葉を喋るようにする
こちらの記事を参考にしました。
はじめに、OpenJTalkをはじめとしたパッケージを導入します。
sudo aptitude install open-jtalk open-jtalk-mecab-naist-jdic htsengine libhtsengine-dev hts-voice-nitech-jp-atr503-m001
こちらの記事で使用されている、jsay.shを作成します。
vi jsay.sh
jsay.shは以下の内容です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #!/bin/sh HV=/usr/share/hts-voice/nitech-jp-atr503-m001/nitech_jp_atr503_m001.htsvoice tempfile=`tempfile` option="-m $HV \ -s 16000 \ -p 100 \ -a 0.03 \ -u 0.0 \ -jm 1.0 \ -jf 1.0 \ -x /var/lib/mecab/dic/open-jtalk/naist-jdic \ -ow $tempfile" if [ -z "$1" ] ; then open_jtalk $option else if [ -f "$1" ] ; then open_jtalk $option $1 else echo "$1" | open_jtalk $option fi fi aplay -q $tempfile rm $tempfile |
chmod +x jsay.sh
以下のコマンドでしゃべります。
./jsay.sh こんにちは
声が聞こえたら成功です。
netatmo connectから気温データを取得する
https://dev.netatmo.com/にログインして、CREATE AN APPからAPPを作成します。作成が完了するとClient idとClient secretが発行されますので、これを控えておきます。
以下の内容でAPIを叩きます。
curl -d "grant_type=password&client_id=[クライアントID]&client_secret=[クライアントシークレット]&username=[ユーザ名]&password=[パスワード]&scope=read_station" https://api.netatmo.net/oauth2/token
以下の内容が返ってくるので、access_token部分を取り出します。
{"access_token":"********","refresh_token":"********","scope":["read_station"],"expires_in":10800,"expire_in":10800} |
取り出したaccess_token部分を以下のコマンドで実行します。
curl -d "access_token=********" https://api.netatmo.net/api/devicelist
けっこう長い結果が返ってきますが、main_deviceのところにweather stationのMACアドレスが入っているので、これを控えておきます。
{"body":{"modules":[{"_id":"00:00:00:00:00:00","date_setup":{"sec":1409629381,"usec":727000},"main_device":"xx:xx:xx:xx:xx:xx"...(中略)..."time_server":1441792678} |
続いて取得スクリプトの作成です。
こちらの記事を参考にしました。
jqパッケージを導入します。JSONをパースするためのパッケージです。
sudo aptitude install jq
get_netatmo.shを作成します。
mkdir netatmo vi netatmo/get_netatmo.sh
内容は以下の通りです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #! /bin/bash AUTO_HOME=/home/pi set +x accountfile="${AUTO_HOME}/netatmo/account.json" client_id=`jq -r ".client_id" $accountfile` client_secret=`jq -r ".client_secret" $accountfile` username=`jq -r ".username" $accountfile` password=`jq -r ".password" $accountfile` device_id=`jq -r ".device_id" $accountfile` authurl="https://api.netatmo.net/oauth2/token" tokenfile="${AUTO_HOME}/netatmo/token.json" datafile="${AUTO_HOME}/netatmo/data.json" if [ ! -f $tokenfile ]; then # echo "Get new access token" curl -s -d "grant_type=password&client_id=${client_id}&client_secret=${client_secret}&username=${username}&password=${password}&scope=read_station" "${authurl}" > ${tokenfile} fi atoken=`jq -r ".access_token" $tokenfile` rtoken=`jq -r ".refresh_token" $tokenfile` expiration=`jq -r ".expires_in" $tokenfile` filedate=`date +%Y-%m-%d_%H:%M:%S -r $tokenfile` filedate=${filedate/_/ } filedate=`date -d "$filedate" +%s` limittime=`expr $filedate + $expiration` currenttime=`date +%s` if [ $limittime -lt $currenttime ]; then # echo "Using refresh token" curl -s -d "grant_type=refresh_token&refresh_token=${rtoken}&client_id=${client_id}&client_secret=${client_secret}" "${authurl}" > $tokenfile atoken=`jq -r ".access_token" $tokenfile` fi curl -s -d "access_token=${atoken}&device_id=${device_id}" "https://api.netatmo.net/api/getstationsdata" > $datafile extemp=`jq -r ".body.devices[0].modules[0].dashboard_data.Temperature" $datafile` exhumi=`jq -r ".body.devices[0].modules[0].dashboard_data.Humidity" $datafile` intemp=`jq -r ".body.devices[0].dashboard_data.Temperature" $datafile` inhumi=`jq -r ".body.devices[0].dashboard_data.Humidity" $datafile` #pres=`jq -r ".body.devices[0].dashboard_data.Pressure" $datafile` echo "気温の情報です がい気温 ${extemp}度 湿度 ${exhumi}パーセント 部屋の気温 ${intemp}度 湿度 ${inhumi}パーセント 以上です" |
続いてaccount.jsonを作成します。
vi netatmo/account.json
ここには先ほど控えておいたClient idとClient secret、weather stationのMACアドレス(device_id)を設定します。
1 2 3 4 5 6 7 | { "client_id" : "#####", "client_secret": "#####", "username" : "#####", "password" : "#####", "device_id" : "#####" } |
get_netatmo.shに実行権限を付与します。
chmod +x netatmo/get_netatmo.sh
get_netatmo.shを実行します。
./netatmo/get_netatmo.sh
以下のように表示されると思います。数値の部分がNULLではなく数値が入っていることを確認します。
気温の情報です がい気温 12.5度 湿度 40パーセント 部屋の気温 23.1度 湿度 51パーセント 以上です |
以下のコマンドで、内容をそのまま声でしゃべります。
けっこう流暢でびっくりします。
./netatmo/get_netatmo.sh | ./jsay.sh
読み上げがうまくいっていれば成功です。おつかれさまでした。
※「外気温」を「がい気温」としているのは、OpenJTalkが「そときおん」と読み上げてしまうことを防ぐためです。
関連
Raspberry Pi 2からBluetoothでスピーカーに音を飛ばす
Raspberry Pi 2でNFCタグを読み取って音を鳴らす
Raspberry Pi 2を赤外線リモコンで操作可能にする
Raspberry Pi 2をリモコン操作してNHKラジオを聴く
Raspberry Pi 2で実現するスマートハウス