Ethernet Client, Serial.print Error Under if

Hi Everybody

I have a problem about my code. I am controlling my arduino over ethernet with ethernet shield(w5100). The code is controlling arduino's outputs directly. There are no problem about it for now.

Code is creating html based webserver. I am going to arduino's local ip from any browser. There are some buttons in this page. And this butons is connected to another page with link. When I touch the button from any device. I am redirecting a new page like 192.169.1.178/button1on. And when I go to there arduino reading where am I and controlling arduino outputs with if command.

I want to take an information to my pc over serial. When I add some Serial.print to my code's setup loop. I can reading this data from my pc's serial monitor.

But when I add any serial print command under if command(which if command controlling I/O) I cant receving any data to my pc. I am looking to code for a while but I couldnt found my mistake.

I hope anyone can help me about this problem. I am sorry about my bad english. Thank you for your helps.

Here is an original code from randomnerdtutorials website, Rui Santos;

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

 Arduino with Ethernet Shield
 */

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h> 
int led = 4;
Servo microservo; 
int pos = 0; 
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;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(led, OUTPUT);
  microservo.attach(7);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  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("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Random Nerd Tutorials Project</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off LED</a>
");   
           client.println("
");     
           client.println("
"); 
           client.println("<a href=\"/?button2on\"\">Rotate Left</a>");
           client.println("<a href=\"/?button2off\"\">Rotate Right</a>
"); 
           client.println("<p>Created by Rui Santos. Visit http://randomnerdtutorials.com for more projects!</p>");  
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?button1on") >0){
               digitalWrite(led, HIGH);
           }
           if (readString.indexOf("?button1off") >0){
               digitalWrite(led, LOW);
           }
           if (readString.indexOf("?button2on") >0){
                for(pos = 0; pos < 180; pos += 3)  // goes from 0 degrees to 180 degrees 
                {                                  // in steps of 1 degree 
                  microservo.write(pos);              // tell servo to go to position in variable 'pos' 
                  delay(15);                       // waits 15ms for the servo to reach the position 
                } 
           }
           if (readString.indexOf("?button2off") >0){
                for(pos = 180; pos>=1; pos-=3)     // goes from 180 degrees to 0 degrees 
                {                                
                  microservo.write(pos);              // tell servo to go to position in variable 'pos' 
                  delay(15);                       // waits 15ms for the servo to reach the position 
                } 
           }
            //clearing string for next read
            readString="";  
           
         }
       }
    }
}
}

And that is where and how am I editing the code to take serial data to pc.

//controls the Arduino if you press the buttons
           if (readString.indexOf("?button1on") >0){
               digitalWrite(led, HIGH);

               Serial.print("button1on");   //I add this line!!!!!!!

           }
           if (readString.indexOf("?button1off") >0){
               digitalWrite(led, LOW);

               Serial.print("button1off"); //I add this line!!!!!!

           }
           if (readString.indexOf("?button2on") >0){

               Serial.print("servo++");    //I add this line!!!!!!!

                for(pos = 0; pos < 180; pos += 3)  // goes from 0 degrees to 180 degrees 
                {                                  // in steps of 1 degree 
                  microservo.write(pos);              // tell servo to go to position in variable 'pos' 
                  delay(15);                       // waits 15ms for the servo to reach the position 
                } 
           }
           if (readString.indexOf("?button2off") >0){

               Serial.print("servo--"); //I add this line

                for(pos = 180; pos>=1; pos-=3)     // goes from 180 degrees to 0 degrees 
                {                                
                  microservo.write(pos);              // tell servo to go to position in variable 'pos' 
                  delay(15);                       // waits 15ms for the servo to reach the position 
                }

I see the word "servo" in your code. Are you powering a servo from the arduino?

No, I am not driving mini servo motor. But if you want, yes this code can drive mini servo motor. I am not talking about endustrial servo motors. This library can just drive mini servo motors i think. Cause bigger servo motors using little bit different pulsing type. But I am not gonna use this probably. Thats just an example code. I am gonna change big part of this code. But for now just I need to send serial data when button is pressed at webpage..

Is there an anybody to tell me, why arduino isnt sending serial data under if command? I cant being sure about is my question understandable because I am not sure about my english. So is there an any question about my problem for help me?

I suggest you post the complete code you are trying instead of just the part you "edited".

Here is all code. Please help or comment. I cant sending serial data under if loop...!!

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

 Arduino with Ethernet Shield
 */

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h> 
int led = 4;
Servo microservo; 
int pos = 0; 
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;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(led, OUTPUT);
  microservo.attach(7);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  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("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Random Nerd Tutorials Project</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off LED</a>
");   
           client.println("
");     
           client.println("
"); 
           client.println("<a href=\"/?button2on\"\">Rotate Left</a>");
           client.println("<a href=\"/?button2off\"\">Rotate Right</a>
"); 
           client.println("<p>Created by Rui Santos. Visit http://randomnerdtutorials.com for more projects!</p>");  
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?button1on") >0){
               digitalWrite(led, HIGH);

               Serial.print("button1on");  //I add this line!!!!!!! And not sending data over serial but other loop is
                                                 //  working under if. Just not sending serial data


           }
           if (readString.indexOf("?button1off") >0){
               digitalWrite(led, LOW);

               Serial.print("button1off");  //I add this line!!!!!!! And not sending data over serial but other loop is
                                                 //  working under if. Just not sending serial data



           }
           if (readString.indexOf("?button2on") >0){

               Serial.print("button2on");   //I add this line!!!!!!! And not sending data over serial but other loop is
                                                 //  working under if. Just not sending serial data


                for(pos = 0; pos < 180; pos += 3)  // goes from 0 degrees to 180 degrees 
                {                                  // in steps of 1 degree 
                  microservo.write(pos);              // tell servo to go to position in variable 'pos' 
                  delay(15);                       // waits 15ms for the servo to reach the position 
                } 
           }
           if (readString.indexOf("?button2off") >0){


               Serial.print("button2off");   //I add this line!!!!!!! And not sending data over serial but other loop is
                                                 //  working under if. Just not sending serial data


                for(pos = 180; pos>=1; pos-=3)     // goes from 180 degrees to 0 degrees 
                {                                
                  microservo.write(pos);              // tell servo to go to position in variable 'pos' 
                  delay(15);                       // waits 15ms for the servo to reach the position 
                } 
           }
            //clearing string for next read
            readString="";  
           
         }
       }
    }
}
}

Maybe you have run out of SRAM. Use the F() function.

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

And this is not correct.

  Ethernet.begin(mac, ip, gateway, subnet);

Is that F function, using microsd or something? So what is different then normal if i use F function?

And if this is not correct, can you write correct line to show me and let me understand easily.

So if I use F function is it gonna be ok? So if I use F function am i gonna send data under if function over serial?

And dont forget same serial print line is working out of if function.

I am gonna be crazy heeellpp...

An example of a static web page placed within an F() macro. Easy to do by copying the web page source code and putting all the lines in quotes and then adding the F() start and termination.

client.print(F(    //start F() macro
"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
"<HTML>"
"<HEAD>"
"<meta name='apple-mobile-web-app-capable' content='yes' />"
"<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />"
"<TITLE>JAVA Page</TITLE>"
"</HEAD>"
"<BODY>"
"<H1>JAVA</H1>"
"<hr />"
"
"
"<FORM ACTION='/' method=get >"
"Enter Code: <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
"
"
"
"<input type=submit value='5 ON' style=width:100px;height:45px onClick=location.href='/?on8;'><input type=submit value='5 OFF' style=width:100px;height:45px onClick=location.href='/?off9;'>
"
"<input type=submit value='6 ON' style=width:100px;height:45px onClick=location.href='/?on8;'><input type=submit value='6 OFF' style=width:100px;height:45px onClick=location.href='/?off9;'>
"
"<input type=submit value='7 ON' style=width:100px;height:45px onClick=location.href='/?on8;'><input type=submit value='7 OFF' style=width:100px;height:45px onClick=location.href='/?off9;'>
"
"<input type=submit value='8 ON' style=width:100px;height:45px onClick=location.href='/?on8;'><input type=submit value='8 OFF' style=width:100px;height:45px onClick=location.href='/?off9;'>
"
"</FORM>"
"</BODY>"
"</HTML>"
));   //end  F() macro

Ok, zoomkat as I understand F function is making my job easier. Am i right? But serial communucation problem is not about F function, is it?

I just need to solve my problem about serial because i have no more time to done this project... Nobody to help me?

Thank you so much for all comments and helps.

Up!

You can try the below code to see if the serial info is printed to the serial monitor.

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

Click this URL to load web page http://192.168.1.178:80 
open serial monitor at 9600 to see what the arduino receives

 */

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h> 
int led = 4;
Servo microservo; 
int pos = 0; 
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;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(led, OUTPUT);
  microservo.attach(7);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  Serial.println("server is at ");
  Serial.println(Ethernet.localIP());
  Serial.println();
}


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.print(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://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Random Nerd Tutorials Project</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off LED</a>
");   
           client.println("
");     
           client.println("
"); 
           client.println("<a href=\"/?button2on\"\">Rotate Left</a>");
           client.println("<a href=\"/?button2off\"\">Rotate Right</a>
"); 
           client.println("<p>Created by Rui Santos. Visit http://randomnerdtutorials.com for more projects!</p>");  
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?button1on") >0){
               digitalWrite(led, HIGH);

               Serial.println("button1on");  //I add this line!!!!!!! And not sending data over serial but other loop is
               Serial.println();                                  //  working under if. Just not sending serial data


           }
           if (readString.indexOf("?button1off") >0){
               digitalWrite(led, LOW);

               Serial.println("button1off");  //I add this line!!!!!!! And not sending data over serial but other loop is
               Serial.println();                                  //  working under if. Just not sending serial data



           }
           if (readString.indexOf("?button2on") >0){

               Serial.println("button2on");   //I add this line!!!!!!! And not sending data over serial but other loop is
               Serial.println();                                  //  working under if. Just not sending serial data


                for(pos = 0; pos < 180; pos += 3)  // goes from 0 degrees to 180 degrees 
                {                                  // in steps of 1 degree 
                  microservo.write(pos);              // tell servo to go to position in variable 'pos' 
                  delay(15);                       // waits 15ms for the servo to reach the position 
                } 
           }
           if (readString.indexOf("?button2off") >0){


               Serial.println("button2off");   //I add this line!!!!!!! And not sending data over serial but other loop is
               Serial.println();                                  //  working under if. Just not sending serial data


                for(pos = 180; pos>=1; pos-=3)     // goes from 180 degrees to 0 degrees 
                {                                
                  microservo.write(pos);              // tell servo to go to position in variable 'pos' 
                  delay(15);                       // waits 15ms for the servo to reach the position 
                } 
           }
            //clearing string for next read
            readString="";  
           
         }
       }
    }
}
}

My guess is that readString doesn't contain any of "?button1on","?button1off" "?button2on"or "?button2off"

Actually I/O is working. For example when I press the button on html page, buttons state changing. So I think there are no problem about virtual html buttons. Because that if function is working.

Am I understanding wrongly? I cant being sure because of my english. As. I said my english is not good.
If I understood anything wrongly please let me know.

Up!??

Dexterslab:
Up!??

You need to post YOUR code, and an EXACT description of the problem. Your serial output would be useful, too.

Only YOU can see what the problem is. Where you are sitting, you are blocking our view of your Arduino, your PC, etc.

Dear Pauls firstly good to see you here. As I remember you helped me so much before.

And I cant seeing where is the problem, but I just know when I write same command out of if loop, arduino transmitting data. But when write the serial print command under the while and if loops code line isnt working.

The exact problem is I cant understanding what am I missing. And why arduino is not sending data. Am I using the C language wrongly? Or maybe I have not got so much information about ethernet communucation, clients etc. Am I skipping something at there? What do you want to know please ask directly.

Because as I said my english isnt perfect and just when you say what do you want to know directly, I can understand better maybe.

I hope, one day my english is gonna be better. So I am waiting for your comments about my problem. And waiting to answer your questions about my problem. But please solve my problem. You or another person. Three days happened and I cant solving my problem....

Thank you

Am I using the C language wrongly?

It's hard to say, without seeing your current code.

You can see my codes in first page, I share my codes... Anybody will help me?

Several suggestions have been made and are worth implementing eg. use of the F() macro. This will move strings out of sram leaving more space for other things such as strings used with your Serial debugging. Initialising Ethernet properly is a must.

If you have made these changes and the problem still appears repost your revised code and any output you see on the serial line as requested.