Current Program:
#include <SPI.h>
#include <Ethernet.h>
#include <EEPROM.h>
#include <EthernetUdp.h>
#include <Time.h>
// Ethernet library configuration
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 222 }; // ip in lan
EthernetServer server(82); //server port
// HMTL processing variables
String readString = ""; //string to get incoming data
char c;
char buffer[10];
int dataLength =0;
// Buffer containing processed hmtl data
char data[80];
char datamotor[5];
int index=3;
//Setting the time
int wdstarth = 0;
int wdstartm = 0;
int wdstartp = 0;
int westarth = 0;
int westartm = 0;
int westartp = 0;
int wdstoph = 0;
int wdstopm = 0;
int wdstopp = 0;
int westoph = 0;
int westopm = 0;
int westopp = 0;
int control = 2;
// Setup function
void setup()
{
Ethernet.begin(mac, ip); //start Ethernet
Serial.begin(9600);
Serial.print("The server is at: ");
Serial.println(Ethernet.localIP());
wdstarth = EEPROM.read(0);
wdstartm = EEPROM.read(1);
westarth = EEPROM.read(2);
westartm = EEPROM.read(3);
wdstoph = EEPROM.read(4);
wdstopm = EEPROM.read(5);
westoph = EEPROM.read(6);
westopm = EEPROM.read(7);
control = EEPROM.read(8);
}
// Loop function
void loop()
{
EthernetClient client = server.available();
if (client)
{
while (client.connected())
{
while (client.available()) // Receive client data
{
Serial.print(".");
c = client.read(); //read char by char HTTP request
readString +=c;
//Serial.print(c); //output chars to serial port
// If first request upon connexion, the 3 first characters will be "GET"
// If "GET" is caught, skip the request info
if( readString.equals("GET"))
{
Serial.println("");
Serial.println("GET caught, skipping request and printing HTML");
break;
}
// Otherwise, if the request contains data,
// the first characters will be "POST"
// We then skip the request header and this "if" becomes our main function
if( readString.equals("POST"))
{
Serial.println("");
Serial.println("POST caught, skipping header and acquiring DATA");
// 320 is arbitrary. The actual length that has to be skipped depends on
// several user settings ( browser, language, addons...)
// the skipped length has not to be too long to skip relevant data
// and not to short to waste computing time
for(int i=0; i<320; i++)
{
c = client.read();
Serial.print(c); // UNCOMMENT FOR DEBUG
}
//Searches for "Length: "
while(c != 'L')
{
c = client.read();
Serial.print(c); // UNCOMMENT FOR DEBUG
}
// Skip "Length: "
for (int i=0; i<7; i++)
{
c = client.read();
Serial.print(c); // UNCOMMENT FOR DEBUG
}
// Read the data package length
readString="";
c = client.read();
while(c != '\n')
{
readString += c;
Serial.print(c);
c = client.read();
}
// convert data read from String to int
readString.toCharArray(buffer, readString.length());
dataLength = atoi(buffer);
Serial.println("");
Serial.print("dataLength: ");
Serial.println(dataLength);
// gets DATA
client.read(); // skips additional newline
client.read();
for (int i=0; i<dataLength; i++)
{
data[i] = client.read();
}
Serial.println("");
Serial.print("data: ");
Serial.println(data);
checkURL();
readString ="";
}
}
// HTML CODE
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("<center><table border='1'><tr><td colspan='4' align='center'><b>Omni Systems Outdoor Lights Controller</b></td></tr><tr><td colspan='4' align='center'>");
client.println("</td></tr><form method='post'><tr>");
client.println("<td align='center' colspan='2'><b>Weekdays</b></td><td align='center' colspan='2'><b>Weekends</b></td></tr>");
client.println("<tr><td align='center'><b>Start:</b></td><td align='center'><input type='text' name='wdstart' size='7' value='"); if (wdstarth > 9) {client.println(wdstarth);} else {client.println(0);client.println(wdstarth);}client.println(":"); if (wdstartm > 9) {client.println(wdstartm);} else {client.println(0);client.println(wdstartm);} client.println("'></td><td align='center'><b>Start:</b></td><td align='center'><input type='text' name='westart' size='7' value='"); if (westarth > 9) {client.println(westarth);} else {client.println(0);client.println(westarth);} client.println(":"); if (westartm > 9) {client.println(westartm);} else {client.println(0);client.println(westartm);} client.println("'></td>");
client.println("<tr><td align='center'><b>Stop:</b></td><td align='center'><input type='text' name='wdstop' size='7' value='"); if (wdstoph > 9) {client.println(wdstoph);} else {client.println(0);client.println(wdstoph);}client.println(":"); if (wdstopm > 9) {client.println(wdstopm);} else {client.println(0);client.println(wdstopm);} client.println("'></td><td align='center'><b>Stop:</b></td><td align='center'><input type='text' name='westop' size='7' value='"); if (westoph > 9) {client.println(westoph);} else {client.println(0);client.println(westoph);} client.println(":"); if (westopm > 9) {client.println(westopm);} else {client.println(0);client.println(westopm);} client.println("'></td>");
client.println("</tr><tr><td colspan='4' align='center'><b>Light Control: ");
client.println("<input type='radio' name='on' value='0'"); if (control==0) client.print("checked"); client.println(">On");
client.println("<input type='radio' name='on' value='1'"); if (control==1) client.print("checked"); client.println(">Off");
client.println("<input type='radio' name='on' value='2'"); if (control==2) client.print("checked"); client.println(">Automatic");
client.println("</td></tr><tr><td colspan='4' align='center'><input type='submit' value='Submit Changes'></td></tr></table>");
client.println("</html>");
Serial.println("__________");
delay(100);
client.stop();
}
}
// Reinitializing variables
readString =""; // Reinitialize String
for (int i=0; i<10; i++)
{buffer[i] = '\0';}
for (int i=0; i<50; i++)
{data[i] = '\0';}
for (int i=0; i<5; i++)
{datamotor[i] = '\0';}
dataLength =0;
index = 3;
}
void checkURL() {
wdstarth = atoi(&data[8]);
wdstartm = atoi(&data[13]);
westarth = atoi(&data[24]);
westartm = atoi(&data[29]);
wdstoph = atoi(&data[39]);
wdstopm = atoi(&data[44]);
westoph = atoi(&data[54]);
westopm = atoi(&data[59]);
control = atoi(&data[65]);
EEPROM.write(0, wdstarth);
EEPROM.write(1, wdstartm);
EEPROM.write(2, westarth);
EEPROM.write(3, westartm);
EEPROM.write(4, wdstoph);
EEPROM.write(5, wdstopm);
EEPROM.write(6, westoph);
EEPROM.write(7, westopm);
EEPROM.write(8, control);
}
If anyone could look at these two pieces of code and possibly find a reason why they don't get along, I would very much appreciate it!!