Trouble with client.read and client.parseInt

I am trying to make an online thermostat. The trouble is when one of the buttons are pressed on the site delivered by the arduino it should be sending a ‘c’ and then the integer 1, 2, or, 3. Instead client.read is getting 46 when the buttons are pressed or when the page is loaded and client.parseInt always receives a ‘1’ but the variable ‘mode’ isn’t being updated with the integer received, even though all the buttons send the same client.parseInt.
I also have a text entry with a submit button. When I input a new temp(ie…70) and click submit, client.read gets 32 and client.parseInt receives the 70 but the integer ‘70’ never gets stored in the variable ‘setTemp’.
I’m at a loos with why my variables aren’t getting updated and why client.read is receiving integers instead of the characters being sent.
I have omitted a bunch of code to make it easier to read.

int mode = 0;            //Variable to store mode received from http
int setTemp = 0;         // variable to store temp desired from http
int SSRCPin = 5;         //Turn on A/C unit
int SSRHPin = 6;         //Turn on heat (electric or gas)
int hcLED = 4;           //indicator for Cooling mode
int SSRFan = 7;          // To turn on and off the air handler fan
float currentTemp = 0;   // Store the current tempurature from the sensor

// start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
lcd.begin (16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
}

}
void loop()
 {
  EthernetClient client = server.available();
  if (client)
{
//An HTTP request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;
while (client.connected())
{if (client.available())
{
char c = client.read(); //if clent sends mode store value received on mode
if (receiving && c == ' ') receiving = false; //Done receiving
if(c == '?') receiving = true; receiving = true; //found arguments
//this looks at the GET requests
if(receiving)
{
  //A mode command is specified with the word mode
  if (c == 'cool')
  {
    Serial.print("mode ");
    mode = client.parseInt();
    cool();
    Serial.println(mode);
  }
  else if(c == 'heat'){
    Serial.print("mode ");
    mode = client.parseInt();
    heat();
    Serial.println(mode);
    break;
  }
  else if(c == 'recirc'){
    Serial.print("mode ");
    mode = client.parseInt();
    recirc();
    Serial.println(mode);
    break;
  }  
  else if (c == 'set'){
    Serial.print("Set To ");
    setTemp = client.parseInt();
    Serial.println(setTemp);
    break;
  }
//lcd.clear(); // start with a blank screen
lcd.setCursor(0,0);
lcd.print("Current:");
lcd.setCursor(0,1);
lcd.print("Set:");
lcd.setCursor(9,1);
lcd.print("Mode: ");
Serial.print("Current: ");
Serial.println(currentTemp);
Serial.print("Set Temp: ");
Serial.println(setTemp);
Serial.print("Mode");
Serial.println(mode);
Serial.print("client.parseInt: ");
Serial.println(client.parseInt());
Serial.print("client.print: ");
Serial.println(client.read());
lcd.setCursor(8,0);
printTemperature(insideThermometer);
lcd.setCursor(6,1);
lcd.print(setTemp);
lcd.setCursor(15,1);
lcd.print(mode);
{
//Print the response header and the html page
if(!sentHeader)
{
  //send a standard http response header
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html\n");
                    client.println("<form action='' method='get'>");
                    client.println("<input type='hidden' name'cool' value='1' />");
                    client.println("<input type='submit' value='Cooling Mode' />");
                    client.println("</form>");
                    client.println("                    ");
                    client.println("<form action='' method='get'>");
                    client.println("<input type='hidden' name'heat' value='2' />");
                    client.println("<input type='submit' value='Heating Mode' />");
                    client.println("</form>");
                    client.println("                    ");
                    client.println("<form action='' method='get'>");
                    client.println("<input type='hidden' name'recirc' value='3' />");
                    client.println("<input type='submit' value='Recirculation Mode' />");
                    client.println("</form>");
                    client.println("<form action=\"/?\" method=get>");
                    client.println("<Set Temp To>");
                    client.println("</b><input type=\"text\" name=\"set\" maxlength=\"2\" />");
                    client.println("<input type=\"submit\" value=\"Submit\" />");
                    client.println("</form>");
                    client.println("
");
                    client.println("Current Temp: ");
                    client.println(currentTemp);
                    client.println("
");
                    client.println("Current Mode: ");
                    client.println(mode);
                    client.println("
");
                    client.println("Current Set Temp");
                    client.println(setTemp);                   
                    // send web page
                    sentHeader = true;
                    }                    
                }
                // 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;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
}
}
}
  if (c == 'cool')

Single quotes are for single characters. Please post a picture of your keyboard, with the ONE key circled that you pressed to get that one key in single quotes.

if(c == '?') receiving = true; receiving = true; //found arguments

Perhaps this needs to be:

if(c == '?') receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; //found arguments

I tried your suggestion, and also changed cool, heat, recirc, and set, to 1, 2, 3, and s. Still having the same issue. :frowning:
Also, I included a picture of that ‘1’ key. :astonished:

modsbyus:
Still having the same issue. :frowning:

I suppose that'll be caused by something in your modified code, then.

PeterH:

modsbyus:
Still having the same issue. :frowning:

I suppose that'll be caused by something in your modified code, then.

Those were 2 separate edits. I first tried PaulS's suggestion, then tried changing the rest. Both ways give the same results.

This is my Serial output. I don't know if this helps.

The first one is when I click any one of the mode buttons or when the page is loaded. client.parseInt comes back as a 1, no matter what mode button I click. It should send a 1, 2 ,or 3 for the modes.
Current: 72.50
Set Temp: 0
Mode: 0
client.parseInt: 1
client.print: 46

The second one is when I used the text box to input a new set temp to 55 and clicked submit. client.parseInt is receiving the 55 like it should but client.print should be an s not 32.
Current: 72.50
Set Temp: 0
Mode: 0
client.parseInt: 55
client.print: 32

I found some issues with the html portion. However, Im still basically having the same issue. On every page load, whether a button is clicked or not I receive a 1. But now when the buttons are clicked I can see the 1, 2, or 3 read in client.parseInt. I think the problem now has to do with how I am trying to send the M for client.read. I never read an M, instead I read a 46 or a 32. Any insight into this guys???

if(!sentHeader)
{
  //send a standard http response header
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html\n");
  
                    client.println();
                    client.println("<form action=\"/?\" method=get>");
                    client.println("<input type=\"hidden\" name=\"M\" value=\"1\" />");
                    client.println("<input type=\"submit\" value=\"Cooling Mode\" />");
                    client.println("</form>");
                    
                    client.println("<form action=\"/?\" method=get>");
                    client.println("<input type=\"hidden\" name=\"M\" value=\"2\" />");
                    client.println("<input type=\"submit\" value=\"Heating Mode\" />");
                    client.println("</form>");
                    
                    client.println("<form action=\"/?\" method=get>");
                    client.println("<input type=\"hidden\" name=\"M\" value=\"3\" />");
                    client.println("<input type=\"submit\" value=\"Recirculation Mode\" />");
                    client.println("</form>");
                    client.println("<form action=\"/?\" method=get>");
                    client.println("<Set Temp To>");
                    client.println("<input type=\"text\" name=\"S\" maxlength=\"2\" />");
                    client.println("<input type=\"submit\" value=\"Submit New Temperature\" />");
                    client.println("</form>");
                    client.println("
");
                    client.println("Current Temp: ");
                    client.println(currentTemp);
                    client.println("
");
                    client.println("Current Mode: ");
                    client.println(mode);
                    client.println("
");
                    client.println("Current Set Temp");
                    client.println(setTemp);
                    
                    
                    // send web page
                    sentHeader = true;
                    }
                    
                }
                // 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;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
   

}
}

I have omitted a bunch of code to make it easier to read.

Perhaps you should post all your code (or the html source code for the page) so that it can be checked. Might be easier ways to make the thermostat control. Also, what functions does the control perform?

This is the HTML Portion. I am including the sketch as an attachment.

if(!sentHeader)
{
  //send a standard http response header
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html\n");
  
                    client.println();
                    client.println("<form action=\"/?\" method=get>");
                    client.println("<input type=\"hidden\" name=\"M\" value=\"1\" />");
                    client.println("<input type=\"submit\" value=\"Cooling Mode\" />");
                    client.println("</form>");
                    
                    client.println("<form action=\"/?\" method=get>");
                    client.println("<input type=\"hidden\" name=\"M\" value=\"2\" />");
                    client.println("<input type=\"submit\" value=\"Heating Mode\" />");
                    client.println("</form>");
                    
                    client.println("<form action=\"/?\" method=get>");
                    client.println("<input type=\"hidden\" name=\"M\" value=\"3\" />");
                    client.println("<input type=\"submit\" value=\"Recirculation Mode\" />");
                    client.println("</form>");
                    client.println("<form action=\"/?\" method=get>");
                    client.println("<Set Temp To>");
                    client.println("<input type=\"text\" name=\"S\" maxlength=\"2\" />");
                    client.println("<input type=\"submit\" value=\"Submit New Temperature\" />");
                    client.println("</form>");
                    client.println("
");
                    client.println("Current Temp: ");
                    client.println(currentTemp);
                    client.println("
");
                    client.println("Current Mode: ");
                    client.println(mode);
                    client.println("
");
                    client.println("Current Set Temp");
                    client.println(setTemp);
                    
                    
                    // send web page
                    sentHeader = true;
                    }
                    
                }
                // 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;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
   

}
}

thermostatDallasV4Online.ino (10.5 KB)

So I knew that client.read was reading a 32, but I realized I hadn't accounted for char c = client.read();
So I added a little more debug on the serial output and found that even though I am sending an M for the name from the form button on the web page, I am getting a G.

Current Temperature: 0.00
Set Temp: 0
Mode: 0
client.parseInt: 1
client.print: 46
Current c value: G
Line --------------- Break
Current Temperature: 74.30
Set Temp: 0
Mode: 0
client.parseInt: 1
client.print: 32
Current c value: G
Line --------------- Break
Current Temperature: 74.30
Set Temp: 0
Mode: 0
client.parseInt: 1
client.print: 32
Current c value: G
Line --------------- Break
Current Temperature: 73.40
Set Temp: 0
Mode: 0
client.parseInt: 2
client.print: 32
Current c value: G
Line --------------- Break
Current Temperature: 73.40
Set Temp: 0
Mode: 0
client.parseInt: 3
client.print: 32
Current c value: G
Line --------------- Break
Current Temperature: 73.40
Set Temp: 0
Mode: 0
client.parseInt: 82
client.print: 32
Current c value: G
Line --------------- Break

if(c == '?') receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; //found arguments

WTF? I can't believe you did this.

EVERY if statement needs curly braces until you KNOW that they can be omitted.
EVERY statement goes on a line all by itself.

Code
needs
to
be
properly
indented
to
be readable.

Do NOT post any more code without using Tools + Auto Format FIRST.

if(c == '?') receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; //found arguments

WTF? I can't believe you did this.

EVERY if statement needs curly braces until you KNOW that they can be omitted.
EVERY statement goes on a line all by itself.

Code
needs
to
be
properly
indented
to
be readable.

Do NOT post any more code without using Tools + Auto Format FIRST.

PaulS,
I appreciate your help. However, I do believe that was quite unnecessary . I am still learning (on my own) and did not realize that was what auto format was for.
I ran the auto format and it left that line completely alone. I am all ears for constructive criticism and ready for any advice and tips that I receive from anyone here. I just ask for a little patience while I learn the basics.
So, do I need all of this(each on it own line)? if(c == '?') receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; receiving = true; //found arguments or since I am just sending an M or an S do I only need if(c == '?') receiving = true;  //found arguments?
Also, would this account for receiving a G instead of an M and if so then why? As far as I can tell receiving G instead of an M is why my loop doesn't progress when I click one of the buttons.

So, do I need all of this(each on it own line)?

If you put that on separate lines, like you should have from the beginning, and used curly braces, like you should have, that line would be:

  if(c == '?') 
  {
     receiving = true;
  }
  receiving = true;
  receiving = true;
  receiving = true;
  receiving = true;
  receiving = true;
  receiving = true;
  receiving = true; //found arguments

which make it real clear that it is nonsense.

PaulS:

So, do I need all of this(each on it own line)?

If you put that on separate lines, like you should have from the beginning, and used curly braces, like you should have, that line would be:

  if(c == '?') 

{
     receiving = true;
  }
  receiving = true;
  receiving = true;
  receiving = true;
  receiving = true;
  receiving = true;
  receiving = true;
  receiving = true; //found arguments



which make it real clear that it is nonsense.[\quote]


Are you planning to say something helpful or do just plan on chastising me?

Are you planning to say something helpful or do just plan on chastising me?

Post some reasonable looking code. Define exactly what the problem is.

so, would this account for receiving a G instead of an M and if so then why? As far as I can tell receiving G instead of an M is why my loop doesn't progress when I click one of the buttons

This is what I believe is the issue I am having. As best I can tell I have defined the problem.
If there is a problem with my code, Please give insightful and useful information to help me understand my short comings in coding and to further the knowledge of myself and the community. Just simply stating that I suck doesn't get me or anyone else anywhere new. This project is my learning experience for working with the Ethernet shield , and following the advice of many other people in the community, I am learning by trying. That in and of itself implies that there will be faults in my coding and poor coding etiquette. Please bear with me as I work through these faults and better my understanding and skills.

There were some issues pointed out with your code. I can't tell which you have dealt with. Post your code after you have dealt with the issues that have been pointed out.

Thank you. Let me know if I’ve fubared my code any more. :cold_sweat:
I had to attach the sketch since it exceeded the maximum characters.

thermostatDallasV4Online.ino (11.1 KB)

Please bear with me as I work through these faults and better my understanding and skills.

Sorry you have encountered “you-know-who”. With so much useless churn not sure what you want to do now. Based on the earlier code you posted it seems you want some web page text boxes to submit data to the arduino server. I took some simple code below and added two more submit text boxes for an example. Modify the code to your network settings, upload the code, open the serial monitor, and load the web page. The serial monitor will display the String received from the browser. The String will have an identifier as to which submit text box ( LED, temp, or smoke) was used, and the data typed in the text box. Using the String functions it is very easy to identify the intended component and the text sent for the component.

//open serial monitor to see what the arduino receives
//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); 
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("Text box test"); 
}

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); 

          //output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Changing Settings</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Change system status</H1>");

          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Enter Status of motion sensor 'on' or 'off': <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Change'>");

          client.println("</FORM>");

          client.println("
");

          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Enter temprature: <INPUT TYPE=TEXT NAME='temp' VALUE='' SIZE='25' MAXLENGTH='50'>
");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Change'>");

          client.println("</FORM>");

          client.println("
");
          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Enter Status of smoke detector 'on' or 'off': <INPUT TYPE=TEXT NAME='smoke' VALUE='' SIZE='25' MAXLENGTH='50'>
");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Change'>");

          client.println("</FORM>");

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

          delay(1);
          client.stop();

          if(readString.indexOf("on5") >0)
          {
            digitalWrite(5, HIGH);    
            Serial.println("Led On");
          }
          if(readString.indexOf("off5") >0)
          {
            digitalWrite(5, LOW);    
            Serial.println("Led Off");
          }
          readString="";

        }
      }
    }
  }
}

Thank you very much for the reply. Before I proceed, can you explain the benefit/s of using strings in this scenario?
I Would also like to know why I may be receiving a G instead of an M when I debug char c = client.read(); but a 32 when I debug client.read() if you may know. It doesn't seem to mach the ascii table for anything and I don't know where they are coming from.