Code: Select all
/*--------------------------------------------------------------
Program: eth_websrv_SD
Description: Arduino web server that serves up a basic web
page. The web page is stored on the SD card.
Hardware: Arduino Uno and official Arduino Ethernet
shield. Should work with other Arduinos and
compatible Ethernet shields.
2Gb micro SD card formatted FAT16
Software: Developed using Arduino 1.0.3 software
Should be compatible with Arduino 1.0 +
SD card contains web page called index.htm
References: - WebServer example by David A. Mellis and
modified by Tom Igoe
- SD card examples by David A. Mellis and
Tom Igoe
- Ethernet library documentation:
http://arduino.cc/en/Reference/Ethernet
- SD Card library documentation:
http://arduino.cc/en/Reference/SD
Date: 10 January 2013
Author: W.A. Smith, http://startingelectronics.com
--------------------------------------------------------------*/
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip( 169,x,x,x); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile;
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for debugging
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
webFile = SD.open("index.htm"); // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
The configuration from above is as follows - Ethermega plugged into PC, PC wifi card picks up signal from a wifi hotspot and I can communicate with the outside world. Enter the IP of the mega into the address bar and I am connected to the script on then SD card in the browser.
I have also used a phone connected to the same hotspot as the PC the mega is plugged into and attempted to connect to the IP address of the mega unsuccessfully.
However when I attempt to connect to the mega's IP address from another PC using a different IP address ie. a dongle plugged into a d-link router on the other side of the room I am unable to connect?
I have gone into the admin section for the d-link router and entered the IP address for the mega on port 80 in the use as server section, then going back to the PC with the Hotspot ( different IP ) from the dongle router combo and attempted to connect to the mega the connection times out or I get the message server may be too busy or may have moved permanently to a new address.
I emailed a fellow Arduino enthusiast the IP address he was unable to connect, so he pinged the IP of the mega while it was "online" and had no contact/could not connect.
I have also spoken to my ISP one tech told me I was using DHCPIP addresses and and needed a static IP another tech told me I had a static IP and needed a DHCPIP addresses. I spent 8 hours ( Yes 8 hours many calls I'm not kidding! ) on the phone just trying to get an straight answer they said they would ring back ha.
So my problem is this, I don't use an ADSL cable connection to the house ( it's very old ) and I travel often so WiFi is my only option. I had a preconceived idea that the mega would act as the "Server" the "router WiFi dongle combo" would act like a "tower" and I would be able to pick up the signal and connect that way.
Perhaps I have some misconceptions about how these things work ( Arduino/TCP-IP ), however I am trying to learn and would like to get this working. I believe in the learning by doing philosophy, seems all I'm doing is making mistakes, so I'm learning from this all is not lost

Any advice would be greatly appreciated
Thanks in advance,
vero