Pages: [1]   Go Down
Author Topic: How i upload web page from sd card of ethernet shield w5100 for control leds?  (Read 2150 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 175
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

i want upload web page from sd card (ethernet w5100) for control leds...any idea for code with sd libraries?I doing without sd but no work for big web page...with many functions...

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


#define maxLength 40

String inString = String(maxLength);       // allocate a new String


const byte IR_PIN4=4;//Ir led pin

const byte LEDpin=8;//Indicator

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {192,168,1,15};
const byte Port = 80;

Server server(Port);

int counter=0;
char c;
word xClicksProcessed=0;
char codeOK='0';//start Code is blank....

void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();

  Serial.begin(9600);//Restore, if you want to monitor program's
       //operation via serial monitor

 
  pinMode(IR_PIN4,OUTPUT);
  digitalWrite(IR_PIN4,LOW);
  pinMode(LEDpin,OUTPUT);
  LEDOff();
 
}

void loop()
{//1
  Client client = server.available();
    
     if (client) {//2
    boolean currentLineIsBlank = true;//3
    counter=0;
    inString="";//format inString
     while (client.connected()) {//4
      if (client.available()) {//45
         c = client.read();//6
        
          
         if ((c!='\n') && (c!='\r')){
         if (inString.length() < maxLength) {
              inString+=c;
         }        
         }
         // Serial.print(c);
         // Serial.print(inString);
         if (c == '\n' && currentLineIsBlank ) {
           delay(300);
           xClicksProcessed++;
          
          
           client.println("HTTP/1.1 200 OK");//8
            client.println("Content-Type: text/html");//9
            client.println();//10

            

            client.println("<html>");
            client.println("<head>");
            client.print("<title>Web Remote Control</title>");
            client.println("</head>");
            client.println("</head>");
            client.println();
            client.println("<body>");

            client.println("<center>");
            client.print("<h2>Web Remote Control A/C with Arduino and W5100 ver.22/12/2011");
          
            client.println("</h2>");

            

            client.println("<p>----");

            client.print("<br>Analog input A3 (Temperature) reads: <b>");//Temperature sensor Lm35
            client.print(analogRead(3)*0.488);
            client.println("</b>");
                  
              client.println("<form METHOD=get action=\"\">");
              client.println("<input type=password name=\"code\" size=\"4\" maxlength=\"4\">");
              client.println("<input type=submit name=\"Submit\" value=\"Code\">");
              client.println("</form>");
              client.println();
              
              
              client.println("<form METHOD=get action=\"\">");//to action isws na min xreiazete
              client.println("<input type=hidden name=\"cmd\" value=\"AC_ON\">");
              client.println("<input type=submit value=\"AC On\">");//Ac on
              client.println("</form>");
              client.println();

              client.println("<form METHOD=get action=\"\">");
              client.println("<input type=hidden name=\"cmd\" value=\"AC_OFF\">");
              client.println("<input type=submit value=\"AC Off\">");//Ac off
              client.println("</form>");
              client.println();
              
              client.println("<form METHOD=get action=\"\">");
              client.println("<select name=\"cmd\">");
              client.println("<option value=\"COOL\" selected=\"selected\" >Cool</option>");
              client.println("<option value=\"HEAT\">Heat</option>");
              client.println("<option value=\"DRY\">Dry</option>");      
              client.println("</select>");
              client.println(" ");
              client.println("<input type=\"submit\" name=\"Submit\" value=\"Mode\">");
              client.println("</form>");
              client.println();
            
              
              client.println("<form METHOD=get action=\"\">");
              client.println("<select name=\"cmd\">");
              client.println("<option value=\"temp30\" selected=\"selected\" >Temp 30</option>");
              //client.println("<option value=\"temp29\">Temp 29</option>");
              //client.println("<option value=\"temp28\">Temp 28</option>");
            
             // client.println("<option value=\"temp16\">Temp 16</option>");
              client.println("</select>");
              client.println(" ");
              client.println("<input type=\"submit\" name=\"Submit\" value=\"Temperature\">");
              client.println("</form>");
              client.println();
              
              client.println("<form METHOD=get action=\"\">");
              client.println("<select name=\"cmd\">");
              client.println("<option value=\"Fan_Low\" selected=\"selected\" >Fan low</option>");
              client.println("<option value=\"Fan_Medium\">Fan medium</option>");
              client.println("<option value=\"Fan_High\">Fan high</option>");      
              client.println("</select>");
              client.println(" ");
              client.println("<input type=\"submit\" name=\"Submit\" value=\"Fan speed\">");
              client.println("</form>");
              client.println();
            
    

              client.print("<p>Clicks since server booted: ");
              client.print(xClicksProcessed);
              client.println("</p>");

          client.println("<br>");

          
          client.println("<center>");


          break;//15
         }
        
         if (c == '\n') {//17
          currentLineIsBlank = true;//18
          
          Serial.print(inString);
          
          if (inString.startsWith("GET")>0) {//19
            
            if  (inString.startsWith("GET /?code=xxxx")>0) {//Set the code
                     codeOK='1';
                     Serial.println("codeOK=");
                     Serial.print(codeOK);
            }
            
            if ((codeOK=='1') && (counter <1)){counter++;
              if (inString.startsWith("GET /?cmd=AC_ON")>0 ){
                   LEDOn();
                   SendACCodeOn();
                  
                   //Serial.println("codeOK=");
                   //Serial.print(codeOK);
                   //Serial.print(inString);
                  
              }
              if (inString.startsWith("GET /?cmd=AC_OFF")>0){
                   LEDOff();
                   SendACCodeOff();
              }
              
              if (inString.startsWith("cmd=COOL",6)>0){
              
                 SendACCode_modeCool();    
                  
              }
              if (inString.startsWith("cmd=HEAT",6)>0){
                  
                 SendACCode_modeHeat();
              }
              if (inString.startsWith("cmd=DRY",6)>0){
                  
                 SendACCode_modeDry();
              }
               if (inString.startsWith("cmd=Fan_Low",6)>0){
                  
                 SendACCode_modeFanspeedLow();
              }
              if (inString.startsWith("cmd=Fan_Medium",6)>0){
                  
                 SendACCode_modeFanspeedMedium();
              }
              if (inString.startsWith("cmd=Fan_High",6)>0){
                  
                 SendACCode_modeFanspeedHigh();
              }
              if (inString.startsWith("cmd=temp30",6)>0){
                  
                 SendACCode_Temp_30();
              }
              
              
              //if (inString.indexOf("cmd=temp25")>0){
                  
                 //SendACCode_Temp_25();
             // }
              //if (inString.indexOf("cmd=temp24")>0){
                  
                 //SendACCode_Temp_24();
             // }
              if (inString.startsWith("cmd=temp23",6)>0){
                  
                 SendACCode_Temp_23();
              }
            
              if (inString.indexOf("cmd=temp16")>0){
                  
                 //SendACCode_Temp_16();
              } */
              
            }//codeok
          }//get
         }//current line
                    
               else if (c != '\r') {        //20
          currentLineIsBlank = false;//21
               }
         }//client avalaible
      }
      delay(1);//25
    client.stop();//26
  }//27

}//28

void LEDOn(){
digitalWrite(LEDpin,HIGH);
}

void LEDOff(){
digitalWrite(LEDpin,LOW);
}




Logged

Web / sms / bluetooth / arduino / android remote control. Home Automation projects.
http://www.power7.net

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

There are examples provided with the SD library that show how to read from and write to the SD card. Learn how to use them independent of the Ethernet shield, and then combine the two to send data from the SD card.
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 175
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I try this but no work...only display page...the led no work...file not found...

Code:
//This is the basic SDFat Webserver sketch, without any modifications
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <Ethernet.h>

byte mac[] = {
  0x90,0xA2,0xDA,0x00,0x26,0xEB};
byte ip[] = {
  192,168,1,15};
char rootFileName[] = "index.htm";  //root file name is what the homepage will be.
Server server(80);

const byte IR_PIN4=2;//Ir led pin

const byte LEDpin=3;//Indicator


Sd2Card card;  //SD Stuff
SdVolume volume;
SdFile root;
SdFile file;

#define error(s) error_P(PSTR(s))  //SD card errors stored in Program memory
void error_P(const char* str) {  //Error function
  PgmPrint("error: ");
  SerialPrintln_P(str);
  if (card.errorCode()) {
    PgmPrint("SD error: ");
    Serial.print(card.errorCode(), HEX);
    Serial.print(',');
    Serial.println(card.errorData(), HEX);
  }
  while(1);
}

void setup() {  //setup stuff
 
  pinMode(IR_PIN4,OUTPUT);
  digitalWrite(IR_PIN4,LOW);
  pinMode(LEDpin,OUTPUT);
  LEDOff();
 
  Serial.begin(9600);
  PgmPrint("Free RAM: ");
  Serial.println(FreeRam());
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  if (!card.init(SPI_FULL_SPEED, 4)) error("card.init failed!");  //If you are having errors when reading from the SD card, change FULL to HALF
  if (!volume.init(&card)) error("vol.init failed!");
  PgmPrint("Volume is FAT");
  Serial.println(volume.fatType(),DEC);
  Serial.println();
  if (!root.openRoot(&volume)) error("openRoot failed");
  PgmPrintln("Files found in root:");
  root.ls(LS_DATE | LS_SIZE);
  Serial.println();
  PgmPrintln("Files found in all dirs:");
  root.ls(LS_R);
  Serial.println();
  PgmPrintln("Done");
  Ethernet.begin(mac, ip);
  server.begin();
}
#define BUFSIZ 100  //defines the buffer size.  100 gives plenty of room.  reduce size if more ram is needed.

void loop()
{
  char clientline[BUFSIZ];
  char *filename;
  int index = 0;
  int image = 0;
  Client client = server.available();
  if (client) {
    boolean current_line_is_blank = true;
    index = 0;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (c != '\n' && c != '\r') {
          clientline[index] = c;
          index++;
          if (index >= BUFSIZ)
            index = BUFSIZ -1;
          continue;
        }
        clientline[index] = 0;
        filename = 0;
        Serial.println(clientline);
        if (strstr(clientline, "GET / ") != 0) {  //If you are going to the homepage, the filename is set to the rootFileName
          filename = rootFileName;
        }
        if (strstr(clientline, "GET /") != 0) {
          if (!filename) filename = clientline + 5;  //gets rid of the GET / in the filename
          (strstr(clientline, " HTTP"))[0] = 0;  //gets rid of everything from HTTP to the end.
          Serial.println(filename);
          if (! file.open(&root, filename, O_READ)) {  //if the file doesn't exist a 404 is sent
            client.println("HTTP/1.1 404 Not Found");
            client.println("Content-Type: text/html");
            client.println();
            client.println("<h2>File Not Found!</h2>");
            break;
          }
          Serial.println("Opened!");
          client.println("HTTP/1.1 200 OK");
          if (strstr(filename, ".htm") != 0)  //Sets content type.
            client.println("Content-Type: text/html");
          else if (strstr(filename, ".css") != 0)
            client.println("Content-Type: text/css");
          else if (strstr(filename, ".png") != 0)
            client.println("Content-Type: image/png");
          else if (strstr(filename, ".jpg") != 0)
            client.println("Content-Type: image/jpeg");
          else if (strstr(filename, ".gif") != 0)
            client.println("Content-Type: image/gif");
          else if (strstr(filename, ".3gp") != 0)
            client.println("Content-Type: video/mpeg");
          else if (strstr(filename, ".pdf") != 0)
            client.println("Content-Type: application/pdf");
          else if (strstr(filename, ".js") != 0)
            client.println("Content-Type: application/x-javascript");
          else if (strstr(filename, ".xml") != 0)
            client.println("Content-Type: application/xml");
          else
            client.println("Content-Type: text");
          client.println();
         
          int16_t c;
          while ((c = file.read()) >= 0) {
              client.write((char)c);  //sends file to website client
          }
          file.close();
           
         
        }
        /*else {
          client.println("HTTP/1.1 404 Not Found");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<h2>File Not Found!</h2>");
        }*/
        break;
      }
    if(strstr(clientline,"GET /?cmd=AC_ON")!=0) {  //look for the command to turn the led on
              LEDOn();  //turn the led on
              }
    else if(strstr(clientline,"GET /?cmd=AC_OFF")!=0) {  //look for command to turn led off. Note: If led is on, it will stay on until command is given to turn it off, even if multiple computers are on the site
              LEDOff();  //turn led off
              }
  }
   
    delay(1);
    client.stop();
  }
}

void LEDOn(){
digitalWrite(LEDpin,HIGH);
}

void LEDOff(){
digitalWrite(LEDpin,LOW);
}
Logged

Web / sms / bluetooth / arduino / android remote control. Home Automation projects.
http://www.power7.net

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

Can you lean  bit to the left? You're blocking the serial monitor.

A little more. Still can't see.

A little more. Still can't see.

A little more. Still can't see. Careful you don't fall out of the chair.

Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 175
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

ok work web page ...i send data from sd card but no display my analog input...i use
Code:
while (myFile.available()) {
    client.write(myFile.read());
    }
    // close the file:
    myFile.close();
client.print display only mumbers...
Logged

Web / sms / bluetooth / arduino / android remote control. Home Automation projects.
http://www.power7.net

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

Quote
i send data from sd card but no display my analog input...i use
Do you want to serve up a static page or a dynamic page?

If you want to serve a static page, you are going about it right, but you have unrealistic expectations about the resulting page.

If you want to serve a dynamic page, then you can't just read a file off the SD card. You must add dynamic content to the page. That dynamic content would include the analog sensor data.
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 175
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes i want dynamic  web page...what javascript code i use for display web page the analog sensor value?
Logged

Web / sms / bluetooth / arduino / android remote control. Home Automation projects.
http://www.power7.net

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

Quote
Yes i want dynamic  web page...what javascript code i use for display web page the analog sensor value?
You need to design the web page you want to see on a PC using any web designer EXCEPT Front Page. Then, you make the Arduino serve up that page.

Your question is impossible to answer, since we have no idea what sensor values you want to display, or how you want them displayed.
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 175
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In my first simple code without sd card i use... client.print(analogsensor(3)) and work
i have connect Lm35 temperature sensor...
When i refresh page display new value...
Logged

Web / sms / bluetooth / arduino / android remote control. Home Automation projects.
http://www.power7.net

Offline Offline
Full Member
***
Karma: 2
Posts: 175
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry .... client.print(analogRead(3)*0.488);
Logged

Web / sms / bluetooth / arduino / android remote control. Home Automation projects.
http://www.power7.net

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

You can't serve a dynamic page from static storage. You can serve the static parts of the page from the SD card, but the dynamic stuff must be served dynamically.
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 175
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

ok i make the code...but display the value at the buttom of page....
Code:
if (c == '\n' && currentLineIsBlank ) {
           delay(300);
           xClicksProcessed++;
           
           
           client.println("HTTP/1.1 200 OK");//8
            client.println("Content-Type: text/html");//9
            client.println();//10

             myFile = SD.open("index.htm");
  if (myFile) {
    Serial.println("index.htm:");
   
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    client.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
  // if the file didn't open, print an error:
    Serial.println("error opening index.htm");
  }

            client.print("<br>Analog input A3 (Temperature) reads: <b>");//Temperature sensor Lm35
            client.print(analogRead(3)*0.488);
            client.println("</b>");

            client.print("<p>Clicks since server booted: ");
            client.print(xClicksProcessed);
            client.println("</p>");
Logged

Web / sms / bluetooth / arduino / android remote control. Home Automation projects.
http://www.power7.net

Offline Offline
Full Member
***
Karma: 2
Posts: 175
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have many functions for control a/c (pulse Ir) and aruino flash memory 30k is not enough...could i use the sd card for upload code?
Logged

Web / sms / bluetooth / arduino / android remote control. Home Automation projects.
http://www.power7.net

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

Quote
I have many functions for control a/c (pulse Ir) and aruino flash memory 30k is not enough...could i use the sd card for upload code?
No.
Logged

Pages: [1]   Go Up
Jump to: