Created
February 16, 2016 20:43
-
-
Save koffienl/0e0c1a83cb9b8d53ca1a to your computer and use it in GitHub Desktop.
interrupt pulse counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include <ESP8266WebServer.h> | |
#include <ESP8266mDNS.h> | |
const char* ssid = "xx"; | |
const char* password = "xx.com"; | |
ESP8266WebServer server(80); | |
// http://www.engineerathome.com/elektronica/meet+je+energieverbruik+met+arduino/9 | |
// STROOM | |
const int Stroom_C = 1000; // meetconstante elektra, 375 rondjes per kWh | |
const int drempelStroom = 1000; // drempelwaarde stroomsensor | |
int sensorStroom = 0; // waarde van de LDR | |
boolean pulseStroom = false; // puls gedetecteerd stroom | |
unsigned long pulseCountS = 0; // pulsenteller Stroom | |
unsigned long prevMillisS = 0; // onthoud starttijd Stroom | |
unsigned long pulseTimeS = 0; // tijd die 1 puls duurt Stroom | |
int curWatts = 0; // berekende huidige verbruik in Watt | |
int totalWh = 0; // totaal aantal Wh | |
int ADC; | |
int pulseCount; | |
void setup() { | |
Serial.begin(115200); | |
delay(500); | |
WiFi.begin(ssid, password); | |
Serial.println(""); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
server.on("/", handleRoot); | |
server.onNotFound(handleNotFound); | |
server.begin(); | |
Serial.println("HTTP server started"); | |
pinMode(13, INPUT); | |
attachInterrupt(13, handle_interrupt, FALLING); | |
} | |
void loop() { | |
server.handleClient(); | |
/* | |
sensorStroom = analogRead(A0); | |
if (sensorStroom > drempelStroom) | |
{ | |
pulseStroom = true; | |
} | |
if (sensorStroom < drempelStroom && pulseStroom) | |
{ | |
pulseStroom = false; | |
pulseCountS++; | |
pulseTimeS = (millis() - prevMillisS); | |
prevMillisS = millis(); | |
curWatts = (( 3600000 / Stroom_C ) * 1000 ) / pulseTimeS; | |
totalWh = (pulseCountS * 1000) / Stroom_C; // LET OP Wh! | |
Serial.print("curWatts: "); | |
Serial.println(curWatts); | |
Serial.print("totalWh: "); | |
Serial.println(totalWh); | |
Serial.print("pulsen tot nu toe: "); | |
Serial.println(pulseCountS); | |
} | |
//delay(500); | |
yield(); | |
*/ | |
} | |
void handleRoot() { | |
String page = "curWatts: " + String(curWatts) + "<br>"; | |
page += "totalWh: " + String(totalWh) + "<br>"; | |
page+= "pulseCountS: " + String(pulseCountS) + "<br>"; | |
server.send(200, "text/plain", page); | |
} | |
void handleNotFound(){ | |
String message = "File Not Found\n\n"; | |
message += "URI: "; | |
message += server.uri(); | |
message += "\nMethod: "; | |
message += (server.method() == HTTP_GET)?"GET":"POST"; | |
message += "\nArguments: "; | |
message += server.args(); | |
message += "\n"; | |
for (uint8_t i=0; i<server.args(); i++){ | |
message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; | |
} | |
server.send(404, "text/plain", message); | |
} | |
void handle_interrupt() | |
{ | |
//Serial.println("ik detecteer een interrupt!"); | |
//pulseStroom = false; | |
pulseCountS++; | |
pulseTimeS = (millis() - prevMillisS); | |
prevMillisS = millis(); | |
//curWatts = (( 3600000 / Stroom_C ) * 1000 ) / pulseTimeS; | |
curWatts = (( 3600000 / Stroom_C ) * 1000 ); | |
totalWh = (pulseCountS * 1000) / Stroom_C; // LET OP Wh! | |
Serial.print("curWatts: "); | |
Serial.println(curWatts); | |
Serial.print("totalWh: "); | |
Serial.println(totalWh); | |
Serial.print("pulsen tot nu toe: "); | |
Serial.println(pulseCountS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment