From 47d58777c56344d6cc1c2d455f98868af7bcb476 Mon Sep 17 00:00:00 2001 From: Timo Volkmann Date: Tue, 30 Jun 2020 21:15:30 +0200 Subject: [PATCH] introduced soil thresholds --- platformio.ini | 3 ++- src/connections.cpp | 34 ++++++++++++++++++++++++++++------ src/header.h | 7 ++++++- src/peripherals.cpp | 19 +++++++++++++++++++ 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/platformio.ini b/platformio.ini index fe810f5..130393d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -19,6 +19,7 @@ lib_deps = 19 #DHT sensor library 31 #Adafruit Unified Sensor AutoConnect@^1.1.7 - ; ESPRandom@^1.3.3 AsyncMqttClient@^0.8.2 + ArduinoJson@^6.15.2 + ; ESPRandom@^1.3.3 ; PubSubClient@^2.8 \ No newline at end of file diff --git a/src/connections.cpp b/src/connections.cpp index 9971b25..4646376 100644 --- a/src/connections.cpp +++ b/src/connections.cpp @@ -1,4 +1,5 @@ #include +#include extern "C" { @@ -6,6 +7,9 @@ extern "C" #include "freertos/timers.h" } +#define MQTT_VALVE_COMMAND MQTT_TOPIC_BASE_SUB "/" MQTT_DEVICE_ID "/valve" +#define MQTT_SOIL_PROPERTIES MQTT_TOPIC_BASE_SUB "/" MQTT_DEVICE_ID "/soil" + AsyncMqttClient mqttClient; TimerHandle_t mqttReconnectTimer; TimerHandle_t wifiReconnectTimer; @@ -87,7 +91,7 @@ void onMqttSubscribe(uint16_t packetId, uint8_t qos) void onMqttUnsubscribe(uint16_t packetId) { - Serial.println("Unsubscribe acknowledged."); + Serial.print("Unsubscribe acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); } @@ -98,18 +102,36 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties Serial.print(" topic: "); Serial.println(topic); - if (strcmp(topic, "smartgarden/commands") == 0 ) { - Serial.println("executing command..."); + if (strcmp(topic, MQTT_VALVE_COMMAND) == 0 ) { + Serial.println("toggling valve..."); Serial.println(topic); toggleValve(); } + if (strcmp(topic, MQTT_SOIL_PROPERTIES) == 0 ) { + Serial.println("recieving soil thresholds..."); + Serial.println(topic); + Serial.println(payload); + // const int capacity = JSON_OBJECT_SIZE(3) + 2 * JSON_OBJECT_SIZE(1); + StaticJsonDocument<1024> doc; + DeserializationError err = deserializeJson(doc, payload); + if (err == DeserializationError::Ok) { + int fc = doc["fc"]; + int pwp = doc["pwp"]; + int sat = doc["sat"]; + Serial.println(doc.size()); + Serial.println(doc["fc"].as()); + Serial.println(doc["pwp"].as()); + Serial.println(doc["sat"].as()); + setSoilProperties(fc, pwp, sat); + } + } } void onMqttPublish(uint16_t packetId) { - Serial.print("Publish acknowledged: "); - Serial.print(" packetId: "); - Serial.println(packetId); + // Serial.print("Publish acknowledged: "); + // Serial.print(" packetId: "); + // Serial.println(packetId); } void setupConnections() diff --git a/src/header.h b/src/header.h index 4b91a52..f5d9aa4 100644 --- a/src/header.h +++ b/src/header.h @@ -17,6 +17,10 @@ #include #include +// fix for core panic during wifi initialization +// #define configMINIMAL_STACK_SIZE 2048 +// #define CONFIG_TIMER_TASK_STACK_SIZE 8192 + // DHT11 #define PIN_DHT11 14 @@ -70,4 +74,5 @@ extern void publishMessage(const char *topic, const char *msg); // sensors void readSensors(); -void toggleValve(); \ No newline at end of file +void toggleValve(); +void setSoilProperties(int FC, int PWP, int SAT); \ No newline at end of file diff --git a/src/peripherals.cpp b/src/peripherals.cpp index f7d68d2..6af2657 100644 --- a/src/peripherals.cpp +++ b/src/peripherals.cpp @@ -14,6 +14,13 @@ extern "C" char buffer[16]; +// Feldkapazität des Bodens in Prozent: Standard ist Humus +int fieldCapacity = 44; +// PWP des Bodens in Prozent: Standard ist Humus +int permanentWiltingPoint = 25; +// Boden vollständig gesättigt bei (Prozent): Standard ist Humus +int soilSaturation = 69; + void readSensors() { float lxValue = readLightSensorValue(); @@ -88,3 +95,15 @@ void toggleValve() 1, /* Priority of the task. */ NULL); /* Task handle. */ } + +void setSoilProperties(int FC, int PWP, int SAT) { + fieldCapacity = FC; + permanentWiltingPoint = PWP; + soilSaturation = SAT; + Serial.print("fieldCapacity: "); + Serial.println(fieldCapacity); + Serial.print("permanentWiltingPoint: "); + Serial.println(permanentWiltingPoint); + Serial.print("soilSaturation: "); + Serial.println(soilSaturation); +}