Pages: 1 2 [3]   Go Down
Author Topic: HTML Server Web Pin Control  (Read 3500 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 125
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem was what both of you pointed out. Firefox was requesting a favicon.ico directly after I made my on or off request so there was a request where the program saw 'on' from favicON.ico and thought it needed to turn the light on right after I clicked on or off which is why it was always going back on.

Here is a generalized version of the code I modified from the program that zoomkat so generously shared with me:
Code:
//zoomkat 4-1-12 (modified by durkinnj 12-20-12)
//simple pin control through html
//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 ' instead of "
//address will look like http://192.168.0.80 when submitted
//for use with W5100 based ethernet shields
//modified to control an LED (on pin 5) but can be applied to a number of
//applications which displays updated status of LED etc.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 80 }; // ip in lan // this may be changed if necessary
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router //not needed
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask //not needed
EthernetServer server(80); //server port
boolean led = false; //led status
String readString;

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

void setup(){

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

  //enable serial data print
  Serial.begin(9600);
  Serial.println("Server/ pin 5 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') {

          ///////////////////// control arduino pin
          if(readString.indexOf("ledon") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            led = true;
            Serial.println("Led On");
          }
          if(readString.indexOf("ledoff") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            led = false;
            Serial.println("Led Off");
          }
          
          ///////////////
          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 HTTP-EQUIV='refresh' CONTENT='5'>");
          client.println("<TITLE>CARduino</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<h1 style='text-align:center;font-size:70px;'>LED Control Center<br /></h1>");
          if(led == false){
          client.println("<div style='padding-left:25px;font-size:50px;display:inline;'>LED Status: OFF</div><a href='/?ledon'><div style='font-size:60px;display:inline;padding-left:100px;'><button type='submit'><div style='font-size:60px;'>Turn On</div></button></div></a>"); }
          else {
          client.println("<div style='padding-left:25px;font-size:50px;display:inline;'>LED Status: ON&nbsp;</div><a href='/?ledoff'><div style='font-size:60px;display:inline;padding-left:100px;'><button type='submit'><div style='font-size:60px;'>Turn Off</div></button></div></a>"); }

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();
          
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}
« Last Edit: December 20, 2012, 07:05:30 pm by durkinnj » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 125
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright I'm stuck again. For some reason when I run the code no html page loads and the serial monitor displays nothing. When I remove the following lines of code, it works fine. What's going on?

Code:
else if(wipers == 1){
            client.println("<div style='padding-left:25px;font-size:50px;float:left;'>Wiper Status: INT</div>");
            client.println("<form><div style='font-size:60px;float:right;><input type='radio'> OFF<br><a href='/?wipersintermittnet'><input type='radio' checked></a> INT<br><a href='/?wiperslow'><input type='radio'></a> SLOW<br><a href='/?wipershigh'><input type='radio'></a> FAST<br></div></form>");
          }

Code:
//zoomkat 4-1-12
//simple button GET for servo and 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, or use ' instead of "
//address will look like http://192.168.0.80 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, 0, 80 }; // ip in lan
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
boolean engine = false; //engine status
int wipers = 0; //windshield wipers status 0-off 1-intermittent 2-slow 3-high
String readString;
unsigned long WiperResetStart = 0;
unsigned long WiperResetStop = 0;
int S = 6; //Wiper Fast pin
int B = 7; //Wiper Slow pin
int BA = 8; //Wiper Reset pin
unsigned long IntermittentRunStart = 0;
unsigned long IntermittentRunStop = 0;


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

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
    pinMode(6, OUTPUT); //pin selected to control
      pinMode(7, OUTPUT); //pin selected to control
        pinMode(8, OUTPUT); //pin selected to control
          pinMode(9, OUTPUT); //pin selected to control
            pinMode(10, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip);
  server.begin();

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

void loop(){
  // Check if timer has expired for wiper position to reset
  if(BA == HIGH){
    WiperResetStop=millis();
    if(WiperResetStop > WiperResetStart + 5000){
      if(wipers == 1){
        digitalWrite(S, HIGH); //Turn on high setting for 1 second
        IntermittentRunStart = millis();
      }
      else{
        wipers = 0;
        Serial.println("Wipers Off");
      }
    }
  }
  // Check if timer has expired for intermittent run
  if(wipers == 1){
    IntermittentRunStop = millis();
    if(IntermittentRunStop > IntermittentRunStart + 1000){
      digitalWrite(S, LOW);//Turn off fast
      delay(1);//Let it disconnect
      digitalWrite(BA, HIGH);//Reset Wiper position
        WiperResetStart=millis();
    }
  }
  // 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') {

          ///////////////////// Engine Control
          if(readString.indexOf("?engineon") >0)
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            engine = true;
            Serial.println("Starting Engine...");
          }
          if(readString.indexOf("?engineoff") >0)
          {
            digitalWrite(5, LOW);    // set pin 5 low
            engine = false;
            Serial.println("Turning Engine Off...");
          }
          ///////////////////// Wiper Control
          if(readString.indexOf("?wipersoff") >0)
          {
            digitalWrite(B, LOW);//Turn off slow
            digitalWrite(S, LOW);//Turn off fast
            delay(1);//Let it disconnect
            digitalWrite(BA, HIGH);//Reset Wiper position
              WiperResetStart=millis();
            Serial.println("Resetting Wiper Position...");
          }
          if(readString.indexOf("?wipersintermittent") >0)
          {
            digitalWrite(B, LOW);//Turn off slow
            digitalWrite(S, LOW);//Turn off fast
            delay(1);//Let it disconnect
            digitalWrite(BA, HIGH);//Reset Wiper position
              WiperResetStart=millis();
            wipers = 1;
            Serial.println("Wiper Status: Intermittent");
          }
          if(readString.indexOf("?wiperslow") >0)
          {
            digitalWrite(S, LOW);//Turn off fast
            digitalWrite(BA, LOW);//Turn off reset incase it is in the process of resetting
            delay(1);//Let it disconnect
            digitalWrite(B, HIGH);//Turn on slow
            wipers = 2;
            Serial.println("Wiper Status: SLOW");
          }
          if(readString.indexOf("?wipershigh") >0)
          {
            digitalWrite(B, LOW);//Turn off slow
            digitalWrite(BA, LOW);//Turn off reset incase it is in the process of resetting
            delay(1);//Let it disconnect
            digitalWrite(S, HIGH);//Turn on fast
            wipers = 3;
            Serial.println("Wiper Status: FAST");
          }

          ///////////////
          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 HTTP-EQUIV='refresh' CONTENT='5'>");
          client.println("<TITLE>CARduino</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<h1 style='text-align:center;font-size:70px;'>Welcome to Project CARduino!<br /></h1>");
          if(engine == false){
            client.println("<div style='padding-left:25px;font-size:50px;display:inline;'>Engine Status: OFF</div><a href='/?engineon'><div style='font-size:60px;display:inline;padding-left:100px;'><button type='submit'><div style='font-size:60px;'>Start Engine</div></button></div></a><br><br><br><br>");
          }
          if(engine == true){
            client.println("<div style='padding-left:25px;font-size:50px;display:inline;'>Engine Status: ON&nbsp;</div><a href='/?engineoff'><div style='font-size:60px;display:inline;padding-left:100px;'><button type='submit'><div style='font-size:60px;'>Engine Off</div></button></div></a><br><br><br><br>");
          }
          if(wipers == 0){
            client.println("<div style='padding-left:25px;font-size:50px;float:left;'>Wiper Status: OFF</div>");
            client.println("<form><div style='font-size:60px;float:right;><input type='radio' checked> OFF<br><a href='/?wipersintermittnet'><input type='radio'></a> INT<br><a href='/?wiperslow'><input type='radio'></a> SLOW<br><a href='/?wipershigh'><input type='radio'></a> FAST<br></div></form>");
          }
          else if(wipers == 1){
            client.println("<div style='padding-left:25px;font-size:50px;float:left;'>Wiper Status: INT</div>");
            client.println("<form><div style='font-size:60px;float:right;><input type='radio'> OFF<br><a href='/?wipersintermittnet'><input type='radio' checked></a> INT<br><a href='/?wiperslow'><input type='radio'></a> SLOW<br><a href='/?wipershigh'><input type='radio'></a> FAST<br></div></form>");
          }
          //else if(wipers == 2){
            //client.println("<div style='padding-left:25px;font-size:50px;display:inline;float:left;'>Wiper Status: SLW</div><form><div style='padding-left:40px;float:right;'><br><a href='/?wipersoff'><input type='radio'></a> OFF<br><a href='/?wipersintermittnet'><input type='radio'></a> INTERMITTENT<br><input type='radio' checked> ON (SLOW)<br><a href='/?wipershigh'><input type='radio'></a> ON (FAST)<br></div></form>");
          //}
          //else{
            //client.println("<div style='padding-left:25px;font-size:50px;display:inline;float:left;'>Wiper Status: FST</div><form><div style='padding-left:40px;float:right;'><br><a href='/?wipersoff'><input type='radio'></a> OFF<br><a href='/?wipersintermittnet'><input type='radio'></a> INTERMITTENT<br><a href='/?wiperslow'><input type='radio'></a> ON (SLOW)<br><input type='radio' checked> ON (FAST)<br></div></form>");
          //}
          client.println("</BODY>");
          client.println("</HTML>");

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

          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Logged

0
Offline Offline
Tesla Member
***
Karma: 143
Posts: 9601
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have most likely hit a memory limit and need to use the F() memory saver like below.

Code:
            client.println(F("HTTP/1.1 200 OK")); //send new page on browser request
            client.println(F("Content-Type: text/html"));
            client.println();

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

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Those are some hefty strings - is it possible you're running out of RAM? You could put them in PROGMEM to reduce the RAM use considerably.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Full Member
***
Karma: 0
Posts: 125
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there some way to use my sd card to store some of the information on my ethernet shield? Or should I just use the F()?

Also, do you guys know anything about refreshing the page? What happens is it will send a request like http://192.168.1.1/?turnledon and then it will keep refreshing that request which basically keeps turning it on when it's already on. How do I, after making the request, automatically get back to the regular http://192.168.1.1/ page and refresh that?

This is what I'm using now:

Code:
<META HTTP-EQUIV='refresh' CONTENT='5'>
« Last Edit: December 21, 2012, 02:15:06 pm by durkinnj » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One way is to respond with a redirect request which directs the client to load the 'normal' page (i.e. the one without any form parameters). So the scheme would be:

Client submits the URL for the base page.
Server sends back the base page with the form in it, representing the current status.
User clicks on the control to submit the form.
Client submits the URL request with form parameters indicating the command to be carried out.
Server actions the command and returns a redirect response back to the base page.
Client submits the URL for the base page.
Server sends back the base page with the form in it, representing the current status.

Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Full Member
***
Karma: 0
Posts: 125
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Got it.

Code:
client.println("<meta HTTP-EQUIV='REFRESH' content='5;url=http://192.168.0.80/'");

Easy as pie. Thank you very very much.
« Last Edit: December 21, 2012, 02:58:58 pm by durkinnj » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes the fifth line. I don't know how to do that. I've been looking it up (still am). I don't know how to request a redirect from the client.

You Google-fu is weak. smiley

This article describes how this approach works:

http://en.wikipedia.org/wiki/Post/Redirect/Get

This article shows what your web server needs to output to implement it:

http://en.wikipedia.org/wiki/HTTP_303
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Full Member
***
Karma: 0
Posts: 125
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there a way to write over a line on the sd card?

So you do:

myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

but how do you write to a specific line?

My goal is to change lines of text in a file with the program.
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the lines were fixed-length you could use lseek() to set the file pointer to a computed length and write a fixed-length record there.

Real program lines haven't been fixed-length since the Hollerith days, so the practical answer is "not really", since you don't know how long the line you are overwriting is.

-br

Logged

Pages: 1 2 [3]   Go Up
Jump to: