日本ITストラテジスト協会オープンフォーラム2015

投稿日:

JISTA_l 私は「ITストラテジスト」という聞き慣れない資格を持っている。経済産業省所管の国家資格で、「情報処理技術者試験」の一つだ。全部で12種類ある情報処理技術者試験のうちでも最難関の一つと言われる資格である。

 ITと経営を融合させることがITストラテジストの使命だ、とでも言えば、資格の性質を端的に言い表し得ていると思う。また、経営とITの両方をよく知る者としての位置づけの他に、専門家として高いIT技能を持っていることも期待されており、その意味から開発の難しい「組込みシステム」に関する技術も有資格者には期待されている。

 この資格の保有者を中心としたコミュニティが「日本ITストラテジスト協会」で、私も仲間に入れていただいている。今日(こんにち)、会員は600名以上おり、全国各地域ごとの支部に分かれてそれぞれ活発に活動している。

 私が所属する関東支部でも、月例会が開催され、テーマ別ディスカッションやスピーチ、講演会や勉強会などを通じて会員相互に研鑽している。

 年に一度は秋葉原で「オープンフォーラム」を催している。斯界の著名人を招いての講演や、パネルディスカッションなどが行われ、充実している。

 今年(平成27年(2015))は例年のとおり秋葉原UDXで、11月7日(土)に行われる。私も端役ながら、タイムキーパーとして当日運営のお手伝いをする予定だ。

 ITストラテジスト協会への入会そのものは、資格をまだ持っていなくても、取得を目指しているのであれば準会員として入会可能なので、興味のある方はオープンフォーラムをご覧になって、入会を検討されては如何かと思う。

日本ITストラテジスト協会関東支部9月月例会

投稿日:

 最近欠席ばかり続いた日本ITストラテジスト協会関東支部月例会、今月は出れそうなので申し込むなど。申し込みは最近スマート化していて、「Peatix」というチケットサービスを使って手軽化している。

Raspberry Piで温度を測ろう

投稿日:

 以前、Arduinoのアナログ入力にサーミスタをつなぎ、温度計にして遊んだ最近はLCDディスプレイに温度を表示してみたり、なかなか面白い。

 一方、Raspberry PiはWebによくなじむ。ApacheもPHPも走る。

IMG_3317 それで、先週宮城大学小島研究室のサイトで公開しておられるADCアクセスのためのソースコードを拝借して遊んだが、これを使ってWeb温度計にしてみた。

 まず、ブレッドボードにAD変換ICのMCP3208と、サーミスタ、分圧用の抵抗1kΩを置き、適切に配線する。これには、以前Arduinoで遊ぶために買った「Seeedstudio Arduino Sidekick Basic Kit」に入っていたMF11-503Kというサーミスタをそのまま使う。

 それから、C++でこんなプログラムを書く。先週も書いたが、宮城大学小島研究室のサイトで公開しておられるコードは、Raspberry Pi 1では何の支障もなく動くが、Raspberry Pi 2 Model Bではそのままでは動かない。一部の型キャストなどを変更する必要がある。それについては先週のエントリで記してある。

//
//    tempMesure.cpp
//      佐藤俊夫
//      Sun Sep  6 13:57:53 JST 2015
//      サーミスタMF11-503Kと1kΩ抵抗を直列につなぎ、3.3Vを印加して、
//      その間から分圧をとって温度を測る。
//      宮城大学小島研究室でウェブ公開しておられるソースコードを拝借した。
//      コンパイル
//        cc -lm tempMesure.cpp raspSPI.o raspADC.o -o tempMesure
//
#include <stdio.h>
#include <math.h>
#include "raspADC.h"

#define ADC_CHIP  ADC_3208
float tempMesure(int srcVal);

int main() {
  ADC adc;

  adc.init("/dev/spidev0.1", ADC_CHIP);
  printf("%2.2f\n", tempMesure(adc.get(0)));
}

float tempMesure(int srcVal){
  const float B = 4350.0, Ta = 25.0, Rt0 = 50000.0;  //  MF11-503Kスペックシート記載
  const float K = 273.15;  //  熱力学温度の定数
  const float v0 = 3.3, r0 = 1000.0;  //  Arduino +5Vと電流調整抵抗1kΩ
  const int resolution = 4096;  //  アナログ入力の分解能
  float vt = 0.0, rt = 0.0;

  vt = srcVal * (v0 / (resolution - 1));
  rt = (v0 * r0 - vt * r0) / vt;
  return(1.0 / (log(rt / Rt0) / B + 1.0 / (Ta + K)) - K);
}

 これは単に、サーミスタで計った現在の温度を標準出力に書くだけだ。

 これを「popen();」で開けば、PHPで読める。簡単である。

<html>
 <head>
  <meta name="Editor" content="vim">
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <link rel="icon" href="favicon.ico" type="image/x-icon" />
  <link rel="Shortcut Icon" type="image/x-icon" href="favicon.ico" />
  <title>PHPで温度計</title>
  <meta http-equiv="Keyword" content="佐藤俊夫, 佐藤, 俊夫, SATOTOSHIO, SatoToshio, sato, toshio, Raspberry Pi, Raspberry Pi 2 Model B">
 </head>
 <body bgcolor="#888888">
<body>
  <center>
  <h1><img src="raspberry_pi.png" width="50px" align="middle"/>Raspberry Pi + PHPで温度計</h1>
  <hr>
  </center>
<?
ini_set( 'display_errors', 1 );
$process = popen("sudo /home/toshio/SPI_ADC/tempMesure", "r");
$temp = fgets($process);
?>
<center>
<table border=1 width="120px">
<tr> <th>0</th><th>10</th> <th>20</th> <th>30</th> <th>40</th> <th>50</th> <th>60</th></tr>
<tr><td colspan = 7>
<table>
<tr>
<td bgcolor="red" width="<?print $temp * 2?>px"><?print $temp?>℃</td&t;</tr></table>
</tr>
</table>
</td></tr></tr></table>
</center>
</body>
</html>

raspberry_tempMesure
 テーブルの幅を温度計のアルコール柱に見立てて、赤い帯で表すようにしてみた。


 Arduinoでも同じようなことはできるが、Arduinoで難しいのは、こういうふうに、無駄に画像を配置したり、少し分量の多いHTMLを書いたりすることだ。だが、こんなことはRaspberry Piには造作もなくできてしまう。

 反面、Arduinoはアナログ入力が直接可能だが、Raspberry Piで同じことをしようとすると、アナログ入力にAD変換ICを準備し、これとの通信にもいろいろと研究の上で取り組む必要がある。簡単で刺激的な目的のために少しプロトタイピングをしてみようと考えている多くのクリエイターがなしうるところではない。

 入門書籍「Raspberry Piをはじめよう」にもそうしたことが少し書かれてある。

そうか、PHPでもモノにつながるな

投稿日:

raspi_index Raspberry Piは要するにLinuxマシンなので、ウェブサーバも選り取り見取りだ。単にウェブインターフェイスを備えたモノのプロトをするならArduinoのほうが手っ取り早いが、いかんせん、リッチなウェブ・コンテンツをArduino+ETHERNET SHIELD 2でサービスするのは難しく、不可能ではないにもせよ、ウェブ・コンテンツに写真を置くなどというのはほとんど無理であった。

 Raspberry Piはコンピュータ・パワーが大きいから、大きなウェブ・コンテンツも本体内に飲み込んでしまえる。

 そこで、である。Raspberry Piで遊ぶのに、なにも世間の教科書通りにPythonやRubyの作例を打ち込むだけが能ではない。CだってC++だって、PerlだってPHPだって動くはずである。SMTPもPOPもNTPも、なんだって動く。多分MySQLだのPostgreSQLだのも動くだろう。

 Apacheをインストールして動かしてみよう。それから、PHPでGPIOを突っついてみよう。これは、PHPでハードウェアを動かすことにつながる。無駄に贅沢なWebインターフェイスでLチカ、というのも面白い。

Apacheのインストールに先立ち、今動いているサービスを確かめよう

 まず、今起動しているモノを確認してみよう。RedHat系のLinux、Fedoraなどはこういう時に「chkconfig –list」などとするが、Raspberry Piに入っているのは「Raspbian」だ。RaspbianはDebian系のディストリビューションなので、chkconfigはない。

 私はUNIX育ちのオッサンで、SVR4.2の管理人を長年やっきた。Linuxに関してはフリー時代のRedHat(4~9ぐらいまで)、それ以降はずっとFedora Coreを使ってきたので、Debianは良く知らないのである。ま、これも機会だから、ちょっと体験してみようではないの。

 ネット情報によると「insserv」「update-rc.d」「sysv-rc-conf」「rcconf」「upstart」などがあるという。

# insserv -s
K:01:0 1 6:triggerhappy
K:07:0 6:umountfs
K:04:0 6:umountnfs.sh
K:02:0 6:sendsigs
K:01:0 6:plymouth
K:03:0 1 6:rsyslog
K:06:0 6 S:hwclock.sh
K:01:0 1 6:alsa-utils
K:06:0 6:networking
K:05:0 1 2 3 4 5 6 S:rpcbind
K:05:0 1 2 3 4 5 6 S:nfs-common
K:01:0 6:urandom
K:01:0 1 6:avahi-daemon
K:01:0 1 6:dhcpcd
K:01:0 1 6:cgroup-bin
K:08:0 6:umountroot
K:09:0:halt
K:09:6:reboot
K:01:0 1 6:fake-hwclock
K:01:0 1 2 6:lightdm
K:01:0 1 6:ifplugd
S:02:S:udev
S:03:S:keyboard-setup
S:15:S:console-setup
S:01:2 3 4 5:triggerhappy
S:08:S:mountall.sh
S:09:S:mountall-bootclean.sh
S:12:S:mountnfs.sh
S:13:S:mountnfs-bootclean.sh
S:04:2 3 4 5:plymouth
S:01:2 3 4 5:rsyslog
S:16:S:alsa-utils
S:11:S:networking
S:10:S:urandom
S:04:S:mountdevsubfs.sh
S:05:S:checkroot.sh
S:03:2 3 4 5:avahi-daemon
S:02:2 3 4 5:dbus
S:01:2 3 4 5:dhcpcd
S:01:2 3 4 5:cgroup-bin
S:01:S:mountkernfs.sh
S:01:S:fake-hwclock
S:03:3 4 5:lightdm
S:16:S:x11-common
S:14:S:kbd
S:01:2 3 4 5:ifplugd
S:02:1:single
S:01:1:killprocs
S:01:1 2 3 4 5:bootlogs
S:01:S:hostname.sh
S:01:1 2 3 4 5:motd
S:02:2 3 4 5:dphys-swapfile
S:04:2 3 4 5:rc.local
S:04:2 3 4 5:rmnologin
S:02:2 3 4 5:cron
S:02:2 3 4 5:rsync
S:02:2 3 4 5:ssh
S:02:2 3 4 5:ntp
S:01:2 3 4 5:sudo
S:16:S:raspi-config
S:10:S:udev-mtab
S:06:S:checkroot-bootclean.sh
S:16:S:bootmisc.sh
S:06:S:kmod
S:16:S:plymouth-log
S:07:S:checkfs.sh
S:06:S:mtab.sh
S:10:S:procps

 ほほー、なるほど、コレは多分、左から現状(StartかKillか)、起動順、起動すべきRun-Levelのリスト、サービス名、だろうなあ。はて、S、Kじゃないほうの、Run-Levelの「S」は、なんだろ、と調べると、「起動順で一番はじめ、かつ、どのRun-Levelでも共通で起動」ということのようである。

 で、

# insserv -s | egrep '(pache)|(ttp)'
#

 なんてことをやっても、全然何も出ないから、デフォルトのRaspbianではhttpらしきものやApacheらしきものは何も動いていない。

Apacheをインストール

 それでは、というわけで、

# apt-get -s install apache2
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
以下の特別パッケージがインストールされます:
  apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapr1 libaprutil1
  libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert
提案パッケージ:
  apache2-doc apache2-suexec apache2-suexec-custom openssl-blacklist
以下のパッケージが新たにインストールされます:
  apache2 apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapr1
  libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert
アップグレード: 0 個、新規インストール: 10 個、削除: 0 個、保留: 0 個。
Inst libapr1 (1.4.6-3+deb7u1 Raspbian:7.0/oldstable [armhf])
Inst libaprutil1 (1.4.1-3 Raspbian:7.0/oldstable [armhf])
Inst libaprutil1-dbd-sqlite3 (1.4.1-3 Raspbian:7.0/oldstable [armhf])
Inst libaprutil1-ldap (1.4.1-3 Raspbian:7.0/oldstable [armhf])
Inst apache2.2-bin (2.2.22-13+deb7u6 Raspbian:7.0/oldstable [armhf])
Inst apache2-utils (2.2.22-13+deb7u6 Raspbian:7.0/oldstable [armhf])
Inst apache2.2-common (2.2.22-13+deb7u6 Raspbian:7.0/oldstable [armhf])
Inst apache2-mpm-worker (2.2.22-13+deb7u6 Raspbian:7.0/oldstable [armhf])
Inst apache2 (2.2.22-13+deb7u6 Raspbian:7.0/oldstable [armhf])
Inst ssl-cert (1.0.32 Raspbian:7.0/oldstable [all])
Conf libapr1 (1.4.6-3+deb7u1 Raspbian:7.0/oldstable [armhf])
Conf libaprutil1 (1.4.1-3 Raspbian:7.0/oldstable [armhf])
Conf libaprutil1-dbd-sqlite3 (1.4.1-3 Raspbian:7.0/oldstable [armhf])
Conf libaprutil1-ldap (1.4.1-3 Raspbian:7.0/oldstable [armhf])
Conf apache2.2-bin (2.2.22-13+deb7u6 Raspbian:7.0/oldstable [armhf])
Conf apache2-utils (2.2.22-13+deb7u6 Raspbian:7.0/oldstable [armhf])
Conf apache2.2-common (2.2.22-13+deb7u6 Raspbian:7.0/oldstable [armhf])
Conf apache2-mpm-worker (2.2.22-13+deb7u6 Raspbian:7.0/oldstable [armhf])
Conf apache2 (2.2.22-13+deb7u6 Raspbian:7.0/oldstable [armhf])
Conf ssl-cert (1.0.32 Raspbian:7.0/oldstable [all])
#

 特に問題なさそうだな、というわけで……

# apt-get install apache2
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
以下の特別パッケージがインストールされます:
  apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapr1 libaprutil1
  libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert
提案パッケージ:
  apache2-doc apache2-suexec apache2-suexec-custom openssl-blacklist
以下のパッケージが新たにインストールされます:
  apache2 apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapr1
  libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert
アップグレード: 0 個、新規インストール: 10 個、削除: 0 個、保留: 0 個。
1,356 kB のアーカイブを取得する必要があります。
この操作後に追加で 4,585 kB のディスク容量が消費されます。
続行しますか [Y/n]? y
取得:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libapr1 armhf 1.4.6-3+deb7u1 [90.9 kB]
取得:2 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libaprutil1 armhf 1.4.1-3 [77.1 kB]
取得:3 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libaprutil1-dbd-sqlite3 armhf 1.4.1-3 [17.2 kB]
取得:4 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libaprutil1-ldap armhf 1.4.1-3 [16.0 kB]
取得:5 http://mirrordirector.raspbian.org/raspbian/ wheezy/main apache2.2-bin armhf 2.2.22-13+deb7u6 [676 kB]
取得:6 http://mirrordirector.raspbian.org/raspbian/ wheezy/main apache2-utils armhf 2.2.22-13+deb7u6 [163 kB]
取得:7 http://mirrordirector.raspbian.org/raspbian/ wheezy/main apache2.2-common armhf 2.2.22-13+deb7u6 [292 kB]
取得:8 http://mirrordirector.raspbian.org/raspbian/ wheezy/main apache2-mpm-worker armhf 2.2.22-13+deb7u6 [2,238 B]
取得:9 http://mirrordirector.raspbian.org/raspbian/ wheezy/main apache2 armhf 2.2.22-13+deb7u6 [1,440 B]
取得:10 http://mirrordirector.raspbian.org/raspbian/ wheezy/main ssl-cert all 1.0.32 [19.5 kB]
1,356 kB を 4秒 で取得しました (281 kB/s)
パッケージを事前設定しています ...
以前に未選択のパッケージ libapr1 を選択しています。
(データベースを読み込んでいます ... 現在 78616 個のファイルとディレクトリがインストールされています。)
(.../libapr1_1.4.6-3+deb7u1_armhf.deb から) libapr1 を展開しています...
以前に未選択のパッケージ libaprutil1 を選択しています。
(.../libaprutil1_1.4.1-3_armhf.deb から) libaprutil1 を展開しています...
以前に未選択のパッケージ libaprutil1-dbd-sqlite3 を選択しています。
(.../libaprutil1-dbd-sqlite3_1.4.1-3_armhf.deb から) libaprutil1-dbd-sqlite3 を展開しています...
以前に未選択のパッケージ libaprutil1-ldap を選択しています。
(.../libaprutil1-ldap_1.4.1-3_armhf.deb から) libaprutil1-ldap を展開しています...
以前に未選択のパッケージ apache2.2-bin を選択しています。
(.../apache2.2-bin_2.2.22-13+deb7u6_armhf.deb から) apache2.2-bin を展開しています...
以前に未選択のパッケージ apache2-utils を選択しています。
(.../apache2-utils_2.2.22-13+deb7u6_armhf.deb から) apache2-utils を展開しています...
以前に未選択のパッケージ apache2.2-common を選択しています。
(.../apache2.2-common_2.2.22-13+deb7u6_armhf.deb から) apache2.2-common を展開しています...
以前に未選択のパッケージ apache2-mpm-worker を選択しています。
(.../apache2-mpm-worker_2.2.22-13+deb7u6_armhf.deb から) apache2-mpm-worker を展開していま す...
以前に未選択のパッケージ apache2 を選択しています。
(.../apache2_2.2.22-13+deb7u6_armhf.deb から) apache2 を展開しています...
以前に未選択のパッケージ ssl-cert を選択しています。
(.../ssl-cert_1.0.32_all.deb から) ssl-cert を展開しています...
man-db のトリガを処理しています ...
libapr1 (1.4.6-3+deb7u1) を設定しています ...
libaprutil1 (1.4.1-3) を設定しています ...
libaprutil1-dbd-sqlite3 (1.4.1-3) を設定しています ...
libaprutil1-ldap (1.4.1-3) を設定しています ...
apache2.2-bin (2.2.22-13+deb7u6) を設定しています ...
apache2-utils (2.2.22-13+deb7u6) を設定しています ...
apache2.2-common (2.2.22-13+deb7u6) を設定しています ...
Enabling site default.
Enabling module alias.
Enabling module autoindex.
Enabling module dir.
Enabling module env.
Enabling module mime.
Enabling module negotiation.
Enabling module setenvif.
Enabling module status.
Enabling module auth_basic.
Enabling module deflate.
Enabling module authz_default.
Enabling module authz_user.
Enabling module authz_groupfile.
Enabling module authn_file.
Enabling module authz_host.
Enabling module reqtimeout.
apache2-mpm-worker (2.2.22-13+deb7u6) を設定しています ...
[....] Starting web server: apache2apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
. ok
apache2 (2.2.22-13+deb7u6) を設定しています ...
ssl-cert (1.0.32) を設定しています ...
#

 なんてことなく終わる。

Apacheの起動状況を確かめる

 次に、

# insserv -s | egrep '(pache)|(ttp)'
K:01:0 1 6:apache2
S:02:2 3 4 5:apache2
#

……ぬぅ、StartなのかKillなのか、どっちやねん、というわけで、ブラウザにアドレスを入れてみると、

raspi-apache2start

……と、普通にApacheは動いている。KとS両方出るのは、起動順が01、02となっていることから、Run-Level 0、1、6ではどうあろうと強制終了し、改めて2、3、4、5では起動、ということであろうか。

 えーっと、自動起動のほうはこれでいいから、サービスの起動停止のテストは……、と。

# /etc/init.d/apache2 stop
[....] Stopping web server: apache2apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[ ok waiting ..
root@satoraspi:~# /etc/init.d/apache2 start
[....] Starting web server: apache2apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
. ok
#

 ふむ、だいだい動いとる。

ドキュメントルートのindex.htmlを作る

 とりあえず、indexだけ、かっこよくしておこう。えーっと、ドキュメントルートはどこかいな、と。

# ls -Fla /etc/ | egrep '(http)|(apache)'
drwxr-xr-x   7 root root    4096  8月 30 09:01 apache2/
root@satoraspi:~# ls /etc/apache2
apache2.conf  envvars  mods-available  ports.conf       sites-enabled
conf.d        magic    mods-enabled    sites-available
# cd /etc/apache2
# grep 'DocumentRoot' */*
sites-available/default:        DocumentRoot /var/www
sites-available/default-ssl:    DocumentRoot /var/www
sites-enabled/000-default:      DocumentRoot /var/www

 ……と、いうわけで普通に「/var/www」の下じゃのう。普通のユーザに戻って、

toshio@satoraspi:~$ cd /var/www
toshio@satoraspi:/var/www$ ls -Fla
合計 12
drwxr-xr-x  2 root root 4096  8月 30 09:01 ./
drwxr-xr-x 12 root root 4096  8月 30 09:01 ../
-rw-r--r--  1 root root  177  8月 30 09:01 index.html
toshio@satoraspi:/var/www$ cat index.html
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>toshio@satoraspi:/var/www$

 はあ、さっきブラウザに出てたヤツでんな、と。

 一応残しておいて……

toshio@satoraspi:/var/www$ sudo cp index.html .index.html.ORG
[sudo] password for toshio:
toshio@satoraspi:/var/www$ sudo chown toshio:pi index.html
toshio@satoraspi:/var/www$ ls -Fla
合計 16
drwxr-xr-x  2 root   root 4096  8月 30 09:37 ./
drwxr-xr-x 12 root   root 4096  8月 30 09:01 ../
-rw-r--r--  1 root   root  177  8月 30 09:37 .index.html.ORG
-rw-r--r--  1 toshio pi    177  8月 30 09:01 index.html
toshio@satoraspi:/var/www$cd ..
toshio@satoraspi:/var$sudo chown toshio:pi www
toshio@satoraspi:/var$cd www
toshio@satoraspi:/var/www$vi index.html

 んで、ばーっ、って、書くですよ。

<html>
 <head>
  <meta name="Editor" content="vim">
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <link rel="icon" href="favicon.ico" type="image/x-icon" />
  <link rel="Shortcut Icon" type="image/x-icon" href="favicon.ico" />
  <title>おっさん用Raspberry Pi 2 Model B</title>
  <meta http-equiv="Keyword" content="佐藤俊夫, 佐藤, 俊夫, SATOTOSHIO, SatoToshio, sato, toshio, Raspberry Pi, Raspberry Pi 2 Model B">
 </head>
 <body bgcolor="#888888">
  <basefont size=4">
  <center>
  <table>
  <tr>
  <td>
    <h1>おっさん用Raspberry Pi 2 Model B</h1>
    <hr>
    <center><img src="raspberry_pi.png"></center>
  </td>
  </tr>
  <tr>
  <td>
    とりあえずまだ何もない。
  </td>
  </tr>
 </table>
 </center>
 </body>
</html>

 んで、画像なんかをSCPでコピーして、まずはこんなindex.htmlですな。

raspi_index

 それにしても、こんな、5千円かそこらのシングルボードでapache2のウェブサービスができるなんて、隔世の感があるな。

同じようにして、PHPをインストール

 さておき、続いてPHPを入れよう。

toshio@satoraspi:/var/www$ cd
toshio@satoraspi:~$ su -
パスワード:
root@satoraspi:~# apt-cache search php5
dwoo - PHP5 template engine
libapache2-mod-php5 - server-side, HTML-embedded scripting language (Apache 2 module)
libapache2-mod-php5filter - server-side, HTML-embedded scripting language (apache 2 filter module)
libexpect-php5 - expect module for PHP 5
libgv-php5 - PHP5 bindings for graphviz
libkohana2-modules-php - lightweight PHP5 MVC framework (extension modules)
libkohana2-php - lightweight PHP5 MVC framework
libkohana3.1-core-php - PHP5 framework core classes
libkohana3.1-php - PHP5 framework metapackage
libkohana3.2-core-php - PHP5 framework core classes
libkohana3.2-php - PHP5 framework metapackage
libow-php5 - Dallas 1-wire support: PHP5 bindings
libphp-jpgraph - Object oriented graph library for php5
libphp-jpgraph-examples - Object oriented graph library for php5 (examples)
libphp5-embed - HTML-embedded scripting language (Embedded SAPI library)
php-doc - Documentation for PHP5
php-imlib - PHP Imlib2 Extension
php5 - server-side, HTML-embedded scripting language (metapackage)
php5-adodb - Extension optimising the ADOdb database abstraction library
php5-cgi - server-side, HTML-embedded scripting language (CGI binary)
php5-cli - command-line interpreter for the php5 scripting language
php5-common - Common files for packages built from the php5 source
php5-curl - CURL module for php5
php5-dbg - Debug symbols for PHP5
php5-dev - Files for PHP5 module development
php5-enchant - Enchant module for php5
php5-exactimage - fast image manipulation library (PHP bindings)
php5-ffmpeg - audio and video support via ffmpeg for php5
php5-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary)
php5-gd - GD module for php5
php5-gdcm - Grassroots DICOM PHP5 bindings
php5-geoip - GeoIP module for php5
php5-gmp - GMP module for php5
php5-imagick - ImageMagick module for php5
php5-imap - IMAP module for php5
php5-interbase - interbase/firebird module for php5
php5-intl - internationalisation module for php5
php5-lasso - Library for Liberty Alliance and SAML protocols - PHP 5 bindings
php5-ldap - LDAP module for php5
php5-librdf - PHP5 language bindings for the Redland RDF library
php5-mapscript - php5-cgi module for MapServer
php5-mcrypt - MCrypt module for php5
php5-memcache - memcache extension module for PHP5
php5-memcached - memcached extension module for PHP5, uses libmemcached
php5-midgard2 - Midgard2 Content Repository - PHP5 language bindings and module
php5-ming - Ming module for php5
php5-mysql - MySQL module for php5
php5-mysqlnd - MySQL module for php5 (Native Driver)
php5-odbc - ODBC module for php5
php5-pgsql - PostgreSQL module for php5
php5-ps - ps module for PHP 5
php5-pspell - pspell module for php5
php5-radius - PECL radius module for PHP 5
php5-recode - recode module for php5
php5-remctl - PECL module for Kerberos-authenticated command execution
php5-rrd - rrd module for PHP 5
php5-sasl - Cyrus SASL extension for PHP 5
php5-snmp - SNMP module for php5
php5-sqlite - SQLite module for php5
php5-svn - PHP Bindings for the Subversion Revision control system
php5-sybase - Sybase / MS SQL Server module for php5
php5-tidy - tidy module for php5
php5-tokyo-tyrant - PHP interface to Tokyo Cabinet's network interface, Tokyo Tyrant
php5-vtkgdcm - Grassroots DICOM VTK PHP bindings
php5-xcache - Fast, stable PHP opcode cacher
php5-xdebug - Xdebug Module for PHP 5
php5-xmlrpc - XML-RPC module for php5
php5-xsl - XSL module for php5
phpunit - Unit testing suite for PHP5
root@satoraspi:~#

 はあ、いっぱい出たけど、要するにPHP5があるんだよな。うん、うん。

root@satoraspi:~# apt-get -s install php5
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
以下の特別パッケージがインストールされます:
  apache2-mpm-prefork libapache2-mod-php5 libonig2 libqdbm14 lsof php5-cli php5-common
提案パッケージ:
  php-pear
以下のパッケージは「削除」されます:
  apache2-mpm-worker
以下のパッケージが新たにインストールされます:
  apache2-mpm-prefork libapache2-mod-php5 libonig2 libqdbm14 lsof php5 php5-cli
  php5-common
アップグレード: 0 個、新規インストール: 8 個、削除: 1 個、保留: 0 個。
Remv apache2-mpm-worker [2.2.22-13+deb7u6] [apache2:armhf ]
Inst apache2-mpm-prefork (2.2.22-13+deb7u6 Raspbian:7.0/oldstable [armhf])
Conf apache2-mpm-prefork (2.2.22-13+deb7u6 Raspbian:7.0/oldstable [armhf])
Inst lsof (4.86+dfsg-1 Raspbian:7.0/oldstable [armhf])
Inst php5-common (5.4.44-0+deb7u1 Raspbian:7.0/oldstable [armhf])
Inst libonig2 (5.9.1-1 Raspbian:7.0/oldstable [armhf])
Inst libqdbm14 (1.8.78-2 Raspbian:7.0/oldstable [armhf])
Inst libapache2-mod-php5 (5.4.44-0+deb7u1 Raspbian:7.0/oldstable [armhf])
Inst php5 (5.4.44-0+deb7u1 Raspbian:7.0/oldstable [all])
Inst php5-cli (5.4.44-0+deb7u1 Raspbian:7.0/oldstable [armhf])
Conf lsof (4.86+dfsg-1 Raspbian:7.0/oldstable [armhf])
Conf php5-common (5.4.44-0+deb7u1 Raspbian:7.0/oldstable [armhf])
Conf libonig2 (5.9.1-1 Raspbian:7.0/oldstable [armhf])
Conf libqdbm14 (1.8.78-2 Raspbian:7.0/oldstable [armhf])
Conf libapache2-mod-php5 (5.4.44-0+deb7u1 Raspbian:7.0/oldstable [armhf])
Conf php5 (5.4.44-0+deb7u1 Raspbian:7.0/oldstable [all])
Conf php5-cli (5.4.44-0+deb7u1 Raspbian:7.0/oldstable [armhf])
root@satoraspi:~#

 入れても大丈夫みたいだ。

root@satoraspi:~# apt-get  install php5
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
以下の特別パッケージがインストールされます:
  apache2-mpm-prefork libapache2-mod-php5 libonig2 libqdbm14 lsof php5-cli php5-common
提案パッケージ:
  php-pear
以下のパッケージは「削除」されます:
  apache2-mpm-worker
以下のパッケージが新たにインストールされます:
  apache2-mpm-prefork libapache2-mod-php5 libonig2 libqdbm14 lsof php5 php5-cli
  php5-common
アップグレード: 0 個、新規インストール: 8 個、削除: 1 個、保留: 0 個。
6,142 kB のアーカイブを取得する必要があります。
この操作後に追加で 17.3 MB のディスク容量が消費されます。
続行しますか [Y/n]? y
取得:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main apache2-mpm-prefork armhf 2.2.22-13+deb7u6 [2,342 B]
取得:2 http://mirrordirector.raspbian.org/raspbian/ wheezy/main lsof armhf 4.86+dfsg-1 [321 kB]
取得:3 http://mirrordirector.raspbian.org/raspbian/ wheezy/main php5-common armhf 5.4.44-0+deb7u1 [621 kB]
取得:4 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libonig2 armhf 5.9.1-1 [130 kB]
取得:5 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libqdbm14 armhf 1.8.78-2 [119 kB]
取得:6 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libapache2-mod-php5 armhf 5.4.44-0+deb7u1 [2,479 kB]
取得:7 http://mirrordirector.raspbian.org/raspbian/ wheezy/main php5 all 5.4.44-0+deb7u1 [1,024 B]
取得:8 http://mirrordirector.raspbian.org/raspbian/ wheezy/main php5-cli armhf 5.4.44-0+deb7u1 [2,469 kB]
6,142 kB を 6秒 で取得しました (1,016 kB/s)
dpkg: apache2-mpm-worker: 依存関係に問題があります。しかし要求に従い削除しています:
 apache2 は以下に依存 (depends) します: apache2-mpm-worker (= 2.2.22-13+deb7u6) | apache2-mpm-prefork (= 2.2.22-13+deb7u6) | apache2-mpm-event (= 2.2.22-13+deb7u6) | apache2-mpm-itk (= 2.2.22-13+deb7u6) ...しかし:
  パッケージ apache2-mpm-worker は削除されようとしています。
  パッケージ apache2-mpm-prefork はまだインストールされていません。
  パッケージ apache2-mpm-event はまだインストールされていません。
  パッケージ apache2-mpm-itk はまだインストールされていません。

(データベースを読み込んでいます ... 現在 79208 個のファイルとディレクトリがインストールされています。)
apache2-mpm-worker を削除しています ...
[....] Stopping web server: apache2apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[ ok waiting ..
以前に未選択のパッケージ apache2-mpm-prefork を選択しています。
(データベースを読み込んでいます ... 現在 79203 個のファイルとディレクトリがインストールされています。)
(.../apache2-mpm-prefork_2.2.22-13+deb7u6_armhf.deb から) apache2-mpm-prefork を展開してい ます...
apache2-mpm-prefork (2.2.22-13+deb7u6) を設定しています ...
[....] Starting web server: apache2apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
. ok
以前に未選択のパッケージ lsof を選択しています。
(データベースを読み込んでいます ... 現在 79208 個のファイルとディレクトリがインストールされています。)
(.../lsof_4.86+dfsg-1_armhf.deb から) lsof を展開しています...
以前に未選択のパッケージ php5-common を選択しています。
(.../php5-common_5.4.44-0+deb7u1_armhf.deb から) php5-common を展開しています...
以前に未選択のパッケージ libonig2 を選択しています。
(.../libonig2_5.9.1-1_armhf.deb から) libonig2 を展開しています...
以前に未選択のパッケージ libqdbm14 を選択しています。
(.../libqdbm14_1.8.78-2_armhf.deb から) libqdbm14 を展開しています...
以前に未選択のパッケージ libapache2-mod-php5 を選択しています。
(.../libapache2-mod-php5_5.4.44-0+deb7u1_armhf.deb から) libapache2-mod-php5 を展開しています...
以前に未選択のパッケージ php5 を選択しています。
(.../php5_5.4.44-0+deb7u1_all.deb から) php5 を展開しています...
以前に未選択のパッケージ php5-cli を選択しています。
(.../php5-cli_5.4.44-0+deb7u1_armhf.deb から) php5-cli を展開しています...
man-db のトリガを処理しています ...
lsof (4.86+dfsg-1) を設定しています ...
php5-common (5.4.44-0+deb7u1) を設定しています ...

Creating config file /etc/php5/mods-available/pdo.ini with new version
libonig2 (5.9.1-1) を設定しています ...
libqdbm14 (1.8.78-2) を設定しています ...
libapache2-mod-php5 (5.4.44-0+deb7u1) を設定しています ...

Creating config file /etc/php5/apache2/php.ini with new version
[....] Restarting web server: apache2apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
 ... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
. ok
php5 (5.4.44-0+deb7u1) を設定しています ...
php5-cli (5.4.44-0+deb7u1) を設定しています ...

Creating config file /etc/php5/cli/php.ini with new version
update-alternatives: /usr/bin/php (php) を提供するために 自動モード で /usr/bin/php5 を使います
root@satoraspi:~#

……と、いうわけで、サクッと入る。

PHPの作動を確かめる

 それでは早速……。

root@satoraspi:/var/www# ログアウト
toshio@satoraspi:~$ cd /var/www
toshio@satoraspi:/var/www$ vi infotest.php

 で、中身はこじんまりとこう書いて……

<html>
<head></head>
<body>
<?phpinfo();?>
</body>
</html>

 早速ブラウザで見ると……

raspi-phpinfo

うん。動いちょる動いちょる。

さっそくPHPでLチカ

 さて、次に、phpからGPIOだな。マンネリだけどやっぱり「Lチカ」か。

「web2LED.php」

<html>
 <head>
  <meta name="Editor" content="vim">
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <link rel="icon" href="favicon.ico" type="image/x-icon" />
  <link rel="Shortcut Icon" type="image/x-icon" href="favicon.ico" />
  <title>PHPでLチカ</title>
  <meta http-equiv="Keyword" content="佐藤俊夫, 佐藤, 俊夫, SATOTOSHIO, SatoToshio, sato, toshio, Raspberry Pi, Raspberry Pi 2 Model B">
 </head>
 <body bgcolor="#888888">
<body>
  <center>
  <h1>PHPでLチカ</h1>
  <hr>
  <form method="POST">
  <input type="submit" name="on"  value="LED on"><br>
  <input type="submit" name="off" value="LED off">
  </form>
  </center>
<?
ini_set( 'display_errors', 1 );
file_put_contents('/sys/class/gpio/export', 17);
file_put_contents('/sys/class/gpio/gpio17/direction', 'out');
if(isset($_POST['on'])){
  file_put_contents('/sys/class/gpio/gpio17/value', 1);
  print("LED ON.");
}elseif(isset($_POST['off'])){
  file_put_contents('/sys/class/gpio/gpio17/value', 0);
  print("LED OFF.");
}
file_put_contents('/sys/class/gpio/unexport', 17);
?>
</body>
</html>

 ブレッドボードにLEDと330Ωの抵抗を直列につけて、17番ピンとアースにつなぐ。

IMG_3313

 で、こういうフォームになるのだが……

web2LED_permission_deny

 いや、そりゃまあ、動くわけないわな、GPIOはrootでなきゃ読み書きできんのだから。

GPIOのパーミションの調整

 えーっと、どうやったらいいかな、ということで、

  • 「sudoする」
  • 「GPIOのアクセス権をヌルくする」
  • 「PHPの実行ユーザを強くする」
  • 「何かをSUID」

……などと、いろいろあるが、どうも、どれもいまいち、ピンと来ないな……。

 まあ、GPIOのアクセスをヌルくする、これかなあ……。

toshio@satoraspi:~$ cd /sys/class
toshio@satoraspi:/sys/class$ su
パスワード:
root@satoraspi:/sys/class# chmod -R 777 gpio
root@satoraspi:/sys/class# ls -Flad gpio
drwxrwxrwx 2 root gpio 0  8月 30 13:20 gpio/
root@satoraspi:/sys/class#

 なんっか、もう、力いっぱい777丸出しですけどね(笑)。多分、700とか760でも大丈夫じゃないかなあ。試してないけど。

 で、まあ、これで、フォームをクリックするとLEDが点いたり消えたりする。

Raspberry Pi + Apache + PHP + SSRでAC100Vの家電製品を制御

 これをそのまま、いつぞやArduinoでやったSSR(ソリッド・ステート・リレー)につなぐと、家電製品のオン・オフなどができるわけだ。

SSRモジュール
IMG_3047

 では、やってみよう。

Raspberry Pi 2で扇風機のオン・オフ

 真ん中の黒い箱がSSRだ。Arduinoは5V、Raspberry Pi 2は3.3Vで電圧が違うが、このSSRは3Vから8Vまでの入力を受け付けるので、大丈夫なのである。

とにかく目一杯

投稿日:

 実用的なモノというとネット便器ぐらいしか作らず、Lチカばかりやっている今日この頃であるが、Lチカは楽しいので仕方がない。というか、「ネット便器のどこが実用的なのか?」というツッコミに極めて脆弱な文章をついつい書いてしまっているところもなかなか痛いが、さておき、昼間の「亀の子ブレボー」に、いつぞや大量生産したビーズ付きLEDを目一杯大量に植え込み、TLC5940NTでゴンゴンドライブしてみた。

 前回TLC5940NTを使ったときは、単純に右から左へ光るだけだったが、今度と言う今度は、もう、今思いつくだけあらん限りいろんな光らせ方をさせてみた。

 亀の子ブレッドボードは、なかなか省スペースで、うまく組み付けられる。

TLC5940NTを亀の子ブレボー(笑)に設置した状況
IMG_3259

 LEDをタップリ植え込むとこうなる。

IMG_3261

 動かすとこんな感じだ。

 スケッチはこうなった。はじめはタクトスイッチで手動切り替えをしていたが、面倒臭くなり、乱数で切り替えるようにした。

//
//  tlcVariation.ino
//    TLC5940NTにつないだLED、ランダムにいろんな光らせ方をしてみる。
//    27.08.11(火) 1515~
//    佐藤俊夫
//
#include "Tlc5940.h"
//
const int switchInterval = 5000;
//
void setup() {
  Tlc.init();
  Tlc.clear();
  Tlc.update();
}
//
void loop() {
  static int mode = 0;
  static long int prevtime = 0;
  if(millis() > prevtime + switchInterval){
    mode = random(0, 8);
    prevtime = millis();
    //  今のエフェクトを徐々に消す。
    int maxlum = 0;
    do{
      maxlum = 0;
      for(int i = 0; i <= 15; i++){
        Tlc.set(i, Tlc.get(i) * 0.9);
        maxlum = Tlc.get(i) > maxlum ? Tlc.get(i) : maxlum;
      }
      Tlc.update();
      delay(20);
    }while(maxlum > 0);
  }
  switch(mode){
    case 0:
      sinCurve_diff();
      break;
    case 1:
      inOrder_left();
      break;
    case 2:
      toCenter();
      break;
    case 3:
      inOrder_tail();
      break;
    case 4:
      left_right();
      break;
    case 5:
      left_right_tail();
      break;
    case 6:
      allAtOnce_tail();
      break;
    case 7:
      inOrder_right();
      break;
    default:
      sinCurve_diff();
      break;
  }
}
//
void inOrder_right(){
  //  0.1秒おきに右から切り替えていく。
  static long int prevtime = 0.0;
  static int led = 15;
  Tlc.clear();
  if(millis() >= prevtime + 100){
    Tlc.clear();
    Tlc.set(led--, 4095);
    led = led < 0 ? 15 : led;
    Tlc.update();
    prevtime = millis();
  }
}
//
void allAtOnce_tail(){
  //  一斉に点灯して徐々に消える。
  static long int prevtime = 0;
  if(millis() > prevtime + 100){
    prevtime = millis();
    if(Tlc.get(0) <= 0){
      for(int i = 0; i <= 15; i++){
        Tlc.set(i, 4095);
      }
    }else{
      for(int i = 0; i <= 15; i++){
        Tlc.set(i, Tlc.get(i) * 0.8);
      }
    }
    Tlc.update();
  }
}
    
//
void left_right_tail(){
  //  尾を引きながら右へ行ったり左へ行ったり。
  static long int prevtime = 0.0;
  static int led = 0, order = 1;
  if(millis() >= prevtime + 100){
    for(int i = 0; i <= 15; i++){
      Tlc.set(i, (int)((float)Tlc.get(i) * 0.5));
    }
    led = led + order;
    Tlc.set(led, 4095);
    order = (led >= 15) || (led <= 0) ? order * -1 : order;
    Tlc.update();
    prevtime = millis();
  }
}
//
void left_right(){
  //  右へ行ったり左へ行ったり
  static long int prevtime = 0.0;
  static int led = 0, order = 1;
  if(millis() >= prevtime + 100){
    Tlc.clear();
    led = led + order;
    Tlc.set(led, 4095);
    order = (led >= 15) || (led <= 0) ? order * -1 : order;
    Tlc.update();
    prevtime = millis();
  }
}
//
void inOrder_tail(){
  //  尾を引きながら0.1秒おきに左から切り替えていく。
  static long int prevtime = 0.0;
  static int led = 0;
  if(millis() >= prevtime + 100){
    for(int i = 0; i <= 15; i++){
      Tlc.set(i, (int)((float)Tlc.get(i) * 0.5));
    }
    Tlc.set(led++, 4095);
    led = led > 15 ? 0 : led;
    Tlc.update();
    prevtime = millis();
  }
}
//
void toCenter(){
  //  中央付近のLEDから外へ光らせる。
  static long int prevtime = 0.0;
  static int ledleft = 7, ledright = 8;
  if(millis() >= prevtime + 100){
    for(int i = 0; i <= 15; i++){
      Tlc.set(i, (int)((float)Tlc.get(i) * 0.5));
    }
    Tlc.set(ledright++, 4095);
    Tlc.set(ledleft--,  4095);
    ledright = ledright > 15 ? 8 : ledright;
    ledleft = ledleft < 0 ? 7 : ledleft;
    Tlc.update();
    prevtime = millis();
  }
  
}
//
void inOrder_left(){
  //  0.1秒おきに切り替えていく。
  static long int prevtime = 0.0;
  static int led = 0;
  Tlc.clear();
  if(millis() >= prevtime + 100){
    Tlc.clear();
    Tlc.set(led++, 4095);
    led = led > 15 ? 0 : led;
    Tlc.update();
    prevtime = millis();
  }
}
//
void sinCurve_diff(){
  //  呼ぶたびにサインカーブをちょっとづつずらしながら光らせる。
  static float x[16] = {
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
  };
  for(int i = 0; i <= 15; i++){
    x[i] = x[i] >= (2.0 * PI) ? 0.0 : x[i] + ((2.0 * PI) / 1000.0) * (1.0 + (float)i / 10.0);
    Tlc.set(i, (int)(((sin(x[i]) + 1.0) / 2.0) * 4095));
  }
  Tlc.update();
}

ブレボー亀の子

投稿日:

 ブレッドボードに小さいプロトタイプを作るとき、この「プロトタイピングシールド」も捨てがたい。だが、しばらく使ってみての感想なのだが、小さくて不便だ。また、電源ラインもどうも使いづらい。シールド上の赤ソケットには実は5Vは到達していないので、自分で配線しなければならないのだ。

便利だが、小さいところが難
IMG_3031

 と言って、大きい方だと、Arduinoに重ねるわけにもいかず、昨日のようになってしまう。

たかが7セグLED2個のためにゴチャつくブレッドボード(笑)
IMG_3240

 何かいいことないかな、と思ってネットを漁っていたら、ある方がブレッドボードの脇に直接Arduino用のピンヘッダを接着剤で取り付けて、目的を達成しておられるのを見つけた。ほほー、なるほどなるほど……。

 つまり、大抵のブレッドボードは、同じメーカーの物なら、こういうふうに脇を取り外したり組み合わせたりできるのだが、電源ラインを一つ外すと、Arduinoの上下のピン幅とピッタリなのである。

IMG_3252
IMG_3253

 チナミに、このようにバラしたり組み合わせたりするには、ブレッドボード裏の両面テープの、つけ外しする部分をカッターナイフでスーッと切ると良い。

 しかし、接着剤でピンソケットを付けてしまう、てのも、なあ……。他のことにブレッドボードが使えなくなってしまうし……。

 で、考えてみたら、私は次の写真のようなArduino用のプラスチックケースを使っているが……

IMG_3254

……この上に電源ラインを一本外したブレッドボードを亀の子にし、輪ゴムか何かで縛っておけば、目的は達成できるではないか。

IMG_3255

 どうして今までこうしなかったかと言うと、そのままだとプラスチックケースの上下に細く開けてあるピンソケットにアクセスするためのスリットがふさがり、Arduinoから信号線が出せないからである。しかし、電源ラインを外すと、上下ともちょうどピッタリ、スリットが隠れずにすむのだ。

 ほうほう、なるほど、というわけで、この前作ったビーズのLEDなど植えてみる。Arduinoの「Arduino AT HEART♥」みたいな感じで、ハートですな、男のくせにw。

IMG_3256

 動かすとこんな感じ。

 このLチカはシンプルで、スケッチはこう。

//
//  differSin.ino
//    サインカーブをちょっとづつずらしてLチカ
//    佐藤俊夫
//    27.08.11(火) 1100~
//    
void setup() {
  pinMode( 9, OUTPUT);
  pinMode(10, OUTPUT);
}

void loop() {
  static float x1 = 0.0, x2 = 0.0;
  x1 = x1 > (2.0 * PI) ? 0.0 : x1 + (2.0 * PI / 1000.0);
  x2 = x2 > (2.0 * PI) ? 0.0 : x2 + (2.0 * PI / 1100.0);
  analogWrite( 9, (int)((sin(x1) + 1) * 128));
  analogWrite(10, (int)((sin(x2) + 1) * 128));
  delay(2);
}

 そう言えば、以前、「なんとっ!Arduinoって3項演算子ないのかッ!?」「ううっ、あったらなあ」などと書いたことがあったが、これは早とちりで、私の間違い。ちゃんとありました。上記スケッチでご覧のとおり(苦笑)

2桁順番

投稿日:

 次に、TLC5940NTを使って、数字をちゃんと表示させる。

 前と同じ回路で、スケッチは次のようにする。

//
//  tlc594027segmentLED.ino
//    7セグメントLEDをTLC5940NTで制御
//    27.08.10(月)1800~
//    佐藤俊夫
//
#include "Tlc5940.h"
//
void setup()
{
  Tlc.init();
}

void loop()
{
  Tlc.clear();
  static int n = 0;
  disp7LED(n++);
  if(n >= 100) n = 0;
  delay(1000);
}
//
void disp7LED(int n){
  const int on = 4095, off = 0;
  const int pat[10][8] = {
    // 0    1    2    3    4    5    6    7pin   num
    { on,  on,  on, off,  on,  on,  on, off},  //  0
    {off, off,  on, off,  on, off, off, off},  //  1
    { on,  on, off, off,  on,  on, off,  on},  //  2
    {off,  on,  on, off,  on,  on, off,  on},  //  3
    {off, off,  on, off,  on, off,  on,  on},  //  4
    {off,  on,  on, off, off,  on,  on,  on},  //  5
    { on,  on,  on, off, off,  on,  on,  on},  //  6
    {off, off,  on, off,  on,  on,  on, off},  //  7
    { on,  on,  on, off,  on,  on,  on,  on},  //  8
    {off,  on,  on, off,  on,  on,  on,  on}   //  9
  };
  //  一旦消す
  Tlc.clear();
  //  10の位
  int n10 = n / 10, i = 0;
  for(i = 0; i <= 7; i++){
    Tlc.set(i, pat[n10][i]);
  }
  // 1の位
  int n1 = n % 10;
  for(i = 0; i <= 7; i++){
    Tlc.set(i + 8, pat[n1][i]);
  }
  Tlc.update();    
}

 次のようにカウントアップしていく。

 そういえば昔の刑事ドラマなんかで、時限爆弾のカウントって、こんな感じで、電線がゴチャゴチャしてたっけなあ。ふふっ、爆発しそうだな、これじゃ(笑)。

耽るLチカ

投稿日:

 LEDは半導体であるから、電球と違って、光り始める前は抵抗は無限大であり、電流は流れない。ところが電圧がVfを超えて光り始めたら最後、抵抗がなくなって電源をショートさせた状態でぶっ飛び、自分自身も壊れる。従って必ず抵抗を一緒にくっつけてやるのだ。

 その抵抗を選ぶのは簡単で、基本的に E = I\cdot R という、この式のみでよい。LEDを買ってくると、流すべき電流値(If)、光り始める電圧(Vf)がどこかに書いてあるから、それに従って計算する。すなわち、

R = \frac{V - Vf}{If}

ここに、

V 自分が用意する電源の電圧
Vf 買ってきたLEDのVf(順方向電圧)、つまり光り始める電圧
If 買ってきたLEDのIf(順方向電流)、つまり光らせるために必要な電流

 ただ、抵抗は入手可能な数値が決まっており、そのものピタリという抵抗値のものは売っていない。なので、計算した値と一番近い抵抗を選び、その抵抗値で再び電流を計算して、買ってきたLEDのIfを超えていないかどうかを確かめる。

 これが、簡単な計算ではあるけどいちいち面倒くさい。

 それでまあ、抵抗を選ぶのにこういうスプレッドシートを作る。

 それから、これを使ってみよう。

IMG_3181

 これは、「武蔵野電波のプロトタイパーズ第15回『TLC5940で16個のLEDを遊ぶ』」で取り上げられている「TLC5940NT」というICだ。千石電商本店2階、入って左側の、一番奥のほうの抽斗で売られている。武蔵野電波のページでは400~700円とされているが、千石電商の店頭売りでは390円である。

 Arduinoで使うには、GitHubにあるライブラリをダウンロードし、zipを展開して出てくる「tlc5940」というディレクトリをArduino IDEのインストールディレクトリの下にある「libraries」の中にコピーすればよい。

 スペックシートはコレだが、スペックシートを見るより、Arduinoにライブラリを入れると出てくるようになるサンプルスケッチの「BasicUse」を見た方が分かり易いと思う。こんな風にサンプルは書かれている。

/*
    Basic Pin setup:
    ------------                                  ---u----
    ARDUINO   13|-> SCLK (pin 25)           OUT1 |1     28| OUT channel 0
              12|                           OUT2 |2     27|-> GND (VPRG)
              11|-> SIN (pin 26)            OUT3 |3     26|-> SIN (pin 11)
              10|-> BLANK (pin 23)          OUT4 |4     25|-> SCLK (pin 13)
               9|-> XLAT (pin 24)             .  |5     24|-> XLAT (pin 9)
               8|                             .  |6     23|-> BLANK (pin 10)
               7|                             .  |7     22|-> GND
               6|                             .  |8     21|-> VCC (+5V)
               5|                             .  |9     20|-> 2K Resistor -> GND
               4|                             .  |10    19|-> +5V (DCPRG)
               3|-> GSCLK (pin 18)            .  |11    18|-> GSCLK (pin 3)
               2|                             .  |12    17|-> SOUT
               1|                             .  |13    16|-> XERR
               0|                           OUT14|14    15| OUT channel 15
    ------------                                  --------

    -  Put the longer leg (anode) of the LEDs in the +5V and the shorter leg
         (cathode) in OUT(0-15).
    -  +5V from Arduino -> TLC pin 21 and 19     (VCC and DCPRG)
    -  GND from Arduino -> TLC pin 22 and 27     (GND and VPRG)
    -  digital 3        -> TLC pin 18            (GSCLK)
    -  digital 9        -> TLC pin 24            (XLAT)
    -  digital 10       -> TLC pin 23            (BLANK)
    -  digital 11       -> TLC pin 26            (SIN)
    -  digital 13       -> TLC pin 25            (SCLK)
    -  The 2K resistor between TLC pin 20 and GND will let ~20mA through each
       LED.  To be precise, it's I = 39.06 / R (in ohms).  This doesn't depend
       on the LED driving voltage.
    - (Optional): put a pull-up resistor (~10k) between +5V and BLANK so that
                  all the LEDs will turn off when the Arduino is reset.

    If you are daisy-chaining more than one TLC, connect the SOUT of the first
    TLC to the SIN of the next.  All the other pins should just be connected
    together:
        BLANK on Arduino -> BLANK of TLC1 -> BLANK of TLC2 -> ...
        XLAT on Arduino  -> XLAT of TLC1  -> XLAT of TLC2  -> ...
    The one exception is that each TLC needs it's own resistor between pin 20
    and GND.

    This library uses the PWM output ability of digital pins 3, 9, 10, and 11.
    Do not use analogWrite(...) on these pins.

    This sketch does the Knight Rider strobe across a line of LEDs.

    Alex Leone <acleone ~AT~ gmail.com>, 2009-02-03 */

#include "Tlc5940.h"

void setup()
{
  /* Call Tlc.init() to setup the tlc.
     You can optionally pass an initial PWM value (0 - 4095) for all channels.*/
  Tlc.init();
}

/* This loop will create a Knight Rider-like effect if you have LEDs plugged
   into all the TLC outputs.  NUM_TLCS is defined in "tlc_config.h" in the
   library folder.  After editing tlc_config.h for your setup, delete the
   Tlc5940.o file to save the changes. */

void loop()
{
  int direction = 1;
  for (int channel = 0; channel < NUM_TLCS * 16; channel += direction) {

    /* Tlc.clear() sets all the grayscale values to zero, but does not send
       them to the TLCs.  To actually send the data, call Tlc.update() */
    Tlc.clear();

    /* Tlc.set(channel (0-15), value (0-4095)) sets the grayscale value for
       one channel (15 is OUT15 on the first TLC, if multiple TLCs are daisy-
       chained, then channel = 16 would be OUT0 of the second TLC, etc.).

       value goes from off (0) to always on (4095).

       Like Tlc.clear(), this function only sets up the data, Tlc.update()
       will send the data. */
    if (channel == 0) {
      direction = 1;
    } else {
      Tlc.set(channel - 1, 1000);
    }
    Tlc.set(channel, 4095);
    if (channel != NUM_TLCS * 16 - 1) {
      Tlc.set(channel + 1, 1000);
    } else {
      direction = -1;
    }

    /* Tlc.update() sends the data to the TLCs.  This is when the LEDs will
       actually change. */
    Tlc.update();

    delay(75);
  }

}

 この最初のほうのコメントにアスキー・アートで書かれている図を見て結線するとよい。こんな感じだ。

IMG_3185

 LEDは秋葉原・千石電商の隣の店、「akiba LEDピカリ館」で売っていた10個入り300円の白色LEDで、Vfが3.0V~3.4V、Ifが20mAとある。電源が5Vならば100Ωばかり抵抗を付けてやればいい理屈だが、全部点灯させるとArduinoがダメになってしまうから、さらに絞って10KΩつけてやる。

 コンパイルして動かすとこうなる。

 10kΩでもこれくらい明るい。

 このICを使うと、パルス幅変調の幅も、Arduinoが256段階であるのに比べ、4096段階と格段に細かくなる。

 アレンジを加えてみよう。昨日買ってきたポテンショメータを使う。アナログの4番ピンと5番ピンに50kΩのポテンショメータと10kΩの抵抗をつなぎ、それぞれを強さと速さにして、「尾を引いたみたいに」明るさ制御をする。

 ポテンショメータの回路はこうする。

IMG_3191

 図の「E1」をアナログ入力で読めばよい。ポテンショメータのつまみの位置は、次の計算でR2を求めれば明らかになる。

E_{0} = I_{0}\cdot(R_{1} + R_{2})
I_{0} = \frac{E_{0}}{R_{1} + R_{2}}…①
I_{0} = \frac{E_{1}}{R_{2}}…②
① = ②
\frac{E_{0}}{R_{1} + R_{2}} = \frac{E_{1}}{R_{2}}
E_{0}\cdot R_{2} = E_{1}\cdot R_{1} + E_{1}\cdot R_{2}
R_{2}(E_{0} - E_{1}) = E_{1}\cdot R_{1}
R_{2} = \frac{E_{1}\cdot R_{1}}{E_{0}-E_{1}}

 組み付けるとこうなる。

IMG_3187

 動かすとこんな感じ。

 スケッチはこんな感じ。

//
//  wPotentio2tlc5940.ino
//    ポテンショメータとTLC5940でLチカ
//    27.08.02(日)0900~
//    佐藤俊夫
//
#include "Tlc5940.h"
//
const float
  R1 = 10000.0,     //  ポテンショメータ前の抵抗10kΩ, 
  E0 = 5.0,         //  電源電圧5V, 
  MAXVR = 50000.0;  //  ポテンショメータの最大抵抗
const unsigned int   VR1 = 4, VR2 = 5;  //  ポテンショメータはアナログピンのA4・A5
const unsigned int MAX_LED = 15;  //  LEDは0~15の16個
//
void setup()
{
  Tlc.init();
  pinMode(VR1, INPUT);
  pinMode(VR2, INPUT);
}

void loop()
{
  float vr1 = 0.0, vr2 = 0.0, e11 = 0.0, e21 = 0.0;
  static unsigned int topLed = 0, tailLen = 10;
  e11 = analogRead(VR1) * (5.0 / 1024);
  e21 = analogRead(VR2) * (5.0 / 1024);
  vr1 = (e11 * R1) / (E0 - e11);  //  明るさ
  vr2 = (e21 * R1) / (E0 - e21);  //  速さ
  if(++topLed > MAX_LED + tailLen)  topLed = 0;
  Tlc.clear();
  int bright = constrain(fmap(vr1, 0.0, MAXVR, 0, 4095), 0, 4095);
  Tlc.set(topLed, bright);
  for(int i = topLed - 1; i >= 0; i--){
    bright -= (4096 / tailLen);
    if(bright < 0) bright = 0;
    Tlc.set(i, bright);
  }
  Tlc.update();
  unsigned int delayTime = constrain(fmap(vr2, 0.0, MAXVR, 100, 10), 10, 100);
  delay(delayTime);
}
//
float fmap(float x, float in_min, float in_max, float out_min, float out_max) {
  //  もともとの「map()」がlong int型でこの用途に合わないので、float型を定義
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

空の写真リベンジ

投稿日:

 先週試したAdafruit製の「小型TTLシリアルjpegカメラ」での間欠撮影先週はどうしたわけか同じ画像ばかり撮れてしまい、失敗である。

 (チナミに、このカメラのメーカーの「Adafruit」という会社、有名なマッシモ・バンジのTEDの中で紹介されていたことに気付いた。)

https://youtube.com/watch?v=UoBUXOOdLXY%3Ft%3D5m50s

 気を取り直して、スケッチを直す。撮影時のカメラのステータスを確認し、撮れていなければ何回でも撮り続ける。

「while(撮れてない)撮る;」

……というわけだ。まあ、万が一ハードウェアエラーなどがあるとループが回り続けるので良くないが、ループが回り続けようが結局のところはモノとしては電源を入れ直す他にどうしようもないので、こんなものだろう。

 それと、撮影が終わったらカメラをそのつどリセットすることにした。また、既に存在するファイル名は避けるようにした。こういう時、Arduinoには書式文字列付きの「sprintf」がないので、少し不便だなと思う。

 夜明けから日没までの一日の空の雲を撮りたいので、天気予報を見て雨が降らぬ確信を持ってから、昨夜寝る前にベランダにカメラをセットしておいた。朝早く起きるより楽だからだ。夜のうちに1000枚くらい写真が撮れてしまうが、SDカードには余裕があるので大丈夫である。

 昼間は用事があるのでカメラの面倒は見れないが、放置しておけば淡々と写真は撮れていく。

IMG_3143

 で、撮れた写真をWindows Movie Makerに流し込むと、微速撮影動画の一丁上がりだ。

 スケッチは次のとおりである。

//
//  camera2Web.ino
//    27.07.20(月) 0850~
//    佐藤俊夫
//    Adafruit製小型TTLシリアルJPEGカメラ+ETHERNET SHIELD 2で
//    間欠撮影をし、Webでダウンロードできるようにする。
//
#include <Adafruit_VC0706.h>
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>
#include <Ethernet2.h>

#define CHIPSELECT 4

SoftwareSerial CAMCONNECTION(2, 3);
Adafruit_VC0706 CAM = Adafruit_VC0706(&CAMCONNECTION);
const unsigned long int INTERVAL = 30L * 1000L;
byte MAC[] = {  0x90, 0xA2, 0xDA, 0x0F, 0xF6, 0x74 };
IPAddress IP(192, 168, 1, 129);
EthernetServer SERVER(80);
EthernetClient CLIENT;

void setup() {
  pinMode(10, OUTPUT);
  if(!SD.begin(CHIPSELECT)) return;
  if(!CAM.begin()) return;
  CAM.setImageSize(VC0706_320x240);
  Ethernet.begin(MAC, IP);
  SERVER.begin();
  delay(1000);
}

void loop() {
  static unsigned long int prevtime = 0;
  char c;
  String rstr = "";
  //  INTERVALおきに写真を撮る
  if(millis() >= prevtime + INTERVAL){
    prevtime = millis();
    takePicture();
  }
  //  Webサーバ
  CLIENT = SERVER.available();
  if(CLIENT) {
    while(CLIENT.connected()) {
      if(CLIENT.available()) {
        c = CLIENT.read();
        rstr += c;
        if(rstr.endsWith("\r\n")){
          break;
        }
      }
    }
    if(rstr.indexOf("IMG") >= 0){
      String filename = "DCIM/";
      char cfilename[17];
      filename.concat(rstr.substring(rstr.indexOf("IMG"), rstr.indexOf("JPG") + 3));
      filename.toCharArray(cfilename, 17);
      CLIENT.println("HTTP/1.1 200 OK");
      CLIENT.println("Content-Type: image/jpg");
      CLIENT.println("Connection: close");
      CLIENT.println();
      File img = SD.open(cfilename);
      while(img.available()){
        CLIENT.write(img.read());
      }
      img.close();
    }else{
      sendform();
    }
    rstr = "";
    delay(1);
    // close the connection:
    CLIENT.stop();
  }
  delay(20);
}

void sendform(){
  //  フォームを送る。
  CLIENT.println("HTTP/1.1 200 OK");
  CLIENT.println("Content-Type: text/html");
  CLIENT.println("Connection: close");
  CLIENT.println();
  CLIENT.println("<!DOCTYPE HTML>");
  CLIENT.println("<html><head></head><body><center>");
  File dcim = SD.open("/DCIM");
  while(true) {
    File imgfile =  dcim.openNextFile();
    if(!imgfile){
      dcim.rewindDirectory();
      break;
    }
    CLIENT.write("<a href=\"");
    CLIENT.write(imgfile.name());
    CLIENT.write("\">");    
    CLIENT.write(imgfile.name());
    CLIENT.println("</a><br>");
    imgfile.close();    
  }
  dcim.close();
  CLIENT.println("</center></body></html>");
}

void takePicture(){
  static unsigned int pnum = 0;
  char filename[] = "DCIM/img0000.jpg";
  while(!CAM.takePicture());
  do{
    filename[8]  = '0' + pnum / 1000;
    filename[9]  = '0' + (pnum / 100) % 10;
    filename[10] = '0' + (pnum /  10) % 10;
    filename[11] = '0' + pnum % 10;
    pnum ++;
  }while(SD.exists(filename));
  if(pnum > 9999) pnum = 0;
  File imgFile = SD.open(filename, FILE_WRITE);
  uint16_t jpglen = CAM.frameLength();
  pinMode(8, OUTPUT);
  while (jpglen > 0) {
    uint8_t *buffer;
    uint8_t bytesToRead = min(32, jpglen);
    buffer = CAM.readPicture(bytesToRead);
    imgFile.write(buffer, bytesToRead);
    jpglen -= bytesToRead;
  }
  while(!CAM.reset());
  imgFile.close();
}




カメラで遊んでみる

投稿日:

 秋月電子のサイトを見ていると、小さなカメラがあり、Arduinoに付きそうな感じだ。

 3850円。Arduino自体が2800円かそこらなので、それに比べるとちょっと高いが、早速行って購入。

 だが、あまり情報は多くない。まず、メーカーのサイトを見ていくと、チュートリアルがあり、「とりあえずテストするには、電源をくれてやって、一番端のピンをテレビにつなぎゃあ絵が出る」みたいなザックリ感満載の解説が。それで、テレビにつなぐためのRCAジャックなども買う。

 チュートリアルはこれを読んでおけばだいたいいいようだ。

 ほどいてみるとこんな感じで、かなり小さい。

IMG_3112

 ピンのピッチが2mmで、ブレッドボードで扱いにくい。それで、普通の2.54mmのピンヘッダを出して、その根元をこんなふうにムリヤリ(笑)2mmピッチにせばめる。

IMG_3116

 こいつをカメラの基盤にえいやっ、とねじ込み、半田付けする。

IMG_3118

 なかなか小さいので、ルーペと老眼鏡を併用しつつ、ICなんか壊しちゃってもナンだから、20Wのぬるくて細い半田鏝でさっさとつける。

IMG_3117

 我ながらなかなかスピーディな仕事だなあ(笑)。

 で、メーカーのサイトには「5V」と書いてあるが、これは互換品の別の製品のためのチュートリアルのようで、買ってきたものの基盤をよく見ると「3.3V」と印刷されている。壊してはもったいないから、3.3Vで試す。3.3Vの電源代わりにArduinoの3.3Vピンを使う。

 基盤の印刷通り、3.3V、GND、それから右端のピンをRCAジャックのセンターに、RCAジャックのアースを同じくGNDに入れて、テレビの前に持っていく。

IMG_3119

 テレビにつなぐと、おお、確かに、値段なりのフザけた画質(笑)で、自分の顔が映る。

IMG_3121

 上下が逆だが、まあ、いいや。

 で、今度はArduinoで画像を撮影してみよう。

 チュートリアルにしたがってArduino用のライブラリをダウンロードし、これをArduinoのインストールフォルダの「libraries」に配置する。

 そうしておいてArduinoのIDEを起動すると、「ファイル」→「スケッチの例」の中に「Adafruit VC0706 Serial Camera Library」が現れるから、この中から「Snapshot」を選ぶ。これは静止画をjpegで撮影するスケッチのサンプルだ。

// This is a basic snapshot sketch using the VC0706 library.
// On start, the Arduino will find the camera and SD card and
// then snap a photo, saving it to the SD card.
// Public domain.

// If using an Arduino Mega (1280, 2560 or ADK) in conjunction
// with an SD card shield designed for conventional Arduinos
// (Uno, etc.), it's necessary to edit the library file:
//   libraries/SD/utility/Sd2Card.h
// Look for this line:
//   #define MEGA_SOFT_SPI 0
// change to:
//   #define MEGA_SOFT_SPI 1
// This is NOT required if using an SD card breakout interfaced
// directly to the SPI bus of the Mega (pins 50-53), or if using
// a non-Mega, Uno-style board.

#include <Adafruit_VC0706.h>
#include <SPI.h>
#include <SD.h>

// comment out this line if using Arduino V23 or earlier
#include <SoftwareSerial.h>         

// uncomment this line if using Arduino V23 or earlier
// #include <NewSoftSerial.h>       

// SD card chip select line varies among boards/shields:
// Adafruit SD shields and modules: pin 10
// Arduino Ethernet shield: pin 4
// Sparkfun SD shield: pin 8
// Arduino Mega w/hardware SPI: pin 53
// Teensy 2.0: pin 0
// Teensy++ 2.0: pin 20
#define chipSelect 10

// Pins for camera connection are configurable.
// With the Arduino Uno, etc., most pins can be used, except for
// those already in use for the SD card (10 through 13 plus
// chipSelect, if other than pin 10).
// With the Arduino Mega, the choices are a bit more involved:
// 1) You can still use SoftwareSerial and connect the camera to
//    a variety of pins...BUT the selection is limited.  The TX
//    pin from the camera (RX on the Arduino, and the first
//    argument to SoftwareSerial()) MUST be one of: 62, 63, 64,
//    65, 66, 67, 68, or 69.  If MEGA_SOFT_SPI is set (and using
//    a conventional Arduino SD shield), pins 50, 51, 52 and 53
//    are also available.  The RX pin from the camera (TX on
//    Arduino, second argument to SoftwareSerial()) can be any
//    pin, again excepting those used by the SD card.
// 2) You can use any of the additional three hardware UARTs on
//    the Mega board (labeled as RX1/TX1, RX2/TX2, RX3,TX3),
//    but must specifically use the two pins defined by that
//    UART; they are not configurable.  In this case, pass the
//    desired Serial object (rather than a SoftwareSerial
//    object) to the VC0706 constructor.

// Using SoftwareSerial (Arduino 1.0+) or NewSoftSerial (Arduino 0023 & prior):
#if ARDUINO >= 100
// On Uno: camera TX connected to pin 2, camera RX to pin 3:
SoftwareSerial cameraconnection = SoftwareSerial(2, 3);
// On Mega: camera TX connected to pin 69 (A15), camera RX to pin 3:
//SoftwareSerial cameraconnection = SoftwareSerial(69, 3);
#else
NewSoftSerial cameraconnection = NewSoftSerial(2, 3);
#endif

Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);

// Using hardware serial on Mega: camera TX conn. to RX1,
// camera RX to TX1, no SoftwareSerial object is required:
//Adafruit_VC0706 cam = Adafruit_VC0706(&Serial1);

void setup() {

  // When using hardware SPI, the SS pin MUST be set to an
  // output (even if not connected or used).  If left as a
  // floating input w/SPI on, this can cause lockuppage.
#if !defined(SOFTWARE_SPI)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  if(chipSelect != 53) pinMode(53, OUTPUT); // SS on Mega
#else
  if(chipSelect != 10) pinMode(10, OUTPUT); // SS on Uno, etc.
#endif
#endif

  Serial.begin(9600);
  Serial.println("VC0706 Camera snapshot test");
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }  
  
  // Try to locate the camera
  if (cam.begin()) {
    Serial.println("Camera Found:");
  } else {
    Serial.println("No camera found?");
    return;
  }
  // Print out the camera version information (optional)
  char *reply = cam.getVersion();
  if (reply == 0) {
    Serial.print("Failed to get version");
  } else {
    Serial.println("-----------------");
    Serial.print(reply);
    Serial.println("-----------------");
  }

  // Set the picture size - you can choose one of 640x480, 320x240 or 160x120 
  // Remember that bigger pictures take longer to transmit!
  
  cam.setImageSize(VC0706_640x480);        // biggest
  //cam.setImageSize(VC0706_320x240);        // medium
  //cam.setImageSize(VC0706_160x120);          // small

  // You can read the size back from the camera (optional, but maybe useful?)
  uint8_t imgsize = cam.getImageSize();
  Serial.print("Image size: ");
  if (imgsize == VC0706_640x480) Serial.println("640x480");
  if (imgsize == VC0706_320x240) Serial.println("320x240");
  if (imgsize == VC0706_160x120) Serial.println("160x120");

  Serial.println("Snap in 3 secs...");
  delay(3000);

  if (! cam.takePicture()) 
    Serial.println("Failed to snap!");
  else 
    Serial.println("Picture taken!");
  
  // Create an image with the name IMAGExx.JPG
  char filename[13];
  strcpy(filename, "IMAGE00.JPG");
  for (int i = 0; i < 100; i++) {
    filename[5] = '0' + i/10;
    filename[6] = '0' + i%10;
    // create if does not exist, do not open existing, write, sync after write
    if (! SD.exists(filename)) {
      break;
    }
  }
  
  // Open the file for writing
  File imgFile = SD.open(filename, FILE_WRITE);

  // Get the size of the image (frame) taken  
  uint16_t jpglen = cam.frameLength();
  Serial.print("Storing ");
  Serial.print(jpglen, DEC);
  Serial.print(" byte image.");

  int32_t time = millis();
  pinMode(8, OUTPUT);
  // Read all the data up to # bytes!
  byte wCount = 0; // For counting # of writes
  while (jpglen > 0) {
    // read 32 bytes at a time;
    uint8_t *buffer;
    uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
    buffer = cam.readPicture(bytesToRead);
    imgFile.write(buffer, bytesToRead);
    if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appear locked up
      Serial.print('.');
      wCount = 0;
    }
    //Serial.print("Read ");  Serial.print(bytesToRead, DEC); Serial.println(" bytes");
    jpglen -= bytesToRead;
  }
  imgFile.close();

  time = millis() - time;
  Serial.println("done!");
  Serial.print(time); Serial.println(" ms elapsed");
}

void loop() {
}

 で、これはSDカードに書き込むようになっている。

 私の手持ちの、ArduinoにSDカードをつなぐ手段は、先日から愛用中の「ETHERNET SHIELD 2」に搭載されているSDカードスロットだけだから、とりあえずこれを使う。

 手持ちのSDカードをETHERNET SHIELD 2に挿し、Adafruitのサイトのチュートリアルを参考に回路をブレッドボードに組む。

IMG_3122

 注意する点は2つだ。

  1.  サンプルスケッチをよく読むと、普通のSDカードは10番ピンを使うが、ETHERNET SHIELD 2を使う場合は4番ピンにつながる。なので、サンプルスケッチの中の「#define chipSelect 10」というところを「#define chipSelect 4」に書き換えなければならない。
  2.  メーカーサイトのチュートリアルでは、カメラに添付の10kΩの抵抗をTXの次に直列に二つ入れて、1本目と2本目の間からTXをとり、それをアースしているが、どうもこれだとうまく行かなかった。多分、このチュートリアルは給電が5Vだからだと思う。そこで、アースはそのままに、1本目の手前でTXをとるとうまくいった。
    IMG_3127

 そうやってArduinoをスタートさせると、写真が1枚だけ撮れる。

 下は、そうやって撮った私の顔である。

IMAGE02

 ……むっちゃむさくるしいなあw。