Energy monitor
Posted on February 19, 2012
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
/*
*
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* input light sensor to analog pin 0
*
*/
#include "meterkast.h"
#include
#include
// 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 };
byte ip[] = { 192,168,0,177 };
byte gw[] = {192,168,0,254};
byte subnet[] = {255,255,255,0};
byte server[] = { 192,168,0,254 }; // server
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Client client(server, 80);
int waitSecondsForSend = 60; //1 minute
int cycleSeconds=0;
int cycleWait=5;
int lightThresshold=600; //minimaal analoge waarde voor licht
int lightLowThressholdCounter=1; // vanaf halve seconde flits
int lightHighThressholdCounter=3; // tot 1 sec = flits
int cycleCounter=0;
int pulseCounter=0;
int lightCounter=0;//aantal cycles met licht
boolean lightActive =false;
void setup() {
// start the serial library:
Serial.begin(9600);
// start the Ethernet connection:
Ethernet.begin(mac, ip, gw, subnet);
// give the Ethernet shield a second to initialize:
delay(1000);
if (client.connect()) {
Serial.println("ethernet connected");
} else {
// if you didn’t get a connection to the server:
Serial.println("ethernet connection failed");
}
// if you get a connection, report back via serial:
}
void loop() {
int sensorValue = analogRead(A0);
if (sensorValue > lightThresshold){
lightCounter++;
} else {
if(lightCounter > 5 && lightCounter < 10 ){
Serial.print("light pulse gevonden ");
Serial.println (lightCounter);
pulseCounter++;
if (cycleSeconds >= waitSecondsForSend){
if(!client.connected()){
client.stop();
if (client.connect()) {
Serial.println("ethernet connected");
} else {
// if you didn’t get a connection to the server:
Serial.println("ethernet connection failed");
}
}
if (client.connected()) {
Serial.print ("Verzenden van data, aantal pulsen: ");
Serial.println (pulseCounter);
// Make a HTTP request:
client.print("GET /arduino/meterkast.php?pulse=");
client.print(pulseCounter);
client.println(" HTTP/1.0");
client.println();
} else {
Serial.print ("Niet Verzendon van data, aantal pulsen: ");
Serial.println (pulseCounter);
}
//altijd resetten
cycleSeconds=0;
pulseCounter=0;
}
}
lightCounter=0;
lightActive=false;
}
//clear data
if (client.available()) {
char c = client.read();
//Serial.print(c);
}
//debug
//Serial.println(sensorValue, DEC);
//counting
cycleCounter = cycleCounter+cycleWait;
if (cycleCounter == 1000){
cycleSeconds++;
cycleCounter=0;
//debug
//if(cycleSeconds==10){
//lightCounter=8;
//pulseCounter=5;
//}
//Serial.println (cycleSeconds);
}
//wait a little bit
delay(cycleWait);
}