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.gzFix post time from sensor side
client_sensor/ClientSensor.cpp | 6 ++---- client_sensor/client_sensor.ino | 1 + globals.h | 13 ++----------- post_server/WiFiManager.cpp | 2 +- post_server/WiFiManager.h | 2 +- server/server.ino | 26 +++++++++++++++++++------- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/client_sensor/ClientSensor.cpp b/client_sensor/ClientSensor.cpp index cb98842..877007e 100644 --- a/client_sensor/ClientSensor.cpp +++ b/client_sensor/ClientSensor.cpp @@ -117,7 +117,7 @@ void restore_SensorConfigs() { } else { DBG_PRINTLN("No stored configs found, using defaults."); for (size_t i = 0; i < SENSOR_CONFIG_COUNT; i++) - memcpy(sensor_configs[i].pump_mac, PEER_PUMP, MAC_ADDR_LEN); + memcpy(sensor_configs[i].pump_mac, PEER_PUMP, 6); } } @@ -650,7 +650,7 @@ void send_DataToPeer(size_t config_id, uint32_t value) { DBG_PRINTF("send_DataToPeer: %s\n", p.sensor_name); - snprintf(message, sizeof(message), "%s:%u", p.sensor_name, value); + snprintf(message, sizeof(message), "%s:%u:%lld", p.sensor_name, value, p.last_sensor_read); DBG_PRINT("Sending to peer: "); printMac(p.pump_mac); @@ -658,8 +658,6 @@ void send_DataToPeer(size_t config_id, uint32_t value) { if (!send_AndWaitAck(p.pump_mac, message)) { DBG_PRINTLN("Delivery failed or peer offline!"); send_ack_process_complete = true; - } else { - value++; // we should remove this lateron! } while (!send_ack_process_complete) delay(100); diff --git a/client_sensor/client_sensor.ino b/client_sensor/client_sensor.ino index f5b257c..5562f38 100644 --- a/client_sensor/client_sensor.ino +++ b/client_sensor/client_sensor.ino @@ -51,6 +51,7 @@ void loop() { // send to server and configured peer (e.g. pump) send_DataToPeer(config_id, value_to_send); + fake_value_to_send++; } } diff --git a/globals.h b/globals.h index 0301c7c..352858e 100644 --- a/globals.h +++ b/globals.h @@ -20,8 +20,6 @@ // Network Time #define NTP_SERVER "pool.ntp.org" -#define GMT_OFFSET_SEC 0 -#define DAYLIGHT_OFFSET_SEC 0 // Size for buffering data @@ -31,9 +29,10 @@ #define JSON_PER_ENTRY (29 + (TABLE_SIZE-1) + (VALUE_SIZE-1) + 10) #define JSON_BUFFER_SIZE (BUFFER_SIZE * JSON_PER_ENTRY + 2) -#define MAC_ADDR_LEN 6 +#define SENSOR_CONFIG_COUNT 2 + struct SensorConfigServer_s { char sensor_name[32]; bool success; @@ -70,17 +69,10 @@ struct SensorEntry { int64_t time; }; -typedef struct struct_message_server { - int64_t time; -} struct_message_server; -typedef struct struct_message_client { - char data[64]; -} struct_message_client; extern SensorEntry g_buffer[BUFFER_SIZE]; extern uint8_t g_buffer_count; -extern int64_t g_time_now; extern char g_pending_sensor_configs[4][32]; extern uint8_t g_pending_sensor_configs_count; extern bool g_received_ok_from_server; @@ -93,6 +85,5 @@ extern const uint32_t min_sleep_time; extern RTC_DATA_ATTR bool all_init_configs_received; extern const uint8_t PEER_SERVER[]; extern const uint8_t PEER_PUMP[]; -#define SENSOR_CONFIG_COUNT 2 extern RTC_DATA_ATTR SensorConfig_s sensor_configs[]; extern uint8_t config_request_attempts_max; diff --git a/post_server/WiFiManager.cpp b/post_server/WiFiManager.cpp index b135b38..884d87e 100644 --- a/post_server/WiFiManager.cpp +++ b/post_server/WiFiManager.cpp @@ -197,7 +197,7 @@ void WIFI_Try(uint8_t max_attempts) { void WIFI_SyncTime() { DBG_PRINTLN("Syncing time..."); - configTime(GMT_OFFSET_SEC, DAYLIGHT_OFFSET_SEC, NTP_SERVER); + configTime(0, 0, NTP_SERVER); int64_t t = now(); uint8_t tries = 0; while (t < 1600000000 && tries++ < 20) { delay(500); t = time(nullptr); } diff --git a/post_server/WiFiManager.h b/post_server/WiFiManager.h index 4883f55..d93362a 100644 --- a/post_server/WiFiManager.h +++ b/post_server/WiFiManager.h @@ -7,7 +7,7 @@ bool WIFI_GetSensorConfigServer(const char* sensor_name, SensorConfigServer_s &cfg); -using WIFI_ReadingCallback = void(*)(const char* table, const char* value); +using WIFI_ReadingCallback = void(*)(const char* table, const char* value, const int64_t time); extern WIFI_ReadingCallback WIFI_OnAddReading; int64_t now(); diff --git a/server/server.ino b/server/server.ino index 365f218..14d1474 100644 --- a/server/server.ino +++ b/server/server.ino @@ -22,7 +22,7 @@ SensorConfigServer_s cfg_response; // Server handling (g_buffer) // ---------------------------------------------------------------------------- -void add_ReadingCallback(const char* table, const char* value) { +void add_ReadingCallback(const char* table, const char* value, const int64_t time) { if (g_buffer_count >= BUFFER_SIZE) { for (int i = 1; i < g_buffer_count; i++) g_buffer[i-1] = g_buffer[i]; g_buffer_count--; @@ -30,7 +30,7 @@ void add_ReadingCallback(const char* table, const char* value) { strncpy(g_buffer[g_buffer_count].table, table, TABLE_SIZE-1); g_buffer[g_buffer_count].table[TABLE_SIZE-1] = '\0'; strncpy(g_buffer[g_buffer_count].value, value, VALUE_SIZE-1); - g_buffer[g_buffer_count].time = now(); + g_buffer[g_buffer_count].time = time; g_buffer_count++; } @@ -135,14 +135,26 @@ void on_DataRecv(const esp_now_recv_info *info, const uint8_t *data, int len) { return; } - // --- Normal table:value handling --- + // --- Normal table:value:time handling --- DBG_PRINTLN("Message:"); DBG_PRINTF(" %s\n", msg); g_received_ok_from_server = false; - char *sep = strchr(msg, ':'); - if (sep) { - *sep = '\0'; - add_ReadingCallback(msg, sep + 1); + + // expected format: <sensor_name>:<value>:<timestamp> + char *first = strchr(msg, ':'); + char *second = first ? strchr(first + 1, ':') : NULL; + + if (first && second) { + *first = '\0'; + *second = '\0'; + + const char *sensor_name = msg; + const char *value_str = first + 1; + const int64_t time = atoll(second + 1); + + add_ReadingCallback(sensor_name, value_str, time); + } else { + DBG_PRINTLN("Invalid message format (expected <name>:<value>:<time>)"); } // --- Default reply with time ---