Generator Automatic
Generator Automatic
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)
*/
#include <SPI.h>
#include <Ethernet.h>
//generator-related constants
#define K_TIME_IGN_TO_CRANK 2000ul //mS time from ignition on to
starter
#define K_TIME_CRANK_DURATION 2000ul //mS maximum engine cranking time
#define K_TIME_CRANK_COOLDN 3000ul //mS time to allow starter to
cool between attempts
#define K_TIME_TURNOFF_DELAY 4000ul //mS time after running before
returning to top of state machine
#define K_MAX_START_ATTEMPTS 3 //# maximum start attempts
before giving up on generator
#define K_TIME_COOLDN 3000ul //mS time to allow GENERATOR to cool
beFOR STOPING
#define K_TIME_PREAHN 3000ul //mS TIME TO ALLOW GENERATOR HEATING
EFOR CONNETED TO LOAD
//generator state machine state names
enum eGenStates
{
ST_GEN_OFF = 0,
ST_GEN_IGNITION_ON,
ST_GEN_CRANK,
ST_GEN_MONITOR_START,
ST_GEN_RUNNING,
ST_GEN_TIMEDELAY,
ST_GEN_PREAH,
ST_GEN_COOLD,
};
String RECIEVER;
String RECIEVER2;
byte mac[] =
{
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
//generator-specific
const uint8_t pinIgnition = 9; //generator ignition output (high == turn
on ignition)
const uint8_t pinStarter = 7; //generator starter output (high == engage
starter)
const uint8_t pinGeneratorStatus = 5; //generator running input (low == generator
running)
const uint8_t pinLineStatus = 8; //AC mains status (low == mains available)
const uint8_t COLANDHEAT = 2; //AC mains status (low == mains available)
void setup()
{
Serial.begin(9600);
while (!Serial); //wait for serial monitor
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
//gen specific
pinMode( pinGeneratorStatus, INPUT_PULLUP );
pinMode( pinLineStatus, INPUT_PULLUP );
pinMode( pinIgnition, OUTPUT );
pinMode( pinStarter, OUTPUT );
digitalWrite(RESET_PIN, HIGH);
pinMode(RESET_PIN, OUTPUT);
pinMode( COLANDHEAT, OUTPUT );
}//setup
void(* resetFunc) (void) = 0;
void loop()
{
GeneratorStateMachine();
DoWebClient();
if ( millis() >= 10000 ) digitalWrite(RESET_PIN, LOW); //call reset
}//loop
if (client)
{
Serial.println("new client");
client.println("</font>");
client.println("<CENTER>");
client.println(" <h2> SWITCH1 <a href=\"/?pin9=on\"\"> <button > ON
</button> </a> ");
client.println(" <a href=\"/?pin9=OFF\"\"> <button > OFF </button> </a>
");
client.println("</CENTER>");
client.println("</br>");
client.println(" <h2>GRID ");
client.println("<CENTER>");
if (S == LOW)
client.print(" ON ");
else
client.print("OFF ");
client.println("</h2>");
//client.println(RECIEVER2);
client.println("</html>");
break;
}//if
if (c == '\n')
{
// you're starting a new line
currentLineIsBlank = true;
}//if
else if (c != '\r')
{
// you've gotten a character on the current line
currentLineIsBlank = false;
}//else
}//if
}//while
delay(1);
RECIEVER = " ";
RECIEVER2 = " ";
}//if
}//DoWebClient
static uint32_t
timeGen,
timeGenDelay;
static uint8_t
startAttempts,
nextState,
stateGen = ST_GEN_OFF;
switch ( stateGen )
{
case ST_GEN_OFF:
if ( digitalRead( pinLineStatus ) == HIGH )
{
if ( digitalRead( pinGeneratorStatus ) == LOW )
{
//if generator is already running, go to that state
stateGen = ST_GEN_RUNNING;
}
else
{
//proceed to start the generator
stateGen = ST_GEN_IGNITION_ON;
startAttempts = 0;
}//else
}//if
break;
case ST_GEN_IGNITION_ON:
startAttempts++;
break;
case ST_GEN_CRANK:
//activate the starter motor
digitalWrite( pinStarter, HIGH );
timeGen = timeNow;
timeGenDelay = K_TIME_CRANK_DURATION; //set max starter run time
stateGen = ST_GEN_MONITOR_START;
break;
case ST_GEN_MONITOR_START:
//has crank time exceeded the limit?
if ( (timeNow - timeGen) >= timeGenDelay )
{
//crank duration exceeded; msg for operator
Serial.print( "Generator start unsuccessful on attempt # " );
Serial.println( startAttempts );
//shut off the starter and ignition
digitalWrite( pinStarter, LOW );
digitalWrite( pinIgnition, LOW );
}//if
else
{
//allow the starter to cool and then go back for another attempt
nextState = ST_GEN_IGNITION_ON; //when delay is finished, go to this
state
timeGen = timeNow;
timeGenDelay = K_TIME_CRANK_COOLDN; //cool down time delay
stateGen = ST_GEN_TIMEDELAY;
}//else
}//if
else
{
//not timed out yet; is generator status showing running?
if ( digitalRead( pinGeneratorStatus ) == LOW )
{
//generator has started; disable the starter
Serial.println( "Generator running" );
Serial.println( "Starter off" );
digitalWrite( pinStarter, LOW );
nextState = ST_GEN_PREAH;
timeGen = timeNow;
stateGen = ST_GEN_RUNNING;
}//if
}//else
break;
case ST_GEN_PREAH :
Serial.println( "prheat" );
digitalWrite(COLANDHEAT, HIGH);
timeGen = timeNow;
stateGen = ST_GEN_RUNNING;
break;
case ST_GEN_RUNNING:
//look for:
// - generator not running (e.g. out of fuel), OR
// - a server command to turn it off, OR
// - mains power is available
//
// If any case true, turn off the generator
if ( digitalRead( pinLineStatus ) == LOW )
{
//generator needs to be shut-down
//turn off the colandheat
stateGen = ST_GEN_TIMEDELAY;
}//if
break;
case ST_GEN_TIMEDELAY:
if ( (timeNow - timeGen) >= timeGenDelay )
stateGen = nextState;
break;
}//switch
}//GeneratorStateMachine