PLEAS ANY ONE CAN HELPE ME . I have tow code i want to compine them together.ats9 is for generator auto start stop and the second ( ETHERNET_WEB_SERVER)for web server switch on off

Here's something you can try. It's based on your original but not a copy of it. Compiles as far as I can tell but not tested:

/*
  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 <SD.h>

//generator-related constants
#define K_TIME_IGN_TO_CRANK         2000ul      //mS    time from ignition on to starter
#define K_TIME_CRANK_DURATION       10000ul     //mS    maximum engine cranking time
#define K_TIME_CRANK_COOLDN         30000ul     //mS    time to allow starter to cool between attempts
#define K_TIME_TURNOFF_DELAY        15000ul     //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

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

String RECIEVER;
String RECIEVER2;
   
byte mac[] = 
{
    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

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

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

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

//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)
bool
    bWebSrvr_GenEnable;

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
    bWebSrvr_GenEnable = false;
    pinMode( pinGeneratorStatus, INPUT_PULLUP ); 
    pinMode( pinLineStatus, INPUT_PULLUP );        
    pinMode( pinIgnition, OUTPUT );
    pinMode( pinStarter, OUTPUT );
        
}//setup

void loop() 
{
    GeneratorStateMachine();
    DoWebClient();
        
}//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=\"/?pin8=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();
    static uint32_t
        timeGen,
        timeGenDelay;
    static uint8_t
        startAttempts,
        nextState,
        stateGen = ST_GEN_OFF;

    switch( stateGen )
    {
        case    ST_GEN_OFF:
            if( bWebSrvr_GenEnable == true || 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!" );
                    bWebSrvr_GenEnable = false;
                    
                    stateGen = ST_GEN_ERROR;
                    
                }//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 );

                    stateGen = ST_GEN_RUNNING;
                    
                }//if

            }//else
            
        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( pinGeneratorStatus ) == HIGH || bWebSrvr_GenEnable == false || digitalRead( pinLineStatus ) == LOW )
            {
                //generator needs to be shut-down
                //turn off the ignition    
                bWebSrvr_GenEnable = false;     //in case generator was off due to some other reason
                digitalWrite( pinIgnition, LOW );
                nextState = ST_GEN_OFF;         //when delay is finished, go to this state
                timeGen = timeNow;
                timeGenDelay = K_TIME_TURNOFF_DELAY; //shut-down delay
                stateGen = ST_GEN_TIMEDELAY;           
                                    
            }//if
            
        break;

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

        case    ST_GEN_ERROR:
            //do nothing
            //we're here if the generator failed to start after three tries
            //only way out is to reset the Arduino
            //if web srvr requests another start, send msg to serial monitor and clear flag
            if( bWebSrvr_GenEnable == true )
            {
                Serial.println( "**ERROR**: Generator failed to start!" );
                Serial.println( "Please check generator!" );
                
                bWebSrvr_GenEnable = false;
                
            }//if
            
        break;
        
    }//switch
    
}//GeneratorStateMachine
1 Like