improve stability
This commit is contained in:
		
							
								
								
									
										4
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							| @ -7,7 +7,9 @@ | ||||
|         "outputs.h": "c", | ||||
|         "task.h": "c", | ||||
|         "metrics.h": "c", | ||||
|         "freertos.h": "c" | ||||
|         "freertos.h": "c", | ||||
|         "compare": "c", | ||||
|         "inputs.h": "c" | ||||
|     }, | ||||
|     "idf.openOcdConfigs": [ | ||||
|         "board/esp32-wrover-kit-3.3v.cfg" | ||||
|  | ||||
| @ -20,6 +20,7 @@ onewire_addr_t uOneWireAddresses[MAX_DN18B20_SENSORS]; | ||||
| float fDS18B20Temps[MAX_DN18B20_SENSORS]; | ||||
| size_t sSensorCount = 0U; | ||||
|  | ||||
| static SemaphoreHandle_t xMutexAccessInputs = NULL; | ||||
| static eBurnerErrorState sBurnerErrorState; | ||||
| static float fChamperTemperature; | ||||
| static float fOutdoorTemperature; | ||||
| @ -41,6 +42,13 @@ void initInputs(void) | ||||
|  | ||||
|     gpio_config(&ioConfBurnerFault); | ||||
|  | ||||
|     xMutexAccessInputs = xSemaphoreCreateBinary(); | ||||
|     if (xMutexAccessInputs == NULL) | ||||
|     { | ||||
|         ESP_LOGE(TAG, "Unable to create mutex"); | ||||
|     } | ||||
|     xSemaphoreGive(xMutexAccessInputs); | ||||
|  | ||||
|     BaseType_t taskCreated = xTaskCreate( | ||||
|         taskInput,   // Function to implement the task | ||||
|         "taskInput", // Task name | ||||
| @ -105,23 +113,27 @@ void taskInput(void *pvParameters) | ||||
|                 { | ||||
|                     float temp_c = fDS18B20Temps[j]; | ||||
|                     ESP_LOGI(TAG, "Sensor: %08" PRIx64 " reports %.3f°C", (uint64_t)uOneWireAddresses[j], temp_c); | ||||
|  | ||||
|                     switch ((uint64_t)uOneWireAddresses[j]) | ||||
|                     if (xSemaphoreTake(xMutexAccessInputs, portMAX_DELAY) == pdTRUE) | ||||
|                     { | ||||
|                     case ((uint64_t)uChamperTempSensorAddr): | ||||
|                         fChamperTemperature = temp_c; | ||||
|                         break; | ||||
|                     case ((uint64_t)uOutdoorTempSensorAddr): | ||||
|                         fOutdoorTemperature = temp_c; | ||||
|                         break; | ||||
|                     case ((uint64_t)uInletFlowTempSensorAddr): | ||||
|                         fInletFlowTemperature = temp_c; | ||||
|                         break; | ||||
|                     case ((uint64_t)uReturnFlowTempSensorAddr): | ||||
|                         fReturnFlowTemperature = temp_c; | ||||
|                         break; | ||||
|                     default: | ||||
|                         break; | ||||
|                         switch ((uint64_t)uOneWireAddresses[j]) | ||||
|                         { | ||||
|                         case ((uint64_t)uChamperTempSensorAddr): | ||||
|                             fChamperTemperature = temp_c; | ||||
|                             break; | ||||
|                         case ((uint64_t)uOutdoorTempSensorAddr): | ||||
|                             fOutdoorTemperature = temp_c; | ||||
|                             break; | ||||
|                         case ((uint64_t)uInletFlowTempSensorAddr): | ||||
|                             fInletFlowTemperature = temp_c; | ||||
|                             break; | ||||
|                         case ((uint64_t)uReturnFlowTempSensorAddr): | ||||
|                             fReturnFlowTemperature = temp_c; | ||||
|                             break; | ||||
|                         default: | ||||
|                             break; | ||||
|                         } | ||||
|  | ||||
|                         xSemaphoreGive(xMutexAccessInputs); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
| @ -131,21 +143,51 @@ void taskInput(void *pvParameters) | ||||
|  | ||||
| float getChamberTemperature(void) | ||||
| { | ||||
|     return fChamperTemperature; | ||||
|     float ret = 0.0f; | ||||
|     if (xSemaphoreTake(xMutexAccessInputs, portMAX_DELAY) == pdTRUE) | ||||
|     { | ||||
|         ret = fChamperTemperature; | ||||
|         xSemaphoreGive(xMutexAccessInputs); | ||||
|     } | ||||
|     return ret; | ||||
| } | ||||
| float getOutdoorTemperature(void) | ||||
| { | ||||
|     return fOutdoorTemperature; | ||||
|     float ret = 0.0f; | ||||
|     if (xSemaphoreTake(xMutexAccessInputs, portMAX_DELAY) == pdTRUE) | ||||
|     { | ||||
|         ret = fOutdoorTemperature; | ||||
|         xSemaphoreGive(xMutexAccessInputs); | ||||
|     } | ||||
|     return ret; | ||||
| } | ||||
| float getInletFlowTemperature(void) | ||||
| { | ||||
|     return fInletFlowTemperature; | ||||
|     float ret = 0.0f; | ||||
|     if (xSemaphoreTake(xMutexAccessInputs, portMAX_DELAY) == pdTRUE) | ||||
|     { | ||||
|         ret = fInletFlowTemperature; | ||||
|         xSemaphoreGive(xMutexAccessInputs); | ||||
|     } | ||||
|     return ret; | ||||
| } | ||||
| float getReturnFlowTemperature(void) | ||||
| { | ||||
|     return fReturnFlowTemperature; | ||||
|     float ret = 0.0f; | ||||
|     if (xSemaphoreTake(xMutexAccessInputs, portMAX_DELAY) == pdTRUE) | ||||
|     { | ||||
|         ret = fReturnFlowTemperature; | ||||
|         xSemaphoreGive(xMutexAccessInputs); | ||||
|     } | ||||
|     return ret; | ||||
| } | ||||
| eBurnerErrorState getBurnerError(void) | ||||
| { | ||||
|     return sBurnerErrorState; | ||||
|     eBurnerErrorState ret = FAULT; | ||||
|     if (xSemaphoreTake(xMutexAccessInputs, portMAX_DELAY) == pdTRUE) | ||||
|     { | ||||
|         ret = sBurnerErrorState; | ||||
|         xSemaphoreGive(xMutexAccessInputs); | ||||
|     } | ||||
|     return ret; | ||||
| } | ||||
| @ -30,6 +30,9 @@ static sMetric aMetrics[METRIC_MAX_COUNT]; | ||||
| static uint16_t u16MetricCounter = 0U; | ||||
|  | ||||
| void taskMetrics(void *pvParameters); | ||||
| void connect_wifi(void); | ||||
| httpd_handle_t setup_server(void); | ||||
| esp_err_t get_metrics_handler(httpd_req_t *req); | ||||
|  | ||||
| void initMetrics(void) | ||||
| { | ||||
| @ -39,7 +42,7 @@ void initMetrics(void) | ||||
|     BaseType_t taskCreated = xTaskCreate( | ||||
|         taskMetrics,   // Function to implement the task | ||||
|         "taskMetrics", // Task name | ||||
|         2048,          // Stack size (in words, not bytes) | ||||
|         4096,          // Stack size (in words, not bytes) | ||||
|         NULL,          // Parameters to the task function (none in this case) | ||||
|         5,             // Task priority (higher number = higher priority) | ||||
|         NULL           // Task handle (optional) | ||||
| @ -268,24 +271,22 @@ esp_err_t get_metrics_handler(httpd_req_t *req) | ||||
|     } | ||||
| } | ||||
|  | ||||
| httpd_uri_t uri_get = { | ||||
|     .uri = "/metrics", | ||||
|     .method = HTTP_GET, | ||||
|     .handler = get_metrics_handler, | ||||
|     .user_ctx = NULL}; | ||||
|  | ||||
| httpd_handle_t setup_server(void) | ||||
| { | ||||
|     httpd_config_t config = HTTPD_DEFAULT_CONFIG(); | ||||
|     config.server_port = 9100; | ||||
|     httpd_handle_t server = NULL; | ||||
|  | ||||
|     httpd_uri_t uri_get = { | ||||
|         .uri = "/metrics", | ||||
|         .method = HTTP_GET, | ||||
|         .handler = get_metrics_handler, | ||||
|         .user_ctx = NULL}; | ||||
|  | ||||
|     xMutexAccessMetricResponse = xSemaphoreCreateBinary(); | ||||
|     if (xMutexAccessMetricResponse == NULL) | ||||
|     { | ||||
|         ESP_LOGE(TAG, "Unable to create mutex for metric response"); | ||||
|         vTaskDelay(pdMS_TO_TICKS(300 * 60)); // wait 5min before restart | ||||
|         esp_restart(); | ||||
|     } | ||||
|     xSemaphoreGive(xMutexAccessMetricResponse); | ||||
|  | ||||
|  | ||||
| @ -4,7 +4,7 @@ | ||||
|  | ||||
| #define WIFI_CONNECTED_BIT BIT0 | ||||
| #define WIFI_FAIL_BIT BIT1 | ||||
| #define HTML_RESPONSE_SIZE 256U | ||||
| #define HTML_RESPONSE_SIZE 512U | ||||
| #define METRIC_NAME_MAX_SIZE 64U | ||||
| #define METRIC_MAX_COUNT 32U | ||||
|  | ||||
| @ -15,7 +15,4 @@ typedef struct _metric | ||||
| } sMetric; | ||||
|  | ||||
| void initMetrics(void); | ||||
|  | ||||
| void connect_wifi(void); | ||||
| httpd_handle_t setup_server(void); | ||||
| void vSetMetrics(sMetric *paMetrics, uint16_t u16Size); | ||||
| @ -5,9 +5,11 @@ | ||||
|  | ||||
| #include "outputs.h" | ||||
|  | ||||
| static const char *TAG = "smart-oil-heater-control-system-outputs"; | ||||
| const uint8_t uCirculationPumpGpioPin = 27U; | ||||
| const uint8_t uBurnerGpioPin = 14U; | ||||
|  | ||||
| static SemaphoreHandle_t xMutexAccessOutputs = NULL; | ||||
| static eOutput sCirculationPumpState; | ||||
| static eOutput sBurnerState; | ||||
|  | ||||
| @ -30,6 +32,13 @@ void initOutputs(void) | ||||
|     }; | ||||
|     gpio_config(&ioConfCirculationPump); | ||||
|     gpio_config(&ioConfBurner); | ||||
|  | ||||
|     xMutexAccessOutputs = xSemaphoreCreateBinary(); | ||||
|     if (xMutexAccessOutputs == NULL) | ||||
|     { | ||||
|         ESP_LOGE(TAG, "Unable to create mutex"); | ||||
|     } | ||||
|     xSemaphoreGive(xMutexAccessOutputs); | ||||
| } | ||||
|  | ||||
| eOutput getCirculationPumpState(void) | ||||
| @ -39,35 +48,49 @@ eOutput getCirculationPumpState(void) | ||||
|  | ||||
| void setCirculationPumpState(eOutput in) | ||||
| { | ||||
|     sCirculationPumpState = in; | ||||
|     switch (sCirculationPumpState) | ||||
|     if (xSemaphoreTake(xMutexAccessOutputs, portMAX_DELAY) == pdTRUE) | ||||
|     { | ||||
|     case ENABLED: | ||||
|         gpio_set_level(uCirculationPumpGpioPin, 1U); // Switch on Circulation Pump | ||||
|         break; | ||||
|     case DISABLED: | ||||
|         gpio_set_level(uCirculationPumpGpioPin, 0U); // Switch off Circulation Pump | ||||
|     default: | ||||
|         break; | ||||
|         sCirculationPumpState = in; | ||||
|         switch (sCirculationPumpState) | ||||
|         { | ||||
|         case ENABLED: | ||||
|             gpio_set_level(uCirculationPumpGpioPin, 1U); // Switch on Circulation Pump | ||||
|             break; | ||||
|         case DISABLED: | ||||
|             gpio_set_level(uCirculationPumpGpioPin, 0U); // Switch off Circulation Pump | ||||
|         default: | ||||
|             break; | ||||
|         } | ||||
|         xSemaphoreGive(xMutexAccessOutputs); | ||||
|     } | ||||
| } | ||||
|  | ||||
| eOutput getBurnerState(void) | ||||
| { | ||||
| return sBurnerState; | ||||
|     eOutput ret = ENABLED; | ||||
|     if (xSemaphoreTake(xMutexAccessOutputs, portMAX_DELAY) == pdTRUE) | ||||
|     { | ||||
|         ret = sBurnerState; | ||||
|         xSemaphoreGive(xMutexAccessOutputs); | ||||
|     } | ||||
|     return ret; | ||||
| } | ||||
|  | ||||
| void setBurnerState(eOutput in) | ||||
| { | ||||
|     sBurnerState = in; | ||||
|     switch (sBurnerState) | ||||
|     if (xSemaphoreTake(xMutexAccessOutputs, portMAX_DELAY) == pdTRUE) | ||||
|     { | ||||
|     case ENABLED: | ||||
|         gpio_set_level(uBurnerGpioPin, 0U); // Switch on Burner | ||||
|         break; | ||||
|     case DISABLED: | ||||
|         gpio_set_level(uBurnerGpioPin, 1U); // Switch off Burner | ||||
|     default: | ||||
|         break; | ||||
|         sBurnerState = in; | ||||
|         switch (sBurnerState) | ||||
|         { | ||||
|         case ENABLED: | ||||
|             gpio_set_level(uBurnerGpioPin, 0U); // Switch on Burner | ||||
|             break; | ||||
|         case DISABLED: | ||||
|             gpio_set_level(uBurnerGpioPin, 1U); // Switch off Burner | ||||
|         default: | ||||
|             break; | ||||
|         } | ||||
|         xSemaphoreGive(xMutexAccessOutputs); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user