2

It's been days that I'm trying to figure it out how to edit this perfectly working code in order to load load a webpage from the sd card.

#include <SPI.h>
#include <Ethernet2.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 178 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //server port     
String readString;
boolean L1chk;
boolean L2chk;
boolean L3chk;
boolean L4chk;
boolean L5chk;
boolean L6chk;
boolean L7chk;
boolean L8chk;
int led;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


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("<TITLE>CONTROLLO LEDS</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>FLASHATI</H1>");
           client.println("<hr />");
           client.println("<a href=\"/?1\"\">LED1</a>");
           client.println("<a href=\"/?2\"\">LED2</a>");
           client.println("<a href=\"/?3\"\">LED3</a>");
           client.println("<a href=\"/?4\"\">LED4</a>");
           client.println("<a href=\"/?5\"\">LED5</a>");
           client.println("<a href=\"/?6\"\">LED6</a>");
           client.println("<a href=\"/?7\"\">LED7</a>");
           client.println("<a href=\"/?8\"\">LED8</a>");
           client.println("<br />");     
           client.println("<br />"); 
           client.println("<p>Created by Rui Santos. Visit http://randomnerdtutorials.com for more projects!</p>");  
           client.println("<br />"); 
           client.println("</BODY>");
           client.println("</HTML>");

           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?1") >0){
               ledUno();
            }
           if (readString.indexOf("?2") >0){
               ledDue();
            }
            if (readString.indexOf("?3") >0){
               ledTre();
            }
            if (readString.indexOf("?4") >0){
               ledQuattro();
            }
            if (readString.indexOf("?5") >0){
               ledCinque();
            }
            if (readString.indexOf("?6") >0){
               ledSei();
            }
            if (readString.indexOf("?7") >0){
               ledSette();
            }
            if (readString.indexOf("?8") >0){
               ledOtto();
            }
           readString="";
        }

        }
    }
}
}

So I came out with this after a lot of research:

/*
    Created by Rui Santos
    Visit: http://randomnerdtutorials.com for more arduino projects

    Arduino with Ethernet Shield
*/

#include <SPI.h>
#include <Ethernet2.h>
#include <SD.h>
File webFile;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 178 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //server port     
String readString;
boolean L1chk;
boolean L2chk;
boolean L3chk;
boolean L4chk;
boolean L5chk;
boolean L6chk;
boolean L7chk;
boolean L8chk;
int led;

void setup() {
    // Open serial communications and wait for port to open:
    Serial.begin(9600);

    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    // start the Ethernet connection and the server:
    Ethernet.begin(mac);
    server.begin();
    Serial.print("server is at ");
    Serial.println(Ethernet.localIP());
    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() {
    // Create a client connection
    EthernetClient client = server.available();
    if (client) {
        boolean currentLineIsBlank = true;
        while (client.connected()) {   
            if (client.available()) {
                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;
                    if (readString.indexOf("?1") >0){
                        ledUno();
                    }
                    if (readString.indexOf("?2") >0){
                        ledDue();
                    }
                    if (readString.indexOf("?3") >0){
                        ledTre();
                    }
                    if (readString.indexOf("?4") >0){
                        ledQuattro();
                    }
                    if (readString.indexOf("?5") >0){
                        ledCinque();
                    }
                    if (readString.indexOf("?6") >0){
                        ledSei();
                    }
                    if (readString.indexOf("?7") >0){
                        ledSette();
                    }
                    if (readString.indexOf("?8") >0){
                        ledOtto();
                    }
                    readString="";
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
                //controls the Arduino if you press the buttons

            }

        }
        delay(1);      // give the web browser time to receive the data
        client.stop();
        if (readString.indexOf("?1") >0){
            ledUno();
        }
        if (readString.indexOf("?2") >0){
            ledDue();
        }
        if (readString.indexOf("?3") >0){
            ledTre();
        }
        if (readString.indexOf("?4") >0){
            ledQuattro();
        }
        if (readString.indexOf("?5") >0){
            ledCinque();
        }
        if (readString.indexOf("?6") >0){
            ledSei();
        }
        if (readString.indexOf("?7") >0){ 
            ledSette();
        }
        if (readString.indexOf("?8") >0){
            ledOtto();
        }
        readString="";
    }
}


void ledUno(){
    led = 2;
    if (L1chk == 0){                                             //Il Ledled 1 è Spento
        digitalWrite(led, HIGH); 
        L1chk = 1;
        return;                   //Led 1 acceso           
    }

    if (L1chk == 1){                                             //Il Ledled 1 è Spento
        digitalWrite(led, LOW); 
        L1chk = 0;
        return;                   //Led 1 acceso           
    }
}

void ledDue(){
    led = 3;
    if (L2chk == 0){                                             //Il Ledled 1 è Spento
        digitalWrite(led, HIGH); 
        L2chk = 1;
        return;                   //Led 1 acceso           
    }

    if (L2chk == 1){                                             //Il Ledled 1 è Spento
        digitalWrite(led, LOW); 
        L2chk = 0;
        return;                   //Led 1 acceso           
    }
}

void ledTre(){
    led = 4;
    if (L3chk == 0){                                             //Il Ledled 1 è Spento
        digitalWrite(led, HIGH); 
        L3chk = 1;
        return;                   //Led 1 acceso           
    }

    if (L3chk == 1){                                             //Il Ledled 1 è Spento
        digitalWrite(led, LOW); 
        L3chk = 0;
        return;                   //Led 1 acceso           
    }
}

void ledQuattro(){
    led = 5;
    if (L4chk == 0){                                             //Il Ledled 1 è Spento
        digitalWrite(led, HIGH); 
        L4chk = 1;
        return;                   //Led 1 acceso           
    }

    if (L4chk == 1){                                             //Il Ledled 1 è Spento
        digitalWrite(led, LOW); 
        L4chk = 0;
        return;                   //Led 1 acceso           
    }
}

void ledCinque(){
    led = 6;
    if (L5chk == 0){                                             //Il Ledled 1 è Spento
        digitalWrite(led, HIGH); 
        L5chk = 1;
        return;                   //Led 1 acceso           
    }

    if (L5chk == 1){                                             //Il Ledled 1 è Spento
        digitalWrite(led, LOW); 
        L5chk = 0;
        return;                   //Led 1 acceso           
    }
}

void ledSei(){
    led = 7;
    if (L6chk == 0){                                             //Il Ledled 1 è Spento
        digitalWrite(led, HIGH); 
        L6chk = 1;
        return;                   //Led 1 acceso           
    }

    if (L6chk == 1){                                             //Il Ledled 1 è Spento
        digitalWrite(led, LOW); 
        L6chk = 0;
        return;                   //Led 1 acceso           
    }
}

void ledSette(){
    led = 8;
    if (L7chk == 0){                                             //Il Ledled 1 è Spento
        digitalWrite(led, HIGH); 
        L7chk = 1;
        return;                   //Led 1 acceso           
    }

    if (L7chk == 1){                                             //Il Ledled 1 è Spento
        digitalWrite(led, LOW); 
        L7chk = 0;
        return;                   //Led 1 acceso           
    }
}

void ledOtto(){
    led = 9;
    if (L8chk == 0){                                             //Il Ledled 1 è Spento
        digitalWrite(led, HIGH); 
        L8chk = 1;
        return;                   //Led 1 acceso           
    }

    if (L8chk == 1){                                             //Il Ledled 1 è Spento
        digitalWrite(led, LOW); 
        L8chk = 0;
        return;                   //Led 1 acceso           
    }
}

So far the web page is correctly loaded, but the sketch of the LED light logic seems to not work anymore like it was working correctly before. I thought that maybe someone of you guys could help point me in the right direction.

4
  • What happened to the "readString" after your edit? Commented Sep 10, 2016 at 20:49
  • @MikaelPatel What do you mean?
    – FabioEnne
    Commented Sep 11, 2016 at 7:37
  • You removed the section after the comment //read char by char HTTP request. Commented Sep 11, 2016 at 10:01
  • @MikaelPatel it was just for debuggin purpose
    – FabioEnne
    Commented Sep 11, 2016 at 10:35

1 Answer 1

1

It looks that I found the solution to my problem, the first skecth was working correctly because the SD card is using the pin 4 for his purpose, so the sketch was working good becuse the SD card was not used here, in the second code where I used th SD card to serve a webpage was not working anymore not because of the code, but becuse the PIN 4 where I connected a led was used by SDcard and so it messed up by blinking the LED

DON'T USE PIN4 WITH AN ETHERNET SHIELD IF YOU PLAN TO USE THE SD CARD

1
  • A pin plan can help. Download fritzing.org/download and save a plan. You don't need to create a full schema to still document how you are going to use your pins. Very handy when you pick up this project in half a year or so.
    – Joeri
    Commented Jun 30, 2019 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.