Ethernet Shield based server which selects automatically an

Hello ,

I have an arduino mega with Ethernet Shield and also with a microSD card inserted

I’ve been trying to configure the arduino as a server which has the ability of selecting automatically a IP to host the webpage , because when faced with the situation of trying to connect to a wireless router which assigns IP addresses to a lot of devices is very time consuming to find manually a free IP address .

So far i tried to approaches but i failed : 1. I start with an address , but if i don’t receive any client data within 5 seconds i proceed to incrementing the last byte of the IP address.

But this approach doesn’t do anything but incrementing all the time the IP address without finding a free one to host the webpage

#include <SPI.h>

#include <Ethernet.h>

#include <SD.h>

// initialize the library with the numbers of the interface pins

// MAC address from Ethernet shield sticker under board

byte mac = {

0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192, 168, 0, 14); // server IP address

EthernetServer server(80); // create a server at port 80 coresponds to HTTP

File webFile; // file stored on the uSD card

int i=1;

void setup() {

Ethernet.begin(mac, ip); // initialize Ethernet device

server.begin(); // start to listen for clients

Wire.begin(); // join i2c bus

Serial1.begin(9600);

Serial.begin(9600);

Serial.print("server is at ");

Serial.println(Ethernet.localIP());

//

// initialize SD card

Serial.println(“Initializing SD card…”);

if (!SD.begin(4)) { // pin 4 stands for CS ( Chip Select ) of SPI

Serial.println(“ERROR - SD card initialization failed!”);

return; // init failed

}

Serial.println(“SUCCESS - SD card initialized.”);

// check for google2 file

if (!SD.exists(“google2.htm”)) {

Serial.println(“ERROR - Can’t find google2.htm file!”);

return; // can’t find google2 file

}

Serial.println(“SUCCESS - Found google2.htm file.”);

}

void loop() {

EthernetClient client = server.available(); // try to get client

while ( !client )

{ if ( client || millis()-timer>5000)

break;

}

if (client) { // got client?

Serial.println(" Gotclient ");

} // end if (client)

else if ( !client) {

i++;

IPAddress ipv( 192,168,0,i);

Ethernet.begin(mac,ipv);

server.begin();

delay(5);

timer=millis();

Serial.print("server is at ");

Serial.println(Ethernet.localIP());

} // loop

  1. In the second approach i tried to use the DHCP to get a free address and then create the server with that address . This approach works fine for this task but i cannot use anymore my SD card - it’s not being initialized anymore ( a get an IP address and by page displays everything except the content of the SD card ).

I read that because SD and ethernet controller share one SPI bus they cannot be used at the same time , but i found out that this restriction doesn’t apply if i don’t use the DHCP and select manually the IP address

#include <Wire.h> // for IIC communication

#include <SPI.h>

#include <Ethernet.h>

#include <SD.h>

// initialize the library with the numbers of the interface pins

// MAC address from Ethernet shield sticker under board

byte mac = {

0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

EthernetServer server(80); // create a server at port 80 coresponds to HTTP

File webFile; // file stored on the uSD card

int i=1;

char ipAddr={’ ‘,’ ‘,’ ‘,’ ‘,’ '};

void setup() {

// 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(;:wink:

;

}

// 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:

ipAddr[thisByte] = Ethernet.localIP()[thisByte];

Serial.print(Ethernet.localIP()[thisByte], DEC);

Serial.print(“.”);

}

IPAddress ip (ipAddr[0],ipAddr[1],ipAddr[2],ipAddr[3]);

Ethernet.begin(mac, ip); // initialize Ethernet device

server.begin(); // start to listen for clients

Serial1.begin(9600);

Serial.print("server is at ");

Serial.println(Ethernet.localIP());

Serial.println(“Initializing SD card…”);

if (!SD.begin(4)) { // pin 4 stands for CS ( Chip Select ) of SPI

Serial.println(“ERROR - SD card initialization failed!”);

return; // init failed

}

Serial.println(“SUCCESS - SD card initialized.”);

// check for google2 file

if (!SD.exists(“google2.htm”)) {

Serial.println(“ERROR - Can’t find google2.htm file!”);

return; // can’t find google2 file

}

Serial.println(“SUCCESS - Found google2.htm file.”);

}

//

}

// initialize SD card

void loop() {

EthernetClient client = server.available(); // try to get client

if (client) { // got client?

Serial.println(" Gotclient ");

} // end if (client)

} // loop

Does anyone have a clue how to solve this problem ??

Thank you

Just use DHCP and then put a reservation in on the DHCP server (ie: the router.)

I think the current Ethernet library included with the Arduino supports this - Just leave the IP out of the .begin call.

Ethernet.begin(mac);