§メール送信
このページの目標は、「温湿度データを収集しそのデータを監視サーバで可視化、設定した温度域から外れたら警告メールを送信させること。 さらに、自動でデータを収集・処理し、1日の平均気温を算出・メール送信させること。」でした。 メール送信できなければ、目標達成できません。 「あれpythonモジュールで、以前に行ったのでは?」と思った方がいると思いますが、後に登場する監視サーバMuninのためにもmailコマンドがつかえた方が便利でしょう。 このセクションの内容は、参考文献[1]に詳しく記載してあります。 まずsSMTPのインストール・設定をしましょう。
pi@raspberrypi:~ $ sudo apt-get install -y ssmtp // sSMTPのインストール pi@raspberrypi:~ $ sudo vi /etc/ssmtp/ssmtp.conf // SMTP設定ファイルを開く
iキーを押し、設定ファイルに以下のように記述します。 必要事項のみを記述しています。 全項目”(ダブルクォーテイション)は記述しません。
root="xxx@gmail.com" mailhub="smtp.gmail.com:587" AuthUser="xxx@gmail.com" AuthPass="password" UseSTARTTLS="YES"
Escキー、「:wq」を記述、Enterキーを押して保存します。
最後にmailutilsのインストールして、mailコマンドで送信してみましょう。
pi@raspberrypi:~ $ sudo apt-get install -y mailutils // mailutilsのインストール pi@raspberrypi:~ $ mail yyy@docomo.ne.jp Cc: Subject:test I'm Shaun. // Ctrl+d で送信
記述したアドレス宛にメールが届いていれば成功です。当たり前か・・・。
§監視サーバのインストール
次に監視サーバMuninをインストールしましょう。 これは、後に登場する温湿度センサーから一定間隔でデータを収集し、得たデータを可視化させるために使います。 「させる」と書いたのは、自動で行ってくれるからです。 感想を言えば、非常に優れものでしょう。
pi@raspberrypi:~ $ sudo apt-get update pi@raspberrypi:~ $ sudo aptitude install apache2 // Webサーバのインストール pi@raspberrypi:~ $ sudo apt-get install munin munin-node munin-plugins-extra // Muninのインストール
参考文献[2]には、パーミッションの設定等いろいろと注意書きがされています。 ここでは割合させていただきます。省略された方は、自己責任でお願いします。 それぞれ起動させます。
pi@raspberrypi:~ $ sudo /etc/init.d/munin-node restart // Muninの再起動 pi@raspberrypi:~ $ sudo /etc/init.d/apache2 restart // Webサーバの再起動 pi@raspberrypi:~ $ sudo -u munin munin-cron // Muninのデータを即反映させる
完了したら、リモートデスクトップ接続を行い、ブラウザをクリックします。 URLに http://192.168.xxx.xxx といった感じでIPアドレスを入力しEnterキーを押します。 Apache2のページが見れればWebサーバの方は成功です。 またMuninの方は、 http://192.168.xxx.xxx/munin でページが表示されれば成功です。
§温湿度センサー作成
それでは、温湿度センサーの作成にかかりましょう。 このセクション・次セクションの内容は、参考文献[3](再掲サイト)に詳しく記載されています。
STEP.1 電子工作
Raspberry Piと温湿度センサーを電子工作しましょう。その際、
電子工作は、必ず、Raspberry Piの電源を切ってから行いましょう。
感電しないでくださいね。[3]に画像や詳しい説明がありますから、説明は割合させていただきます。
STEP.2 Pythonモジュールのインストール
Pythonモジュールのインストールと、温湿度センサーの動作確認をしましょう。 やっと感動できるでしょうか?
pi@raspberrypi:xxx~ $ cd pi@raspberrypi:~ $ sudo apt-get install build-essential python-dev pi@raspberrypi:~ $ sudo git clone https://github.com/adafruit/Adafruit_Python_DHT.git pi@raspberrypi:~ $ cd Adafruit_Python_DHT pi@raspberrypi:/Adafruit_Python_DHT~ $ sudo python setup.py install // ここまでがインストール。 pi@raspberrypi:/Adafruit_Python_DHT~ $ cd examples pi@raspberrypi:/Adafruit_Python_DHT/examples~ $ sudo ./AdafruitDHT.py 22 4 // ここまでで動作確認。TempとHumidityが表示されれば、動作確認は成功です。
§監視サーバにグラフを作成させる
監視サーバMuninに温湿度の取得をさせ、グラフ表示をさせましょう。
STEP.1 Munin用のモジュールを作成
まずは、温度から作成しましょう。
pi@raspberrypi:/Adafruit_Python_DHT/examples~ $ sudo cp AdafruitDHT.py temp.py // ファイルをコピー。 pi@raspberrypi:/Adafruit_Python_DHT/examples~ $ sudo vim temp.py // コピーしたファイルを編集する。
ファイルを以下のように編集します。29行目に注目してください。
import sys import Adafruit_DHT # Parse command line parameters. sensor_args = { '11': Adafruit_DHT.DHT11, '22': Adafruit_DHT.DHT22, '2302': Adafruit_DHT.AM2302 } if len(sys.argv) == 3 and sys.argv[1] in sensor_args: sensor = sensor_args[sys.argv[1]] pin = sys.argv[2] else: print 'usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#' print 'example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4' sys.exit(1) # Try to grab a sensor reading. Use the read_retry method which will retry up # to 15 times to get a sensor reading (waiting 2 seconds between each retry). humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) # Note that sometimes you won't get a reading and # the results will be null (because Linux can't # guarantee the timing of calls to read the sensor). # If this happens try again! if humidity is not None and temperature is not None: print '{}'.format(temperature) else: print 'Failed to get reading. Try again!'
湿度に関しても、同様にコピーを作成・編集します。
pi@raspberrypi:/Adafruit_Python_DHT/examples~ $ sudo cp AdafruitDHT.py humid.py // ファイルをコピー。 pi@raspberrypi:/Adafruit_Python_DHT/examples~ $ sudo vim humid.py // コピーしたファイルを編集する。
変更箇所は同じで、以下のように編集します。
import sys import Adafruit_DHT # Parse command line parameters. sensor_args = { '11': Adafruit_DHT.DHT11, '22': Adafruit_DHT.DHT22, '2302': Adafruit_DHT.AM2302 } if len(sys.argv) == 3 and sys.argv[1] in sensor_args: sensor = sensor_args[sys.argv[1]] pin = sys.argv[2] else: print 'usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#' print 'example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4' sys.exit(1) # Try to grab a sensor reading. Use the read_retry method which will retry up # to 15 times to get a sensor reading (waiting 2 seconds between each retry). humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) # Note that sometimes you won't get a reading and # the results will be null (because Linux can't # guarantee the timing of calls to read the sensor). # If this happens try again! if humidity is not None and temperature is not None: print '{}'.format(humidity) else: print 'Failed to get reading. Try again!'
これらのファイルをパスが通るところに置きます。
pi@raspberrypi:/Adafruit_Python_DHT/examples~ $ sudo cp temp.py /usr/local/bin pi@raspberrypi:/Adafruit_Python_DHT/examples~ $ sudo cp humid.py /usr/local/bin
STEP.2 グラフ表示設定
上のファイルは、温湿度を取得するMunin用モジュールなので、以下ではグラフを表示するための設定ファイルを作成します。 まずは温度の方の設定です。
pi@raspberrypi:/Adafruit_Python_DHT/examples~ $ sudo vim /usr/share/munin/plugins/temp
以下を記述します。
#!/bin/sh #%# family=auto #%# capabilities=autoconf GETNUM=`python /usr/local/bin/temp.py 22 4` if [ "$1" = "autoconf" ]; then if [ -n ${GETNUM} ] ; then echo yes exit 0 else echo no exit 0 fi fi if [ "$1" = "config" ]; then echo 'graph_title temperature' echo 'graph_args -r --lower-limit 0' echo 'graph_vlabel C' echo 'graph_category Weather' echo 'temp.label temperature' echo 'temp.min 0' echo 'temp.draw LINE2' echo 'temp.type GAUGE' exit 0 fi echo "temp.value $GETNUM";
湿度の方も同様です。
pi@raspberrypi:/Adafruit_Python_DHT/examples~ $ sudo vim /usr/share/munin/plugins/humid
以下を記述します。
#!/bin/sh #%# family=auto #%# capabilities=autoconf GETNUM=`python /usr/local/bin/humid.py 22 4` if [ "$1" = "autoconf" ]; then if [ -n ${GETNUM} ] ; then echo yes exit 0 else echo no exit 0 fi fi if [ "$1" = "config" ]; then echo 'graph_title humidity' echo 'graph_args -r --lower-limit 0' echo 'graph_vlabel %' echo 'graph_category Weather' echo 'humid.label humidity' echo 'humid.min 0' echo 'humid.draw LINE2' echo 'humid.type GAUGE' exit 0 fi echo "humid.value $GETNUM";
Muninが実行できるパーミッションに変更します。
pi@raspberrypi:/Adafruit_Python_DHT/examples~ $ cd pi@raspberrypi:~ $ cd /usr/share/munin/plugins pi@raspberrypi:/usr/share/munin/plugins~ $ ls -l // pluginsフォルダの中身を表示。tempとhumidのパーミッションを確認。 pi@raspberrypi:/usr/share/munin/plugins~ $ sudo chmod 755 temp // パーミッションを755に変更。 pi@raspberrypi:/usr/share/munin/plugins~ $ sudo chmod 755 humid // 同様に変更。
パーミッションはもっと緩い設定があるかもしれません。 試してください。
次は、これらのファイルのシンボリックリンクを張りましょう。 シンボリックリンクとは、簡単に言えば、Munin用のショートカットファイルです。 Muninは、このシンボリックファイルを読み込んでグラフを作成します。 またroot権限を与えておきます。
pi@raspberrypi:/usr/share/munin/plugins~ $ sudo ln -s /usr/share/munin/plugins/temp /etc/munin/plugins/temp // 温度の方のシンボリックリンク。 pi@raspberrypi:/usr/share/munin/plugins~ $ sudo ln -s /usr/share/munin/plugins/humid /etc/munin/plugins/humid // 湿度の方。 pi@raspberrypi:/usr/share/munin/plugins~ $ sudo vim /etc/munin/plugin-conf.d/temp
以下のように入力して、保存します。
[temp] user root
湿度に関しても同様です。
pi@raspberrypi:/usr/share/munin/plugins~ $ sudo vim /etc/munin/plugin-conf.d/humid
以下のように記述します。
[humid] user root
Muninの再起動をします。
pi@raspberrypi:/usr/share/munin/plugins~ $ sudo /etc/init.d/munin-node restart
しばらくしてからリモートデスクトップ接続をして、Muninのページで温湿度のグラフが表示されていれば成功です。
次ページでは、いよいよ本題に移ります。 まずはアラートメールの送信設定をします。 最後にMuninが取得・保存したデータを時間になったら処理させ、平均気温を自動でメール送信させるようなpythonモジュールの作成と設定をしましょう。