esp_garden git · master
Distributed ESP sensor and actuator system (garden)
C++ 70.7% Markdown 18% C 10.7%git clone https://git.christianimmanuel.de/embedded/esp_garden.gitwget https://git.christianimmanuel.de/embedded/esp_garden/archive/esp_garden.tar.gzUpdate send pumping_delay and pumping_duration
client_sensor/ClientSensor.cpp | 18 ++++++++++-------- database.sql | 3 +-- globals.cpp | 4 ++-- globals.h | 6 ++++-- post_server/WiFiManager.cpp | 15 ++++++++------- server/server.ino | 10 ++++++---- 6 files changed, 31 insertions(+), 25 deletions(-) diff --git a/client_sensor/ClientSensor.cpp b/client_sensor/ClientSensor.cpp index 898370e..2f3e33e 100644 --- a/client_sensor/ClientSensor.cpp +++ b/client_sensor/ClientSensor.cpp @@ -52,8 +52,9 @@ void print_SensorConfig(const SensorConfig_s& cfg) { DBG_PRINTF(" is_active: %s\n", cfg.is_active ? "true" : "false"); DBG_PRINTF(" pin: %d\n", cfg.pin); DBG_PRINTF(" min_moisture: %d\n", cfg.min_moisture); - DBG_PRINTF(" notification_delay_ms: %d\n", cfg.notification_delay_ms); DBG_PRINTF(" notification_name: %s\n", cfg.notification_name); + DBG_PRINTF(" pumping_delay_sec: %d\n", cfg.pumping_delay_sec); + DBG_PRINTF(" pumping_duration: %d\n", cfg.pumping_duration); DBG_PRINTF(" pump_mac: "); printMac(cfg.pump_mac); DBG_PRINTF(" delay_ms: %d\n", cfg.delay_ms); DBG_PRINTF(" use_time: %s\n", cfg.use_time ? "true" : "false"); @@ -570,19 +571,20 @@ bool request_SensorConfig(const char* sensor_name) { cfg->is_active = atoi(tokens[1]) != 0; cfg->pin = atoi(tokens[2]); cfg->min_moisture = atoi(tokens[3]); - cfg->notification_delay_ms = atoi(tokens[4]); - strncpy(cfg->notification_name, tokens[5], sizeof(cfg->notification_name)-1); + strncpy(cfg->notification_name, tokens[4], sizeof(cfg->notification_name)-1); cfg->notification_name[sizeof(cfg->notification_name)-1] = '\0'; + cfg->pumping_delay_sec = atoi(tokens[5]); + cfg->pumping_duration = atoi(tokens[6]); uint8_t new_mac[6]; - sscanf(tokens[6], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", + sscanf(tokens[7], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &new_mac[0], &new_mac[1], &new_mac[2], &new_mac[3], &new_mac[4], &new_mac[5]); - cfg->delay_ms = atoi(tokens[7]); - cfg->use_time = atoi(tokens[8]) != 0; + cfg->delay_ms = atoi(tokens[8]); + cfg->use_time = atoi(tokens[9]) != 0; // times cfg->time_count = 0; - if (cfg->use_time && count_token > 9 && strncmp(tokens[9], "times=", 6) == 0) { - char *time_str = tokens[9] + 6; + if (cfg->use_time && count_token > 10 && strncmp(tokens[10], "times=", 6) == 0) { + char *time_str = tokens[10] + 6; char *time_tok = strtok(time_str, "|"); while (time_tok && cfg->time_count < 8) { cfg->times[cfg->time_count++] = (uint16_t)atoi(time_tok); diff --git a/database.sql b/database.sql index f67a63f..6f1e66f 100644 --- a/database.sql +++ b/database.sql @@ -58,7 +58,6 @@ CREATE TABLE esp_sensors ( pin INT NOT NULL, use_time BOOLEAN NOT NULL DEFAULT 0, delay_ms INT DEFAULT 0, - notification_delay_ms INT DEFAULT 0, min_moisture INT DEFAULT 0, is_active BOOLEAN NOT NULL DEFAULT 1, FOREIGN KEY (esp_id) REFERENCES esps(id) ON DELETE CASCADE, @@ -101,7 +100,7 @@ CREATE TABLE esp_pumps ( name VARCHAR(64) NOT NULL, info TEXT DEFAULT NULL, pin INT NOT NULL, - delay_ms INT DEFAULT 0, + pumping_delay_ms INT DEFAULT 0, pumping_duration INT UNSIGNED DEFAULT 0, UNIQUE KEY ux_esp_pumps_esp_name (esp_id, name), CONSTRAINT fk_esp_pumps_esp_id FOREIGN KEY (esp_id) REFERENCES esps(id) ON DELETE CASCADE ON UPDATE CASCADE diff --git a/globals.cpp b/globals.cpp index e658ac8..6e813c3 100644 --- a/globals.cpp +++ b/globals.cpp @@ -22,8 +22,8 @@ constexpr uint8_t PEER_SERVER[] = {0x58,0xcf,0x79,0x1e,0xed,0x18}; constexpr uint8_t PEER_PUMP[] = {0x51,0xc1,0x79,0x1e,0xed,0x18}; RTC_DATA_ATTR SensorConfig_s sensor_configs[] = { - {"moisture_1", true, 11, 10, 0, "pump_1", {0}, 90000, true, {465, 466}, 2, 0, 0}, - {"moisture_2", true, 11, 10, 0, "pump_2", {0}, 120000, false, {0}, 0, 0, 0} + {"moisture_1", true, 11, 10, "pump_1", 0, 0, {0}, 90000, true, {465, 466}, 2, 0, 0}, + {"moisture_2", true, 11, 10, "pump_2", 0, 0, {0}, 120000, false, {0}, 0, 0, 0} }; uint8_t config_request_attempts_max = 3; diff --git a/globals.h b/globals.h index c3f787d..a885c74 100644 --- a/globals.h +++ b/globals.h @@ -44,8 +44,9 @@ struct SensorConfigServer_s { bool is_active; int pin; int min_moisture; - int notification_delay_ms; char notification_name[64]; + int pumping_delay_sec; + int pumping_duration; char pump_mac[18]; // "AA:BB:CC:DD:EE:FF" int delay_ms; bool use_time; @@ -58,8 +59,9 @@ struct SensorConfig_s { bool is_active; int pin; int min_moisture; - int notification_delay_ms; char notification_name[64]; + int pumping_delay_sec; + int pumping_duration; uint8_t pump_mac[6]; // {0x51,0xc1,0x79,0x1e,0xed,0x18} int delay_ms; bool use_time; diff --git a/post_server/WiFiManager.cpp b/post_server/WiFiManager.cpp index 884d87e..77690a1 100644 --- a/post_server/WiFiManager.cpp +++ b/post_server/WiFiManager.cpp @@ -123,17 +123,18 @@ bool WIFI_GetSensorConfigServer(const char* sensor_name, SensorConfigServer_s &c out[out_size - 1] = '\0'; }; - cfg.success = extractBool("success"); - cfg.pin = extractInt("pin"); + cfg.success = extractBool("success"); + extractString("sensor_name", cfg.sensor_name, sizeof(cfg.sensor_name)); + cfg.pin = extractInt("pin"); cfg.use_time = extractBool("use_time"); cfg.delay_ms = extractInt("delay_ms"); - cfg.notification_delay_ms = extractInt("notification_delay_ms"); cfg.min_moisture = extractInt("min_moisture"); - cfg.is_active = extractBool("is_active"); + cfg.is_active = extractBool("is_active"); - extractString("sensor_name", cfg.sensor_name, sizeof(cfg.sensor_name)); extractString("notification_name", cfg.notification_name, sizeof(cfg.notification_name)); extractString("pump_mac", cfg.pump_mac, sizeof(cfg.pump_mac)); + cfg.pumping_delay_sec = extractInt("pumping_delay_sec"); + cfg.pumping_duration = extractInt("pumping_duration"); cfg.time_count = 0; int times_start = body.indexOf("\"times\":["); @@ -163,8 +164,8 @@ bool WIFI_GetSensorConfigServer(const char* sensor_name, SensorConfigServer_s &c DBG_PRINTF("Parsed: success=%d pin=%d min=%d delay=%d use_time=%d time_count=%u\n", cfg.success, cfg.pin, cfg.min_moisture, cfg.delay_ms, cfg.use_time, cfg.time_count); - DBG_PRINTF("sensor_name='%s' notification='%s' pump_mac='%s'\n", - cfg.sensor_name, cfg.notification_name, cfg.pump_mac); + DBG_PRINTF("sensor_name='%s' notification='%s' pump_mac='%s' pumping_delay_sec='%d' pumping_duration='%d'\n", + cfg.sensor_name, cfg.notification_name, cfg.pump_mac, cfg.pumping_delay_sec, cfg.pumping_duration); return cfg.success; } diff --git a/server/server.ino b/server/server.ino index c7a26c9..3fd0797 100644 --- a/server/server.ino +++ b/server/server.ino @@ -189,9 +189,9 @@ void setup() { init_Server(); init_EspNow(); - /* SensorConfigServer_s cfg; + /* if (WIFI_GetSensorConfigServer("moisture_1", cfg)) { DBG_PRINTF("Got cfg for moisture_1:\n"); DBG_PRINTF("Pin: %d\n", cfg.pin); @@ -201,7 +201,8 @@ if (WIFI_GetSensorConfigServer("moisture_1", cfg)) { DBG_PRINTF("Notification Name: %s\n", cfg.notification_name); DBG_PRINTF("Min Moisture: %d\n", cfg.min_moisture); DBG_PRINTF("Active: %d\n", cfg.is_active); - DBG_PRINTF("Notification Delay: %d\n", cfg.notification_delay_ms); + DBG_PRINTF("Pumping Delay: %d\n", cfg.pumping_delay_sec); + DBG_PRINTF("Pumping Duration: %d\n", cfg.pumping_duration); for (uint8_t i = 0; i < cfg.time_count; i++) { uint16_t minutes = cfg.times[i]; @@ -225,13 +226,14 @@ void loop() { if (WIFI_GetSensorConfigServer(pending_config.sensor_name, cfg_response)) { char response[250]; int len_out = snprintf(response, sizeof(response), - "cfg:%s,%d,%d,%d,%d,%s,%s,%d,%d", + "cfg:%s,%d,%d,%d,%s,%d,%d,%s,%d,%d", cfg_response.sensor_name, cfg_response.is_active, cfg_response.pin, cfg_response.min_moisture, - cfg_response.notification_delay_ms, cfg_response.notification_name, + cfg_response.pumping_delay_sec, + cfg_response.pumping_duration, cfg_response.pump_mac, cfg_response.delay_ms, cfg_response.use_time);