Merge branch 'develop' into sherzog_dev

This commit is contained in:
Sebastian 2020-07-11 12:06:15 +02:00
commit a50ca3f4eb
5 changed files with 74 additions and 35 deletions

View File

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

View File

@ -14,8 +14,6 @@ extern "C" {
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
// TimerHandle_t mqttProcessingTimer;
TaskHandle_t mqttTask;
WebServer Server;
@ -23,12 +21,10 @@ AutoConnect Portal(Server);
WiFiClient client;
AutoConnectConfig Config;
// Config.autoReconnect = true;
// WiFi.config(Config);
// MQTTClient mqttClient;
PubSubClient mqttClient(client);
void connectWiFi() {
yield();
Serial.println("Start WiFi...");
if (Portal.begin()) {
digitalWrite(PIN_LED_G, HIGH);
@ -37,14 +33,15 @@ void connectWiFi() {
void mqttLoop(void *parameter) {
do {
yield();
mqttClient.loop();
delay(50);
delay(100);
} while (mqttClient.connected());
Serial.println("Disconnected from MQTT.");
if (!mqttClient.connected()) {
Serial.println("Checking WiFi Connection.");
if (WiFi.isConnected()) {
Serial.println("Starting reconnect timer for MQTT.");
Serial.println("WiFi OK. Starting reconnect timer for MQTT.");
xTimerStart(mqttReconnectTimer, 0);
}
}
@ -52,9 +49,12 @@ void mqttLoop(void *parameter) {
}
void connectMQTT() {
yield();
if (mqttClient.connected()) return;
Serial.println("Connecting to MQTT...");
// mqttClient.begin(MQTT_HOST, MQTT_PORT, client);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
mqttClient.disconnect();
mqttClient.connect(MQTT_DEVICE_ID);
if (mqttClient.connected()) {
Serial.println("Connected!");
@ -64,17 +64,24 @@ void connectMQTT() {
mqttClient.subscribe(MQTT_PATH_SUB);
Serial.print("subscribed to: ");
Serial.println(MQTT_PATH_SUB);
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. */
//xTimerStart(mqttProcessingTimer, 0);
xTaskCreatePinnedToCore(
mqttLoop,
"mqttLoop",
8192,
NULL,
2,
&mqttTask,
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);
switch (event) {
case SYSTEM_EVENT_STA_GOT_IP:
@ -85,14 +92,18 @@ void WiFiEvent(WiFiEvent_t event) {
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
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(mqttProcessingTimer, 0);
xTimerStart(wifiReconnectTimer, 0);
break;
}
}
// void onMqttMessage(MQTTClient *client, char topic[], char payload[], int payload_length) {
void onMqttMessage(char *topic, byte *payload, unsigned int payload_length) {
Serial.print("Message arrived [");
Serial.print(topic);
@ -123,9 +134,6 @@ Serial.println("receiving light treshold...");
if (strcmp(topic, MQTT_SOIL_PROPERTIES) == 0) {
Serial.println("receiving soil thresholds...");
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;
DeserializationError err = deserializeJson(doc, payload);
if (err == DeserializationError::Ok) {
@ -142,23 +150,32 @@ Serial.println("receiving light treshold...");
void setupConnections() {
Serial.println();
Serial.println();
disableCore0WDT();
// disableCore1WDT();
Config.autoReconnect = true;
// Config.autoReset = true;
Portal.config(Config);
mqttReconnectTimer = xTimerCreate(
"mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void *)0, reinterpret_cast<TimerCallbackFunction_t>(connectMQTT));
wifiReconnectTimer = xTimerCreate(
"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);
// mqttClient.onMessageAdvanced(onMqttMessage);
connectWiFi();
connectMQTT();
mqttClient.setCallback(onMqttMessage);
connectWiFi();
}
void publishMessage(const char *topic, const char *msg) {
mqttClient.publish(topic, msg);
// mqttClient.publish(topic, msg, true, 1);
if(mqttClient.connected()) {
mqttClient.publish(topic, msg);
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>
// fix for core panic during wifi initialization
// #define configMINIMAL_STACK_SIZE 2048
// #define CONFIG_TIMER_TASK_STACK_SIZE 8192
#define configMINIMAL_STACK_SIZE 4096
#define CONFIG_TIMER_TASK_STACK_SIZE 8192
// BH1750 lightsensor
#define MIN_LIGHT 0
@ -62,7 +62,7 @@
// MQTT_DEVICE_ID "/#"
// PUBLISH FREQUENCY (MS)
#define FREQUENCY 3000
#define FREQUENCY 5000 //30000
// moisture
extern void setupCapacitiveSoilMoistureSensor();
@ -92,4 +92,7 @@ void readSensors();
void toggleValve();
void setSoilProperties(int FC, int PWP, int SAT);
void setupStore();
void setLightningProperties(int NM, int minLX);
void setLightningProperties(int NM, int minLX);
void takeSemaphore();
void releaseSemaphore();

View File

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

View File

@ -75,6 +75,7 @@ bool closeValve() {
}
void valveTask(void *parameter) {
takeSemaphore();
unsigned long valveTimeoutTimer = millis();
bool valveOpen = openValve();
@ -89,6 +90,7 @@ void valveTask(void *parameter) {
// valveOpen = closeValve();
// }
}
releaseSemaphore();
vTaskDelete(NULL);
}
@ -103,6 +105,7 @@ void toggleValve() {
}
void persistSoilProps(int FC, int PWP, int SAT) {
takeSemaphore();
Serial.println("persistSoilProps");
bool f = NVS.setInt("fieldCapacity", FC);
bool p = NVS.setInt("permanentWilt", PWP);
@ -114,6 +117,7 @@ void persistSoilProps(int FC, int PWP, int SAT) {
else {
Serial.println("error occured while trying to persist soil properties");
}
releaseSemaphore();
}
void restoreSoilProps() {