THE WEBPAGE FINALLY LOADS!

After a lot of internet browsing through online forums, I was finally able to access the Arduino and load a very simply webpage!

The Arduino forum bellow offered a lot of helpful information on configuring an Arduino over a network. Eventually, this will be more important if I want people to be able to access the Arduino from beyond the local (LAN) dorm network.

http://forum.arduino.cc/index.php?PHPSESSID=uo1p8pdm6omi29fjd0fs9khqa5&topic=112955.15

Basically, to make the Arduino visible over the network I had to give it a unique identity. I did this by inputing 4 different variables:

The mac is the ardunio’s unique hardware identification number. It was found on the back of my ethernet shield as 90-A2-DA-2A-FB. the ‘ox’ before each string seems to be an important part of the sketch, just put the number after each ‘ox’.

byte mac[] = { 0x90, 0xA2, 0xDA, ox0F, 0x2A, 0xFB };

Next I had to give the Arduino a unique IP address. Contrary to what many forums and guides claimed, you CANNOT simply put in whatever number you want into the 4th section of the IP. This is done through an automated process called DHCP or Dynamic Host Configuration Protocol. This requires running the Arduino IDE DhcpAddressPrinter sketch. Running that sketch will print out the  IP address assigned to the Arduino by the network in the serial monitor. That IP can then be copy-pasted into a browser to later access the Arduino server. If you are in Kerry Hall at TNS and my Arduino is connected via ethernet, you might be able to see my webpage if you paste that ip into your browser!

IPAddress ip(10, 1, 217, 139);

Next I found the gateway. The gateway is the router’s address. I found this by going into system preferences>network>advanced>TCP/IP where it can be found under ‘Router’.

IPAddress gateway(10,1,217,1);

Next, I input the subnet. According to various forums, the subnet stays the same and rarely deviates from what is bellow.

IPAddress subnet(255,255,255,0);

Finally, I defined which port to forward to and from. I chose 80 because it is the most common for HTTP requests. I’m not sure why, but various internet forums have suggested this.

EthernetServer server(80);

 

 

 

/*
A combination of sketches.
Sketches include examples from
W.A. Smith, http://startingelectronics.com
Tom Igoe
Arduino ChatServer example sketch by David A. Mellis
————————————————————–*/

#include <SPI.h>
#include <Ethernet.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x2A, 0xFB };
IPAddress ip(10, 1, 217, 139);// IP address, may need to change depending on network. This is found by running the DhcpAddressPrinter example sketch by Tom Igoe.
IPAddress gateway(10,1,217,1);// Added gateway, found in system preferences network panel under router
IPAddress subnet(255,255,255,0); //Added subnet as well – this is usually the same regardless.
EthernetServer server(80); // create a server at port 80, or port 23(default) Port 80 works with HTTP requests
int led = 4;
//I took this setup code from the DhcpAddressPrinter Arduino example sketch. Use serial monitor to recieve the generated DCHP address.
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
Serial.println(“Trying to get an IP address using DHCP”);
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// initialize the ethernet device not using DHCP:
Ethernet.begin(mac, ip, gateway, subnet);
}
// print your local IP address:
Serial.print(“My IP address: “);
ip = Ethernet.localIP();
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(ip[thisByte], DEC);
Serial.print(“.”);
}
Serial.println();
// start listening for clients
server.begin();

}
/*void setup()
{
pinMode(led,OUTPUT);
Ethernet.begin(mac, ip, gateway, subnet); // initialize Ethernet device
server.begin(); // start to listen for clients
}*/
//This code is taken from a startingelectronics.com tutorial on hosting an html webpage with Arduino.
void loop()
{

digitalWrite(led,HIGH);
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
client.println(“<!DOCTYPE html>”);
client.println(“<html>”);
client.println(“<head>”);
client.println(“<title>Arduino Web Page Test</title>”);
client.println(“</head>”);
client.println(“<body>”);
client.println(“<h1>Hello from Arduino!</h1>”);
client.println(“<p>A web page from the Arduino server</p>”);
client.println(“</body>”);
client.println(“</html>”);
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)
}

Leave a Reply