/*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
 *  
 *  pins 4 10 11 12 13 not available with Enet shield
 *  Using Analog pins as digital outputs
 */

#include <SPI.h>        //Libraries required to communicate with the arduino ethernet shield
#include <Ethernet.h>

// var for counting number of passes thru the sketch
// int count = 1;

// Arduino Uno GPIO pin number definitions  x_PIN
int HvpsOnCMD_PIN  = 2;   // p/s On-Off  Arduino Uno pin 2  "Inhibit"
int Hvps_Input_PIN = 3;
int HvpsDC_PIN     = 5;  // pins 4 10 11 12 13 not available with Enet shield
int HvpsOV_PIN     = 6;
int HvpsOT_PIN     = 7;
int HvpsOC_PIN     = 8;

// Using Analog pins as digital outputs x_LED
int HvpsInput_LED    = A5;
int HvpsOnCMD_LED    = A4;  
int HvpsIsOn_LED     = A3;
int Hvps_OV_LED      = A2;
int Hvps_OT_LED      = A1;
int Hvps_OC_LED      = A0;

// vars for status inputs  x_STAT
int HvpsInput_STAT = 1;   // High == OK
int HvpsOnCMD_STAT = 1;
int HvpsDC_STAT    = 1;
int HvpsOV_STAT    = 1;
int HvpsOT_STAT    = 1;
int HvpsOC_STAT    = 1;

byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x65, 0x42 };   //physical mac address
byte ip[] = { 192,168,1,177 };                        //ip in lan 
//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);

  // Set up Arduino Uno GPIO pin direction
  pinMode(HvpsOnCMD_PIN,   OUTPUT);   // Inhibit ( p/s On-Off ) pin declared output
  pinMode(Hvps_Input_PIN,  INPUT);
  pinMode(HvpsDC_PIN,      INPUT);
  pinMode(HvpsOV_PIN,      INPUT);
  pinMode(HvpsOT_PIN,      INPUT);
  pinMode(HvpsOC_PIN,      INPUT);

  // Using Analog pins as digital outputs
  pinMode(HvpsInput_LED,     OUTPUT);
  pinMode(HvpsOnCMD_LED,     OUTPUT);
  pinMode(HvpsIsOn_LED,      OUTPUT);
  pinMode(Hvps_OV_LED,       OUTPUT);
  pinMode(Hvps_OT_LED,       OUTPUT);
  pinMode(Hvps_OC_LED,       OUTPUT);
      
  // 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 input pins


    if ( digitalRead(Hvps_Input_PIN) == 0 )
    {
      // HvpsInput 208 VAC is not detected by the power supply;
      char Input[] = "Input Power FAULTED";
      Serial.println(Input);
      HvpsInput_STAT = 0;
      digitalWrite(HvpsInput_LED, LOW);
    }
    else if ( digitalRead(Hvps_Input_PIN) == 1 )
    {
      // HvpsInput is OK
      char Input[] = "Input Power OK";
      Serial.println(Input);   
      HvpsInput_STAT = 1;         
      digitalWrite(HvpsInput_LED, HIGH); 
    }

    if ( digitalRead(HvpsDC_PIN) == 0 )   
    {
      // HvpsDC output is OFF;
      char DC[] = "DC Output FAULTED";
      Serial.println(DC);
      HvpsDC_STAT = 0;
      digitalWrite(HvpsIsOn_LED, LOW);
    }
    else if ( digitalRead(HvpsDC_PIN) == 1 )
    {
      // HvpsDC output is ON
      char DC[] = "DC Output OK";      
      Serial.println(DC);   
      HvpsDC_STAT = 1;          
      digitalWrite(HvpsIsOn_LED, HIGH);
    }

    if ( digitalRead(HvpsOV_PIN) == 0 )
    {
      // HvpsOV is reading Over Voltage by the power supply;
      char Voltage[] = "Over Voltage FAULTED";
      Serial.println(Voltage);
      HvpsOV_STAT = 0;
      digitalWrite(Hvps_OV_LED, HIGH);
    } 
    else if ( digitalRead(HvpsOV_PIN) == 1 )
    {
      // HvpsOV is OK
      char Voltage[] = "Over Voltage OK";
      Serial.println(Voltage);  
      HvpsOV_STAT = 1;   
      digitalWrite(Hvps_OV_LED, LOW);       
    }     

    if ( digitalRead(HvpsOT_PIN) == 0 )
    {
      // HvpsOT is reading Over Temperature condition;
      char Temp[] = "Over Temp FAULTED";
      Serial.println(Temp);     
      HvpsOT_STAT = 0;
      digitalWrite(Hvps_OT_LED, HIGH);
    }
    else if ( digitalRead(HvpsOT_PIN) == 1 )
    {
      // HvpsOT is OK
      char Temp[] = "Over Temp OK";
      Serial.println(Temp);   
      HvpsOT_STAT = 1;       
      digitalWrite(Hvps_OT_LED, LOW);   
    }    

    if ( digitalRead(HvpsOC_PIN) == 0 )
    {
     // HvpsOC is reading Over Current;
      char Current[] = "Over Current FAULTED";
      Serial.println(Current);
      HvpsOC_STAT = 0;
      digitalWrite(Hvps_OC_LED, HIGH);
    }
    else if ( digitalRead(HvpsOC_PIN) == 1 )
    {
      // HvpsOC is OK
      char Current[] = "Over Current OK";
      Serial.println(Current);   
      HvpsOC_STAT = 1;          
      digitalWrite(Hvps_OC_LED, LOW);
    }     

    Serial.println();
    Serial.println();
    //Serial.print("Number of passes thru the sketch: ");
    //Serial.println(count);
    
  // 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");
           
           // Auto update
           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 K1979 HVPS Control and Status</TITLE>");

           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Schaefer K1979 HVPS Control and Status </H1>");
           client.println("<hr />");
           client.println("<br />");  
           client.println("<br />");  

           // Buttons and user clicks
           client.print("<input type=submit value=TURN_ON style=width:100px;height:45px;background-color:lime onClick=location.href='/?on8;'>");
           client.print("<input type=submit value=TURN_OFF style=width:100px;height:45px;background-color:red onClick=location.href='/?off9;'>");
           
           //SPACES
           client.println("<br />");     
           client.println("<br />"); 
           client.println("<H2>");

           client.println("Status from Schaefer:");

           client.println("<br />");     
           client.println("<br />"); 
           
           client.println("Hvps 208 VAC Input: ");
           if (HvpsInput_STAT == 1)
           {
             client.print("<font color='lime'> OK </font>");
           }
           else if (HvpsInput_STAT == 0)
           {
             client.print("<font color='red'> FAULTED </font>");
           }
           client.println("<br />");
           client.println("<br />");  
          
           client.println("DC Output: ");
           if (HvpsDC_STAT == 1)
           {
             client.print("<font color='lime'> ON </font>");
           }
           else if (HvpsDC_STAT == 0)
           {
             client.print("<font color='red'> OFF </font>");
           }
           client.println("<br />"); 
           client.println("<br />"); 

           client.println("Over Voltage: ");
           if (HvpsOV_STAT == 1)
           {
             client.print("<font color='lime'> OK </font>");
           }
           else if (HvpsOV_STAT == 0)
           {
             client.print("<font color='red'> FAULTED </font>");
           }
           client.println("<br />"); 
           client.println("<br />"); 

           client.println("Over Temperature:  ");
           if (HvpsOT_STAT == 1)
           {
             client.print("<font color='lime'> OK </font>");
           }
           else if (HvpsOT_STAT == 0)
           {
             client.print("<font color='red'> FAULTED </font>");
           }
           client.println("<br />"); 
           client.println("<br />"); 

           client.println("Over Current: ");
           if (HvpsOC_STAT == 1)
           {
             client.print("<font color='lime'> OK </font>");
           }
           else if (HvpsOC_STAT == 0)
           {
             client.print("<font color='red'> FAULTED </font>");
           }

           client.println("<br />");
           client.println("<br />");
           //client.print("Number of passes thru the sketch: ");
           //client.print(count);           

           client.println("</H2>");
           client.println("</BODY>");
           client.println("</HTML>");
   
           delay(1);
           //stopping client
           client.stop();
           
           //Translate the user request and check to switch Hvps ON or OFF
           if (readString.indexOf('8') >0)
           {
               if((HvpsInput_STAT == 1) && (HvpsOV_STAT == 1) && (HvpsOT_STAT == 1) && (HvpsOC_STAT == 1))
               
               {
               digitalWrite(HvpsOnCMD_PIN, HIGH);
               digitalWrite(HvpsOnCMD_LED, HIGH);
               }
           }
           if (readString.indexOf('9') >0)
           {
               digitalWrite(HvpsOnCMD_PIN, LOW);
               digitalWrite(HvpsOnCMD_LED, LOW);
           }      
            //clearing string for next read
            readString="";  
           
         }
       }
    }
  }
  delay(10);

  //count++;  // increment number of passes thru the sketch
}
// loop() ends
