FEATURE: added daily and hourly history to uart mqtt export

This commit is contained in:
Robin Dietzel 2023-02-02 13:17:44 +01:00
parent 5820a8a129
commit 526546f0a4
2 changed files with 123 additions and 14 deletions

View File

@ -115,6 +115,12 @@ void CheckRefillConditions(Config_Store &cfg, io::PressureChannel &tankLevel, io
}
}
void SendUartString(UART_HandleTypeDef *huart, char out[]) {
HAL_UART_Transmit(huart, (uint8_t *) out, strlen(out), 500);
char newline[1] = {'\n'};
HAL_UART_Transmit(huart, (uint8_t *) newline, 1, 100);
}
int main(void) {
// Step 1: Initialize HAL
@ -128,7 +134,7 @@ int main(void) {
//MX_RTC_Init();
MX_TIM2_Init();
MX_TIM3_Init();
MX_USB_DEVICE_Init();
//MX_USB_DEVICE_Init();
MX_ADC1_Init();
MX_I2C1_Init();
MX_USART1_UART_Init();
@ -183,7 +189,8 @@ int main(void) {
//Instantiate Input and Output modules
io::PressureChannel tankLevel0(&hadc1, MPWR0_GPIO_Port, MPWR0_Pin, 50);
tankLevel0.LinkCalibConfig(Config_Store::getInstance().TankCalibLow.getLink(), Config_Store::getInstance().TankCalibHigh.getLink());
tankLevel0.LinkCalibConfig(Config_Store::getInstance().TankCalibLow.getLink(),
Config_Store::getInstance().TankCalibHigh.getLink());
io::GPIChannel refillBlocker0(GPI0_GPIO_Port, GPI0_Pin);
io::RelayChannel tankPump(OCHAN0_GPIO_Port, OCHAN0_Pin, true, io::RelayChannel::state::OFF);
@ -202,8 +209,13 @@ int main(void) {
static int8_t history_h[59];
static int8_t history_d[23];
static int8_t diff_hist_hour = 0;
static int8_t diff_hist_day = 0;
static uint16_t old_minute_counter = minute_counter;
SendUartString(&huart1, "Hello from FloatPUMP Controller");
SendUartString(&huart1, {"Revision: " GIT_HASH});
while (1) {
@ -338,10 +350,38 @@ int main(void) {
old_minute_counter = minute_counter;
history_h[minute_counter % 60] = tankLevel0.getPercent();
history_d[minute_counter / 60] = tankLevel0.getPercent();
//Calculate diff to value 1h ago -> minute_counter+1's entry is always 60mins ago !
diff_hist_hour = history_h[minute_counter % 60] - history_h[(minute_counter + 1) % 60];
//Same here for daily history
diff_hist_day = history_d[minute_counter / 60] - history_d[(minute_counter + 60) / 60];
}
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);
HAL_Delay(1000);
//Periodically send stats via uart in json format:
char buffer[400];
sprintf(buffer, "{"
"\"Status\": true, "
"\"TankLevel\": %hhd, "
"\"TankPump\": %s, "
"\"RefillPump\": %s, "
"\"RefillEmpty\": %s, "
"\"RefillCooldown\": %hhd, "
"\"HourlyHist\": %hhd, "
"\"DailyHist\": %hhd, "
"}",
tankLevel0.getPercent(),
(S_tankempty) ? "false" : "true",
(S_refilling) ? "true" : "false",
(S_refillempty) ? "true" : "false",
S_refillcooldown,
diff_hist_hour,
diff_hist_day);
SendUartString(&huart1, buffer);
//HAL_UART_Transmit(&huart1, (uint8_t*) buffer, sizeof(buffer), 100);
}
@ -349,6 +389,45 @@ int main(void) {
void SystemClock_Config(void) {
/*RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
*//** Configure the main internal regulator output voltage
*//*
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
*//** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*//*
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.LSEState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 12;
RCC_OscInitStruct.PLL.PLLN = 144;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV6;
RCC_OscInitStruct.PLL.PLLQ = 3;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
*//** Initializes the CPU, AHB and APB buses clocks
*//*
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
Error_Handler();
}*/
//Workaround by using internal oscillator
//TODO: Fix this issue later, must work with HSE as well
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
@ -360,15 +439,15 @@ void SystemClock_Config(void) {
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.LSEState = RCC_LSI_ON;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 12;
RCC_OscInitStruct.PLL.PLLN = 144;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV6;
RCC_OscInitStruct.PLL.PLLQ = 3;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 192;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV8;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
@ -604,7 +683,7 @@ static void MX_USART1_UART_Init(void) {
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;

30
net/tastmota-rules.txt Normal file
View File

@ -0,0 +1,30 @@
rule1
ON SerialReceived#Status DO var1 %value% ENDON
ON SerialReceived#TankLevel DO var2 %value% ENDON
ON SerialReceived#TankPump DO var3 %value% ENDON
ON SerialReceived#RefillPump DO var4 %value% ENDON
ON SerialReceived#RefillEmpty DO var5 %value% ENDON
ON SerialReceived#RefillCooldown DO var6 %value% ENDON
ON SerialReceived#HourlyHist DO var7 %value% ENDON
ON SerialReceived#DailyHist DO var8 %value% ENDON
ON SerialReceived#Status DO publish /floatpump-stats {"Status": "%var1%", "TankLevel": "%var2%", "TankPump": "%var3%", "RefillPump": "%var4%", "RefillEmpty": "%var5%", "RefillCooldown": "%var6%", "HourlyHist": "%var7%", "DailyHist": "%var8%"} ENDON
rule1 1
rule2
ON System#Boot DO SerialBuffer 520 ENDON
rule2 1
rule3
ON System#Boot DO publish homeassistant/binary_sensor/floatpump1/config {"name": "FloatPump Controller Status", "unique_id": "floatpump-status", "state_topic": "/floatpump-stats", "payload_on": "TRUE", "payload_off": "FALSE", "value_template": "{{ value_json.Status }}", "device": { "name": "FloatPUMP Controller", "mf": "Robin Dietzel (robtor.de)", "cns": [["mac", "BC:DD:C2:26:39:36"]] }} ENDON
ON System#Boot DO publish homeassistant/binary_sensor/floatpump2/config {"name": "Tank Pump", "unique_id": "floatpump-tankpump-status", "icon": "mdi:water-pump", "state_topic": "/floatpump-stats", "payload_on": "TRUE", "payload_off": "FALSE", "value_template": "{{ value_json.TankPump }}", "device": { "name": "FloatPUMP Controller", "mf": "Robin Dietzel (robtor.de)", "cns": [["mac", "BC:DD:C2:26:39:36"]] }} ENDON
ON System#Boot DO publish homeassistant/binary_sensor/floatpump3/config {"name": "Refill Pump", "unique_id": "floatpump-refillpump-status", "icon": "mdi:water-sync", "state_topic": "/floatpump-stats", "payload_on": "TRUE", "payload_off": "FALSE", "value_template": "{{ value_json.RefillPump }}", "device": { "name": "FloatPUMP Controller", "mf": "Robin Dietzel (robtor.de)", "cns":[["mac", "BC:DD:C2:26:39:36"]] }} ENDON
ON System#Boot DO publish homeassistant/binary_sensor/floatpump4/config {"name": "Refill Source Empty", "unique_id": "floatpump-refillempty-status", "icon": "mdi:water-alert-outline", "state_topic": "/floatpump-stats", "payload_on": "TRUE", "payload_off": "FALSE", "value_template": "{{ value_json.RefillEmpty }}", "device": { "name": "FloatPUMP Controller", "mf": "Robin Dietzel (robtor.de)", "cns": [["mac", "BC:DD:C2:26:39:36"]] }} ENDON
ON System#Boot DO publish homeassistant/sensor/floatpump5/config {"name": "Tank Water Level", "unique_id": "floatpump-tanklevel", "icon": "mdi:car-coolant-level", "unit_of_measurement": "%", "state_topic": "/floatpump-stats", "value_template": "{{ value_json.TankLevel }}", "device": { "name": "FloatPUMP Controller", "mf": "Robin Dietzel (robtor.de)", "cns": [["mac", "BC:DD:C2:26:39:36"]] }} ENDON
ON System#Boot DO publish homeassistant/sensor/floatpump6/config {"name": "Refill Cooldown", "unique_id": "floatpump-refillcooldown", "icon": "mdi:clock-alert-outline", "unit_of_measurement": "min", "state_topic": "/floatpump-stats", "value_template": "{{ value_json.RefillCooldown }}", "device": { "name": "FloatPUMP Controller", "mf": "Robin Dietzel (robtor.de)", "cns": [["mac", "BC:DD:C2:26:39:36"]] }} ENDON
ON System#Boot DO publish homeassistant/sensor/floatpump7/config {"name": "Hourly Stats", "unique_id": "floatpump-hourly-stats", "icon": "mdi:chart-line", "unit_of_measurement": "%", "state_topic": "/floatpump-stats", "value_template": "{{ value_json.HourlyHist }}", "device": { "name": "FloatPUMP Controller", "mf": "Robin Dietzel (robtor.de)", "cns": [["mac", "BC:DD:C2:26:39:36"]] }} ENDON
ON System#Boot DO publish homeassistant/sensor/floatpump8/config {"name": "Daily Stats", "unique_id": "floatpump-daily-stats", "icon": "mdi:chart-line", "unit_of_measurement": "%", "state_topic": "/floatpump-stats", "value_template": "{{ value_json.DailyHist }}", "device": { "name": "FloatPUMP Controller", "mf": "Robin Dietzel (robtor.de)", "cns": [["mac", "BC:DD:C2:26:39:36"]] }} ENDON
rule3 1