'Arduino send GET request to server on local network
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
