0% found this document useful (0 votes)
38 views

Generator Automatic

The document describes code for an Arduino web server that controls a generator. It includes constants and definitions for the generator's state machine and functions for the web client and state machine logic. The state machine controls starting, running and stopping the generator based on sensor readings and time delays.

Uploaded by

ali mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Generator Automatic

The document describes code for an Arduino web server that controls a generator. It includes constants and definitions for the generator's state machine and functions for the web client and state machine logic. The state machine controls starting, running and stopping the generator based on sensor readings and time delays.

Uploaded by

ali mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

/*

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>

//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
};

byte gateway[] = { 172, 17, 91, 1 };

IPAddress ip(172, 17, 91, 99);


// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
int RESET_PIN = 3;

//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

void DoWebClient( void )


{
EthernetClient client = server.available();
int S = digitalRead( pinLineStatus );

if (client)
{
Serial.println("new client");

// an http request ends with a blank line


boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
RECIEVER += c;
// 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 ( RECIEVER.length() < 50 )
RECIEVER2 += c;

// if( RECIEVER2.indexOf( "gen=on" ) > 0 )


// bWebSrvr_GenEnable = true;
// else if( RECIEVER2.indexOf( "gen=off" ) > 0 )
// bWebSrvr_GenEnable = false;

if ( c == '\n' && currentLineIsBlank)


{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed
after completion of the response
client.println("Refresh: 5"); // refresh the page automatically
every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("</br>");
client.println (" <center>");
client.println("<font size='60%' color='black'> ");
client.println("<table border='4' <td align='center '> GENERATOR POWER
CONTROL </td> </table>");
client.println(" </center>");

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 = " ";

// close the connection:


client.stop();
Serial.println("client disconnected");

}//if

}//DoWebClient

void GeneratorStateMachine( void )


{
uint32_t
timeNow = millis();
unsigned long timeheatcool = K_TIME_PREAHN ;
unsigned long timecool = K_TIME_COOLDN ;

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

//turn on the ignition and wait the ign-to-crank delay


digitalWrite( pinIgnition, HIGH );
nextState = ST_GEN_CRANK;
timeGen = timeNow;
timeGenDelay = K_TIME_IGN_TO_CRANK;
stateGen = ST_GEN_TIMEDELAY;

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 ( startAttempts >= K_MAX_START_ATTEMPTS )


{
//generator has failed to start after max # of tries
Serial.println( "**ERROR**: Generator failed to start!" );
Serial.println( "Please check generator!" );

}//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 :

if ( digitalRead( pinGeneratorStatus ) == LOW ) {


if ( (timeNow - timeGen) >= timeheatcool)

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

digitalWrite( COLANDHEAT , LOW );


nextState = ST_GEN_OFF; //when delay is finished, go to this state
timeGen = timeNow;
if ( (timeNow - timeGen) >= timecool )
Serial.println( "cooldown" );
digitalWrite( pinIgnition, LOW );

stateGen = ST_GEN_TIMEDELAY;

}//if

break;

case ST_GEN_TIMEDELAY:
if ( (timeNow - timeGen) >= timeGenDelay )
stateGen = nextState;
break;

}//switch

}//GeneratorStateMachine

You might also like