Go Down

Topic: Ethernet Shield based server which selects automatically an IP to host the page (Read 122 times) previous topic - next topic

Laur91

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


2.  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(;;)
      ;
  }
  // 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
 

 

 

 

SurferTim

Why don't you modify your router dhcp server to not issue a some IPs in the localnet range? Then you can use one of those.

Laur91


At my router it's no point in modifying my settings , because i usually don't have that much devices connected to it in order to challenge me in finding a free IP.

This arduino + ethernet shield  is part of a bigger project , which i intend to present also elsewhere  and i don't have the guarantee of having access all the time to all the available routers settings .  I'm trying to find a solution in order to avoid just that

Thank you for your time ,

SurferTim

Quote
This arduino + ethernet shield  is part of a bigger project , which i intend to present also elsewhere  and i don't have the guarantee of having access all the time to all the available routers settings.
Then you have no choice. You must use dhcp to obtain network settings.

Laur91

Yes i know that , but my problem is how can i use in the second approach both the DHCP server connection and the SD card

SurferTim

Yes i know that , but my problem is how can i use in the second approach both the DHCP server connection and the SD card
Exactly the same as you would with a static IP. Are you having problems with that?

edit: This will cause you problems. You are trying to start the ethernet service without disabling the SD SPI. That will almost guarantee a dhcp start fail.
Code: [Select]
void setup() {
  Ethernet.begin(mac, ip);  // initialize Ethernet device
  server.begin();           // start to listen for clients

Laur91

Initializing the  DHCP server first , followed by the SD card initialization generates errors to the SD card , i get an IP but the card doesn't initialize.

void setup () {
 Ethernet.begin(mac, ip);  // initialize Ethernet device
  server.begin();

....
  // 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
  }

I found somewhere to set HIGH pin 10 to disable the ethernet controller and pin 4 for the SD card , and set them LOW whenever they have to be renabled and to keep only one enabled at a time ,  but  it still doesn't work.   Maybe i didn't find a successful combination.
What do you suggest me to try ? 

SurferTim

This should do it. Works for me.
Code: [Select]
void setup () {

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

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

  // rest of your setup


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy