[[FrontPage]] #contents 2013/05/26からのアクセス回数 &counter; 8章は、EthernetとBluetooth等の接続を扱っていますが、ここではEthernetの接続だけを試してみました。 *** Ethernetライブラリのカスタマイズ [#sf8d22e0] もっとも簡単にArduinoをイーサネットに接続するなら、イーサネットシールドを購入するのが良いのですが、 他のCPUボードでもイーサネットに接続テストをしてみたいので、ブレッドボードに差して使える [[WIZ820io>http://www.switch-science.com/catalog/875/]] を購入しました。 イーサネットシールドでは、W5100というICを使っているのですが、WIZ820ioはW5200を使っているので、 若干の変更が必要です。 Mac OSXの場合で説明します。 + Arduinoアプリケーションを右クリックして、「パッケージの内容を表示」を選択し、 + Contents/Resources/Java/libraries/Ethernetをコピーし、 + ユーザのホームページ/Documents/Arduinoにlibraries以下にペーストします + WIZnetの [[WIZ820io>http://www.wiznettechnology.com/Sub_Modules/en/product/product_detail.asp?Refid=491&page=1&cate1=&cate2=&cate3=&pid=1161&cType=2]] から、Library for Arduino + Ethernet(IDE ver1.0)をダウンロードします + ZIPファイルを解凍し、library_Arduino_v1_0ディレクトリのw5100.cpp, w5100.hを3)でコピーしたEthernet/utilityのw5100.cpp, w5100.hと置き換えます カスタマイズしたEthernetライブラリを使用する場合には、スケッチ→ライブラリを使用→ユーザ提供のEthernetを選択します。 *** 配線 [#y5144579] WIZ820ioは、3.3Vで動くのですが、WIZ820ioの最大消費電流120mAには足りないので、5Vから3.3Vを作ります。 (([[WIZ820ioをつかってみました>http://trac.switch-science.com/wiki/WIZ820ioTest ]]参照)) 5Vから3.3Vへの変換は、秋月の [[TA48M033F>http://akizukidenshi.com/catalog/g/gI-00538/]] を使いました。 ArduinoとWIZ820ioの接続は、以下の様にします。 - D10 → nSS - D11 → MOSI - D12 ← MISO - D13 → SCLK - RESET → nRESET - GND ⇔ GND &ref(WIZ820_pin.png); 作成したブレッドボードは以下のようになります。 &ref(Bread_WIZ820io.png); *** スケッチと動作確認 [#xccc6369] スケッチは、EthernetのDhcpAdressPrinterの例題を修正して使用しました。 残念ながら、この例題はATmega32U4ボードでは動きませんでした。 この例題は特にサーバのプログラムを作らなくても動作を確認ができるので接続試験に向いています。 #pre{{ #include <Dhcp.h> #include <Dns.h> #include <Ethernet.h> #include <EthernetClient.h> #include <EthernetServer.h> #include <EthernetUdp.h> #include <util.h> #include <SPI.h> // Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; // Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): EthernetClient client; void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); // this check is only needed on the Leonardo: while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } Serial.println("Start Test"); // start the Ethernet connection: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); // no point in carrying on, so do nothing forevermore: for(;;) ; } // print your local IP address: Serial.print("My IP address: "); for (byte thisByte = 0; thisByte < 4; thisByte++) { // print the value of each byte of the IP address: Serial.print(Ethernet.localIP()[thisByte], DEC); Serial.print("."); } Serial.println(); Serial.println("End Test"); } void loop() { } }} 実行するとシリアルモニタに以下の様に取得したアドレスが表示されます。 #pre{{ Start Test My IP address: 192.168.1.109. End Test }} ** Webサーバへのアクセス [#qc94c7fa] 次にWebサーバへのアクセスをテストします。 Arduinoの例題にあるWebClientは、googleのサーバにアクセスに行くように作られているのですが、 現在はGoogleサーバが接続を制限しており、そのままでは動きません。 そこで、私の借りている「さくらVPSサーバ」上のSageサーバにアクセスして動作を確認しました。 - SageサーバのIPアドレスは、49.212.164.205 - ポート番号は、8000 です。 スケッチは、以下の様に修正しました。 #pre{{ #include <Dhcp.h> #include <Dns.h> #include <Ethernet.h> #include <EthernetClient.h> #include <EthernetServer.h> #include <EthernetUdp.h> #include <util.h> #include <SPI.h> // Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; IPAddress server(49,212,164,205); // Sage Server // Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): EthernetClient client; void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } // start the Ethernet connection: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); // no point in carrying on, so do nothing forevermore: for(;;) ; } // give the Ethernet shield a second to initialize: delay(1000); Serial.println("connecting..."); // if you get a connection, report back via serial: if (client.connect(server, 8000)) { Serial.println("connected"); // Make a HTTP request: // client.println("GET /search?q=arduino HTTP/1.0"); client.println("GET / HTTP/1.0"); client.println(); } else { // kf you didn't get a connection to the server: Serial.println("connection failed"); } } void loop() { // if there are incoming bytes available // from the server, read them and print them: if (client.available()) { char c = client.read(); Serial.print(c); } // if the server's disconnected, stop the client: if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); // do nothing forevermore: for(;;) ; } } }} 実行するとシリアルモニタに以下の様にSageサーバからのレスポンスが送られてきます。 #pre{{ connecting... connected HTTP/1.1 200 OK Content-Length: 3626 Set-Cookie: cookie_test_8000=cookie_test Accept-Ranges: bytes Server: Twisted/9.0.0 TwistedWeb/[twisted.web2, version 8.1.0] Date: Sun, 26 May 2013 01:46:27 GMT Content-Type: text/html; charset=utf-8 Connection: close <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Sign in -- Sage</title> <link type="text/css" rel="stylesheet" href="/css/main.css" /> <script type="text/javascript" src="/javascript/jquery/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="/javascript/sage/ws_list.js"></script> 以下省略 }} ArduinoのEthernetClientを使うと非常に簡単にWebサーバに接続できることが確認できました。 ** コメント [#lc885b54] #vote(おもしろかった[2],そうでもない[0],わかりずらい[0]) #vote(おもしろかった[3],そうでもない[0],わかりずらい[0]) 皆様のご意見、ご希望をお待ちしております。 #comment_kcaptcha