Questions about the arduino ethernet shield

Hi,guys
I just an senior school student from china. And i am new to the arduino. i have taken an interest in using the arduino cause it is tons of fun . Anyway I am using it to do a project. My project is to build a home security system.
Now i am using the arduino ethernet shield to control the system from internet with the android mobile phone. And the software on the mobile phone is Domotichome. I can control the arduino with this software. But now i have some problems about it. I have done some research on the ethernet shield but have become really confused on how it works.

I am used the IDE 1.0.5.

Here are my questiones!

  1. I just want to know how the ethernet shield works.
  2. I had used two software:Domotichome, Arduino controller. But i was failed! I want to know the difference between them.
    3)I had succeed in controlling one LED. But i couldn't control two LED with the arduino.This is the code for one LED:
#include <SPI.h>
#include <Ethernet.h>
#define action_none -1
#define action_out_all 0
#define action_on_light 1
#define action_off_light 2
byte mac[] = {0x8C,0x21,0x0A,0x35,0xAE,0x85}; //physical mac address
byte ip[] = {192,168,1,4}; //ip
byte gateway[] = {192,168,1,1};
byte subnet[] = {255,255,255,0};
EthernetServer server = EthernetServer(80);
// arduino out
int pinOutPlight = 7;
String readString = String(30); //string for fetching data from address
String ipstr;
// incoming GET command
String r_pinOnLight = "GET /?out=" + String(pinOutPlight) +"&status=1";
String r_pinOffLight = "GET /?out=" + String(pinOutPlight) +"&status=0";
String r_out_all = "GET /?out=all";
// current action
int current_action;

void setup(){
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  delay(1000);
  pinMode(pinOutPlight, OUTPUT);
  digitalWrite(pinOutPlight, LOW);
  //enable serial datada print
  Serial.begin(9600);
  current_action = -1;
  //ipstr=String(ip[1]);
  ipstr=String(ip[0]) + "." + String(ip[1]) + "." + String(ip[2]) + "." + String(ip[3]);
}

void loop(){
  current_action = -1;
  // Create a client connection
  EthernetClient client = server.available();
 if (client) {
 while (client.connected()) {
  if (client.available()) {
   char c = client.read();
   //read char by char HTTP request
   if (readString.length() < 30)
   {
  //store characters to string
  readString = readString + c;
   }
   //output chars to serial port
   //Serial.print(c);
   //if HTTP request has ended
   if (c == '\n') {
 Serial.print(readString);
 // ****************************************************
  if(readString.startsWith(r_pinOnLight))
  {
  Serial.print("\n ON UP \n");
  current_action = action_on_light;
  }
  else if(readString.startsWith(r_pinOffLight))
  {
   Serial.print("\n OFF UP \n");
   current_action = action_off_light;
  }
  else if(readString.startsWith(r_out_all))
  {
  Serial.print("\n ALL\n");
  current_action = action_out_all;
  }
  else
  {
  Serial.print("\n None \n");
  current_action = action_none;
  }
 // ****************************************************
  // now output HTML data starting with standart header
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println();
 char buf[12];
 switch(current_action)
 {
 case action_out_all:
  client.print("{\"ip\" :");
  client.print(ipstr);
  client.print("\", \"devices\" : [{ \"type\" : \"light\", \"name\" : \"LED\", \"out\" : \"");
  client.print(pinOutPlight);
  client.print("\"}");
  client.print("]}");
   break;
 case action_on_light:
   digitalWrite(pinOutPlight, HIGH);
   client.print("{\"status\" : \"1\" , \"out\" : \"");
   client.print(pinOutPlight);
   client.print("\"}");
   break;
 case action_off_light:
   digitalWrite(pinOutPlight, LOW);
   client.print("{\"status\" : \"0\" , \"out\" : \"");
   client.print(pinOutPlight);
   client.print("\"}");
   break;
 default:
   current_action = action_none;
 }
 // ****************************************************
  //clearing string for next read
  readString="";
  //stopping client
  client.stop();
   }
 }
 }
  }
}
  1. I want to use the DS18B20 Temperature. It must used DallasTemperature.h and OneWire.h. So who has used it. I want to know how to use it!
    5)If i just want to control the LED, it shows?"An error has accoured.Check your internet connection.Is arduino alive?" So i had to create one new action about the LED.

Sorry about my English. I am not a native speaker. I am from china! If someone can or want to help me, you can email to me :907400644@qq.com or zhusj110373@hotmail.com. Thank you very much !

This is incorrect. You are not including the dns server parameter. Even if you do not dns, you must include the parameter in the function call. If you do not, you will not be able to access any devices outside your localnet.

// Change this
  Ethernet.begin(mac, ip, gateway, subnet);
// to this.
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

edit: Insure you are using the correct network settings for that interface.

sorry sir
I was failed again. Could you tell me why to change into

void setup(){
  //start Ethernet
  Ethernet.begin(mac, ip, gateway,gateway, subnet);
  delay(1000);
  pinMode(pinOutPlight, OUTPUT);
  digitalWrite(pinOutPlight, LOW);
  //enable serial datada print
  Serial.begin(9600);
  current_action = -1;
  //ipstr=String(ip[1]);
  ipstr=String(ip[0]) + "." + String(ip[1]) + "." + String(ip[2]) + "." + String(ip[3]);
}

SurferTim:
This is incorrect. You are not including the dns server parameter. Even if you do not dns, you must include the parameter in the function call. If you do not, you will not be able to access any devices outside your localnet.

// Change this

Ethernet.begin(mac, ip, gateway, subnet);
// to this.
 Ethernet.begin(mac, ip, gateway, gateway, subnet);




edit: Insure you are using the correct network settings for that interface.

sorry sir. i am failed again. could you please tell me why to change the code into
Ethernet.begin(mac, ip, gateway, gateway, subnet)

Actually, the first "gateway" is the dns server ip. If you feel more comfortable, you can use this

byte ip[] = {192,168,1,4}; //ip
byte gateway[] = {192,168,1,1};
byte subnet[] = {255,255,255,0};
byte dnServer[] = {192,168,1,1};


  Ethernet.begin(mac, ip, dnServer, gateway, subnet);

Have you tried using dhcp to get an ip?

  Serial.print("starting w5100...");
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else Serial.println(Ethernet.localIP());

Some routers will not recognize a static ip that is in the dhcp server range. Check your router maunal or settings to see what range of ips it issues with dhcp, and avoid that with your static ip assignment.

SurferTim:
Actually, the first "gateway" is the dns server ip. If you feel more comfortable, you can use this

byte ip[] = {192,168,1,4}; //ip

byte gateway[] = {192,168,1,1};
byte subnet[] = {255,255,255,0};
byte dnServer[] = {192,168,1,1};

Ethernet.begin(mac, ip, dnServer, gateway, subnet);




Have you tried using dhcp to get an ip?


Serial.print("starting w5100...");
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else Serial.println(Ethernet.localIP());




Some routers will not recognize a static ip that is in the dhcp server range. Check your router maunal or settings to see what range of ips it issues with dhcp, and avoid that with your static ip assignment.

thank you,i will try this code. but i am using chinese android. i want to ask if there is something wrong with the software? Because the chinese android doesn‘t contain the GoogleServices Framework. should i use the google nexus or another android mobile phone?And i am the new of the arduino world. Coule you tell me where i should add the code

Serial.print("starting w5100...");
if(!Ethernet.begin(mac)) Serial.println("failed");
else Serial.println(Ethernet.localIP());

I greatly appreciate your help.
From Shanghai

Very simple client test code. If you get a response from the server, then DNS and DHCP are most likely working.

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

zoomkat:
Very simple client test code. If you get a response from the server, then DNS and DHCP are most likely working.

//zoomkat 9-22-12

//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){

if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

Serial.begin(9600);
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  } 
}

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection 
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

that is want i want. You know the education in china is so hard , i just a few time to continue my project . I will try this code as soon as possible , thank you sir.

zoomkat:
Very simple client test code. If you get a response from the server, then DNS and DHCP are most likely working.

//zoomkat 9-22-12

//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){

if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

Serial.begin(9600);
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  } 
}

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection 
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

My wireless router's WAN was DHCP, IP :10.106.83.22,subnet mask:255.255.255.0,gateway:10.106.83.1,DNS:10.104.0.1
LAN's IP :192.168.1.1 subnet mask:255.255.255.0.
So could you tell me how to set up my ip dns gateway in the code. I should use the WAN or LAN 's ip??

If you want a static ip for the Arduino, use something like this. Insure the ip you choose is not in the router's localnet dhcp server ip pool.

byte mac[] = {0xde,0xad,0xbe,0xef,0xfe,0xed};

IPAddress ip(192,168,1,2);
IPAddress gateway(192,168,1,1);
IPAddress dnServer(10,104,0,1);
IPAddress netmask(255,255,255,0);


Ethernet.begin(mac, ip, dnServer, gateway, netmask);