Go Down

Topic: Web server, Reset button. Can someone check my code please! (Read 976 times) previous topic - next topic

Rich196

May 15, 2013, 05:18 pm Last Edit: May 17, 2013, 04:33 pm by Rich196 Reason: 1
I have a webserver on my Arduino ethernet setup, it is currently displaying digital inputs and data stored in some variables perfectly. I have a variable called "floodcount" can anyone tell me how to make a button to go on the webpage that will allow me to reset the varialbe "floodcount" to 0.

Thanks in advance


Rich196


Please post your code.


Please find it below!!

Code: [Select]
/* Water sensor

This sketch will light up the LED on Pin 9, when water bridges the gap in the sensor pins 2-10
   


*/
//************************************************************************ Setting up ethernet port**************************************************
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,99,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);

const int floodSensor1 = 2;     // the number of the Flood Sensor pin
const int floodSensor2 = 3;     // the number of the Flood Sensor pin
const int floodSensor3 = 4;     // the number of the Flood Sensor pin
const int floodSensor4 = 5;     // the number of the Flood Sensor pin
const int floodSensor5 = 6;     // the number of the Flood Sensor pin
const int floodSensor6 = 7;     // the number of the Flood Sensor pin
const int floodSensor7 = 8;     // the number of the Flood Sensor pin
// const int floodSensor8 = 11;     // the number of the Flood Sensor pin (using as pin 9 is test led output!!***********************************
const int ledPin =  9;      // the number of the LED pin (pin 9 is the on board led) this will need to be changed to an output pin.*******************************

// variables will change:
int floodSensorState = 0;         // variable for reading the floodSensors status
int floodcount = 0;               // variable for storing the total number of flood alerts
int currentState = 0;             // variable for the counter
int previousState = 0;            // variable for the counter

void setup() {
//*************************************************************Ehternet setup**********************************
 
   // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

//*********************************************Working serial only**************************************************
     // initialize serial communication at 9600 bits per second:
//  Serial.begin(9600);

//********************************************************************************
   // initialize the LED pin as an output:
   pinMode(ledPin, OUTPUT);     
   // initialize the flood Sensor pin as an input:
   pinMode(floodSensor1, INPUT);
   pinMode(floodSensor2, INPUT);
   pinMode(floodSensor3, INPUT);
   pinMode(floodSensor4, INPUT);
   pinMode(floodSensor5, INPUT);
   pinMode(floodSensor6, INPUT);
   pinMode(floodSensor7, INPUT);
// pinMode(floodSensor8, INPUT);
}
//........................................................... Reading input sensors in to variable************************************************
void loop(){
   

   
// Read each of the digital inputs, using the or function (||) if any of them are on it will make floodsensorstate variable high.
   
if ( digitalRead(floodSensor1) || digitalRead(floodSensor2) || digitalRead(floodSensor3) || digitalRead(floodSensor4) || digitalRead(floodSensor5) || digitalRead(floodSensor6) || digitalRead(floodSensor7) )
{
   // make floodsensorstate variable high.   
   floodSensorState = HIGH;
   currentState = 1;
}
else
{
  // make floodsensorstate variable low.
   floodSensorState = LOW;
   currentState = 0;
}
//************************************************ Using variable to switch on output ****************************************************
  // check floodsensorstate variable if high turn led on.
if (floodSensorState == HIGH) {     
     // turn LED on:   
     digitalWrite(ledPin, HIGH); 
   }
   else {
     // turn LED off:
     digitalWrite(ledPin, LOW);
   }   
//************************************************ counter ********************************************************************************

  if(currentState != previousState){     // current state not equal to previous state
  if(currentState == 1){                 // current state equal to 1
    floodcount = floodcount + 1;         // add one to counter
  }
}
  previousState = currentState;        // current state equal to previous state delay before re checking
delay(250);


//********************************************* Output of floodSensorState to serial *****************************************************
     // print out the state of the button:
  Serial.println(floodSensorState);
  Serial.println(floodcount);
  delay(1);        // delay in between reads for stability
 

//*********************************************Ethernet code *******************************************************************************

  // listen for incoming clients
  EthernetClient client = server.available();
  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();
        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");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
//********************************************************************************Web page tile***********************************         
          client.print("FIELD ELECTRONITCS LTD, WASTE WATER MANAGEMENT SYSTEM");
          client.println("<br />");
          client.println("<br />");
         
                    // add a meta refresh tag, so the browser pulls again every 2 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"2\">");
          // output the value of each analog input pin
         
          for (int analogChannel = 2; analogChannel < 9; analogChannel++) {    //Reads didgial inputs 2, 8. Each time is goes round the loop it read the next input.
            int sensorReading = digitalRead(analogChannel);
            client.print("Sensor input ");
            client.print(analogChannel - 1); // changes the text field so that input 2, its title sensor input 1!
            client.print(" is ");
            client.print(sensorReading);
            client.println("<br />");       
          }
//****************************************************** Displaying The alarm status on the webpage ***************************************************
          client.println("<br />");    //*****************************
          client.print("Alarm Status ");      // Display the words " Alarm Status"
          client.print(floodSensorState);            // Display variable the words " Alarm Status"
          client.println("<br />");    //*************************
         
//****************************************************** Displaying The counter total on the webpage ***************************************************
          client.println("<br />");    //*****************************
          client.print("Alarm Counter ");      // Display the words " Alarm Counter"
          client.print(floodcount);            // Display variable the words " Alarm Counter"
          client.println("<br />");    //*************************
          client.println("</html>");
          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");
  }






//**********************************************************************************************************************************************

 
}



tobyb121

At the moment you are skipping over the request from the client, you will need to parse this request to check for the button press and rest the counter accordingly.

PaulS

Code: [Select]
const int floodSensor1 = 2;     // the number of the Flood Sensor pin
const int floodSensor2 = 3;     // the number of the Flood Sensor pin
const int floodSensor3 = 4;     // the number of the Flood Sensor pin
const int floodSensor4 = 5;     // the number of the Flood Sensor pin
const int floodSensor5 = 6;     // the number of the Flood Sensor pin
const int floodSensor6 = 7;     // the number of the Flood Sensor pin
const int floodSensor7 = 8;     // the number of the Flood Sensor pin

An array would make a lot more sense, and use a lot less code.

Code: [Select]
   floodSensorState = HIGH;
The flood condition might be true. But, there is no (single) flood sensor, so it's state can't possibly be HIGH. A more meaningful name is in order.

Code: [Select]
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        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

Send a standard response, regardless of what the client asked for. Not a good plan.

Rich196

Thanks for you help and suggestion. Sorry I am very new to this, could anyone perhaps do be an example code for this button. I am not very good with all the terminology, Especially with this web server stuff as I have never done it before.

Thanks


PaulS

Quote
I am not very good with all the terminology, Especially with this web server stuff as I have never done it before.

Imagine that you work in a fast food restaurant.

A client comes in, and orders a chicken sandwich and onion rings. You cook a cheeseburger and french fries.
The next client wants a tuna sandwich and cole slaw. You cook a cheeseburger and french fries.

See a problem? The "You get it our way!" sign needs to be prominently displayed (right next to the going out of business sign).

Actually paying attention to what the client wants is a good thing.

Rich196

#7
May 17, 2013, 03:46 pm Last Edit: May 17, 2013, 06:13 pm by Rich196 Reason: 1
Test code

Okay, So I have written some code that successfully zeros the variable "counter". See below:

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,99,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 counter = 5; //*********************
int LEDpin = 9;
String readString = String(30);
String state = String(3);

void setup()


{
 // start the Ethernet connection and the server:
 Ethernet.begin(mac, ip);
 server.begin();
 
 //Sets the LEDpin as an output
 pinMode(LEDpin,OUTPUT);
 
 digitalWrite(LEDpin,LOW);
 state = "OFF";
}

void loop()
{
 // listen for incoming clients
 EthernetClient client = server.available();
 if (client) {
   // an http request ends with a blank line
   boolean currentLineIsBlank = true;
   while (client.connected()) {
     if (client.available()) {
       char c = client.read();
       
 
       // 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 (readString.length() < 30) {
         readString.concat(c);
       }
//************************************************************************************Action of button**************************************************************************
       if (c == '\n' && currentLineIsBlank) {
         // send a standard http response header
         int counter1 = readString.indexOf("counter1=");

         if (readString.substring(counter1,counter1+10) == "counter1=T") {
           counter = 0;

         }
//**********************************************************************************Button to reset variable***************************************************************************************
         client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println();

           client.println("<a href=\"./?counter1=T\">reset<a>");
         
         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;
       }
     }
   }

//******************************************************* Print Variable on screen **********************************************************
      client.println("<br />");
      client.println("<br />");
      client.println(counter);
//*********************************************************************************************************************************************      
   // give the web browser time to receive the data
   delay(1);
   readString = "";
   // close the connection:
   client.stop();
 }



}





Rich196

#8
May 17, 2013, 03:50 pm Last Edit: May 17, 2013, 05:55 pm by Rich196 Reason: 1
My main program works. I have also written a test program, for the web button to reset the variable, and it works fine. However when I put the code into the main program it will not work, but it has not broken anything in my main program either. I have put in all the test variables, into the main program.

The variable I am resetting is caller "counter" it is initialized to have a value of 5 on boot up and then the button should reset it to 0.

When I say it does not work. Everything displays on the web server as it should, however when you click the reset button the variable "counter" does not get set to 0.


Code: [Select]


*/
//******************************** Setting up ethernet port
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,99,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);

const int floodSensor1 = 2;    
const int floodSensor2 = 3;    
const int floodSensor3 = 4;    
const int floodSensor4 = 5;    
const int floodSensor5 = 6;    
const int floodSensor6 = 7;    
const int floodSensor7 = 8;    

const int ledPin =  9;

// variables will change:
int floodSensorState = 0;         // variable for reading the floodSensors status
int floodcount = 0;               // variable for storing the total number of flood alerts
int currentState = 0;             // variable for the counter
int previousState = 0;            // variable for the counter


//************************************** Counter reset


int counter = 5; //
//int LEDpin = 9;
String readString = String(30);
String state = String(3);



void setup() {
//*************************************************************Ehternet setup
 
  // Open serial communications and wait for port to open:
 Serial.begin(9600);
  while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
 }


 // start the Ethernet connection and the server:
 Ethernet.begin(mac, ip);
 server.begin();
 Serial.print("server is at ");
 Serial.println(Ethernet.localIP());

//*********************************************Working serial only
    // initialize serial communication at 9600 bits per second:
//  Serial.begin(9600);

  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the flood Sensor pin as an input:
  pinMode(floodSensor1, INPUT);
  pinMode(floodSensor2, INPUT);
  pinMode(floodSensor3, INPUT);
  pinMode(floodSensor4, INPUT);
  pinMode(floodSensor5, INPUT);
  pinMode(floodSensor6, INPUT);
  pinMode(floodSensor7, INPUT);
// pinMode(floodSensor8, INPUT);
}
//.........................Reading input sensors in to variable
void loop(){
 

 
// Read each of the digital inputs, using the or function (||) if any of them are on it will make floodsensorstate variable high.
 
if ( digitalRead(floodSensor1) || digitalRead(floodSensor2) || digitalRead(floodSensor3) || digitalRead(floodSensor4) || digitalRead(floodSensor5) || digitalRead(floodSensor6) || digitalRead(floodSensor7) )
{
  // make floodsensorstate variable high.    
  floodSensorState = HIGH;
  currentState = 1;
}
else
{
 // make floodsensorstate variable low.
  floodSensorState = LOW;
  currentState = 0;
}
//***************************** Using variable to switch on output
 // check floodsensorstate variable if high turn led on.
if (floodSensorState == HIGH) {    
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }  
//************************************************ counter

 if(currentState != previousState){     // current state not equal to previous state
 if(currentState == 1){                 // current state equal to 1
   floodcount = floodcount + 1;         // add one to counter
 }
}
 previousState = currentState;        // current state equal to previous state delay before re checking
delay(250);


//********************************************* Output of floodSensorState to serial
 // Print to serial port, the Flood alarm value after the text" Flood Alarm" to decribe what it it.

 String stringOne = "Flood Alarm: ";
 String stringThree = stringOne + floodSensorState;
 Serial.println(stringThree);
 
// Print to serial port, the counter total value after the text" Counter total" to decribe what it it.

 String stringTwo = "Counter Total: ";
 String stringFour = stringTwo + floodcount;
 Serial.println(stringFour);
 
//*********************************************Ethernet code

 // listen for incoming clients
 EthernetClient client = server.available();
 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();
       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");
         client.println();
         client.println("<!DOCTYPE HTML>");
         client.println("<html>");
//***********************************************Web page tile        
              client.println("<br />");
         client.println("<br />");
         
                   // add a meta refresh tag, so the browser pulls again every 2 seconds:
//          client.println("<meta http-equiv=\"refresh\" content=\"2\">");
         // output the value of each analog input pin
         
         for (int analogChannel = 2; analogChannel < 9; analogChannel++) {    //Reads didgial inputs 2, 8. Each time is goes round the loop it read the next input.
           int sensorReading = digitalRead(analogChannel);
           client.print("Sensor input ");
           client.print(analogChannel - 1); // changes the text field so that input 2, its title sensor input 1!
           client.print(" is ");
           client.print(sensorReading);
           client.println("<br />");      
         }
//****************************************** Displaying The alarm status on the webpage
         client.println("<br />");           //*****************************
         client.print("Alarm Status ");      // Display the words " Alarm Status"
         client.print(floodSensorState);     // Display variable the words " Alarm Status"
         client.println("<br />");          
         
//****************************************************** Displaying The counter total on the webpage
       
         client.println("<br />");            
         client.print("Alarm Counter ");      // Display the words " Alarm Counter"
         client.print(floodcount);            // Display variable the words " Alarm Counter"
         client.println("<br />");            
         client.println("</html>");
//*********************************************************Counter reset code          
     
     
       // 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 (readString.length() < 30) {
         readString.concat(c);
       }
//*************************************************************Action of button
       if (c == '\n' && currentLineIsBlank) {
         // send a standard http response header
         int counter1 = readString.indexOf("counter1=");

         if (readString.substring(counter1,counter1+10) == "counter1=T") {
           counter = 0;

         }
//****************************Button to reset variable
//          client.println("HTTP/1.1 200 OK");
//          client.println("Content-Type: text/html");
//          client.println();

           client.println("<a href=\"./?counter1=T\">reset<a>");
         

//************************ Print Variable on screen
      client.println("<br />");    
      client.println("<br />");    
      client.println(counter);    
   
   // give the web browser time to receive the data
   delay(1);
   // close the connection:
   client.stop();
   Serial.println("client disonnected");
 }

//*************************CLoses off the program and webpage
//          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;
       }
     }
   }

}
}



Thanks

Arrch


it does not work any more


Sorry, that is not a proper way of describing a problem.

Rich196



it does not work any more


Sorry, that is not a proper way of describing a problem.


My main program works. I have also written a test program, for the web button to reset the variable, and it works fine. However when I put the code into the main program it will not work, but it has not broken anything in my main program either. I have put in all the test variables, into the main program.

The variable I am resetting is caller "counter" it is initialized to have a value of 5 on boot up and then the button should reset it to 0.

When I say it does not work. Everything displays on the web server as it should, however when you click the reset button the variable "counter" does not get set to 0.

Arrch

Everything displays on the web server as it should, however when you click the reset button the variable "counter" does not get set to 0.


So the reset to 0 occurs in this part of the code:

Code: [Select]
if (readString.substring(counter1,counter1+10) == "counter1=T") {
  counter = 0;
}


If what you're describing is true, then this if statement isn't returning true. So, what is the value of the substring when its not true?

Rich196


Everything displays on the web server as it should, however when you click the reset button the variable "counter" does not get set to 0.


So the reset to 0 occurs in this part of the code:

Code: [Select]
if (readString.substring(counter1,counter1+10) == "counter1=T") {
  counter = 0;
}


If what you're describing is true, then this if statement isn't returning true. So, what is the value of the substring when its not true?


That is correct. There is not other value, as I did not need it when I tested it the "test program" I wrote about.

All I want is a button to reset a variable called "counter" When the value stored in" counter" is greater than 0.

Thank you for your time!!

Arrch


That is correct. There is not other value, as I did not need it when I tested it the "test program" I wrote about.

All I want is a button to reset a variable called "counter" When the value stored in" counter" is greater than 0.

Thank you for your time!!


I asked a question that you didn't answer. I'll rephrase:

What is the value being returned by this statement:

Code: [Select]
readString.substring(counter1,counter1+10)

Rich196



That is correct. There is not other value, as I did not need it when I tested it the "test program" I wrote about.

All I want is a button to reset a variable called "counter" When the value stored in" counter" is greater than 0.

Thank you for your time!!


I asked a question that you didn't answer. I'll rephrase:

What is the value being returned by this statement:

Code: [Select]
readString.substring(counter1,counter1+10)


I am sorry but I don't think I understand the question. I am really new to this. I found some example code an manipulated it to do what I wanted.

From what I think I understand. It is looking for the final character to be returned by the button. The tenth, hence the +10.

This is T, for true:

if (readString.substring(counter1,counter1+10) == "counter1=T").

This is the test code I wrote for making a button to zero a variable. This works perfectly in isolation here, but does not work when I put it into my main program. It no longer zeros the variable "counter":

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,99,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 counter = 5; //*********************
int LEDpin = 9;
String readString = String(30);
String state = String(3);

void setup()


{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
 
  //Sets the LEDpin as an output
  pinMode(LEDpin,OUTPUT);
 
  digitalWrite(LEDpin,LOW);
  state = "OFF";
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
       
   
        // 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 (readString.length() < 30) {
          readString.concat(c);
        }
//************************************************************************************Action of button**************************************************************************
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          int counter1 = readString.indexOf("counter1=");

          if (readString.substring(counter1,counter1+10) == "counter1=T") {
            counter = 0;

          }
//**********************************************************************************Button to reset variable***************************************************************************************
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

            client.println("<a href=\"./?counter1=T\">reset<a>");
         
          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;
        }
      }
    }

//******************************************************* Print Variable on screen **********************************************************
       client.println("<br />");
       client.println("<br />");
       client.println(counter);
//*********************************************************************************************************************************************       
    // give the web browser time to receive the data
    delay(1);
    readString = "";
    // close the connection:
    client.stop();
  }

}

Go Up