Hello!! Everyone!!
Most of the peoples had faced this problem, but i haven't found any solution for it.
I am trying to connect the Ethernet Shield connected with Arduino Mega to my PC, through Ethernet.
The code is as follow:-
/*
DHCP-based IP printer
This sketch uses the DHCP extensions to the Ethernet library
to get an IP address via DHCP and print the address obtained.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 12 April 2011
modified 9 Apr 2012
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
#define ETH_CS 10
#define INT_SD_CS 4
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {0x90,0xA2,0xDA,0x0E,0xCD,0x1D};
void setup()
{
SelectEthernetModule();
// Open serial communications and wait for port to open:
Serial.begin(115200);
// this check is only needed on the Leonardo:
while (!Serial)
{
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("DHCP Started..");
// 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();
}
void loop()
{
}
void SelectEthernetModule(void)
{
// Select the Ethernet Module and De-select the Internal SD Card Module.
digitalWrite(ETH_CS,LOW); // Select the Ethernet Module.
digitalWrite(INT_SD_CS,HIGH); // De-Select the internal SD Card
}
void DeSelectEthernetModule(void)
{
// DeSelect both, Internal SD and Ethernet Module
digitalWrite(ETH_CS,HIGH);
digitalWrite(ETH_CS,HIGH);
}
I don't know why i am always getting this error.
Failed to configure Ethernet using DHCP
Apart from that when i tried to connect with static mode, it works properly.
In Static mode, i assigned IP 192.168.1.17 to Ethernet Shield and set IP address of my PC as 192.168.1.10 acting as server, subnet mask 255.255.255.0 and Gateway as 192.168.1.1
The code for static connection is as follow:-
#include <SPI.h>
#include <Ethernet.h>
#define ETH_CS 10
#define INT_SD_CS 4
IPAddress ip(192,168,1,17);
// Enter the IP address of the server you're connecting to:
IPAddress server(192,168,1,10);
EthernetClient client;
void setup()
{
SelectEthernetModule();
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial)
{
; // wait for serial port to connect. Needed for Leonardo only
}
// 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, 5000))
{
Serial.println("connected");
}
else
{
// if 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();
client.write(c);
Serial.print(c);
}
// as long as there are bytes in the serial queue,
// read them and send them out the socket if it's open:
while (Serial.available() > 0) {
char inChar = Serial.read();
if (client.connected()) {
client.print(inChar);
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing:
while(true);
}
}
void SelectEthernetModule(void)
{
// Select the Ethernet Module and De-select the Internal SD Card Module.
digitalWrite(ETH_CS,LOW); // Select the Ethernet Module.
digitalWrite(INT_SD_CS,HIGH); // De-Select the internal SD Card
}
void DeSelectEthernetModule(void)
{
// DeSelect both, Internal SD and Ethernet Module
digitalWrite(ETH_CS,HIGH);
digitalWrite(ETH_CS,HIGH);
}
Please help me, i want both DHCP and Static modes in my project.
