Merge branch 'develop' of https://git.it.hs-heilbronn.de/auribest/smart_garden into auribest_dev

This commit is contained in:
Andrés Uribe Stengel 2020-07-12 10:28:30 +02:00
commit 855f1d958e
5 changed files with 74 additions and 35 deletions

View File

@ -13,6 +13,7 @@ platform = espressif32
board = az-delivery-devkit-v4 board = az-delivery-devkit-v4
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
; build_flags = -DCORE_DEBUG_LEVEL=5
lib_deps = lib_deps =
439 #ID of Lightsensor library BH1750 439 #ID of Lightsensor library BH1750

View File

@ -13,8 +13,6 @@ extern "C" {
TimerHandle_t mqttReconnectTimer; TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer; TimerHandle_t wifiReconnectTimer;
// TimerHandle_t mqttProcessingTimer;
TaskHandle_t mqttTask; TaskHandle_t mqttTask;
WebServer Server; WebServer Server;
@ -22,12 +20,10 @@ AutoConnect Portal(Server);
WiFiClient client; WiFiClient client;
AutoConnectConfig Config; AutoConnectConfig Config;
// Config.autoReconnect = true;
// WiFi.config(Config);
// MQTTClient mqttClient;
PubSubClient mqttClient(client); PubSubClient mqttClient(client);
void connectWiFi() { void connectWiFi() {
yield();
Serial.println("Start WiFi..."); Serial.println("Start WiFi...");
if (Portal.begin()) { if (Portal.begin()) {
digitalWrite(PIN_LED_G, HIGH); digitalWrite(PIN_LED_G, HIGH);
@ -36,14 +32,15 @@ void connectWiFi() {
void mqttLoop(void *parameter) { void mqttLoop(void *parameter) {
do { do {
yield();
mqttClient.loop(); mqttClient.loop();
delay(50); delay(100);
} while (mqttClient.connected()); } while (mqttClient.connected());
Serial.println("Disconnected from MQTT."); Serial.println("Disconnected from MQTT.");
if (!mqttClient.connected()) { if (!mqttClient.connected()) {
Serial.println("Checking WiFi Connection."); Serial.println("Checking WiFi Connection.");
if (WiFi.isConnected()) { if (WiFi.isConnected()) {
Serial.println("Starting reconnect timer for MQTT."); Serial.println("WiFi OK. Starting reconnect timer for MQTT.");
xTimerStart(mqttReconnectTimer, 0); xTimerStart(mqttReconnectTimer, 0);
} }
} }
@ -51,9 +48,12 @@ void mqttLoop(void *parameter) {
} }
void connectMQTT() { void connectMQTT() {
yield();
if (mqttClient.connected()) return;
Serial.println("Connecting to MQTT..."); Serial.println("Connecting to MQTT...");
// mqttClient.begin(MQTT_HOST, MQTT_PORT, client); // mqttClient.begin(MQTT_HOST, MQTT_PORT, client);
mqttClient.setServer(MQTT_HOST, MQTT_PORT); mqttClient.setServer(MQTT_HOST, MQTT_PORT);
mqttClient.disconnect();
mqttClient.connect(MQTT_DEVICE_ID); mqttClient.connect(MQTT_DEVICE_ID);
if (mqttClient.connected()) { if (mqttClient.connected()) {
Serial.println("Connected!"); Serial.println("Connected!");
@ -63,17 +63,24 @@ void connectMQTT() {
mqttClient.subscribe(MQTT_PATH_SUB); mqttClient.subscribe(MQTT_PATH_SUB);
Serial.print("subscribed to: "); Serial.print("subscribed to: ");
Serial.println(MQTT_PATH_SUB); Serial.println(MQTT_PATH_SUB);
xTaskCreate( xTaskCreatePinnedToCore(
mqttLoop, /* Task function. */ mqttLoop,
"mqttLoop", /* String with name of task. */ "mqttLoop",
8192, /* Stack size in bytes. */ 8192,
NULL, /* Parameter passed as input of the task */ NULL,
1, /* Priority of the task. */ 2,
&mqttTask); /* Task handle. */ &mqttTask,
//xTimerStart(mqttProcessingTimer, 0); 1);
// xTaskCreate(
// mqttLoop, /* Task function. */
// "mqttLoop", /* String with name of task. */
// 8192, /* Stack size in bytes. */
// NULL, /* Parameter passed as input of the task */
// 1, /* Priority of the task. */
// &mqttTask); /* Task handle. */
} }
void WiFiEvent(WiFiEvent_t event) { void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.printf("[WiFi-event] event: %d\n", event); Serial.printf("[WiFi-event] event: %d\n", event);
switch (event) { switch (event) {
case SYSTEM_EVENT_STA_GOT_IP: case SYSTEM_EVENT_STA_GOT_IP:
@ -84,14 +91,18 @@ void WiFiEvent(WiFiEvent_t event) {
break; break;
case SYSTEM_EVENT_STA_DISCONNECTED: case SYSTEM_EVENT_STA_DISCONNECTED:
digitalWrite(PIN_LED_G, LOW); digitalWrite(PIN_LED_G, LOW);
// workaround for 202 auth failed bug: https://github.com/espressif/arduino-esp32/issues/2501
if (info.disconnected.reason == 202) {
Serial.println("weirdbehaviour");
Serial.println(info.disconnected.reason);
ESP.restart();
}
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
// xTimerStop(mqttProcessingTimer, 0);
xTimerStart(wifiReconnectTimer, 0); xTimerStart(wifiReconnectTimer, 0);
break; break;
} }
} }
// void onMqttMessage(MQTTClient *client, char topic[], char payload[], int payload_length) {
void onMqttMessage(char *topic, byte *payload, unsigned int payload_length) { void onMqttMessage(char *topic, byte *payload, unsigned int payload_length) {
Serial.print("Message arrived ["); Serial.print("Message arrived [");
Serial.print(topic); Serial.print(topic);
@ -109,9 +120,6 @@ void onMqttMessage(char *topic, byte *payload, unsigned int payload_length) {
if (strcmp(topic, MQTT_SOIL_PROPERTIES) == 0) { if (strcmp(topic, MQTT_SOIL_PROPERTIES) == 0) {
Serial.println("receiving soil thresholds..."); Serial.println("receiving soil thresholds...");
Serial.println(topic); Serial.println(topic);
//Serial.println(reinterpret_cast<const char *>(payload));
// Serial.println(payload);
// const int capacity = JSON_OBJECT_SIZE(3) + 2 * JSON_OBJECT_SIZE(1);
StaticJsonDocument<1024> doc; StaticJsonDocument<1024> doc;
DeserializationError err = deserializeJson(doc, payload); DeserializationError err = deserializeJson(doc, payload);
if (err == DeserializationError::Ok) { if (err == DeserializationError::Ok) {
@ -128,23 +136,32 @@ void onMqttMessage(char *topic, byte *payload, unsigned int payload_length) {
void setupConnections() { void setupConnections() {
Serial.println(); Serial.println();
Serial.println(); Serial.println();
disableCore0WDT();
// disableCore1WDT();
Config.autoReconnect = true;
// Config.autoReset = true;
Portal.config(Config);
mqttReconnectTimer = xTimerCreate( mqttReconnectTimer = xTimerCreate(
"mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void *)0, reinterpret_cast<TimerCallbackFunction_t>(connectMQTT)); "mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void *)0, reinterpret_cast<TimerCallbackFunction_t>(connectMQTT));
wifiReconnectTimer = xTimerCreate( wifiReconnectTimer = xTimerCreate(
"wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void *)0, reinterpret_cast<TimerCallbackFunction_t>(connectWiFi)); "wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void *)0, reinterpret_cast<TimerCallbackFunction_t>(connectWiFi));
// mqttProcessingTimer = xTimerCreate(
// "messageTimer", pdMS_TO_TICKS(50), pdTRUE, (void *)0, reinterpret_cast<TimerCallbackFunction_t>(mqttLoop));
WiFi.onEvent(WiFiEvent); WiFi.onEvent(WiFiEvent);
// mqttClient.onMessageAdvanced(onMqttMessage); connectWiFi();
connectMQTT();
mqttClient.setCallback(onMqttMessage); mqttClient.setCallback(onMqttMessage);
connectWiFi();
} }
void publishMessage(const char *topic, const char *msg) { void publishMessage(const char *topic, const char *msg) {
if(mqttClient.connected()) {
mqttClient.publish(topic, msg); mqttClient.publish(topic, msg);
// mqttClient.publish(topic, msg, true, 1); Serial.print(topic);
Serial.println(" successfully sent.\n");
} else {
Serial.println("couldn't send message. waiting for reconnect.\n");
}
} }

View File

@ -17,8 +17,8 @@
#include <AutoConnect.h> #include <AutoConnect.h>
// fix for core panic during wifi initialization // fix for core panic during wifi initialization
// #define configMINIMAL_STACK_SIZE 2048 #define configMINIMAL_STACK_SIZE 4096
// #define CONFIG_TIMER_TASK_STACK_SIZE 8192 #define CONFIG_TIMER_TASK_STACK_SIZE 8192
// DHT11 // DHT11
#define PIN_DHT11 14 #define PIN_DHT11 14
@ -52,7 +52,7 @@
// MQTT_DEVICE_ID "/#" // MQTT_DEVICE_ID "/#"
// PUBLISH FREQUENCY (MS) // PUBLISH FREQUENCY (MS)
#define FREQUENCY 3000 #define FREQUENCY 5000 //30000
// moisture // moisture
extern void setupCapacitiveSoilMoistureSensor(); extern void setupCapacitiveSoilMoistureSensor();
@ -76,3 +76,6 @@ 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 takeSemaphore();
void releaseSemaphore();

View File

@ -6,6 +6,9 @@
#include <string> #include <string>
#define TIME_TO_SLEEP 30
volatile int noSleepTasks = 0;
unsigned long sensorReadTimer = 0; unsigned long sensorReadTimer = 0;
bool valveOpen = false; bool valveOpen = false;
@ -25,6 +28,7 @@ void setup() {
setupLightSensor(); setupLightSensor();
setupTemperatureSensor(); setupTemperatureSensor();
setupCapacitiveSoilMoistureSensor(); setupCapacitiveSoilMoistureSensor();
// esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * 1000000);
Serial.println("Setup complete..."); Serial.println("Setup complete...");
Serial.println(); Serial.println();
Serial.println(); Serial.println();
@ -35,8 +39,18 @@ void loop() {
if (millis() - sensorReadTimer >= FREQUENCY) { if (millis() - sensorReadTimer >= FREQUENCY) {
readSensors(); readSensors();
sensorReadTimer = millis(); sensorReadTimer = millis();
}
// toggle valve // if(noSleepTasks == 0) {
if (valveOpen) { // delay(200);
// esp_deep_sleep_start();
// }
} }
} }
void takeSemaphore() {
noSleepTasks++;
}
void releaseSemaphore() {
noSleepTasks--;
}

View File

@ -70,6 +70,7 @@ bool closeValve() {
} }
void valveTask(void *parameter) { void valveTask(void *parameter) {
takeSemaphore();
unsigned long valveTimeoutTimer = millis(); unsigned long valveTimeoutTimer = millis();
bool valveOpen = openValve(); bool valveOpen = openValve();
@ -84,6 +85,7 @@ void valveTask(void *parameter) {
// valveOpen = closeValve(); // valveOpen = closeValve();
// } // }
} }
releaseSemaphore();
vTaskDelete(NULL); vTaskDelete(NULL);
} }
@ -98,6 +100,7 @@ void toggleValve() {
} }
void persistSoilProps(int FC, int PWP, int SAT) { void persistSoilProps(int FC, int PWP, int SAT) {
takeSemaphore();
Serial.println("persistSoilProps"); Serial.println("persistSoilProps");
bool f = NVS.setInt("fieldCapacity", FC); bool f = NVS.setInt("fieldCapacity", FC);
bool p = NVS.setInt("permanentWilt", PWP); bool p = NVS.setInt("permanentWilt", PWP);
@ -109,6 +112,7 @@ void persistSoilProps(int FC, int PWP, int SAT) {
else { else {
Serial.println("error occured while trying to persist soil properties"); Serial.println("error occured while trying to persist soil properties");
} }
releaseSemaphore();
} }
void restoreSoilProps() { void restoreSoilProps() {