Issue
I have a local network composed of 2 devices:
- Raspberry Pi running Raspbian with an Apache server. (IP 169.254.90.117)
- Arduino MEGA with an Arduino Ethernet Shield. (IP 169.254.90.110)
The 2 devices are directly connected via Ethernet cable.
What I want to do is pretty straight forward: be able to send a GET request from the Arduino to the server hosted on the Raspberry Pi in order to modify records on a database.
Unfortunately every time I try Arduino fails to connect to the server and the serial console shows this:
Initialising...
Trying to connect to server
connection failed
I know that the Raspberry server is working because I tried connecting the Raspberry to a PC from which I was able to navigate with a browser to the website hosted on the server without any issues.
I also tried pinging the Arduino from the Raspberry and the ping is successful.
After navigating through a few forums I tried changing the variable type on the Arduino where I saved the server's IP from a normal char
variable to a byte
and a IPAddress
but with no success.
Here's the Arduino code I've been using:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 169, 254, 90, 110 };
char server[] = "169.254.90.117";
//byte server[] = { 169, 254, 90, 117 };
//IPAddress server(169,254,90,117);
String HTTP_METHOD = "GET";
String PATH_NAME = "/setNumber.php";
String queryString = "?test=26";
EthernetClient client;
void setup() {
Serial.begin(9600);
Serial.println("Initialising...");
Ethernet.begin(mac, ip);
// connect to web server on port 80:
Serial.println("Trying to connect to server");
if(client.connect(server, 80)>0) {
// if connected:
Serial.println("Connected to server");
// make a HTTP request:
// send HTTP header
client.println(HTTP_METHOD + " " + PATH_NAME + queryString + " HTTP/1.1");
Serial.println("Sending GET: " + HTTP_METHOD + " " + PATH_NAME + queryString + " HTTP/1.1");
client.println("Host: " + String(server));
client.println("Connection: close");
client.println(); // end HTTP header
while(client.connected()) {
if(client.available()){
// read an incoming byte from the server and print it to serial monitor:
char c = client.read();
Serial.print(c);
}
}
// the server's disconnected, stop the client:
client.stop();
Serial.println();
Serial.println("disconnected");
} else {// if not connected:
Serial.println("connection failed");
}
}
void loop() {
}
EDIT:
I've updated the char
variable to a char[]
array but the result is still the same.
To verify that the Arduino Ethernet Shield was working properly I connected it to the internet and succesfully sent a request to www.google.com.
Solution
This is the workaround I ended up using:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Mac address of the Arduino board
EthernetClient client;
void setup(){
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to obtaining an IP address using DHCP");
while(true);
}
//Sending request to server
char ServerAddress[] = "domain.name.com";
if(client.connect(ServerAddress, 80)) {
client.println("GET /file.php?data=10 HTTP/1.1");
client.println("Host: " + String(ServerAddress));
client.println("Connection: close");
client.println(); // end HTTP header
client.stop();
} else {// if not connected:
Serial.println("Server connection failed");
}
}
void loop() {
}
Basically I initialised the Ethernet Shield by only declaring the MAC address.
Then I connected the board to a router, so that during the initalisation the router's DHCP would automatically assing an IP address to the Ethernet Shield. In this way everything starts correctly and I'm able to send GET requests to remote servers.
Answered By - BlueBird931 Answer Checked By - Candace Johnson (WPSolving Volunteer)