さて、一部の変わったユーザ様(ヲイ)のために、Arduinoを使用した未読メールメータの作成方法を書いておきたい。入門キットについてくるサーボを使用する。
プログラムは次のような簡単なものでよろしい。
まず、Arduino側には次のように書く。
// // シリアルからサーボを制御 // 佐藤俊夫 // 27.05.02(土) 1028~ // #include <Servo.h> // Servo meter; const int METER_0 = 5; const int METER_100 = 175; const int METERMIN = 0; const int METERMAX = 100; const int STRLEN = 10; char buf[STRLEN]; int i = 0; // void setup() { meter.attach(9); meter.write(METER_0); Serial.begin(9600); } void loop() { int v = 0, deg = METER_0; if(Serial.available() > 0){ // もし受信したデータが存在したら buf[i] = Serial.read(); if(i >= STRLEN - 1 || buf[i] == '\n'){ buf[i] = '\0'; i = 0; Serial.flush(); // 針をナニする処理 v = atoi(buf); (v > METERMAX) ? v = METERMAX : v; (v < METERMIN) ? v = METERMIN : v; deg = METER_0 + (int)(v / ((float)METERMAX / (float)(METER_100 - METER_0))); meter.write(deg); // Serial.println(deg, DEC); // Serial.write('\n'); delay(1000); }else{ i++; } } }
テストしてうまく動いたら、次に、POP3サーバにたまっている自分のメールの本数を知る工夫をする。
私は次のようにした。まず、手近のLinuxマシンにexpectを入れる。
# yum -y install expect (中略)
そうすると、telnetなどでPOP3サーバに自動ログインできる環境が整う。
シェルでこんなのを書く。まあ、遊びなんでrootで。
# ls -Fla pop2arduino -rwx------ 1 root root 415 2015-05-02 13:58 pop2arduino* # cat pop2arduino #!/bin/sh # pop2arduino # Sat May 2 12:53:57 JST 2015 # Sato Toshio # expect -c " set timeout 5 spawn telnet pop.hogehoge.ne.jp 110 expect \"+OK POP3 ready\" send \"USER fugafuga@hagefuge.hogehoge.ne.jp\n\" expect \"+OK\" send \"PASS passpass\n\" expect \"+OK server ready\" send \"STAT\n\" expect -re \"\\\+OK (.+) .+$\" send \"QUIT\n\" " | egrep "\\+OK ([0-9]+) .+$" | sed -r "s/\\+OK ([0-9]+) .+$/\1/g" >/dev/ttyACM0
で、このシェルは、まあ、なんだっていいんだけど、安直にcronで定期実行する。
# crontab -e (以下crontab内) */5 * * * * /hoge/pop2arduino (crontabおわり) /etc/rc.d/init.d/crond restart (出力略)
……で、メールが届くのを待っていると、こういうふうに動く。