Light will now turn on if lx below minLX also color can be changed based on nanoMeters of prefered light

This commit is contained in:
Sebastian 2020-07-11 10:34:06 +02:00
parent 62bf9a17c5
commit 71e58773cb
4 changed files with 73 additions and 20 deletions

View File

@ -109,7 +109,8 @@ Serial.println("receiving light treshold...");
DeserializationError err = deserializeJson(doc, payload); DeserializationError err = deserializeJson(doc, payload);
if (err == DeserializationError::Ok) { if (err == DeserializationError::Ok) {
int nm = doc["nm"]; int nm = doc["nm"];
setValueNM(nm); int minLX = doc["minLX"];
setLightningProperties(nm, minLX);
} else { } else {
Serial.println(err.c_str()); Serial.println(err.c_str());
} }

View File

@ -40,11 +40,17 @@
#define PIN_VALVE 32 #define PIN_VALVE 32
#define MAX_VALVE_TIMEOUT 10000 #define MAX_VALVE_TIMEOUT 10000
// LED // STATUS LED
#define PIN_LED_R 2 #define PIN_LED_R 2
#define PIN_LED_G 0 #define PIN_LED_G 0
#define PIN_LED_B 4 #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 // MQTT
#define MQTT_HOST "mqtt.timovolkmann.de" #define MQTT_HOST "mqtt.timovolkmann.de"
#define MQTT_PORT 1883 #define MQTT_PORT 1883
@ -77,12 +83,13 @@ extern void publishMessage(const char *topic, const char *msg);
// RGB PWM LED // RGB PWM LED
extern void setupPWM(); extern void setupPWM();
extern bool activateLight();
extern void setValueNM(int NM); extern void setValueNM(int NM);
extern void getColorBasedOnValueNM(); extern void getColorBasedOnValueNM(int valueNM);
extern void triggerLight();
// sensors // sensors
void readSensors(); void readSensors();
void toggleValve(); void toggleValve();
void setSoilProperties(int FC, int PWP, int SAT); void setSoilProperties(int FC, int PWP, int SAT);
void setupStore(); void setupStore();
void setLightningProperties(int NM, int minLX);

View File

@ -1,6 +1,6 @@
#include <header.h> #include <header.h>
int valueNM; bool lightActive = false;
// Colors in Array: purple, blue, green, yellow, orange, red, white // 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 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; int colorCounter = 6;
@ -14,9 +14,9 @@ const int resolution = 8;
void setupPWM() { void setupPWM() {
// Set pins as output // Set pins as output
pinMode(PIN_LED_R, OUTPUT); pinMode(LIGHT_LED_PIN_R, OUTPUT);
pinMode(PIN_LED_G, OUTPUT); pinMode(LIGHT_LED_PIN_G, OUTPUT);
pinMode(PIN_LED_B, OUTPUT); pinMode(LIGHT_LED_PIN_B, OUTPUT);
// Configure LED PWM functionalitites // Configure LED PWM functionalitites
ledcSetup(ledChannelRed, freq, resolution); ledcSetup(ledChannelRed, freq, resolution);
@ -24,35 +24,47 @@ void setupPWM() {
ledcSetup(ledChannelBlue, freq, resolution); ledcSetup(ledChannelBlue, freq, resolution);
// Attach the channel to the GPIO2 to be controlled // Attach the channel to the GPIO2 to be controlled
ledcAttachPin(PIN_LED_R, ledChannelRed); ledcAttachPin(LIGHT_LED_PIN_R, ledChannelRed);
ledcAttachPin(PIN_LED_G, ledChannelGreen); ledcAttachPin(LIGHT_LED_PIN_G, ledChannelGreen);
ledcAttachPin(PIN_LED_B, ledChannelBlue); ledcAttachPin(LIGHT_LED_PIN_B, ledChannelBlue);
} }
void setValueNM(int NM) { void setValueNM(int NM) {
valueNM = NM; getColorBasedOnValueNM(NM);
getColorBasedOnValueNM(); // TODO: add persistenz for colorCounter
} }
void getColorBasedOnValueNM() { void getColorBasedOnValueNM(int valueNM) {
if (valueNM <= 420) { if (valueNM <= 420) { //Purple
colorCounter = 0; colorCounter = 0;
} }
else if (valueNM <= 490) { else if (valueNM <= 490) { //Blue
colorCounter = 1; colorCounter = 1;
} }
else if (valueNM <= 575) { else if (valueNM <= 575) { //Green
colorCounter = 2; colorCounter = 2;
} }
else if (valueNM <= 585) { else if (valueNM <= 585) { //Yellow
colorCounter = 3; colorCounter = 3;
} }
else if (valueNM <= 650) { else if (valueNM <= 650) { //Orange
colorCounter = 4; colorCounter = 4;
} }
else if (valueNM > 650) { // 650 to 750 is red else if (valueNM > 650) { // 650 to 750 is red
colorCounter = 5; 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() { bool activateLight() {
@ -60,4 +72,24 @@ bool activateLight() {
ledcWrite(ledChannelGreen, colorValueArray[colorCounter][1]); ledcWrite(ledChannelGreen, colorValueArray[colorCounter][1]);
ledcWrite(ledChannelBlue, colorValueArray[colorCounter][2]); ledcWrite(ledChannelBlue, colorValueArray[colorCounter][2]);
return true; 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. */
} }

View File

@ -19,6 +19,8 @@ int fieldCapacity = 44;
int permanentWiltingPoint = 25; int permanentWiltingPoint = 25;
// Boden vollständig gesättigt bei (Prozent): Standard ist Humus // Boden vollständig gesättigt bei (Prozent): Standard ist Humus
int soilSaturation = 69; int soilSaturation = 69;
// Helligkeitswert der mindestens vorhanden sein muss
int minimumLightValueLX = 50;
void readSensors() { void readSensors() {
StaticJsonDocument<128> doc; StaticJsonDocument<128> doc;
@ -27,6 +29,9 @@ void readSensors() {
Serial.print(lxValue); Serial.print(lxValue);
Serial.println(" lx"); Serial.println(" lx");
doc["brightness"] = lxValue; doc["brightness"] = lxValue;
if(lxValue < minimumLightValueLX) {
triggerLight();
}
//sprintf(buffer, "%f", lxValue); //sprintf(buffer, "%f", lxValue);
//publishMessage(MQTT_BRIGHTNESS, buffer); //publishMessage(MQTT_BRIGHTNESS, buffer);
@ -148,3 +153,11 @@ void setSoilProperties(int FC, int PWP, int SAT) {
Serial.println(soilSaturation); 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
}