ArduinoにEthernetシールドをつけて動かしてみる。

f:id:garyo:20130518235612j:plain

 

以下のソースでgoogleに接続できた。

>>

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>

byte mac = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
byte ip
= {192,168,11,10};
byte server = {74,125,235,216};

EthernetClient client;

void setup(){
  Ethernet.begin(mac,ip);
  Serial.begin(9600);
 
  delay(1000);
 
  Serial.println("Connecting...");
 
  if(client.connect(server,80)){
    Serial.println("connected");
    client.println("Get /serch?q=arduino HTTP/1.0");
    client.println();
  }else{
    Serial.println("connection failed");
  }
}

void loop(){
  if(client.available()){
    char c = client.read();
    Serial.print(c);
  }
  if(!client.connected()){
    Serial.println();
    Serial.println("disconnected");
    client.stop();
    for(;;)
      ;
  }

<<

 

以下シリアルの出力結果

f:id:garyo:20130518235653j:plain

>>

Connecting...
connected
HTTP/1.0 405 Method Not Allowed
Content-Type: text/html; charset=UTF-8
Content-Length: 963
Date: Sat, 18 May 2013 14:47:36 GMT
Server: GFE/2.0

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 405 (Method Not Allowed)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}
  </style>
  <a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a>
  <p><b>405.</b> <ins>Thatâs an error.</ins>
  <p>The request method <code>Get</code> is inappropriate for the URL <code>/serch</code>.  <ins>Thatâs all we know.</ins>

disconnected
<<

 

 

 

サーバー機能も動かしてみた。問題なく動いた。

>>

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetServer.h>


byte mac = {0xDE,0xAD,0xBE,0xEF,0xED};
byte ip = {192,168,11,10};
byte gateway
= {192,168,1,1};
byte subnet[] = {255,255,255,0};

EthernetServer server = EthernetServer(23);

void setup(){
  Ethernet.begin(mac,ip,gateway,subnet);
  server.begin();
}

void loop(){
  EthernetClient client = server.available();
  if(client == true){
    server.write(toupper(client.read()));
  }
}

<<