/*HvpsStatCtrl
 * 
 *  MARS Schaefer K1979 High Voltage Power Supply 
 *    ON - OFF Control and Digital Status
 * 
 *  Duane R Thompson
 *  drthom@mbari.org
 *  Sept 2020
 *  
 *  Board Stack:
 *    Screw Shield
 *    Arduino Ethernet Shield
 *    Arduino UNO
 *  
 */

#include <SPI.h>        //Libraries required to communicate with the arduino ethernet shield
#include <Ethernet.h>

// Arduino Uno GPIO pin number definitions
int Inhibit   = 2;   //Inhibit ( p/s On-Off  Arduino Uno pin 2
int HvpsInput = 3;
int HvpsDC    = 5;  // 4 CRASHES SERVER
int HvpsOV    = 6;
int HvpsOT    = 7;
int HvpsOC    = 8;

// vars for status inputs
int HvpsInput_OK = 1;   // High == OK
int HvpsDC_OK    = 1;
int HvpsOV_OK    = 1;
int HvpsOT_OK    = 1;
int HvpsOC_OK    = 1;

byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x48, 0x4B };   //physical mac address
byte ip[] = { 192,168,1,177 };                        //ip in lan (that's what you need to use in your browser. ("192.168.56.100")
//byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
//byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //Server port    
 
String readString;            //HTTP request read

// setup() begins
void setup() 
{
 // Open serial monitor and wait for port to open
  Serial.begin(9600);
  /*
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  */

  // Set up Arduino Uno GPIO pin direction
  pinMode(Inhibit,     OUTPUT);   // Inhibit ( p/s On-Off ) pin declared output
  pinMode(HvpsInput,   INPUT);
  pinMode(HvpsDC,      INPUT);
  pinMode(HvpsOV,      INPUT);
  pinMode(HvpsOT,      INPUT);
  pinMode(HvpsOC,      INPUT);
  
    
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);    //Begin Ethernet shield
  server.begin();             //Start Server
  Serial.print("server is at ");          
  Serial.println(Ethernet.localIP());    //Locate the IP assigned to arduino
}
//setup() ends


// loop() begins
void loop() 
{
  // Read the Arduino Uno status input pins


    if ( digitalRead(HvpsInput) == 0 )
    {
      // HvpsInput is BAD;
      char Input[] = "Input Power FAULTED";
      Serial.println(Input);
      HvpsInput_OK = 0;
    }
    else if ( digitalRead(HvpsInput) == 1 )
    {
      // HvpsInput is OK
      char Input[] = "Input Power OK";
      Serial.println(Input);   
      HvpsInput_OK = 1;          
    }

    if ( digitalRead(HvpsDC) == 0 )   
    {
      // HvpsDC is BAD;
      char DC[] = "DC Output FAULTED";
      Serial.println(DC);
      HvpsDC_OK = 0;
    }
    else if ( digitalRead(HvpsDC) == 1 )
    {
      // HvpsInput is OK
      char DC[] = "DC Output OK";      
      Serial.println(DC);   
      HvpsDC_OK = 1;          
    }

    if ( digitalRead(HvpsOV) == 0 )
    {
      // HvpsOV is BAD;
      char Voltage[] = "Over Voltage FAULTED";
      Serial.println(Voltage);
      HvpsOV_OK = 0;
    } 
    else if ( digitalRead(HvpsOV) == 1 )
    {
      // HvpsInput is OK
      char Voltage[] = "Over Voltage OK";
      Serial.println(Voltage);  
      HvpsOV_OK = 1;          
    }     

    if ( digitalRead(HvpsOT) == 0 )
    {
      // HvpsOT is BAD;
      char Temp[] = "Over Temp FAULTED";
      Serial.println(Temp);     
      HvpsOT_OK = 0;
    }
    else if ( digitalRead(HvpsOT) == 1 )
    {
      // HvpsInput is OK
      char Temp[] = "Over Temp OK";
      Serial.println(Temp);   
      HvpsOT_OK = 1;          
    }    

    
    if ( digitalRead(HvpsOC) == 0 )
    {
     // HvpsOC is BAD;
      char Current[] = "Over Current FAULTED";
      Serial.println(Current);
      HvpsOC_OK = 0;
    }
    else if ( digitalRead(HvpsOC) == 1 )
    {
      // HvpsInput is OK
      char Current[] = "Over Current OK";
      Serial.println(Current);   
      HvpsOC_OK = 1;          
    }     

    Serial.println();
    Serial.println();

  
  // Check for client request
  EthernetClient client = server.available();
  
  if (client) 
  {                    //If client request arrived read the request
    while (client.connected()) 
    {   
      if (client.available()) 
      {
        char c = client.read();
     
        if (readString.length() < 100) 
        {  
          readString += c;  
        }

   // Server WEB PAGE if requested by user      
         if (c == '\n') 
         {          
           Serial.println(readString); 
           //html file 
           client.println("HTTP/1.1 200 OK"); 
           client.println("Content-Type: text/html");
           
           // try code from GENMON in auto update attempt
           client.println("Connection: close");  // the connection will be closed after completion of the response
           client.println("Refresh: 1");  // refresh the page automatically every 1 sec
           //
           
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<TITLE>Arduino Schaefer HVPS control switch</TITLE>");

           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Schaefer HVPS ON/OFF Control and Status </H1>");
           client.println("<hr />");
           client.println("<br />");  
           client.println("<br />");  



/*  ORIGINAL CODE:         
           client.println("<a href=\"/?button1on\"\">Turn On HVPS</a>");
           client.println("<br />");
           client.println("<br />");
           client.println("<a href=\"/?button1off\"\">Turn Off HVPS</a><br />");   
*/           
          client.print("<input type=submit value=ON style=width:100px;height:45px;background-color:lime onClick=location.href='/?on8;'>");
          client.print("<input type=submit value=OFF style=width:100px;height:45px;background-color:red onClick=location.href='/?off9;'>");
           
           //SPACES
           client.println("<br />");     
           client.println("<br />"); 

           client.println("<H2>Power Supply Status </H2>");
            
           //client.println("<br />"); 

           client.println("Hvps Input:  ");
           if (HvpsInput_OK == 1)
           {
             client.print(" OK ");
           }
           else if (HvpsInput_OK == 0)
           {
             client.print(" FAULTED ");
           }

           client.println("<br />");
           client.println("<br />");           
           client.println("DC Output:  ");
           if (HvpsDC_OK == 1)
           {
             client.print(" OK ");
           }
           else if (HvpsDC_OK == 0)
           {
             client.print(" FAULTED ");
           }

           client.println("<br />");
           client.println("<br />");
           client.println("Over Voltage:  ");
           if (HvpsOV_OK == 1)
           {
             client.print(" OK ");
           }
           else if (HvpsOV_OK == 0)
           {
             client.print(" FAULTED ");
           }

           client.println("<br />");
           client.println("<br />");
           client.println("Over Temperature:  ");
           if (HvpsOT_OK == 1)
           {
             client.print(" OK ");
           }
           else if (HvpsOT_OK == 0)
           {
             client.print(" FAULTED ");
           }

           client.println("<br />");
           client.println("<br />");
           client.println("Over Current:  ");
           if (HvpsOC_OK == 1)
           {
             client.print(" OK ");
           }
           else if (HvpsOC_OK == 0)
           {
             client.print(" FAULTED ");
           }



           
           client.println("</BODY>");
           client.println("</HTML>");
   
           delay(1);
           //stopping client
           client.stop();
           //Translate the user request and check to switch on or off the Inhibit
           if (readString.indexOf('8') >0)
           {
               digitalWrite(Inhibit, HIGH);
           }
           if (readString.indexOf('9') >0)
           {
               digitalWrite(Inhibit, LOW);
           }      
            //clearing string for next read
            readString="";  
           
         }
       }
    }
  }
  delay(100);


}
// loop() ends
