I am following this tutorial perfectly. The database side is perfect where when i go to this url( localhost/test/test.php?count=120 ) the value count in MySQL DB is added with another record of 120. The issue i am facing is in the Arduino side. I am not able to connect my Arduino with Ethernet shield to my PC via LAN. I tried the web server example and it is fully working... I am stuck with Webclient example. This is my code
/*
Web client
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(169,254,174,37);
EthernetClient client;
int potentiometer = 0;
void setup() {
potentiometer=50;
// start the serial library:
Serial.begin(9600);
// 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(;;)
;
}
// 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, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.print("GET /test/test.php?count=");
client.print(potentiometer);
client.println(" HTTP/1.0");
client.println();
}
else {
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();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
for(;;)
;
}
}
This is the output i get
Failed to configure Ethernet using DHCP
connecting...
connection failed
disconnecting.
What am i doing wrong here! Even i am not able to check Arduino Webclient example. The server is my PC here and the Arduino is my Client. How to configure the sketch? Please help me friends!!!
If the Arduino is not getting any network settings from the router, then it will never work. Is the ethernet shield's CAT5 cable connected to a router that has a dhcp server?
SurferTim:
If the Arduino is not getting any network settings from the router, then it will never work. Is the ethernet shield's CAT5 cable connected to a router that has a dhcp server?
Hello Sir! My Arduino is not connected to router. It is directly connected to my PC via RJ45 LAN cable. I used the IP address of my PC here: IPAddress server(169,254,174,37); Actually, is it correct?
Then your PC doesn't have a dhcp server running. You must either start a dhcp service on the ethernet port of the PC or use a static IP assignment on the Arduino.
SurferTim:
Then your PC doesn't have a dhcp server running. You must either start a dhcp service on the ethernet port of the PC or use a static IP assignment on the Arduino.
I have no idea on how to configure that Static IP address. Can you guide me on what should i do now exactly?
SurferTim:
I don't know how much you know about networking, so here is the reference on the begin function with a minimum static IP example. Ethernet - Arduino Reference
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
Does this Ethernet.begin(mac) == 0 have to do anything? My Ethernet shield didn't come with a MAC address. Any suggestions? I am frustrated with this error!
Failed to configure Ethernet using DHCP
connecting...
connection failed
PaulS:
Then quit trying to use DHCP. Get the Arduino working with a static address first.
Then connect it to the router. Quit trying to solve 47 problems at once. Connect the Arduino, with a static IP address directly to the router.
Can't the Arduino directly connect using RJ45 cable to PC via LAN without router??? Because, my Arduino is directly connected to my PC.... I am not using any router right now!
Failed to configure Ethernet using DHCP
connecting ...
connection failed
disconnecting.
How to configure IP for Arduino ethernet shield? Curently i am using IPv4 address which is already DHCP enabled! But it says failed to configure DHCP... I'm out of ideas... Tried all sorts of corrections including turning off firewall.... nothing successful.
You can't use dhcp to get network settings if there is no dhcp server on the localnet/PC. You must install and configure a dhcp server on the localnet.
edit: If you are using Windows, do a Google search for "internet connection sharing". Or search for "dhcp server for ***" where *** is your particular OS on the PC.
Ok, i followed the steps mentioned in google pages. It is already in the default settings and DHCP is enabled. Obtain IP and DNS address automatically is set.
Finally solved the problem. The issue is that we need to assign the static IP if the DHCP fails... I also disabled the firewall. The following line did the trick
Ethernet.begin(mac, ip);
Webclient example is not illustrated in any of Youtube tutorials. For those who are stuck make sure you assign 2 IP's: One for server and another for Ethernet shield itself. Don't forget MAC... In windows go to Network and sharing center->IPv4 address(Note: this as your server address if you are configuring your PC as a Server). DNS, subnet are used when connecting via router. The rest are the same.
I am ever thankful for your support and suggestions! Thanks for everything :-*