Merge branch 'develop' into sherzog_dev
This commit is contained in:
commit
a50ca3f4eb
@ -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
|
||||||
|
|||||||
@ -14,8 +14,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;
|
||||||
@ -23,12 +21,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);
|
||||||
@ -37,14 +33,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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -52,9 +49,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!");
|
||||||
@ -64,17 +64,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:
|
||||||
@ -85,14 +92,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);
|
||||||
@ -123,9 +134,6 @@ Serial.println("receiving light treshold...");
|
|||||||
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) {
|
||||||
@ -142,23 +150,32 @@ Serial.println("receiving light treshold...");
|
|||||||
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) {
|
||||||
mqttClient.publish(topic, msg);
|
if(mqttClient.connected()) {
|
||||||
// mqttClient.publish(topic, msg, true, 1);
|
mqttClient.publish(topic, msg);
|
||||||
|
Serial.print(topic);
|
||||||
|
Serial.println(" successfully sent.\n");
|
||||||
|
} else {
|
||||||
|
Serial.println("couldn't send message. waiting for reconnect.\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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
|
||||||
|
|
||||||
// BH1750 lightsensor
|
// BH1750 lightsensor
|
||||||
#define MIN_LIGHT 0
|
#define MIN_LIGHT 0
|
||||||
@ -62,7 +62,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();
|
||||||
@ -93,3 +93,6 @@ void toggleValve();
|
|||||||
void setSoilProperties(int FC, int PWP, int SAT);
|
void setSoilProperties(int FC, int PWP, int SAT);
|
||||||
void setupStore();
|
void setupStore();
|
||||||
void setLightningProperties(int NM, int minLX);
|
void setLightningProperties(int NM, int minLX);
|
||||||
|
|
||||||
|
void takeSemaphore();
|
||||||
|
void releaseSemaphore();
|
||||||
|
|||||||
20
src/main.cpp
20
src/main.cpp
@ -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;
|
||||||
|
|
||||||
@ -26,6 +29,7 @@ void setup() {
|
|||||||
setupPWM();
|
setupPWM();
|
||||||
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();
|
||||||
@ -36,8 +40,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--;
|
||||||
|
}
|
||||||
@ -75,6 +75,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();
|
||||||
|
|
||||||
@ -89,6 +90,7 @@ void valveTask(void *parameter) {
|
|||||||
// valveOpen = closeValve();
|
// valveOpen = closeValve();
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
releaseSemaphore();
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,6 +105,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);
|
||||||
@ -114,6 +117,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() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user