From bff8e08e81c3bdb989156a5f40c14d4045dfff9a Mon Sep 17 00:00:00 2001 From: Grigorii Merkushev Date: Wed, 29 Sep 2021 11:56:02 +0200 Subject: [PATCH] feat: add more system telemetry --- esp32/src/climate.cpp | 9 +++++++-- esp32/src/climate.h | 2 +- esp32/src/config.h | 4 ++-- esp32/src/main.cpp | 13 ++++++++++++- esp32/src/real_time.cpp | 10 +++++++++- esp32/src/telemetry.cpp | 8 ++++++-- esp32/src/telemetry.h | 5 +++++ server/server.go | 17 +++++++++++++---- 8 files changed, 55 insertions(+), 13 deletions(-) diff --git a/esp32/src/climate.cpp b/esp32/src/climate.cpp index b061a89..5592c73 100644 --- a/esp32/src/climate.cpp +++ b/esp32/src/climate.cpp @@ -97,7 +97,7 @@ time in UTC //Serial.println("turn relay off"); } - void climateSetup() + void climateSetup(uint32_t now) { pinMode(HEATER_RELAY_PIN, OUTPUT); turnRelayOff(); @@ -105,6 +105,7 @@ time in UTC dhtHotCenter.begin(); dhtColdCenter.begin(); dhtColdSide.begin(); + lastNotNullReadings = now; } Telemetry::TelemteryData climateControl(int hour, int minute, uint32_t now) @@ -124,11 +125,15 @@ time in UTC // Serial.println("4: cold side"); ClimateData coldSide = readTempHumid(dhtColdSide); - if (hotSide.t > 0 || hotCenter.t > 0) + if ((hotSide.t > 0 || hotCenter.t > 0) && (coldCenter.t > 0 || coldSide.t > 0)) { lastNotNullReadings = now; } + Serial.print("reboot in: "); + Serial.print(MAX_NULL_READINGS_SEC - (now - lastNotNullReadings)); + Serial.println(" sec"); + if (lastNotNullReadings != 0 && now - lastNotNullReadings > MAX_NULL_READINGS_SEC) { ESP.restart(); diff --git a/esp32/src/climate.h b/esp32/src/climate.h index 2a8c622..b7580ea 100644 --- a/esp32/src/climate.h +++ b/esp32/src/climate.h @@ -11,7 +11,7 @@ namespace Climate { - void climateSetup(); + void climateSetup(uint32_t now); ClimateData readTempHumid(DHT_Unified dht); void turnRelayOn(); void turnRelayOff(); diff --git a/esp32/src/config.h b/esp32/src/config.h index 9bfd428..d6f69af 100644 --- a/esp32/src/config.h +++ b/esp32/src/config.h @@ -2,8 +2,8 @@ #define CONFIG #define SENSORS_COUNT 2 -#define TERRARIUM_ID 1 +#define TERRARIUM_ID 10 -#define VERSION "v2.0.1" +#define VERSION "v2.2.0" #endif \ No newline at end of file diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 8234ab1..af32f85 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -43,8 +43,10 @@ void setup() //connect(); RealTime::setupRtcModule(); + int now = RealTime::getTimestamp(); + //securitySetup(); - climateSetup(); + climateSetup(now); //ledSetup(); //Encoder::setup(); @@ -70,15 +72,24 @@ void loop() //Encoder::isTurn(); // turnLedOn(255,0,0); + + if (now - lastTimeReinit >= DISPLAY_REINIT_INTERVAL) { Display::displaySetup(); lastTimeReinit = now; + } if (now - lastSensorFetch >= HARVESTING_INTERVAL_SEC) { Telemetry::TelemteryData telemteryData = climateControl(RealTime::getHour(), RealTime::getMinute(), now); + telemteryData.hour = RealTime::getHour(); + telemteryData.minute = RealTime::getMinute(); + telemteryData.second = RealTime::getSecond(); + telemteryData.version = VERSION; + telemteryData.uptime = esp_timer_get_time() / 1000000; + gTelemteryData = telemteryData; lastSensorFetch = now; diff --git a/esp32/src/real_time.cpp b/esp32/src/real_time.cpp index b9fa003..3e822a6 100644 --- a/esp32/src/real_time.cpp +++ b/esp32/src/real_time.cpp @@ -58,7 +58,15 @@ namespace RealTime int getHour() { - return int(rtc.now().hour()); + + int hour = int(rtc.now().hour()); + + if (hour > 23){ + syncTime(); + hour = int(rtc.now().hour()); + } + + return hour; } int getMinute() diff --git a/esp32/src/telemetry.cpp b/esp32/src/telemetry.cpp index 8b2a4d9..cbd2c53 100644 --- a/esp32/src/telemetry.cpp +++ b/esp32/src/telemetry.cpp @@ -72,8 +72,12 @@ namespace Telemetry doc["climate_config"]["night_temp_tolerance_warm"] = telemteryData.climateConfig.nightTempToleranceWarm; doc["climate_config"]["day_temp_tolerance_cold"] = telemteryData.climateConfig.dayTempToleranceCold; doc["climate_config"]["night_temp_tolerance_cold"] = telemteryData.climateConfig.nightTempToleranceCold; - - + doc.createNestedObject("system"); + doc["system"]["version"] = telemteryData.version; + doc["system"]["uptime"] = telemteryData.uptime; + doc["system"]["hour"] = telemteryData.hour; + doc["system"]["minute"] = telemteryData.minute; + doc["system"]["second"] = telemteryData.second; String requestBody; serializeJson(doc, requestBody); diff --git a/esp32/src/telemetry.h b/esp32/src/telemetry.h index dad661c..532774a 100644 --- a/esp32/src/telemetry.h +++ b/esp32/src/telemetry.h @@ -32,6 +32,11 @@ namespace Telemetry bool heater; HeaterPhase heaterPhase; bool fullfilled; + char* version; + int uptime; + int hour; + int minute; + int second; }; void send(TelemteryData telemteryData); diff --git a/server/server.go b/server/server.go index 6205f51..7ef6d53 100644 --- a/server/server.go +++ b/server/server.go @@ -26,6 +26,14 @@ type ClimateConfig struct { NightTempToleranceCold float64 `json:"night_temp_tolerance_cold"` } +type System struct { + Version float64 `json:"version"` + Uptime float64 `json:"uptime"` + Hour float64 `json:"hour"` + Minute float64 `json:"minute"` + Second float64 `json:"second"` +} + type Data struct { HotSide ClimateData `json:"hot_side"` HotCenter ClimateData `json:"hot_center"` @@ -41,8 +49,9 @@ var errResponse struct { } type Storage struct { - Data Data `json:"data"` - Timestamp int64 `json:"timestamp"` + Data Data `json:"data"` + Timestamp int64 `json:"timestamp"` + System System `json:"system"` } var storage map[int]Storage @@ -90,7 +99,7 @@ func recordTelemetry(w http.ResponseWriter, req *http.Request) { } storage[id] = Storage{ - Data: telemetry, + Data: telemetry, Timestamp: time.Now().Unix(), } @@ -116,7 +125,7 @@ func main() { err := http.ListenAndServe(":80", muxRouter) - if err != nil{ + if err != nil { panic(err) } }