Coding a monitoring system with a low pressure stop and web reset

Hey guys!

For a coding scenario with an arduino ethernet shield, I'm setting up a monitor system that watches the state of four switches and pressure. right now I'm simulating the pressure with a potentiometer.

The logic I need is: The maximum pressure of a irrigation system is 100PSI. If the system drops to 20PSI, it displays a warning. if it drops to 10PSI, it will shut down the system (Light up a LED) and wait for a reset signal before continuing.

Right now I got it so that the LED will light up if the pressure drops below 10. What I want to do is have the code stop at that condition until a reset signal is sent (I would also want it to be possible to reset it over the ethernet webserver, as the data has to be displayed on a website, but for now that can wait)

This is the code I have right now:

  //Reading function and conditions for pressure sensor
  int psensor = analogRead(A2);
  // Convert reading to pressure
  float voltage = (psensor * (5.0 / 1023.0));
  //convert voltage to pressure
  float pressure = (100 * ((voltage)/(5.0 - voltage)));
    if (pressure > 100)
    {
      pressure = 100;
    } 
    //checks if the pressure is below 10PSI,  if it is, turn the valve off and print a warning
    if (pressure < 10)
    {
      digitalWrite(valve, HIGH);
      Serial.println("Warning: Line rupture!");
    }
    
    //checks if the pressure is below 20PSI,  if it is, print a warning
    else if (pressure < 20)
    {
      Serial.println("Warning: Low Pressure");
      digitalWrite(valve, LOW);
    }
    
    //turns the valve on if the pressure is okay
    else
    {
      digitalWrite(valve, LOW);
    }

If you really want the code to stop when the pressure is below 20PSI and wait for a reset signal you could simply enter a while loop when the condition is true and read the reset input until it occurs.

A better solution would probably be to set a boolean variable to true when the low pressure condition occurs and use the value of that variable in loop() to start looking for the reset on each pass through loop(). That way the program will not block other functionality should you add more later.

Okay I think I figured it out. What I did was introduced some boolean variables that turned the procedures on using if statements.

What I'm trying to figure out now is how to set those variables using an HTML button. I tried to look up some examples on google, but nothing seemed to make sense.

Thanks!

Chris

-(Snip)-
//digital pin values for the monitor
int valve = 5;                   //The valve that controls the system
int h1 = 6;                      //hydrant 1
int h2 = 7;                      //hydrant 2
int h3 = 8;                      //hydrant 3
int h4 = 9;                      //hydrant 4
int reset = 4;                   //Reset Button

//Variables that will be used in this code
int hydrant1;                    //variable for status of hydrant 1
int hydrant2;                    //variable for status of hydrant 2
int hydrant3;                    //variable for status of hydrant 3
int hydrant4;                    //variable for status of hydrant 4
float pressure;                  //variable for status of pressure
boolean systemshutdown = false;  //variable for the system shutdown state
boolean systemoverload = false;  //variable for if the pressure is getting low
boolean systemreset = false;     //variable that is set with the reset button

-(Snip)-

  /*Monitoring section*/
  //Reading functions for Hydrant switches
  hydrant1 = digitalRead(h1);
  hydrant2 = digitalRead(h2);
  hydrant3 = digitalRead(h3);
  hydrant4 = digitalRead(h4);

  //Serial.println(systemshutdown); 
  //Reading function and conditions for pressure sensor - only reads if systemshutdown is false.

  if (systemshutdown == false){
    int psensor = analogRead(A2);
    // Convert reading to pressure
    float voltage = (psensor * (5.0 / 1023.0));
    //convert voltage to pressure
    float pressure = (100 * ((voltage)/(5.0 - voltage)));

    if (pressure > 100){
      pressure = 100;
    }

    //Serial.println(pressure); 

    //checks if the pressure is below 10PSI,  if it is, turn the valve off and print a warning
    if (pressure < 10){
      systemshutdown =true;
      //Serial.println(systemshutdown);
      //Serial.println("Warning: Line rupture!");
    }

    //checks if the pressure is below 20PSI,  if it is, print a warning
    else if (pressure < 20){
      systemoverload = true;
      //Serial.println("Warning: Low Pressure");
    }

    //resets the systemoverload variable if pressure is over 20PSI
    else if (pressure > 20){
      systemoverload = false;
    }
  }

  //Shutdown Check
  //checks the status of the systemshutdown variable.  if true, closes valve.
  if (systemshutdown == true){
    digitalWrite(valve, HIGH);
  }

  else if (systemshutdown == false){
    digitalWrite (valve, LOW);
  }

  //Reset button read
  //
  int buttonvalue = digitalRead(reset);
  if (buttonvalue = 1){
    systemreset = true;
  }
  //Serial.println(systemshutdown);
  //Serial.println(buttonvalue); 

  //Reset function
  //prodecure for restarting the system
  if ((systemreset = true) || (webreset = true)){

    digitalWrite (valve, LOW); //let's open the valve
    delay(5000); //wait for it to pressurize...

    //reread the pressure
    int psensor = analogRead(A2);
    // Convert reading to pressure
    float voltage = (psensor * (5.0 / 1023.0));
    //convert voltage to pressure
    pressure = (100 * ((voltage)/(5.0 - voltage)));
    if (pressure > 100){
      pressure = 100;
    }
    //Serial.println(pressure); 

    //check if the pressure is still below 10PSI - if pressure is still low, shut 'er back down!
    if (pressure < 10){
      systemshutdown = true;
    }
    else
      systemshutdown = false;
  }

  /*
     //Webserver Section
   */

  -(Snip)-


          //Warning Section
          //
          //Prints a warning if systemoverload or systemshutdown are true

          if (systemshutdown = true){
            client.println("<p align = 'center'><fontsize='6'>Warning! Line rupture detected!</p>");
            client.println("<p align = 'center'>The system has shut down because pressure has dropped below 10 psi.  Find the cause of the low pressure and reset the monitor</p>");
          }

          else if (systemoverload = true){
            client.println("<p align = 'center'><fontsize='6'>Caution: The system is overloaded!</p>");
            client.println("<p align = 'center'>The pressure is below 20 psi. cut back on the number of sprinklers to bring the pressure back up.</p>");
            client.println("<p align = 'center'>If this problem persists, check the lines for leaks</p>");
          }


          //Reset button section
          //
          //displays a reset button to reset the system if the system shuts down
          //only displayed if systemshutdown = true
         //this sets the systemreset variable

          //Shutdown button section
          //
          //this button turns on systemshutdown if the user wants to shut the system down for any other reason.
         //this adjusts the systemshutdown variable.


-(Snip)-
int h1 = 6;                      //hydrant 1
int h2 = 7;                      //hydrant 2
int h3 = 8;                      //hydrant 3
int h4 = 9;                      //hydrant 4

int hydrant1;                    //variable for status of hydrant 1
int hydrant2;                    //variable for status of hydrant 2
int hydrant3;                    //variable for status of hydrant 3
int hydrant4;                    //variable for status of hydrant 4

Arrays! You need arrays.

You need names that clearly reflect what the variable is for, too. hydrantPin and hydrantState mean a lot more than h and hydrant.

What I'm trying to figure out now is how to set those variables using an HTML button. I tried to look up some examples on google, but nothing seemed to make sense.

Typically, this is done with a form with one or more submit buttons and an action field. Pressing one of the submit buttons will generate a GET request that involves the action field.

Thanks for the notes so far. I got the hydrant states in arrays and was able to put in a loop to fill in the data. it also allowed me to simplify the web generating code for the hydrants as well.

One issue I have is I the pressure variable seems to get forgotten by the time I need it for the website - almost like it gets flushed out of memory by the time it gets called again. I can bypass this by doing the pressure reading procedure every time it's needed, but that mean there are three locations in the code right now where I need it.

So I would like to fit it into an function outside void loop().

But when I put the read into the function (shown below) it spews out garbage if the calculated number is not a round number (Stuff like nan and ovf). there seems to be a problem with the number type that is being returned.

float readPressure(){

      int psensor = analogRead(A2);
      // Convert reading to voltage
      float voltage = (psensor * (5.0 / 1023.0));
      //convert voltage to pressure
      float pressure = (100 * ((voltage)/(5.0 - voltage)));
      if (pressure > 100){
        pressure = 100;
      }
return pressure;
}
      float pressure = (100 * ((voltage)/(5.0 - voltage)));

Why are you deliberately mixing int and float arithmetic? 100.0 is not that much harder to type, and makes all the values float.

      if (pressure > 100){
        pressure = 100;
      }

More places where 100.0 would be more appropriate.

The biggest problem, though, is that we have no idea where or how you call that function, or what you do with the output.
You should deal with the case where voltage is 5.0, because dividing by 0 is a bad idea.

Either I'm burning out, or I have ran into an area I have very little idea on what I'm doing.

  /*
     //Webserver Section
   */

  // listen for incoming clients
  EthernetClient client = server.available();
  int index = 0;
  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();
        clientString +=(c);
        
        //Serial.write(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 (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 F("<!DOCTYPE HTML><html><head><title>Irrigation Monitoring System and Control Website</title></head><body><p><h1 align=center>Irrigation Monitoring System</h1>");

(-Snip-)
Irrigation status would go here.  so far this section of code works
(/-snip-)
          

          //Reset button section
          //
          //displays a reset button to reset the system if the system shuts down

          client.println F("<table border = '1' align = 'center'><tr><th>System Reset</th><th>System Shutdown</th></tr>");

          if (systemshutdown == false){
            client.println F("<tr><td>System is functioning</td>");
          }

          else
            client.println F("<tr><td><a href=\"./?reset_system\">Reset Pressure Monitor</a></td>");


          //Shutdown button section
          //
          //this button turns on systemshutdown if the user wants to shut the system down
          //for any other reason.


          if (systemshutdown == false){
            client.println F("<td><a href=\"./?shut_down\">Shut Down System</a></td></tr></table>
");
          }

          else{
            client.println F("<tr><td>System is shut down</td></tr></table>
");
          }
          
          //troubleshooting section - will be commented out when project is ready.
          client.println F("systemshutdown is");
          client.println (systemshutdown);
          client.println F("
systemreset is");
          client.println (systemreset);
          client.println F("
systemoverload is");
          client.println (systemoverload);         


          //Checks for the above commands.
          if (clientString.indexOf("reset_system") >= 0) {
            systemreset = true;
            client.println (clientString);
            clientString = ("");

          }

          else if (clientString.indexOf("shut_down") >= 0) {
            systemshutdown = true;
            clientString = ("");

          }

          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }

  delay(350);        // delay in between reads for stability
}

So what I'm trying to do here is be capable of shutting down the system over the web. basically by adding some text to the URL bar and reading it to set the variable.

This seems to work (albeit somewhat slowly - the page needs to refresh 3 times before it sets), but the problem I have is I now need to clear the URL bar, or else the variable keeps getting set. Is there any way for the arduino to send a redirect request to the client or clear the url bar?

You can either send back an HTTP Redirect response using a 3xx status code, or send back your normal response but include a META REFRESH element to direct the client to immediately load a different page (i.e. the one without the URL arguments). Either way would work just as well; the first way would be slightly cleaner IMO.