Hi, before I get to the problem I would like to provide some insight on what I am trying to do.
Basically I have one of my PCs set up as a server with XAMPP. This PC contains a .WAV file which I would like to download into an SD card on the Arduino Ethernet Shield.
Here is the code which I have attempted to use for this.
// Gets a file from the Internet and saves it to the SD card
// Removes the incoming HTTP header, saves the file only
//
// Author: W.A. Smith Date: 10 June 2015
// http://startingelectronics.org/software/arduino/save-web-file-to-SD-card/
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address of the ethernet shield
IPAddress server(10, 19, 3, 205); // ip address of my PC
IPAddress ip(10, 19, 3, 171); // static ip address that I assigned to the arduino ethernet shield
EthernetClient client;
boolean currentLineIsBlank = true;
File theFile;
void setup() {
//*************************Initialize SD Card**********************************
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
Serial.begin(115200);
Serial.print(F("Initializing SD card..."));
if (!SD.begin(4)) {
Serial.println(F(" initialization failed!"));
return;
}
Serial.println(F(" initialization done."));
if (SD.exists("recording1.wav")) {
SD.remove("recording1.wav");
}
//************************************************************************
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println(F("Failed to configure Ethernet using DHCP")); // no point in carrying on, so do nothing forevermore:
// try to configure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// 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.println("GET /recording1.wav"); // change resource to get here
client.println("Host: 10.19.3.205"); // Set the IP address to the server's (my computer's) IP
client.println("Connection: close");
client.println();
}
else {
// didn't get a connection to the server:
Serial.println("connection failed");
}
// open the file for writing
Serial.println("Creating file.");
theFile = SD.open("recording1.wav", FILE_WRITE); // change file name to write to here
if (!theFile) {
Serial.println("Could not create file");
while (1);
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
if (c == '\n' && currentLineIsBlank) {
// end of HTTP header, now save requested file
while (client.connected()) {
// stay in this loop until the file has been received
if (client.available()) {
c = client.read(); // get file byte
theFile.print(c); // save file byte
}
}
}
// detect the end of the incoming HTTP header
if (c == '\n') {
// starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// got a character on the current line
currentLineIsBlank = false;
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
theFile.close();
Serial.println("Finished writing to file");
// do nothing forevermore:
while (true);
}
}
Somehow I seem to always get stuck at trying to establish a connection with the server.
The serial monitor just shows "connecting..." and nothing happens after that.
Any ideas as to what is going wrong?