Skip to content

Commit

Permalink
feat: add more system telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigorii Merkushev committed Sep 29, 2021
1 parent 14bdd10 commit bff8e08
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 13 deletions.
9 changes: 7 additions & 2 deletions esp32/src/climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@ time in UTC
//Serial.println("turn relay off");
}

void climateSetup()
void climateSetup(uint32_t now)
{
pinMode(HEATER_RELAY_PIN, OUTPUT);
turnRelayOff();
dhtHotSide.begin();
dhtHotCenter.begin();
dhtColdCenter.begin();
dhtColdSide.begin();
lastNotNullReadings = now;
}

Telemetry::TelemteryData climateControl(int hour, int minute, uint32_t now)
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion esp32/src/climate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Climate
{

void climateSetup();
void climateSetup(uint32_t now);
ClimateData readTempHumid(DHT_Unified dht);
void turnRelayOn();
void turnRelayOff();
Expand Down
4 changes: 2 additions & 2 deletions esp32/src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 12 additions & 1 deletion esp32/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ void setup()
//connect();
RealTime::setupRtcModule();

int now = RealTime::getTimestamp();

//securitySetup();
climateSetup();
climateSetup(now);
//ledSetup();

//Encoder::setup();
Expand All @@ -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;

Expand Down
10 changes: 9 additions & 1 deletion esp32/src/real_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 6 additions & 2 deletions esp32/src/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions esp32/src/telemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 13 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand Down Expand Up @@ -90,7 +99,7 @@ func recordTelemetry(w http.ResponseWriter, req *http.Request) {
}

storage[id] = Storage{
Data: telemetry,
Data: telemetry,
Timestamp: time.Now().Unix(),
}

Expand All @@ -116,7 +125,7 @@ func main() {

err := http.ListenAndServe(":80", muxRouter)

if err != nil{
if err != nil {
panic(err)
}
}

0 comments on commit bff8e08

Please sign in to comment.