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);
if (err == DeserializationError::Ok) {
int nm = doc["nm"];
setValueNM(nm);
int minLX = doc["minLX"];
setLightningProperties(nm, minLX);
} else {
Serial.println(err.c_str());
}

View File

@ -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();
void setupStore();
void setLightningProperties(int NM, int minLX);

View File

@ -1,6 +1,6 @@
#include <header.h>
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. */
}

View File

@ -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
}