My first (I'm new to arduino and to C/C++ and while I'm at it HTML) big project is an arduino thermostat with a simple web page hosted by the arduino. I started with a basic web page that displayed the current temperature, target temperature, temperature at which the heat turns on and off and if the arduino was controlling the temperature or not. Just simple no frills text. Through an example that was posted on this forum I was able to figure out how to set the target temperature through the web page using a text input box. Now I would like to add a second (and maybe even more) text input box to the web page. Problem is I just don't understand how the code works for the one box I have now which is makes it difficult to add another one. If someone can explain how the code works and maybe how to add another box it would be appriciated. I'm not looking for someone to do the code for me, or else how would I learn, but if someone does, please explain to me what you did.
Here is my entire code.
#include <SPI.h>
#include <Ethernet.h>
#define maxLength 25
byte mac[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
byte ip[] = { 192, 168, 1, 102 };
EthernetServer server(80);
char TargetTemp[4];
int val;
String inString = String(maxLength);
// Define I/O pins
const int TempSensor = A4; // pin that the temp sensor is attached to
const int HeatONMain = 8; // pin that activates heat on relay
const int HeatONFailSafe = 9; // pin that activates heat on relay 2
// Other constant stuff
const int RelayON = LOW;
const int RelayOFF = HIGH;
char* HeatStatusStg[]={"OFF", "ON"};
char* CntrlStatusStg[]={"NO", "YES"};
// Define the number of readings to take. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// us use this value to determine the size of the readings array.
const int numReadings = 15;
int TempReadings[numReadings]; // the readings from the analog input
int TempIndex = 0; // the index of the current reading
int TempTotal = 0; // the running total
int RoomTemperature = 0; // result after converting analog value to degf
int AvgTemperature = 0; // averaged current room temperature
int TempSetpoint = 70; // room temperature setpoint as set from the web page
int RoomTemperatureSPmin = 69; // room temperature minimum setpoint (SP - temperature deadband)
int RoomTemperatureSPmax = 71; // room temperature maximum setpoint (SP + temperature deadband)
int TemperatureDB = 1; // deadband
int HeatStatus = 0;
int CntrlStatus = 0;
long HeatCntrlDelayTime = 120000; // wait for 2 minutes after bootup before trying to control temp
//==================================================================================================================
void setup()
//==================================================================================================================
{
// Initialize pins so relays are inactive at startup/reset
digitalWrite(HeatONMain, RelayOFF);
digitalWrite(HeatONFailSafe, RelayOFF);
// Set pins that are wired to relay board as outputs
pinMode(HeatONMain, OUTPUT);
pinMode(HeatONFailSafe, OUTPUT);
delay (2000);
// start the serial connection
Serial.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}
//==================================================================================================================
void loop()
//==================================================================================================================
{
// Convert analog reading from temperature sensor to degrees fahrenheit:
RoomTemperature = ((5.0 * analogRead(TempSensor) * 100.0)/1024.0); //convert the analog data to temperature
// subtract the last reading:
TempTotal= TempTotal - A0Readings[TempIndex];
// read from the sensor:
TempReadings[TempIndex] = RoomTemperature;
// add the reading to the total:
TempTotal= TempTotal + TempReadings[TempIndex];
// advance to the next position in the array:
TempIndex = TempIndex + 1;
// if we're at the end of the array...
if (TempIndex >= numReadings) {
// ...wrap around to the beginning:
TempIndex = 0;
}
// calculate the average:
AvgTemperature = TempTotal / numReadings;
// set the min and temperature setpoint relative to the temperature entered in through the web page
RoomTemperatureSPmin = (TempSetpoint - TemperatureDB);
RoomTemperatureSPmax = (TempSetpoint + TemperatureDB);
if (millis() < HeatCntrlDelayTime) {
CntrlStatus = 0;
digitalWrite(HeatONMain, LOW);
digitalWrite(HeatONFailSafe, LOW);
HeatStatus = 0;
}
if (millis() > HeatCntrlDelayTime) {
CntrlStatus = 1;
// monitor Room Temperature and turn on heat when temperature drops below setpoint
if ((AvgTemperature) < (RoomTemperatureSPmin)) {
digitalWrite(HeatONMain, HIGH);
digitalWrite(HeatONFailSafe, HIGH);
HeatStatus = 1;}
if ((AvgTemperature) > (RoomTemperatureSPmax)) {
digitalWrite(HeatONMain, LOW);
digitalWrite(HeatONFailSafe, LOW);
HeatStatus = 0;}
}
int bufLength;
EthernetClient client = server.available();
if (client) {
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (inString.length() < maxLength) {
inString += c;
}
if (c == '\n' && current_line_is_blank) {
if (inString.indexOf("?") > -1) {
int Pos_L = inString.indexOf("L");
int End = inString.indexOf("H", Pos_L);
if(End < 0){
End = inString.length() + 1;
}
bufLength = ((Pos_L) - (Pos_L+2));
if(bufLength > 4){ //dont overflow the buffer
bufLength = 4;
}
inString.substring((Pos_L+2), (End-1)).toCharArray(TargetTemp, bufLength); //transfer substring to buffer
val = atoi(TargetTemp);
Serial.print("TargetTemp = ");
Serial.println( val );
TempSetpoint = val;
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html><head></head><body>");
client.println("<h1>WEB ENABLED ARDUINO THERMOSTAT</h1>");
client.println("
");
client.println("<hr />");
// add a meta refresh tag, so the browser pulls again every 20 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"20\">");
client.print("Room Temperature:");
client.print(" ");
client.print(AvgTemperature);
client.print(" ");
client.print("DEGF");
client.println(); // send a cr/lf
client.println("
");
client.print("Heat turns on below:");
client.print(" ");
client.print(RoomTemperatureSPmin);
client.print(" ");
client.print("DEGF");
client.println(); // send a cr/lf
client.println("
");
client.print("Heat turns off above:");
client.print(" ");
client.print(RoomTemperatureSPmax);
client.print(" ");
client.print("DEGF");
client.println(); // send a cr/lf
client.println("
");
client.print("Temperature is being controlled?");
client.print(" ");
client.print(CntrlStatusStg[CntrlStatus]);
client.println(); // send a cr/lf
client.println("
");
client.print("Heat is:");
client.print(" ");
client.print(HeatStatusStg[HeatStatus]);
client.println(); // send a cr/lf
client.println("<Hr />");
client.print("<form method=get>CHANGE TEMPERATURE SETPOINT:<input type=text size=3 name=L value=");
client.print(val);
client.print("> <input name=H type=submit value=submit></form>");
client.println("<Hr />");
client.println("</body></html>");
break;
}
if (c == '\n') {
current_line_is_blank = true;
}
else if (c != '\r') {
current_line_is_blank = false;
}
}
}
delay(1);
inString = "";
client.stop();
}
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.