commit c2212065711d17cdc067fb740951f588fa00569b Author: Aaron Fischer Date: Wed May 18 22:29:07 2016 +0200 Initial insert diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ec75a8e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +settings.h diff --git a/doc/ACS712-Datasheet.pdf b/doc/ACS712-Datasheet.pdf new file mode 100644 index 0000000..8f25206 Binary files /dev/null and b/doc/ACS712-Datasheet.pdf differ diff --git a/doc/GARO2007_EUde__01777-20.pdf b/doc/GARO2007_EUde__01777-20.pdf new file mode 100644 index 0000000..44b94fa Binary files /dev/null and b/doc/GARO2007_EUde__01777-20.pdf differ diff --git a/doc/d1-mini-esp8266-board-sh_fixled.jpg b/doc/d1-mini-esp8266-board-sh_fixled.jpg new file mode 100644 index 0000000..a6ae70a Binary files /dev/null and b/doc/d1-mini-esp8266-board-sh_fixled.jpg differ diff --git a/doc/d1_mini.pdf b/doc/d1_mini.pdf new file mode 100644 index 0000000..5233f60 Binary files /dev/null and b/doc/d1_mini.pdf differ diff --git a/pumpcheck.ino b/pumpcheck.ino new file mode 100644 index 0000000..be983c5 --- /dev/null +++ b/pumpcheck.ino @@ -0,0 +1,118 @@ +#include +#include +#include +#include "settings.h" + +int16_t idleValue = 0; +int16_t sensorValue = 0; +int8_t cutOffThreshold = cutOffTime; +int16_t cooldownRemaining = 0; +ESP8266WiFiMulti wifi; +Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT); + + +void setup() { + pinMode(D1, OUTPUT); // Relais + pinMode(D2, OUTPUT); // Status-LED + // A0 = Current sensor + + digitalWrite(D1, LOW); + digitalWrite(D2, LOW); + + // Connect to wifi + wifi.addAP(WIFI_SSID, WIFI_PASSWORD); + while (wifi.run() != WL_CONNECTED) { + digitalWrite(D2, !digitalRead(D2)); + delay(200); + } + + // Open DB connecton + digitalWrite(D2, LOW); + influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS); + delay(2000); + + // Open serial connection for debugging + if (DEBUG) { + Serial.begin(115200); + delay(500); + } + + digitalWrite(D2, HIGH); + + if (DEBUG) Serial.println("Ready for action!"); +} + +void loop() { + delay(loopTime*1000); + + // Callibrate the idle value + if (idleValue == 0) { + if (DEBUG) Serial.println("Calibrate ..."); + uint16_t samples; + for (uint8_t i=0; i<30; i++) { + samples += analogRead(A0); + digitalWrite(D2, !digitalRead(D2)); + delay(500); + } + + idleValue = round(samples / 30); + if (DEBUG) Serial.println("Idle value: " + String(idleValue)); + digitalWrite(D2, HIGH); + } + + // Need to cutoff the power to the pump? + if (cutOffThreshold <= 0) { + digitalWrite(D1, HIGH); + digitalWrite(D2, LOW); + cooldownRemaining = cooldownTime - loopTime; + cutOffThreshold = cutOffTime; + + if (DEBUG) Serial.println("The pump is cutoff, because it is running to long"); + + return; + } + + // Cooldown phase? + if (cooldownRemaining > 0) { + digitalWrite(D2, LOW); + cooldownRemaining -= loopTime; + influxdb.write("pump-action cutoff=1"); + + if (DEBUG) Serial.println("Cooldown phase ... " + String(cooldownRemaining) + " seconds remaining."); + + return; + } + + digitalWrite(D1, LOW); + digitalWrite(D2, HIGH); + + // Read the sensor + sensorValue = 0; + for (uint8_t i=0; i<5; i++) { + sensorValue += analogRead(A0); + Serial.println(sensorValue); + delay(10); + } + sensorValue = round(sensorValue / 5); + + + if (DEBUG) Serial.println("Sensor value: " + String(sensorValue)); + + // The pump is on! + if (sensorValue > idleValue+10 || sensorValue < idleValue-10) { + influxdb.write("pump-action current=" + String(sensorValue)); + cutOffThreshold -= loopTime; + digitalWrite(D2, !digitalRead(D2)); + + + if (DEBUG) { + Serial.println("Write to Influxdb: " + String(sensorValue)); + Serial.println(influxdb.response() == DB_SUCCESS ? "Success" : "Failed"); + } + + // The pump is off + } else { + cutOffThreshold = cutOffTime; + digitalWrite(D2, HIGH); + } +} diff --git a/settings.h.defaults b/settings.h.defaults new file mode 100644 index 0000000..2c15d7f --- /dev/null +++ b/settings.h.defaults @@ -0,0 +1,13 @@ +const bool DEBUG = false; + +const char *WIFI_SSID = "sssid"; +const char *WIFI_PASSWORD = "XXX"; +const char *INFLUXDB_HOST = "host"; +const uint16_t INFLUXDB_PORT = 80; +const char *INFLUXDB_DB = "db"; +const char *INFLUXDB_USER = "user"; +const char *INFLUXDB_PASS = "XXX"; + +uint8_t loopTime = 1; // Time in sec. how often the pump is checked +uint16_t cutOffTime = 60 * 10; // Time in sec. how long the pump is running max +uint16_t cooldownTime = 60 * 30; // Time in sec. the cutOffTime is canceled