Pages: [1]   Go Down
Author Topic: two pages from ethernet shield?  (Read 390 times)
0 Members and 1 Guest are viewing this topic.
South UK
Offline Offline
God Member
*****
Karma: 5
Posts: 583
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all. I have been playing with a basic page coming from my arduino which I have been using to switch things on and off. I would ideally like to have at least one other page for some settings, so I would like to be able to click a link from the main page that the arduino serves up to then display a second one?

I am working with a slightly expanded version of this code

Code:
//simple button GET server code to control servo and arduino pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//for use with W5100 based ethernet shields
//Powering a servo from the arduino usually DOES NOT WORK.
//note that the below bug fix may be required
//http://www.instructables.com/id/Control-an-LED-over-the-internet-using-the-Arduino/step2/Connected/
// http://code.google.com/p/arduino/issues/detail?id=605
//http://www.instructables.com/id/Arduino-Control-via-a-Web-Service-with-Teleduino/?ALLSTEPS
 
#include <SPI.h>
#include <Ethernet.h>
 
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 177 }; // ip in lan
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
 
String readString;
 
//////////////////////
 
void setup(){
 
  pinMode(6, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  //the pin for the servo co
  //enable serial data print
  Serial.begin(9600);
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}
 
void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
 
        //read char by char HTTP request
        if (readString.length() < 100) {
 
          //store characters to string
          readString += c;
          //Serial.print(c);
        }
 
        //if HTTP request has ended
        if (c == '\n') {
 
          ///////////////
          Serial.println(readString); //print to serial monitor for debuging
 
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
 
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
          client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
          client.println("<link rel='stylesheet' type='text/css' href='http://chriscosma.co.cc/a.css' />");
          client.println("<TITLE>Home Automation</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Home Automation</H1>");
          client.println("<hr />");
          client.println("<br />");
         
          client.println("<a href=\"/?lighton\"\">Turn On Light</a>");
          client.println("<a href=\"/?lightoff\"\">Turn Off Light</a><br />");       
 
          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();
 
          ///////////////////// control arduino pin
          if(readString.indexOf("?lighton") >0)//checks for on
          {
            digitalWrite(6, HIGH);    // set pin 4 high
            Serial.println("Led On");
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
          }
          else{
          if(readString.indexOf("?lightoff") >0)//checks for off
          {
            digitalWrite(6, LOW);    // set pin 4 low
            Serial.println("Led Off");
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
          }
          }
          //clearing string for next read
          readString="";
 
        }
      }
    }
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so I would like to be able to click a link from the main page that the arduino serves up to then display a second one?
So what's the problem?

Code:
        //if HTTP request has ended
        if (c == '\n') {
Aside from the fact that you don't seem to give a rat's ass what the client requested? How do intend to know what to serve up?

Code:
          ///////////////////// control arduino pin
          if(readString.indexOf("?lighton") >0)//checks for on
          {
After serving up the page is too late to care.

Code:
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
Trying to write to the client after stopping the client is a waste of effort.
Logged

South UK
Offline Offline
God Member
*****
Karma: 5
Posts: 583
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry I'm not sure what you mean with that stuff - could you elaborate? The code is from a tutorial so I don't fully understand it.

I't just confused how I could code the html to point to a new page and the arduino to interpret the request - then serve a different page?

Thanks
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The code is from a tutorial so I don't fully understand it.
Then, I'd suggest that this is not the project for you. Understanding client/server architecture, understanding what the client requested, and knowing how to define a page to be served are all things you must understand BEFORE you begin trying to substitute an Arduino as the server.

Quote
I't just confused how I could code the html to point to a new page and the arduino to interpret the request - then serve a different page?
You should spend some time at http://www.w3schools.com/default.asp then. There is nothing different about having the Arduino server a web page with a link to another page and having any other server serve a page with a link.

Understanding what happens when you click that link will be necessary before you can serve that other page.
Logged

South UK
Offline Offline
God Member
*****
Karma: 5
Posts: 583
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand basic HTML, usually you have to put HMTL files on a webserver, then you can simply request them by pointing to them as I'm sure you are well aware.

However I'm not sure how this would work from an arduino. I guess the first page that gets served is the same as index.html or default.html - If I add a link to display in the page served by the arduino, where do I point it?

Also how do I define the page titles? From what I can see in the code that I'm using there is nothing to define that it should be titled index or default.html?

Thanks for the help
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I understand basic HTML, usually you have to put HMTL files on a webserver, then you can simply request them by pointing to them as I'm sure you are well aware.
I'm well aware of what happens when you click a hyperlink. It is you that is making assumptions, incorrectly, about the "magic" that happens.

You don't request a page by simply pointing to it. The browser has no idea what you are pointing to. You have to actually click the text rendered as a hyperlink. When you do that, a lot of things happen, some on the client end and some on the server end. In order to server the correct page, the server must actually parse the GET request. You aren't even printing what the client is asking for. How you can then presume to supply a response is really beyond me.

Quote
However I'm not sure how this would work from an arduino.
Then, you do NOT understand what the browser is doing, or what the Arduino is being asked to do. Learning what the Arduino is being asked to do is easy. Once you know that, you should be able to serve different pages, and predict what the request will be when the user interacts with the page you do serve. Once you can do that, then serving the correct page in response to the GET request is trivial.

Quote
Also how do I define the page titles?
What do you mean by "the page titles"? There is a html tag, title, but I don't think that is what you are asking about.
Logged

South UK
Offline Offline
God Member
*****
Karma: 5
Posts: 583
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How does the arduino know which html to send once it receives a request? In the example code I'm using all of the HTML gets sent to the browser. I'd like to have a second chunk of html that is sent.

Sorry I wasn't clear about page titles I mean't default pages in root directories on the web are called index or default.html, I'm just confused how one would structure code to put placed under another title, for example a page called data.html.

I'm still at a loss as to how all this could be organised into a secondary page. I will try and do some more research, thanks for the help so far
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How does the arduino know which html to send once it receives a request?
It collects the request in a string, and parses the string.

Quote
In the example code I'm using all of the HTML gets sent to the browser.
Regardless of what the browser asked for.

Quote
I'd like to have a second chunk of html that is sent.
That's easy, if you pay attention to what the client asked for.

Quote
I'm just confused how one would structure code to put placed under another title, for example a page called data.html.
You are collecting the GET request in a String (ugh!) named readString (a crappy name).

Code:
if(readString.indexOf("data.html") >= 0)
{
   // client asked for data.html "page"
}
else
{
   // client didn't ask for data.html "page"
}

Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That instructable code sure looks familiar. Anyhow, you could modify the below code by sending a second page from the code section that sends the ("HTTP/1.1 204 Zoomkat").

Code:
//zoomkat 12-8-11
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html (or use ')
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString;

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging

          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
               client.println();
               client.println(); 
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
         
          client.println("<a href=\"/?on\" target=\"inlineframe\">ON</a>");
          client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>");

          //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
          client.println("<IFRAME name=inlineframe style=\"display:none\" >");         
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: