From 71e58773cbdaada0d870eab412971052e115d991 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 11 Jul 2020 10:34:06 +0200 Subject: [PATCH] Light will now turn on if lx below minLX also color can be changed based on nanoMeters of prefered light --- src/connections.cpp | 3 ++- src/header.h | 15 ++++++++--- src/lightChecker.cpp | 62 +++++++++++++++++++++++++++++++++----------- src/peripherals.cpp | 13 ++++++++++ 4 files changed, 73 insertions(+), 20 deletions(-) diff --git a/src/connections.cpp b/src/connections.cpp index 4abc8a5..87eaff3 100644 --- a/src/connections.cpp +++ b/src/connections.cpp @@ -109,7 +109,8 @@ Serial.println("receiving light treshold..."); DeserializationError err = deserializeJson(doc, payload); if (err == DeserializationError::Ok) { int nm = doc["nm"]; - setValueNM(nm); + int minLX = doc["minLX"]; + setLightningProperties(nm, minLX); } else { Serial.println(err.c_str()); } diff --git a/src/header.h b/src/header.h index 1a34635..2df6a10 100644 --- a/src/header.h +++ b/src/header.h @@ -40,11 +40,17 @@ #define PIN_VALVE 32 #define MAX_VALVE_TIMEOUT 10000 -// LED +// STATUS LED #define PIN_LED_R 2 #define PIN_LED_G 0 #define PIN_LED_B 4 +// LIGHT LED +#define LIGHT_LED_PIN_R 27 +#define LIGHT_LED_PIN_G 26 +#define LIGHT_LED_PIN_B 25 +#define MAX_LIGHT_TIMEOUT 10000 + // MQTT #define MQTT_HOST "mqtt.timovolkmann.de" #define MQTT_PORT 1883 @@ -77,12 +83,13 @@ extern void publishMessage(const char *topic, const char *msg); // RGB PWM LED extern void setupPWM(); -extern bool activateLight(); extern void setValueNM(int NM); -extern void getColorBasedOnValueNM(); +extern void getColorBasedOnValueNM(int valueNM); +extern void triggerLight(); // sensors void readSensors(); void toggleValve(); void setSoilProperties(int FC, int PWP, int SAT); -void setupStore(); \ No newline at end of file +void setupStore(); +void setLightningProperties(int NM, int minLX); \ No newline at end of file diff --git a/src/lightChecker.cpp b/src/lightChecker.cpp index 79713d5..923189c 100644 --- a/src/lightChecker.cpp +++ b/src/lightChecker.cpp @@ -1,6 +1,6 @@ #include -int valueNM; +bool lightActive = false; // Colors in Array: purple, blue, green, yellow, orange, red, white int colorValueArray[][3] = {{125,0,125}, {0,0,255}, {0,255,0}, {255,255,0}, {255,140,0}, {255,0,0}, {255,255,255}}; int colorCounter = 6; @@ -14,9 +14,9 @@ const int resolution = 8; void setupPWM() { // Set pins as output - pinMode(PIN_LED_R, OUTPUT); - pinMode(PIN_LED_G, OUTPUT); - pinMode(PIN_LED_B, OUTPUT); + pinMode(LIGHT_LED_PIN_R, OUTPUT); + pinMode(LIGHT_LED_PIN_G, OUTPUT); + pinMode(LIGHT_LED_PIN_B, OUTPUT); // Configure LED PWM functionalitites ledcSetup(ledChannelRed, freq, resolution); @@ -24,35 +24,47 @@ void setupPWM() { ledcSetup(ledChannelBlue, freq, resolution); // Attach the channel to the GPIO2 to be controlled - ledcAttachPin(PIN_LED_R, ledChannelRed); - ledcAttachPin(PIN_LED_G, ledChannelGreen); - ledcAttachPin(PIN_LED_B, ledChannelBlue); + ledcAttachPin(LIGHT_LED_PIN_R, ledChannelRed); + ledcAttachPin(LIGHT_LED_PIN_G, ledChannelGreen); + ledcAttachPin(LIGHT_LED_PIN_B, ledChannelBlue); } void setValueNM(int NM) { - valueNM = NM; - getColorBasedOnValueNM(); + getColorBasedOnValueNM(NM); + // TODO: add persistenz for colorCounter } -void getColorBasedOnValueNM() { - if (valueNM <= 420) { +void getColorBasedOnValueNM(int valueNM) { + if (valueNM <= 420) { //Purple colorCounter = 0; } - else if (valueNM <= 490) { + else if (valueNM <= 490) { //Blue colorCounter = 1; } - else if (valueNM <= 575) { + else if (valueNM <= 575) { //Green colorCounter = 2; } - else if (valueNM <= 585) { + else if (valueNM <= 585) { //Yellow colorCounter = 3; } - else if (valueNM <= 650) { + else if (valueNM <= 650) { //Orange colorCounter = 4; } else if (valueNM > 650) { // 650 to 750 is red colorCounter = 5; } + Serial.println("New color set based on: "); + Serial.print(valueNM); +} + +bool shutdownLight() { + //digitalWrite(LIGHT_LED_PIN_R, LOW); + //digitalWrite(LIGHT_LED_PIN_G, LOW); + //digitalWrite(LIGHT_LED_PIN_B, LOW); + ledcWrite(ledChannelRed, 0); + ledcWrite(ledChannelGreen, 0); + ledcWrite(ledChannelBlue, 0); + return false; } bool activateLight() { @@ -60,4 +72,24 @@ bool activateLight() { ledcWrite(ledChannelGreen, colorValueArray[colorCounter][1]); ledcWrite(ledChannelBlue, colorValueArray[colorCounter][2]); return true; +} + +void lightTask(void *parameter) { + unsigned long lightTimeoutTimer = millis(); + if (lightActive == false) { + lightActive = activateLight(); + while (millis() - lightTimeoutTimer <= MAX_LIGHT_TIMEOUT) { + } + lightActive = shutdownLight(); + } +} + +void triggerLight() { + xTaskCreate( + lightTask, /* Task function. */ + "lightTask", /* String with name of task. */ + 2048, /* Stack size in bytes. */ + NULL, /* Parameter passed as input of the task */ + 1, /* Priority of the task. */ + NULL); /* Task handle. */ } \ No newline at end of file diff --git a/src/peripherals.cpp b/src/peripherals.cpp index 6d93cf9..b72df8b 100644 --- a/src/peripherals.cpp +++ b/src/peripherals.cpp @@ -19,6 +19,8 @@ int fieldCapacity = 44; int permanentWiltingPoint = 25; // Boden vollständig gesättigt bei (Prozent): Standard ist Humus int soilSaturation = 69; +// Helligkeitswert der mindestens vorhanden sein muss +int minimumLightValueLX = 50; void readSensors() { StaticJsonDocument<128> doc; @@ -27,6 +29,9 @@ void readSensors() { Serial.print(lxValue); Serial.println(" lx"); doc["brightness"] = lxValue; + if(lxValue < minimumLightValueLX) { + triggerLight(); + } //sprintf(buffer, "%f", lxValue); //publishMessage(MQTT_BRIGHTNESS, buffer); @@ -148,3 +153,11 @@ void setSoilProperties(int FC, int PWP, int SAT) { Serial.println(soilSaturation); } +void setLightningProperties(int NM, int minLX) { + setValueNM(NM); + minimumLightValueLX = minLX; + Serial.print("new minimum Light Value LX: "); + Serial.println(minimumLightValueLX); + // TODO: add Persistenz for minLX +} +