element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Need to learn how to code hour meter.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 18 replies
  • Answers 2 answers
  • Subscribers 397 subscribers
  • Views 2829 views
  • Users 0 members are here
  • meters
  • help
  • hour
  • arduino
Related

Need to learn how to code hour meter.

Former Member
Former Member over 11 years ago

Hello,

     I am new to Arduino and I am having a hard time figuring out how to create a hour meter. I am using an uno with an Ethernet/SD shield. I have that hooked up to sump pump and a transducer. I am able to see the liquid level and control the pump via web page. However, I would like to create a hour meter that records the pump run-time hours, and prints it on to the web page. Here is my code,

 

#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,1,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);
*/ LEDpin is used to control relay
int LEDpin = 1;
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
  //LEDpin is actually for relay control
  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);
        }

        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          int LED = readString.indexOf("LED=");

          if (readString.substring(LED,LED+5) == "LED=T") {
            digitalWrite(LEDpin,HIGH);
            state = "ON";
          }
          else if (readString.substring(LED,LED+5) == "LED=F") {
            digitalWrite(LEDpin,LOW);
            state = "OFF";
          }
          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: 3");  // refresh the page automatically every 3 sec
        
          client.println();

  client.println("<!DOCTYPE HTML>");
          client.println("<html><body bgcolor='Gray'>");
          // Pump Name
          client.println("<center><H1>KO6</H1></center>");
          // output the value of the analog input pin
          for (int analogChannel = 0; analogChannel < 1; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            sensorReading = map(sensorReading, 0, 1024, 0, 300);
            client.print("<center> Liquid Level ");
            client.print(" is ");
            client.print ("<b><font color='blue'>");
            client.print(sensorReading);
            client.print ("</b></font color>");
            client.println("<br />");
            client.print(" Hours:  ");
            //client.print(runtime)
            client.println("<br />");     
          }
        
         
          client.print("Pump is ");
          client.print(state);
          client.print("<br><br>");
         
          if (state == "ON") {
            client.println("<a href=\"./?LED=F\">Turn Off<a>");
          }
          else {
            client.println("<a href=\"./?LED=T\">Turn On<a>");
          }
           client.print("</center>");
           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);
    readString = "";
    // close the connection:
    client.stop();
  }

 

Sorry if my code is sloppy, I am still learning, if anyone could help me it would be greatly appreciated.

Thanks.

  • Sign in to reply
  • Cancel
Parents
  • Former Member
    0 Former Member over 11 years ago

    Are you looking to run a cumulative counter, or just keep track of the time that the pump runs in one "session"?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    jbix,

    I am looking to run a cumulative hour meter.

    I have modified my code below. but I am still having problems. all that I am getting is 0 for the hour reading.

    Not sure what I am doing wrong.

     

     

     

     

    [code]
    #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,1,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);
    // LEDpin is used to control relay
      int LEDpin = 1;
        //Declarations for the hour meter
        int startTime = millis();
        int stopTime = millis();
        int runTime =((stopTime) - (startTime))/1000;
        int onTime =+ runTime;

          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
        //LEDpin is actually for relay control
        pinMode(LEDpin,OUTPUT);
        //Sets the STATUSpin as an input
        //STATUSpin reads a signal to tell whether the pump is on or off


          //reset the pump state
          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);
            }

            if (c == '\n' && currentLineIsBlank) {
              // send a standard http response header
              int LED = readString.indexOf("LED=");
             

              if (readString.substring(LED,LED+5) == "LED=T") {
                digitalWrite(LEDpin,HIGH);
                state = "ON";
              }
              else if (readString.substring(LED,LED+5) == "LED=F") {
                digitalWrite(LEDpin,LOW);
                state = "OFF";
              }
             
              if (digitalRead (LEDpin) == HIGH){
                startTime = millis();
              }
             
        if (digitalRead (LEDpin) == LOW){
                stopTime = millis();  
       
        }
            
            
            
            
            
            
            
              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: 3");  // refresh the page automatically every 3 sec
            
              client.println();

      client.println("<!DOCTYPE HTML>");
              client.println("<html><body bgcolor='Gray'>");
              // Pump Name
              client.println("<center><H1>KO6</H1></center>");
              // output the value of the analog input pin
              for (int analogChannel = 0; analogChannel < 1; analogChannel++) {
                int sensorReading = analogRead(analogChannel);
                sensorReading = map(sensorReading, 0, 1024, 0, 300);
                client.print("<center> Liquid Level ");
                client.print(" is ");
                client.print ("<b><font color='blue'>");
                client.print(sensorReading);
                client.print ("</b></font color>");
                client.println("<br />");
                client.print(" Hours:  ");
               
                client.print(onTime);
                client.println("<br />");     
              }
            
             
              client.print("Pump is ");
              client.print(state);
              client.print("<br><br>");
             
              if (state == "ON") {
                client.println("<a href=\"./?LED=F\">Turn Off<a>");
              }
              else {
                client.println("<a href=\"./?LED=T\">Turn On<a>");
              }
               client.print("</center>");
               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);
        readString = "";
        // close the connection:
        client.stop();
      }
        }

    [/code]

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    I am newbie here but is the problem that you have put this line  " int onTime =+ runTime;" in the declaration section.  It is only ever read once by the program.  Should it not go into the loop()? 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    I am still a newbie too,

    my theory was to declare "int onTime =+ runTime ;" in the begin so I could use "client.print(onTime);" when I want to out put the run hours on to the webpage.


    I tried " client.print(onTime =+ runTime);" in the loop but I still get a 0 reading .

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    Scott,

     

    When you declare onTime in the beginning of your code, you are setting it to zero:

     

    //Declarations for the hour meter

        int startTime = millis();  <-- setting start time to current millis()

        int stopTime = millis();  <-- setting stop time (probably only a few microseconds later, depending on clock speed)

        int runTime =((stopTime) - (startTime))/1000;  <-- setting runTime to pretty much zero

        int onTime =+ runTime;  <-- declaring onTime and incrementing it by pretty much zero (also, I believe the increment should be += as opposed to =+

     

    After which, you do not do anything with onTime, other then printing it to the client, so it remains at zero.  You would need to do the math for calculating the current run time somewhere in the loop, probably after turning the pump off, or at a point in the code, you could get the millis() and subtract start time from that and set runTime to equal that, then increment the onTime after shutting the pump off.  In order for this to be a true cumulative timer, however, you will need to store this is some sort of non-volatile memory, otherwise every time you re-connect to the Arduino, or every time it loses power, you will re-set the variables.  I would also suggest getting rid of:


    if (digitalRead (LEDpin) == HIGH){ startTime = millis();  }

    if (digitalRead (LEDpin) == LOW){  stopTime = millis();  }

     

    And moving the startTime = millis() statement to the same if statement where you turn the pump state to on, and the same with the stopTime, except moving it to where you turn it off:

     

    if (readString.substring(LED,LED+5) == "LED=T") {
      digitalWrite(LEDpin,HIGH);
    state = "ON";

    startTime = millis();
    }
    else if (readString.substring(LED,LED+5) == "LED=F") {
    digitalWrite(LEDpin,LOW);
    state = "OFF";

    stopTime = millis()
    }


    This is just to help get moving forward.  You may want to hard-code values into some of your variables for debugging, just to see if you can get a value to display for time.  One important question though, do you plan on ONLY running the pump while connected to an Ethernet client?


    Hope this helps

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    jbix,

    I didn't see your post until later. I have tried your version of the code.

    [code]

       if (readString.substring(LED,LED+5) == "LED=T") {

                digitalWrite(LEDpin,HIGH);

                state = "ON";

                startTime = millis();

               runTime =(stopTime - startTime)/1000;

               onTime =+ runTime; //<-----------when I use =+ it seems to count in increments of 3 seconds which seems to be more accurate since the webpage refreshes every 3 seconds vs with += it seems to count in much bigger increments.

              }

             

              else if (readString.substring(LED,LED+5) == "LED=F") {

                digitalWrite(LEDpin,LOW);

                state = "OFF";

                stopTime = millis()

                }

    [/code]

    this is working to turn on the timer when the pump is on. However, I am still having a problem with it only counting from 30 to -30 and back again.

     

    I do plan on saving the run hours to the eprom or to a SD card if it is not to much trouble once I have the timer issue resolved.

     

    to answer your question about running the pump. I do not plan on only running it over the internet. the current pump system has a controller that kicks on the pump automatically when the level gets high. I just would like to b able to control it remotely for troubleshooting purposes.

     

    I really appreciate both of your guys time. I am learning a lot and still have a long way to go.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    Scott,

     

    I have modified your code as you will see below.  I hope this helps get you pointed in the right direction.  What Peter has submitted is also a great way to go, he is actually using a sense line to detect the state of the pump.  Currently, your code will only generate time accumulated when you start/stop the pump with the web interface.  You will need to introduce a hardware/software solution if you desire the Arduino to track pump time when it is turned on/off by the controller, such as using a sense line as Peter shows, but perhaps using an interrupt for the Arduino to detect when the pump state changes.  I haven't tested the code, but I suggest comparing your code with my proposed changes and understanding what the changes will do before implementing them and turning your code into spaghetti.

     

    #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,1,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);

     

    // LEDpin is used to control relay

    int LEDpin = 1;

     

    // Variables for use with the hour meter

    int startTime = 0;

    int stopTime = 0;

    int runTime = 0;

    int onTime = 0;;

     

    // readString used for incoming data from Ethernet client

    String readString = String(30);

    String state = String(3);

     

    // Strings to hold the un-changing portions of the webpage

    String output_1 = "<!DOCTYPE HTML><HTML><BODY bgcolor='Gray'><CENTER><H1>K06</H1> Liquid Level is <B><FONT color='blue'>";

     

    void setup() {

      // start the Ethernet connection and the server:

      Ethernet.begin(mac, ip);

      server.begin();

     

      //Sets the LEDpin as an output

      //LEDpin is actually for relay control

      pinMode(LEDpin,OUTPUT);

     

      // Turn the pump off initially

      state = "OFF";

     

      // Enter code here to recover total run time from EEPROM

     

    }

     

    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()) {

       

        // Read a character from the client

            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);

            }

     

            if (c == '\n' && currentLineIsBlank) {

     

              // send a standard http response header

              int LED = readString.indexOf("LED=");

            

     

              if (readString.substring(LED,LED+5) == "LED=T") {

                digitalWrite(LEDpin,HIGH);

                state = "ON";

            startTime = millis();

              }

              else if (readString.substring(LED,LED+5) == "LED=F") {

                digitalWrite(LEDpin,LOW);

                state = "OFF";

            stopTime = millis();

            runTime = stopTime - startTime;

            onTime += runTime;

            runTime = 0;

              }

     

          // Moved the sensor read up to before output is sent.

          int sensorReading = analogRead(analogChannel);

              sensorReading = map(sensorReading, 0, 1024, 0, 300);

     

          //Send the output webpage

     

          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: 3");  // refresh the page automatically every 3 sec

           

              client.println();

     

          // Print the first part of the page (up to 'Liquid Level is')

          client.print(output_1);

     

              // Print the liquid level

              client.print(sensorReading);

     

          // Print the next part of the page (up to 'Hours:')

              client.print("</B></FONT color><br />Hours: ");

     

          // Print the total hours

          client.print(onTime);

     

          // Print the next part of the page (up to 'Pump is')

              client.print("<br />Pump is ");

     

          // Print the pump state

          client.print(state);

     

          client.println("<br /><br />");

     

            

              if (state == "ON") {

                client.println("<a href=\"./?LED=F\">Turn Off<a>");

              }

              else {

                client.println("<a href=\"./?LED=T\">Turn On<a>");

              }

     

              client.print("</CENTER></HTML>");

     

              break;

            } // End of if (c == '\n' && currentLineIsBlank)

     

            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;

            }

          } // End of if(client.available())

        } // End of while(client.connected())

     

        // give the web browser time to receive the data

        delay(1);

     

        // Clear readString

        readString = "";

     

        // close the connection:

        client.stop();

      } // End of if(client)

    } // End of loop

     

    Hope this gets you moving forward.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    jbix,

    you bring up a great point, I do need a sense line. I am planning on connecting to a 110v running indicator light with a relay on my pump panel. that way the arduino will be able to record hours if the pump is running in auto or remotely controlled with the webpage. I also think that I am going to use a relay with a built in hardware timer to turn on the pump from the webpage. That way it will only run the pump for a shorter period of time. just incase I forget to turn it off or lose connection and run the pump dry and burn it up. lol

     

    Thank you for your help

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago in reply to Former Member

    All of what you describe can be achieved in software of course but as you said, your new to this so walk before you run, the nice thing about arduino echo system is it is easy to update the software as you learn more skills

     

    Hope you are successful with your project, glad I was able to help you understand the information I provided

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago in reply to Former Member

    All of what you describe can be achieved in software of course but as you said, your new to this so walk before you run, the nice thing about arduino echo system is it is easy to update the software as you learn more skills

     

    Hope you are successful with your project, glad I was able to help you understand the information I provided

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube