energy monitor

With this project you can monitor your energy usage and create real-time stats.

Needed

  • Arduino
  • Ethernet shield
  • Light Sensor
  • Apache, PHP and Mysql server

  • Code

    1. /*
    2.  *
    3. Circuit:
    4.  * Ethernet shield attached to pins 10, 11, 12, 13
    5.  * input light sensor to analog pin 0
    6.  *
    7.  
    8. */
    9.  
    10. #include "meterkast.h"
    11. #include <SPI.h>
    12. #include <Ethernet.h>
    13.  
    14.  
    15.  
    16. // Enter a MAC address and IP address for your controller below.
    17. // The IP address will be dependent on your local network:
    18. byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    19. byte ip[] = { 192,168,0,177 };
    20. byte gw[] = {192,168,0,254};
    21. byte subnet[] = {255,255,255,0};
    22. byte server[] = { 192,168,0,254 }; // server
    23.  
    24. // Initialize the Ethernet client library
    25. // with the IP address and port of the server
    26. // that you want to connect to (port 80 is default for HTTP):
    27. Client client(server, 80);
    28.  
    29. int waitSecondsForSend = 60; //1 minute
    30. int cycleSeconds=0;
    31. int cycleWait=5;
    32. int lightThresshold=600; //minimaal analoge waarde voor licht
    33. int lightLowThressholdCounter=1; // vanaf halve seconde flits
    34. int lightHighThressholdCounter=3; // tot 1 sec = flits
    35. int cycleCounter=0;
    36. int pulseCounter=0;
    37. int lightCounter=0;//aantal cycles met licht
    38. boolean lightActive =false;
    39.  
    40. void setup() {
    41.  
    42.         // start the serial library:
    43.         Serial.begin(9600);
    44.  
    45.         // start the Ethernet connection:
    46.         Ethernet.begin(mac, ip, gw, subnet);
    47.         // give the Ethernet shield a second to initialize:
    48.         delay(1000);
    49.         if (client.connect()) {
    50.                 Serial.println("ethernet connected");
    51.         } else {
    52.                 // if you didn’t get a connection to the server:
    53.                 Serial.println("ethernet connection failed");
    54.         }
    55.  
    56.  
    57.         // if you get a connection, report back via serial:
    58.  
    59. }
    60.  
    61. void loop() {
    62.   int sensorValue = analogRead(A0);
    63.  
    64.   if (sensorValue > lightThresshold){
    65.           lightCounter++;
    66.   } else {
    67.           if(lightCounter > 5 && lightCounter < 10 ){
    68.                   Serial.print("light pulse gevonden ");
    69.                   Serial.println (lightCounter);
    70.                   pulseCounter++;
    71.  
    72.                   if (cycleSeconds >= waitSecondsForSend){
    73.                           if(!client.connected()){
    74.                                           client.stop();
    75.                                         if (client.connect()) {
    76.                                                 Serial.println("ethernet connected");
    77.                                         } else {
    78.                                                 // if you didn’t get a connection to the server:
    79.                                                 Serial.println("ethernet connection failed");
    80.                                         }
    81.                           }
    82.                           if (client.connected()) {
    83.                                   Serial.print ("Verzenden van data, aantal pulsen: ");
    84.                                   Serial.println (pulseCounter);
    85.                                   // Make a HTTP request:
    86.                                   client.print("GET /arduino/meterkast.php?pulse=");
    87.                                   client.print(pulseCounter);
    88.                                   client.println(" HTTP/1.0");
    89.                                   client.println();
    90.                           } else {
    91.                                   Serial.print ("Niet Verzendon van data, aantal pulsen: ");
    92.                                   Serial.println (pulseCounter);
    93.  
    94.                           }
    95.                           //altijd resetten
    96.                           cycleSeconds=0;
    97.                           pulseCounter=0;
    98.                   }
    99.  
    100.           }
    101.  
    102.           lightCounter=0;
    103.           lightActive=false;
    104.   }
    105.  
    106.   //clear data
    107.   if (client.available()) {
    108.       char c = client.read();
    109.       //Serial.print(c);
    110.   }
    111.  
    112.   //debug
    113.   //Serial.println(sensorValue, DEC);
    114.  
    115.   //counting
    116.   cycleCounter = cycleCounter+cycleWait;
    117.   if (cycleCounter == 1000){
    118.           cycleSeconds++;
    119.           cycleCounter=0;
    120.  
    121.           //debug
    122.           //if(cycleSeconds==10){
    123.           //lightCounter=8;
    124.           //pulseCounter=5;
    125.           //}
    126.           //Serial.println (cycleSeconds);
    127.   }
    128.  
    129.   //wait a little bit
    130.    delay(cycleWait);
    131.  
    132.  
    133.  }

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *


    8 + seven =

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>