From 6d3af9e18da32b9f89a457032fa2f9811e109605 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 28 Jun 2020 11:39:05 +0200 Subject: [PATCH 01/12] Changd print message from sensor to light-sensor started --- src/lightSensor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lightSensor.cpp b/src/lightSensor.cpp index 6b2c566..9ffc0e4 100644 --- a/src/lightSensor.cpp +++ b/src/lightSensor.cpp @@ -5,7 +5,7 @@ BH1750 lightMeter; void setupLightSensor() { Wire.begin(); lightMeter.begin(); - Serial.println("Sensor started..."); + Serial.println("Light-Sensor started..."); } int readLightSensorValue() { From ac9327179d935c3a2ff09e68bbab774fb96c5cea Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 28 Jun 2020 18:56:04 +0200 Subject: [PATCH 02/12] new class LightningChecker to control led --- src/LightingChecker.cpp | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/LightingChecker.cpp diff --git a/src/LightingChecker.cpp b/src/LightingChecker.cpp new file mode 100644 index 0000000..01ec405 --- /dev/null +++ b/src/LightingChecker.cpp @@ -0,0 +1,48 @@ +#include + +#define RED_PIN 1 +#define GREEN_PIN 2 +#define BLUE_PIN 3 + +enum colorTypes { + purple, + blue, + green, + yellow, + orange, + red, + white +}; + +int valueNM; + +colorTypes color = white; + +void readPreferableLight() { + //preferableLight = Read from database +} + +void getColorBasedOnPreferableLight() { + if (valueNM <= 420) { + color = purple; + } + else if (valueNM <= 490) { + color = blue; + } + else if (valueNM <= 575) { + color = green; + } + else if (valueNM <= 585) { + color = yellow; + } + else if (valueNM <= 650) { + color = orange; + } + else if (valueNM <= 750) { + color = red; + } +} + +void activateLight(int duration) { + +} \ No newline at end of file From a0066f0811b3ebaa707f36c62f86b88d3321c8d4 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 29 Jun 2020 18:35:12 +0200 Subject: [PATCH 03/12] worked at RGBclass --- src/LightingChecker.cpp | 62 +++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/src/LightingChecker.cpp b/src/LightingChecker.cpp index 01ec405..eebbffa 100644 --- a/src/LightingChecker.cpp +++ b/src/LightingChecker.cpp @@ -1,9 +1,5 @@ #include -#define RED_PIN 1 -#define GREEN_PIN 2 -#define BLUE_PIN 3 - enum colorTypes { purple, blue, @@ -15,14 +11,36 @@ enum colorTypes { }; int valueNM; +// 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; colorTypes color = white; -void readPreferableLight() { - //preferableLight = Read from database +//Setting PWM properties +const int freqRed = 5000; +const int freqGreen = 5000; +const int freqBlue = 5000; +const int ledChannelRed = 0; +const int ledChannelGreen = 1; +const int ledChannelBlue= 2; +const int resolutionRed = 8; +const int resolutionGreen = 8; +const int resolutionBlue = 8; + +void setupPWM() { + ledcSetup(ledChannelRed, freqRed, resolutionRed); + ledcSetup(ledChannelGreen, freqGreen, resolutionGreen); + ledcSetup(ledChannelBlue, freqBlue, resolutionBlue); + + ledcAttachPin() } -void getColorBasedOnPreferableLight() { +void readValueNM() { + //valueNM = Read from database +} + +void getColorBasedOnValueNM() { if (valueNM <= 420) { color = purple; } @@ -38,11 +56,37 @@ void getColorBasedOnPreferableLight() { else if (valueNM <= 650) { color = orange; } - else if (valueNM <= 750) { + else if (valueNM > 650) { //650 to 750 is red color = red; } } void activateLight(int duration) { - + switch (color) { + case purple: + colorCounter = 0; + break; + case blue: + colorCounter = 1; + break; + case green: + colorCounter = 2; + break; + case yellow: + colorCounter = 3; + break; + case orange: + colorCounter = 4; + break; + case red: + colorCounter = 5; + break; + case white: + colorCounter = 6; + break; + } + + void activateLight() { + + } } \ No newline at end of file From 2827573bbc2e92ace3c6b6b48fedf7d0673241b4 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 1 Jul 2020 10:49:42 +0200 Subject: [PATCH 04/12] added lightChecker class and some basic pwm stuff --- src/LightingChecker.cpp | 92 ----------------------------------------- src/header.h | 6 ++- src/lightChecker.cpp | 0 src/main.cpp | 3 ++ 4 files changed, 8 insertions(+), 93 deletions(-) delete mode 100644 src/LightingChecker.cpp create mode 100644 src/lightChecker.cpp diff --git a/src/LightingChecker.cpp b/src/LightingChecker.cpp deleted file mode 100644 index eebbffa..0000000 --- a/src/LightingChecker.cpp +++ /dev/null @@ -1,92 +0,0 @@ -#include - -enum colorTypes { - purple, - blue, - green, - yellow, - orange, - red, - white -}; - -int valueNM; -// 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; - -colorTypes color = white; - -//Setting PWM properties -const int freqRed = 5000; -const int freqGreen = 5000; -const int freqBlue = 5000; -const int ledChannelRed = 0; -const int ledChannelGreen = 1; -const int ledChannelBlue= 2; -const int resolutionRed = 8; -const int resolutionGreen = 8; -const int resolutionBlue = 8; - -void setupPWM() { - ledcSetup(ledChannelRed, freqRed, resolutionRed); - ledcSetup(ledChannelGreen, freqGreen, resolutionGreen); - ledcSetup(ledChannelBlue, freqBlue, resolutionBlue); - - ledcAttachPin() -} - -void readValueNM() { - //valueNM = Read from database -} - -void getColorBasedOnValueNM() { - if (valueNM <= 420) { - color = purple; - } - else if (valueNM <= 490) { - color = blue; - } - else if (valueNM <= 575) { - color = green; - } - else if (valueNM <= 585) { - color = yellow; - } - else if (valueNM <= 650) { - color = orange; - } - else if (valueNM > 650) { //650 to 750 is red - color = red; - } -} - -void activateLight(int duration) { - switch (color) { - case purple: - colorCounter = 0; - break; - case blue: - colorCounter = 1; - break; - case green: - colorCounter = 2; - break; - case yellow: - colorCounter = 3; - break; - case orange: - colorCounter = 4; - break; - case red: - colorCounter = 5; - break; - case white: - colorCounter = 6; - break; - } - - void activateLight() { - - } -} \ No newline at end of file diff --git a/src/header.h b/src/header.h index 7eab5cf..262df6e 100644 --- a/src/header.h +++ b/src/header.h @@ -62,4 +62,8 @@ extern float readTemperature(); // mqtt & wifi extern void setupConnections(); -extern void publishMessage(const char *topic, const char *msg); \ No newline at end of file +extern void publishMessage(const char *topic, const char *msg); + +// RGB PWM LED +extern void setupPWM(); +extern void activateLight(); \ No newline at end of file diff --git a/src/lightChecker.cpp b/src/lightChecker.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/main.cpp b/src/main.cpp index 198940a..6988b3f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ void setup() { pingTimer = millis(); setupConnections(); setupLightSensor(); + setupPWM(); setupTemperatureSensor(); setupCapacitiveSoilMoistureSensor(); Serial.println("Setup complete..."); @@ -47,6 +48,8 @@ void loop() { publishMessage("smartgarden/updates/esp-N2Ff4kaDgs45/temperature", buffer); Serial.print("\n"); + activateLight(); + pingTimer = millis(); } } \ No newline at end of file From 52e3ab8cdc030b38a0ec5d7e1082a7edfca00655 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 1 Jul 2020 10:52:26 +0200 Subject: [PATCH 05/12] update lightChecker --- src/lightChecker.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/lightChecker.cpp b/src/lightChecker.cpp index e69de29..1a30e1e 100644 --- a/src/lightChecker.cpp +++ b/src/lightChecker.cpp @@ -0,0 +1,61 @@ +#include + +int valueNM; +// 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; + +// Setting PWM properties +const int freq = 5000; +const int ledChannelRed = 0; +const int ledChannelGreen = 1; +const int ledChannelBlue= 2; +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); + + // Configure LED PWM functionalitites + ledcSetup(ledChannelRed, freq, resolution); + ledcSetup(ledChannelGreen, freq, resolution); + 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); +} + +void readValueNM() { + // ValueNM = Read from database +} + +void getColorBasedOnValueNM() { + if (valueNM <= 420) { + colorCounter = 0; + } + else if (valueNM <= 490) { + colorCounter = 1; + } + else if (valueNM <= 575) { + colorCounter = 2; + } + else if (valueNM <= 585) { + colorCounter = 3; + } + else if (valueNM <= 650) { + colorCounter = 4; + } + else if (valueNM > 650) { // 650 to 750 is red + colorCounter = 5; + } +} + +void activateLight() { + ledcWrite(ledChannelRed, colorValueArray[colorCounter][0]); + ledcWrite(ledChannelGreen, colorValueArray[colorCounter][1]); + ledcWrite(ledChannelBlue, colorValueArray[colorCounter][2]); +} \ No newline at end of file From 03ea93b74122f08bbe58319674954430eee68388 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 1 Jul 2020 14:31:50 +0200 Subject: [PATCH 06/12] changed mqtt id --- src/header.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/header.h b/src/header.h index 3ecd521..fbd09ee 100644 --- a/src/header.h +++ b/src/header.h @@ -41,7 +41,7 @@ // MQTT #define MQTT_HOST "mqtt.timovolkmann.de" #define MQTT_PORT 1883 -#define MQTT_DEVICE_ID "esp-sebastian +#define MQTT_DEVICE_ID "esp-sebastian" #define MQTT_TOPIC_BASE_SUB "smartgarden/commands" #define MQTT_TOPIC_BASE_PUB "smartgarden/updates" #define MQTT_PATH_PUB MQTT_TOPIC_BASE_PUB "/" MQTT_DEVICE_ID "/" From b4ecfed027aff1a4a2b8cce075232b8b0607677f Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 2 Jul 2020 02:03:30 +0200 Subject: [PATCH 07/12] added example to read data from MQTT Server --- src/connections.cpp | 19 ++++++++++++++++++- src/header.h | 4 ++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/connections.cpp b/src/connections.cpp index 9971b25..0977360 100644 --- a/src/connections.cpp +++ b/src/connections.cpp @@ -64,6 +64,11 @@ void onMqttConnect(bool sessionPresent) uint16_t packetIdSub = mqttClient.subscribe(MQTT_PATH_SUB, 2); Serial.print("subscribed to: "); Serial.println(MQTT_PATH_SUB); + + //Testing read + uint16_t packetIdSub2 = mqttClient.subscribe("smartgarden/updates/esp-sebastian/brightness", 2); + Serial.print("also subscribed to: "); + Serial.println("smartgarden/updates/esp-sebastian/brightness"); } void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) @@ -103,6 +108,18 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties Serial.println(topic); toggleValve(); } + + //Testing reading + String messageTemp; + for (int i = 0; i < len; i++) { + //Serial.print((char)payload[i]); + messageTemp += (char)payload[i]; + } + if (strcmp(topic, "smartgarden/updates/esp-sebastian/brightness") == 0) { + Serial.println("### Received brightness value: ###"); + Serial.println(messageTemp); + } + } void onMqttPublish(uint16_t packetId) @@ -135,4 +152,4 @@ void setupConnections() void publishMessage(const char *topic, const char *msg) { mqttClient.publish(topic, 1, true, msg); -} +} \ No newline at end of file diff --git a/src/header.h b/src/header.h index fbd09ee..5820402 100644 --- a/src/header.h +++ b/src/header.h @@ -17,6 +17,10 @@ #include #include +// BH1750 lightsensor +#define MIN_LIGHT 0 +#define MAX_LIGHT 54612 + // DHT11 #define PIN_DHT11 14 From 68f75f252daf3893f93e58bbf46019692f4d8131 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 3 Jul 2020 13:21:13 +0200 Subject: [PATCH 08/12] deleted test stuff --- src/connections.cpp | 28 ---------------------------- src/lightSensor.cpp | 6 ------ 2 files changed, 34 deletions(-) diff --git a/src/connections.cpp b/src/connections.cpp index 9b7e9fe..3830f28 100644 --- a/src/connections.cpp +++ b/src/connections.cpp @@ -63,13 +63,6 @@ void connectMQTT() { mqttClient.subscribe(MQTT_PATH_SUB); Serial.print("subscribed to: "); Serial.println(MQTT_PATH_SUB); -<<<<<<< HEAD - - //Testing read - uint16_t packetIdSub2 = mqttClient.subscribe("smartgarden/updates/esp-sebastian/brightness", 2); - Serial.print("also subscribed to: "); - Serial.println("smartgarden/updates/esp-sebastian/brightness"); -======= xTaskCreate( mqttLoop, /* Task function. */ "mqttLoop", /* String with name of task. */ @@ -78,7 +71,6 @@ void connectMQTT() { 1, /* Priority of the task. */ &mqttTask); /* Task handle. */ //xTimerStart(mqttProcessingTimer, 0); ->>>>>>> develop } void WiFiEvent(WiFiEvent_t event) { @@ -114,20 +106,6 @@ void onMqttMessage(char *topic, byte *payload, unsigned int payload_length) { Serial.println(topic); toggleValve(); } -<<<<<<< HEAD - - //Testing reading - String messageTemp; - for (int i = 0; i < len; i++) { - //Serial.print((char)payload[i]); - messageTemp += (char)payload[i]; - } - if (strcmp(topic, "smartgarden/updates/esp-sebastian/brightness") == 0) { - Serial.println("### Received brightness value: ###"); - Serial.println(messageTemp); - } - -======= if (strcmp(topic, MQTT_SOIL_PROPERTIES) == 0) { Serial.println("receiving soil thresholds..."); Serial.println(topic); @@ -145,7 +123,6 @@ void onMqttMessage(char *topic, byte *payload, unsigned int payload_length) { Serial.println(err.c_str()); } } ->>>>>>> develop } void setupConnections() { @@ -168,11 +145,6 @@ void setupConnections() { } void publishMessage(const char *topic, const char *msg) { -<<<<<<< HEAD - mqttClient.publish(topic, 1, true, msg); -} -======= mqttClient.publish(topic, msg); // mqttClient.publish(topic, msg, true, 1); } ->>>>>>> develop diff --git a/src/lightSensor.cpp b/src/lightSensor.cpp index 992b5dd..776a3fd 100644 --- a/src/lightSensor.cpp +++ b/src/lightSensor.cpp @@ -3,15 +3,9 @@ BH1750 lightMeter; void setupLightSensor() { -<<<<<<< HEAD - Wire.begin(); - lightMeter.begin(); - Serial.println("Light-Sensor started..."); -======= Wire.begin(); lightMeter.begin(); Serial.println("Sensor started..."); ->>>>>>> develop } float readLightSensorValue() { From 5fb882c59a093ffc078de508dbeb60ddb9cac2ee Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 4 Jul 2020 17:09:44 +0200 Subject: [PATCH 09/12] light treshold will now be set based on mqtt command --- src/connections.cpp | 14 +++++++++++++- src/header.h | 4 +++- src/lightChecker.cpp | 8 +++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/connections.cpp b/src/connections.cpp index 3830f28..88c0d2c 100644 --- a/src/connections.cpp +++ b/src/connections.cpp @@ -10,6 +10,7 @@ extern "C" { #define MQTT_VALVE_COMMAND MQTT_TOPIC_BASE_SUB "/" MQTT_DEVICE_ID "/valve" #define MQTT_SOIL_PROPERTIES MQTT_TOPIC_BASE_SUB "/" MQTT_DEVICE_ID "/soil" +#define MQTT_LIGHT_COMMAND MQTT_TOPIC_BASE_SUB "/" MQTT_DEVICE_ID "/light" TimerHandle_t mqttReconnectTimer; TimerHandle_t wifiReconnectTimer; @@ -101,10 +102,21 @@ void onMqttMessage(char *topic, byte *payload, unsigned int payload_length) { } Serial.println(); + if (strcmp(topic, MQTT_LIGHT_COMMAND) == 0) { +Serial.println("receiving light treshold..."); + Serial.println(topic); + } if (strcmp(topic, MQTT_VALVE_COMMAND) == 0) { Serial.println("toggling valve..."); Serial.println(topic); - toggleValve(); + StaticJsonDocument<1024> doc; + DeserializationError err = deserializeJson(doc, payload); + if (err == DeserializationError::Ok) { + int nm = doc["nm"]; + setValueNM(nm); + } else { + Serial.println(err.c_str()); + } } if (strcmp(topic, MQTT_SOIL_PROPERTIES) == 0) { Serial.println("receiving soil thresholds..."); diff --git a/src/header.h b/src/header.h index 53d67c9..1a34635 100644 --- a/src/header.h +++ b/src/header.h @@ -77,7 +77,9 @@ extern void publishMessage(const char *topic, const char *msg); // RGB PWM LED extern void setupPWM(); -extern void activateLight(); +extern bool activateLight(); +extern void setValueNM(int NM); +extern void getColorBasedOnValueNM(); // sensors void readSensors(); diff --git a/src/lightChecker.cpp b/src/lightChecker.cpp index 1a30e1e..79713d5 100644 --- a/src/lightChecker.cpp +++ b/src/lightChecker.cpp @@ -29,8 +29,9 @@ void setupPWM() { ledcAttachPin(PIN_LED_B, ledChannelBlue); } -void readValueNM() { - // ValueNM = Read from database +void setValueNM(int NM) { + valueNM = NM; + getColorBasedOnValueNM(); } void getColorBasedOnValueNM() { @@ -54,8 +55,9 @@ void getColorBasedOnValueNM() { } } -void activateLight() { +bool activateLight() { ledcWrite(ledChannelRed, colorValueArray[colorCounter][0]); ledcWrite(ledChannelGreen, colorValueArray[colorCounter][1]); ledcWrite(ledChannelBlue, colorValueArray[colorCounter][2]); + return true; } \ No newline at end of file From 62bf9a17c53386f2f9919947ece4f5034fbea496 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 6 Jul 2020 12:41:14 +0200 Subject: [PATCH 10/12] fixed light subscription --- src/connections.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/connections.cpp b/src/connections.cpp index 88c0d2c..4abc8a5 100644 --- a/src/connections.cpp +++ b/src/connections.cpp @@ -10,7 +10,7 @@ extern "C" { #define MQTT_VALVE_COMMAND MQTT_TOPIC_BASE_SUB "/" MQTT_DEVICE_ID "/valve" #define MQTT_SOIL_PROPERTIES MQTT_TOPIC_BASE_SUB "/" MQTT_DEVICE_ID "/soil" -#define MQTT_LIGHT_COMMAND MQTT_TOPIC_BASE_SUB "/" MQTT_DEVICE_ID "/light" +#define MQTT_LIGHT_PROPERTIES MQTT_TOPIC_BASE_SUB "/" MQTT_DEVICE_ID "/light" TimerHandle_t mqttReconnectTimer; TimerHandle_t wifiReconnectTimer; @@ -102,13 +102,9 @@ void onMqttMessage(char *topic, byte *payload, unsigned int payload_length) { } Serial.println(); - if (strcmp(topic, MQTT_LIGHT_COMMAND) == 0) { + if (strcmp(topic, MQTT_LIGHT_PROPERTIES) == 0) { Serial.println("receiving light treshold..."); Serial.println(topic); - } - if (strcmp(topic, MQTT_VALVE_COMMAND) == 0) { - Serial.println("toggling valve..."); - Serial.println(topic); StaticJsonDocument<1024> doc; DeserializationError err = deserializeJson(doc, payload); if (err == DeserializationError::Ok) { @@ -118,6 +114,11 @@ Serial.println("receiving light treshold..."); Serial.println(err.c_str()); } } + if (strcmp(topic, MQTT_VALVE_COMMAND) == 0) { + Serial.println("toggling valve..."); + Serial.println(topic); + toggleValve(); + } if (strcmp(topic, MQTT_SOIL_PROPERTIES) == 0) { Serial.println("receiving soil thresholds..."); Serial.println(topic); From 71e58773cbdaada0d870eab412971052e115d991 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 11 Jul 2020 10:34:06 +0200 Subject: [PATCH 11/12] 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 +} + From 18b5c0bfeceb30e12c570e73ae16fb9bc390c4e1 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 12 Jul 2020 18:28:59 +0200 Subject: [PATCH 12/12] Added class to get timestamps from NTP server --- src/connections.cpp | 2 +- src/header.h | 11 ++++++++++- src/lightChecker.cpp | 4 +++- src/main.cpp | 1 + src/ntpManager.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/peripherals.cpp | 44 ++++++++++++++++++++++++++++++++++++-------- 6 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 src/ntpManager.cpp diff --git a/src/connections.cpp b/src/connections.cpp index a2cea3d..bb0dca5 100644 --- a/src/connections.cpp +++ b/src/connections.cpp @@ -121,7 +121,7 @@ Serial.println("receiving light treshold..."); if (err == DeserializationError::Ok) { int nm = doc["nm"]; int minLX = doc["minLX"]; - setLightningProperties(nm, minLX); + setLightProperties(nm, minLX); } else { Serial.println(err.c_str()); } diff --git a/src/header.h b/src/header.h index c9ec72a..df7db75 100644 --- a/src/header.h +++ b/src/header.h @@ -15,6 +15,7 @@ #include #include #include +#include "time.h" // fix for core panic during wifi initialization #define configMINIMAL_STACK_SIZE 4096 @@ -87,12 +88,20 @@ extern void setValueNM(int NM); extern void getColorBasedOnValueNM(int valueNM); extern void triggerLight(); +// NTP Timestamps +void printTimestamp(); +void setupNTP(); +bool checkForDay(); +int getCurrentHour(); + // sensors void readSensors(); void toggleValve(); void setSoilProperties(int FC, int PWP, int SAT); void setupStore(); -void setLightningProperties(int NM, int minLX); +void setLightProperties(int NM, int minLX); +void restoreLightProbs(); +void persistLightProps(int NM, int minLX); void takeSemaphore(); void releaseSemaphore(); diff --git a/src/lightChecker.cpp b/src/lightChecker.cpp index 923189c..5ecf944 100644 --- a/src/lightChecker.cpp +++ b/src/lightChecker.cpp @@ -31,7 +31,6 @@ void setupPWM() { void setValueNM(int NM) { getColorBasedOnValueNM(NM); - // TODO: add persistenz for colorCounter } void getColorBasedOnValueNM(int valueNM) { @@ -75,6 +74,7 @@ bool activateLight() { } void lightTask(void *parameter) { + takeSemaphore(); unsigned long lightTimeoutTimer = millis(); if (lightActive == false) { lightActive = activateLight(); @@ -82,6 +82,8 @@ void lightTask(void *parameter) { } lightActive = shutdownLight(); } + releaseSemaphore(); + vTaskDelete(NULL); } void triggerLight() { diff --git a/src/main.cpp b/src/main.cpp index 56f2f2c..9b624f6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,6 +29,7 @@ void setup() { setupPWM(); setupTemperatureSensor(); setupCapacitiveSoilMoistureSensor(); + setupNTP(); // esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * 1000000); Serial.println("Setup complete..."); Serial.println(); diff --git a/src/ntpManager.cpp b/src/ntpManager.cpp new file mode 100644 index 0000000..ef5be48 --- /dev/null +++ b/src/ntpManager.cpp @@ -0,0 +1,41 @@ +#include + +const char* ntpServer = "pool.ntp.org"; +const long gmtOffset_sec = 3600; +const int daylightOffset_sec = 3600; + +// start and end hour of the date +const int dayStart = 8; +const int dayEnd = 20; +struct tm timeinfo; + +void setupNTP() { + if (WiFi.isConnected()) { + Serial.println("WiFi OK. Getting Timestamp"); + configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); + printTimestamp(); + } +} + +bool checkForDay() { + printTimestamp(); + if((getCurrentHour() > dayStart) && (getCurrentHour() < dayEnd)) { + return true; + } + else { + return false; + } +} + +int getCurrentHour() { + int currentHour = timeinfo.tm_hour; + return currentHour; +} + +void printTimestamp() { + if(!getLocalTime(&timeinfo)){ + Serial.println("Failed to obtain time"); + return; + } + Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); +} \ No newline at end of file diff --git a/src/peripherals.cpp b/src/peripherals.cpp index 1a47872..730aeb9 100644 --- a/src/peripherals.cpp +++ b/src/peripherals.cpp @@ -29,7 +29,7 @@ void readSensors() { Serial.print(lxValue); Serial.println(" lx"); doc["brightness"] = lxValue; - if(lxValue < minimumLightValueLX) { + if((lxValue < minimumLightValueLX) && checkForDay()) { triggerLight(); } //sprintf(buffer, "%f", lxValue); @@ -98,7 +98,7 @@ void toggleValve() { xTaskCreate( valveTask, /* Task function. */ "valveTask", /* String with name of task. */ - 2048, /* Stack size in bytes. */ + 2048, /* Stack size in bytes. */ NULL, /* Parameter passed as input of the task */ 1, /* Priority of the task. */ NULL); /* Task handle. */ @@ -142,6 +142,7 @@ void restoreSoilProps() { void setupStore() { NVS.begin("store"); restoreSoilProps(); + restoreLightProbs(); } void setSoilProperties(int FC, int PWP, int SAT) { @@ -157,11 +158,38 @@ 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 +void persistLightProps(int NM, int minLX) { + takeSemaphore(); + Serial.println("persistSoilProps"); + bool n = NVS.setInt("nanoMeter", NM); + bool m = NVS.setInt("minimumLux", minLX); + if (n && m) { + Serial.println("Light properties sucessfully stored."); + NVS.commit(); + } + else { + Serial.println("error occured while trying to persist soil properties"); + } + releaseSemaphore(); } +void restoreLightProbs() { + Serial.println("restoreLightProps"); + int nm = NVS.getInt("nanoMeter"); + int minLX = NVS.getInt("minimumLux"); + if (nm != 0) { + setValueNM(nm); + } + if (minLX != 0) { + minimumLightValueLX = minLX; + } + Serial.print(minimumLightValueLX); +} + +void setLightProperties(int NM, int minLX) { + setValueNM(NM); + minimumLightValueLX = minLX; + persistLightProps(NM, minLX); + Serial.print("new minimum Light Value LX: "); + Serial.println(minimumLightValueLX); +}