2013/05/26からのアクセス回数 4362
8章は、EthernetとBluetooth等の接続を扱っていますが、ここではEthernetの接続だけを試してみました。
もっとも簡単にArduinoをイーサネットに接続するなら、イーサネットシールドを購入するのが良いのですが、 他のCPUボードでもイーサネットに接続テストをしてみたいので、ブレッドボードに指して使える WIZ820io を購入しました。
イーサネットシールドでは、W5100というICを使っているのですが、WIZ820ioはW5200を使っているので、 若干の変更が必要です。
Mac OSXの場合で説明します。
カスタマイズしたEthernetライブラリを使用する場合には、スケッチ→ライブラリを使用→ユーザ提供のEthernetを選択します。
WIZ820ioは、3.3Vで動くのですが、WIZ820ioの最大消費電流120mAには足りないので、5Vから3.3Vを作ります。 *1
5Vから3.3Vへの変換は、秋月の TA48M033F を使いました。
ArduinoとWIZ820ioの接続は、以下の様にします。
作成したブレッドボードは以下のようになります。
スケッチは、EthernetのDhcpAdressPrinterの例題を修正して使用しました。 残念ながら、この例題はATmega32U4ボードでは動きませんでした。
この例題は特にサーバのプログラムを作らなくても動作を確認ができるので接続試験に向いています。
#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() {
}
実行するとシリアルモニタに以下の様に取得したアドレスが表示されます。
Start Test My IP address: 192.168.1.109. End Test
次にWebサーバへのアクセスをテストします。
Arduinoの例題にあるWebClientは、googleのサーバにアクセスに行くように作られているのですが、 現在はGoogleサーバが接続を制限しており、そのままでは動きません。
そこで、私の借りている「さくらVPSサーバ」上のSageサーバにアクセスして動作を確認しました。
です。
スケッチは、以下の様に修正しました。
#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サーバからのレスポンスが送られてきます。
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サーバに接続できることが確認できました。
皆様のご意見、ご希望をお待ちしております。