Skip to content

Commit

Permalink
Change time vars to 64-bit (wrapped @ 24.85 days); consistent temp se…
Browse files Browse the repository at this point in the history
…tting; stop resetting relays @ start
  • Loading branch information
smeisner committed Oct 28, 2023
1 parent 0d3961d commit 3747eef
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 54 deletions.
11 changes: 6 additions & 5 deletions app/include/thermostat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ typedef struct

extern OPERATING_PARAMETERS OperatingParameters;
extern WIFI_CREDS WifiCreds;
extern int32_t ui_WifiStatusLabel_timestamp;
extern int64_t ui_WifiStatusLabel_timestamp;

#ifdef MQTT_ENABLED
#define MQTT_RECONNECT_DELAY 75000
Expand All @@ -140,7 +140,7 @@ extern void app_main();
} /*extern "C"*/
#endif

int32_t millis(); // Defined in main.cpp
int64_t millis(); // Defined in main.cpp

void showConfigurationData();
void scanI2cBus();
Expand All @@ -158,7 +158,7 @@ void MqttHomeAssistantDiscovery();

// State Machine
void stateCreateTask();
extern int32_t lastWifiReconnect;
extern int64_t lastWifiReconnect;

// EEPROM
void eepromInit();
Expand Down Expand Up @@ -218,12 +218,13 @@ void updateHvacSetTemp(float setTemp);
// float roundValue(float value, int places = 0);
float roundValue(float value, int places);
float getRoundedFrac(float value);
void initTimeSntp();
// int degCfrac(float tempF);
// int tempOut(float tempF);
// float tempIn(float tempC);
// float degFtoC(float degF);
void resetTempSmooth();
bool sensorsInit();
void sensorsInit();
void initRelays();
int getTemp();
int getHumidity();
Expand All @@ -237,7 +238,7 @@ void audioBeep();

// SNTP Time Sync
void updateTimezone();
bool getLocalTime(struct tm *, uint32_t);
bool getLocalTime(struct tm *, uint64_t);
void updateTimeSntp();

// ui_events.cpp
Expand Down
16 changes: 10 additions & 6 deletions app/src/indicators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void audioStartupBeep()
}

// Use lastBeep to rate limit the beeping
unsigned long lastBeep = 0;
uint64_t lastBeep = 0;

void audioBeep()
{
Expand Down Expand Up @@ -119,19 +119,23 @@ void audioBeep()

void initRelays()
{
vTaskDelay(pdMS_TO_TICKS(500));
vTaskDelay(pdMS_TO_TICKS(250));

gpio_reset_pin((gpio_num_t)HVAC_HEAT_PIN);
gpio_set_direction((gpio_num_t)HVAC_HEAT_PIN, GPIO_MODE_OUTPUT);
gpio_set_level((gpio_num_t)HVAC_HEAT_PIN, LOW);
// gpio_reset_pin((gpio_num_t)HVAC_HEAT_PIN);

gpio_reset_pin((gpio_num_t)HVAC_COOL_PIN);
gpio_set_direction((gpio_num_t)HVAC_COOL_PIN, GPIO_MODE_OUTPUT);
gpio_set_level((gpio_num_t)HVAC_COOL_PIN, LOW);
// gpio_reset_pin((gpio_num_t)HVAC_COOL_PIN);

gpio_reset_pin((gpio_num_t)HVAC_FAN_PIN);
gpio_set_direction((gpio_num_t)HVAC_FAN_PIN, GPIO_MODE_OUTPUT);
gpio_set_level((gpio_num_t)HVAC_FAN_PIN, LOW);
// gpio_reset_pin((gpio_num_t)HVAC_FAN_PIN);

gpio_reset_pin((gpio_num_t)HVAC_RVALV_PIN);
gpio_set_direction((gpio_num_t)HVAC_RVALV_PIN, GPIO_MODE_OUTPUT);
gpio_set_level((gpio_num_t)HVAC_RVALV_PIN, LOW);
// gpio_reset_pin((gpio_num_t)HVAC_RVALV_PIN);
}


Expand Down
12 changes: 8 additions & 4 deletions app/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@

#include "thermostat.hpp"
#include "esp_timer.h"
int32_t millis() { return esp_timer_get_time() / 1000;}
#define TAG "Main"

int64_t millis() { return esp_timer_get_time() / 1000;}

void app_main()
{
ESP_LOGI (TAG, "IDF version: %s", esp_get_idf_version());
Expand All @@ -43,6 +44,10 @@ void app_main()
ESP_LOGI (TAG, "Starting TFT task");
tftCreateTask();

// Initialize sensors (temp, humidity, motion, etc)
ESP_LOGI (TAG, "Initializing sensors");
sensorsInit();

#ifdef MATTER_ENABLED
// Start Matter
ESP_LOGI (TAG, "Starting Matter");
Expand All @@ -62,14 +67,13 @@ void app_main()
MqttInit();
#endif

// Start SNTP connection to get local time
initTimeSntp();

// Initialize indicators (relays, LEDs, buzzer)
ESP_LOGI (TAG, "Initializing indicators");
indicatorsInit();
initRelays();
// Initialize sensors (temp, humidity, motion, etc)
ESP_LOGI (TAG, "Initializing sensors");
sensorsInit();

// Create the RTOS task to drive the state machine
ESP_LOGI (TAG, "Starting state machine task");
Expand Down
57 changes: 29 additions & 28 deletions app/src/sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ const char *gmt_timezones[] =
{"GMT-12", "GMT-11", "GMT-10", "GMT-9", "GMT-8", "GMT-7", "GMT-6", "GMT-5", "GMT-4", "GMT-3", "GMT-2", "GMT-1"
"GMT", "GMT+1", "GMT+2", "GMT+3", "GMT+4", "GMT+5", "GMT+6", "GMT+7", "GMT+8", "GMT+9", "GMT+10", "GMT+11"};

int32_t lastTimeUpdate = 0;
int64_t lastTimeUpdate = 0;

Stream RadarPort;
ld2410 radar;
uint32_t last_ld2410_Reading = 0;
uint64_t last_ld2410_Reading = 0;

Smoothed<float> sensorTemp;
Smoothed<float> sensorHumidity;
Expand All @@ -65,6 +65,7 @@ void updateHvacSetTemp(float setTemp)
{
OperatingParameters.tempSet = setTemp;
eepromUpdateHvacSetTemp();
ESP_LOGI(TAG, "Set temp: %.1f", setTemp);
#ifdef MQTT_ENABLED
MqttUpdateStatusTopic();
#endif
Expand Down Expand Up @@ -126,7 +127,6 @@ float roundValue(float value, int places)
r = (float)((int)(value + 0.5));
if (places == 1)
r = (float)((int)(value + 0.25) + (getRoundedFrac(value + 0.25) / 10.0));
ESP_LOGI(TAG, "%.1f", r);
return r;
}

Expand Down Expand Up @@ -299,6 +299,27 @@ void updateAht(void *parameter)
}
}

bool startAht()
{
if (initAht())
{
xTaskCreate(
updateAht, // Function that should be called
"Update AHT", // Name of the task (for debugging)
4096, // Stack size (bytes)
NULL, // Parameter to pass
tskIDLE_PRIORITY + 1, // Task priority
NULL // Task handle
);

return true;
}
else
{
return false;
}
}

// Read sensor temp and return rounded up and correction applied
int getTemp()
{
Expand Down Expand Up @@ -336,10 +357,10 @@ void updateTimezone()
tzset();
}

bool getLocalTime(struct tm * info, uint32_t ms)
bool getLocalTime(struct tm * info, uint64_t ms)
{
uint32_t start = millis();
uint32_t tmo = ms;
uint64_t start = millis();
uint64_t tmo = ms;
time_t now;

if (!wifiConnected())
Expand Down Expand Up @@ -395,34 +416,14 @@ void initTimeSntp()
/*---------------------------------------------------------------
Init code for sensors entry point
---------------------------------------------------------------*/
bool sensorsInit()
void sensorsInit()
{
sensorTemp.begin(SMOOTHED_EXPONENTIAL, 10);
sensorHumidity.begin(SMOOTHED_EXPONENTIAL, 10);
sensorTemp.clear();
sensorHumidity.clear();

initTimeSntp();

startAht();
ld2410_init();

initLightSensor();

if (initAht())
{
xTaskCreate(
updateAht, // Function that should be called
"Update AHT", // Name of the task (for debugging)
4096, // Stack size (bytes)
NULL, // Parameter to pass
tskIDLE_PRIORITY + 1, // Task priority
NULL // Task handle
);

return true;
}
else
{
return false;
}
}
6 changes: 3 additions & 3 deletions app/src/state_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#define LOW 0

OPERATING_PARAMETERS OperatingParameters;
extern int32_t lastTimeUpdate;
int32_t lastWifiReconnect = 0;
extern int64_t lastTimeUpdate;
int64_t lastWifiReconnect = 0;
#ifdef MQTT_ENABLED
int32_t lastMqttReconnect = 0;
int64_t lastMqttReconnect = 0;
#endif

void stateMachine(void *parameter)
Expand Down
6 changes: 3 additions & 3 deletions app/src/tft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@

static char thermostatModes[48] = {0};
TaskHandle_t xTouchUIHandle;
int32_t lastTouchDetected = 0;
int64_t lastTouchDetected = 0;
bool tftTouchTimerEnabled = true;
int32_t ui_WifiStatusLabel_timestamp = 0;
int64_t ui_WifiStatusLabel_timestamp = 0;
uint16_t calData_2_8[8] = { 3839, 336, 3819, 3549, 893, 390, 718, 3387 };
uint16_t calData_3_2[8] = { 3778, 359, 3786, 3803, 266, 347, 258, 3769 };
uint16_t calData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
int32_t lastMotionDetected = 0;
int64_t lastMotionDetected = 0;

volatile bool tftMotionTrigger = false;

Expand Down
8 changes: 4 additions & 4 deletions app/src/ui/ui_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ void tftDecreaseSetTemp(lv_event_t * e)
if (OperatingParameters.tempUnits == 'C')
{
OperatingParameters.tempSet -= 0.5;
OperatingParameters.tempSet = roundValue(OperatingParameters.tempSet, 1);
updateHvacSetTemp(roundValue(OperatingParameters.tempSet, 1));
} else {
OperatingParameters.tempSet -= 1.0;
OperatingParameters.tempSet = roundValue(OperatingParameters.tempSet, 0);
updateHvacSetTemp(roundValue(OperatingParameters.tempSet, 0));
}
lv_arc_set_value(ui_TempArc, OperatingParameters.tempSet*10);
lv_label_set_text_fmt(ui_SetTemp, "%d°", int(OperatingParameters.tempSet));
Expand All @@ -47,10 +47,10 @@ void tftIncreaseSetTemp(lv_event_t * e)
if (OperatingParameters.tempUnits == 'C')
{
OperatingParameters.tempSet += 0.5;
OperatingParameters.tempSet = roundValue(OperatingParameters.tempSet, 1);
updateHvacSetTemp(roundValue(OperatingParameters.tempSet, 1));
} else {
OperatingParameters.tempSet += 1.0;
OperatingParameters.tempSet = roundValue(OperatingParameters.tempSet, 0);
updateHvacSetTemp(roundValue(OperatingParameters.tempSet, 0));
}
lv_arc_set_value(ui_TempArc, OperatingParameters.tempSet*10);
lv_label_set_text_fmt(ui_SetTemp, "%d°", int(OperatingParameters.tempSet));
Expand Down
2 changes: 1 addition & 1 deletion app/src/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ void WifiDeinit()
}
}

static int32_t lastWifiMillis = 0;
static int64_t lastWifiMillis = 0;
TaskHandle_t ntReconnectTaskHandler = NULL;

void WifiDisconnect()
Expand Down

0 comments on commit 3747eef

Please sign in to comment.