Paul,
Thank you for your help. I believe with your suggestions I was able to figure this out.
If you have time, Give my code a quick look below for any errors or arduino short comings.
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Time.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip( 192,168,100,79 );
IPAddress gateway( 192,168,100,3 );
IPAddress subnet( 255,255,255,0 );
IPAddress ns( 192,168,100,21 );
unsigned long lastRequestTime = -11;
int dataCollected = 0;
int HeadersRead = 0;
int sendBreak = 0;
String data="";
String rcvData = "";
char termCmd[] = { 'Y','Q','P','j' };
#define TIMEOUT 15
#define NUM_CMDS (sizeof(termCmd)/sizeof(char)) //array size
/*
StartSmart Mode (Send Y, Recieve "SM")
BatteryLevel (f, Percentage)
Load (P, Receive Decimal)
SelfTestResult (X, "OK" - good battery, "BT" - failed due to insufficient capacity, "NG" - failed due to overload, "NO" - no results available (no test performed in last 5 minutes)
EstimatedRunTime(j, (Int in Minutes terminated by :)
Status (Q, Should be "08")
*/
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
Serial.begin(2400);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip, ns, gateway, subnet);
server.begin();
}
void printPage(String content,EthernetClient client )
{
// send a standard http response header
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.println(content);
client.println("</html>");
}
void printError(int ErrorClass, EthernetClient client)
{
String content = "";
switch (ErrorClass) {
case 1: // an error occured
content = "An Error has occured";
break;
case 2: // time out
content = "Request Timed Out";
break;
case 3: // Reloaded within 15 seconds - Resend last data set
content = data;
break;
default:
content = "You should not be seeing this";
}
printPage(content,client);
}
void gatherUPSData()
{
int startTime = now();
String lastAction = "";
for( int j = 0; j < NUM_CMDS; j++)
{
char cmd = termCmd[j];
int rcvCmplt = 0;
rcvData = "";
while(sendBreak == 0 && (now() - startTime) < TIMEOUT && rcvCmplt == 0)
{
if(lastAction == "send")
{
while(Serial.available() > 0 && dataCollected == 0)
{
char rt = Serial.read();
if(rt == '\n' || rt == '\r')
{
lastAction = "receive";
if(cmd != 'Y')
{
data.concat("\"");
data.concat(rcvData);
data.concat("\"");
}
if((NUM_CMDS - 1) == j)
{
dataCollected = 1;
data.concat("}");
}
else if(cmd != 'Y')
{
data.concat(",");
}
rcvCmplt = 1;
clearSerialReadBuffer();
}
else
{
if(cmd != 'Y')
{
rcvData.concat(rt);
}
}
}
while(Serial.available() > 0 && dataCollected != 0)
{
clearSerialReadBuffer();
}
}
else if((lastAction == "receive" || lastAction == "") && dataCollected == 0)
{
clearSerialReadBuffer();
//send next command
Serial.write(cmd);
lastAction = "send";
if(j == 0)
{
data.concat("{");
}
if(cmd != 'Y')
{
data.concat("\"");
data.concat(cmd);
data.concat("\":");
}
startTime = now();
}
else
{
clearSerialReadBuffer();
sendBreak = 1;
}
}
if(sendBreak > 0)
{
break;
}
}
if((now() - startTime) >= TIMEOUT)
{
// Timed Out
sendBreak = 2;
}
}
void clearSerialReadBuffer()
{
while(Serial.available() > 0)
{
Serial.read();
}
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client && (now() - lastRequestTime) <= TIMEOUT){
// Resend Last Request
printError(3,client);
delay(1);
client.stop();
}
else if (client) {
// New Client
data = "";
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available() ) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
HeadersRead = 1;
// Gathering Data.. Waiting for Serial Responses
while (HeadersRead == 1 && dataCollected == 0)
{
gatherUPSData();
if(sendBreak > 0)
{
printError(sendBreak,client);
sendBreak = 0;
break;
}
}
if(HeadersRead == 1 && dataCollected == 1)
{
//Sending Page Data to Client
printPage(data,client);
dataCollected = 0;
HeadersRead = 0;
}
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
// Forced Client Disconnect
lastRequestTime = now();
}
}