ホーム » スタッフ » 斉藤徹 » Computer » Arduinoイーサネットシールドを使ってみた

2010年4月
 123
45678910
11121314151617
18192021222324
252627282930  

検索・リンク

Arduinoイーサネットシールドを使ってみた

創造工学演習でハードウェア系を作るネタの導入として、 Ethernet シールド付き Arduino を動かしてみた。単なるメモなので…

// 雛形のスケッチは、
// ..PATH../Arduino.app/Contents/Resources/Java
//    /hardware/libraries/Ethernet/examples/WebServer/WebServer.pde
#include <ethernet.h>
byte mac[]	= { 0x02 , 0x11 , 0x22 , 0x33 , 0x44 , 0x55 } ;
byte ip[]	= { 192 , 168 , 2 , 88 } ;  //
byte gateway[]	= { 192 , 168 , 2 ,  1 } ;  //
byte subnet[]	= { 255 , 255 , 255 , 0 } ; // Class-C
Server server = Server( 80 ) ;
void setup() {
Ethernet.begin( mac , ip , gateway , subnet ) ;
server.begin() ;
Serial.begin( 9600 ) ; // 動作確認用に受信データを表示
}
void loop() {
Client client = server.available() ;
if ( client ) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
while( client.connected() ) {
if ( client.available() ) {
char c = client.read();
Serial.write( c ) ; // 動作確認用に、受信データを表示
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if ( c == '\n' && current_line_is_blank ) {
// send a standard http response header
client.println( "HTTP/1.1 200 OK" ) ;
client.println( "Content-Type: text/html" ) ;
client.println();
// output the value of each analog input pin
for (int i = 0; i < 6; i++) {
client.print("analog input ");
client.print(i);
client.print(" is ");
client.print(analogRead(i));
client.println("<br />");
}
break;
}
if (c == '\n') {
// we're starting a new line
current_line_is_blank = true;
} else if (c != '\r') {
// we've gotten a character on the current line
current_line_is_blank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}

上記プログラムを検証&書き込みの後、ブラウザで http://192.168.2.88/ にてアクセスする。すると、シリアルモニタには下記の出力が得られた。 後半は、ブラウザがfavicon.icoを取得しようとしたアクセスが行われている。 しかし上記のプログラムは、何をGETしようとしたのか判別せずに、リクエスト末尾の 『空行』を見つけたら、アナログ値を出力しようとするだけ。 このため、Reload する度に、2回アナログ値サンプリングが行われる。

GET / HTTP/1.1
Host: 192.168.2.88
Connection: keep-alive
User-Agent: Mozilla/5.0 ...Safari/532.5
Cache-Control: max-age=0
Accept: application/xml,application/xhtml+xml,text/html;...
Accept-Encoding: gzip,deflate,sdch
Accept-Language: ja,en-US;q=0.8,en;q=0.6
Accept-Charset: UTF-8,*;q=0.5
GET /favicon.ico HTTP/1.1
Host: 192.168.2.88
Connection: keep-alive
User-Agent: Mozilla/5.0 ...Safari/532.5
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: ja,en-US;q=0.8,en;q=0.6
Accept-Charset: UTF-8,*;q=0.5

ブラウザで、http://192.168.2.88/ で表示された結果は、下記の通り。 ちなみにアナログポートは、実験だし開放のまんまで、ゴミデータ。

analog input 0 is 393
analog input 1 is 331
analog input 2 is 282
analog input 3 is 286
analog input 4 is 269
analog input 5 is 284

システム

最新の投稿(電子情報)

アーカイブ

カテゴリー