diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..379e402 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cquery.cacheDirectory": "${workspaceFolder}/.vscode/cquery_cached_index/" +} \ No newline at end of file diff --git a/src/connections.cpp b/src/connections.cpp index 299d809..c62572c 100644 --- a/src/connections.cpp +++ b/src/connections.cpp @@ -1,9 +1,8 @@ -#include -#include #include +#include +#include -extern "C" -{ +extern "C" { #include "freertos/FreeRTOS.h" #include "freertos/timers.h" } @@ -26,45 +25,36 @@ AutoConnectConfig Config; // WiFi.config(Config); MQTTClient mqttClient; -void connectWiFi() -{ +void connectWiFi() { Serial.println("Start WiFi..."); - if (Portal.begin()) - { + if (Portal.begin()) { digitalWrite(PIN_LED_G, HIGH); } } -void mqttLoop(void *parameter) -{ +void mqttLoop(void *parameter) { bool x; do { x = mqttClient.loop(); delay(50); } while (mqttClient.connected()); - if (!mqttClient.connected()) - { + if (!mqttClient.connected()) { Serial.println("Disconnected from MQTT."); - if (WiFi.isConnected()) - { + if (WiFi.isConnected()) { xTimerStart(mqttReconnectTimer, 0); } } vTaskDelete(NULL); } -void connectMQTT() -{ +void connectMQTT() { Serial.println("Connecting to MQTT..."); mqttClient.begin(MQTT_HOST, MQTT_PORT, client); mqttClient.connect(MQTT_DEVICE_ID); - if (mqttClient.connected()) - { + if (mqttClient.connected()) { Serial.println("Connected!"); - } - else - { + } else { Serial.println("NOT Connected!"); } mqttClient.subscribe(MQTT_PATH_SUB, 2); @@ -80,67 +70,57 @@ void connectMQTT() //xTimerStart(mqttProcessingTimer, 0); } -void WiFiEvent(WiFiEvent_t event) -{ +void WiFiEvent(WiFiEvent_t event) { Serial.printf("[WiFi-event] event: %d\n", event); - switch (event) - { - case SYSTEM_EVENT_STA_GOT_IP: - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - connectMQTT(); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - digitalWrite(PIN_LED_G, LOW); - xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi - // xTimerStop(mqttProcessingTimer, 0); - xTimerStart(wifiReconnectTimer, 0); - break; + switch (event) { + case SYSTEM_EVENT_STA_GOT_IP: + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + connectMQTT(); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + digitalWrite(PIN_LED_G, LOW); + xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi + // xTimerStop(mqttProcessingTimer, 0); + xTimerStart(wifiReconnectTimer, 0); + break; } } -void onMqttMessage(MQTTClient *client, char topic[], char payload[], int payload_length) -{ +void onMqttMessage(MQTTClient *client, char topic[], char payload[], int payload_length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); - for (int i = 0; i < payload_length; i++) - { + for (int i = 0; i < payload_length; i++) { Serial.print((char)payload[i]); } Serial.println(); - if (strcmp(topic, MQTT_VALVE_COMMAND) == 0) - { + if (strcmp(topic, MQTT_VALVE_COMMAND) == 0) { Serial.println("toggling valve..."); Serial.println(topic); toggleValve(); } - if (strcmp(topic, MQTT_SOIL_PROPERTIES) == 0) - { + if (strcmp(topic, MQTT_SOIL_PROPERTIES) == 0) { Serial.println("receiving 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) - { + if (err == DeserializationError::Ok) { int fc = doc["fc"]; int pwp = doc["pwp"]; int sat = doc["sat"]; setSoilProperties(fc, pwp, sat); - } - else - { + } else { Serial.println(err.c_str()); } } } -void setupConnections() -{ +void setupConnections() { Serial.println(); Serial.println(); @@ -158,7 +138,6 @@ void setupConnections() connectWiFi(); } -void publishMessage(const char *topic, const char *msg) -{ +void publishMessage(const char *topic, const char *msg) { mqttClient.publish(topic, msg, true, 1); } diff --git a/src/lightSensor.cpp b/src/lightSensor.cpp index 1dca49b..776a3fd 100644 --- a/src/lightSensor.cpp +++ b/src/lightSensor.cpp @@ -3,13 +3,12 @@ BH1750 lightMeter; void setupLightSensor() { - Wire.begin(); - lightMeter.begin(); - Serial.println("Sensor started..."); + Wire.begin(); + lightMeter.begin(); + Serial.println("Sensor started..."); } -float readLightSensorValue() -{ - float intensity = lightMeter.readLightLevel(); - return intensity; +float readLightSensorValue() { + float intensity = lightMeter.readLightLevel(); + return intensity; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 2e450c8..7f05ea8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,13 +3,13 @@ */ #include + #include unsigned long sensorReadTimer = 0; bool valveOpen = false; -void setup() -{ +void setup() { Serial.begin(115200); sensorReadTimer = millis(); @@ -29,17 +29,13 @@ void setup() Serial.println(); } -void loop() -{ +void loop() { // read sensors - if (millis() - sensorReadTimer >= FREQUENCY) - { + if (millis() - sensorReadTimer >= FREQUENCY) { readSensors(); sensorReadTimer = millis(); } // toggle valve if (valveOpen) { - } } - diff --git a/src/peripherals.cpp b/src/peripherals.cpp index b1d3c59..5da69ee 100644 --- a/src/peripherals.cpp +++ b/src/peripherals.cpp @@ -1,8 +1,8 @@ #include + #include //using namespace std; -extern "C" -{ +extern "C" { #include "freertos/FreeRTOS.h" #include "freertos/task.h" } @@ -21,8 +21,7 @@ int permanentWiltingPoint = 25; // Boden vollständig gesättigt bei (Prozent): Standard ist Humus int soilSaturation = 69; -void readSensors() -{ +void readSensors() { float lxValue = readLightSensorValue(); Serial.print("Light intensity: "); Serial.print(lxValue); @@ -64,18 +63,15 @@ bool closeValve() { return false; } -void valveTask(void *parameter) -{ +void valveTask(void *parameter) { unsigned long valveTimeoutTimer = millis(); bool valveOpen = openValve(); - while (valveOpen) - { + while (valveOpen) { delay(500); int mstValue = readCapacitiveSoilMoistureSensor(); - if (millis() - valveTimeoutTimer >= MAX_VALVE_TIMEOUT) - { + if (millis() - valveTimeoutTimer >= MAX_VALVE_TIMEOUT) { valveOpen = closeValve(); } // if (mstValue > 80) { @@ -85,8 +81,7 @@ void valveTask(void *parameter) vTaskDelete(NULL); } -void toggleValve() -{ +void toggleValve() { xTaskCreate( valveTask, /* Task function. */ "valveTask", /* String with name of task. */