Mon, 17 Apr 2023 16:20:58 +0200
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
32
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | /** |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | * @file task_temp.c |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * @brief The FreeRTOS task to query the temperature sensor. |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | */ |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | #include "config.h" |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | static const char *TAG = "task_temp"; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | SemaphoreHandle_t xSemaphoreTEMP = NULL; ///< Semaphore TEMP task |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | EventGroupHandle_t xEventGroupTEMP; ///< Events TEMP task |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | TEMP_State *temp_state; ///< Public state for other tasks |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | const int TASK_TEMP_REQUEST_DONE = BIT0; ///< All requests are done. |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | const int TASK_TEMP_REQUEST_TEMP = BIT1; ///< Request Temperature and Barometer |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
21 | void request_temp(void) |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
22 | { |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
23 | xEventGroupClearBits(xEventGroupTEMP, TASK_TEMP_REQUEST_DONE); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
24 | xEventGroupSetBits(xEventGroupTEMP, TASK_TEMP_REQUEST_TEMP); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | } |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | bool ready_temp(void) |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | { |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
31 | if (xEventGroupGetBits(xEventGroupTEMP) & TASK_TEMP_REQUEST_DONE) |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
32 | return true; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
33 | return false; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | } |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
35 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
36 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | /* |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
38 | * Task to read the chip temperature sensor on request. |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
39 | */ |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | void task_temp(void *pvParameter) |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | { |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
42 | float tsens_value; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | int error = 0; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
44 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | ESP_LOGI(TAG, "Starting task temperature"); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
46 | temp_state = malloc(sizeof(TEMP_State)); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
47 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
48 | temp_state->valid = false; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
49 | temp_state->error = TEMP_ERR_NONE; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
50 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | temperature_sensor_handle_t temp_sensor = NULL; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
52 | temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
53 | ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor)); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
54 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
55 | /* event handler and event group for this task */ |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
56 | xEventGroupTEMP = xEventGroupCreate(); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | EventBits_t uxBits; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
58 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
59 | /* |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
60 | * Task loop forever. |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
61 | */ |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
62 | while (1) { |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
63 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
64 | uxBits = xEventGroupWaitBits(xEventGroupTEMP, TASK_TEMP_REQUEST_TEMP, pdFALSE, pdFALSE, portMAX_DELAY ); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
65 | if (uxBits & TASK_TEMP_REQUEST_TEMP) { |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
66 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
67 | ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor)); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
68 | error = temperature_sensor_get_celsius(temp_sensor, &tsens_value); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
69 | ESP_ERROR_CHECK(temperature_sensor_disable(temp_sensor)); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
70 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
71 | if (xSemaphoreTake(xSemaphoreTEMP, 25) == pdTRUE) { |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
72 | if (error == ESP_OK) { |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
73 | temp_state->error = TEMP_ERR_NONE; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
74 | temp_state->valid = true; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
75 | temp_state->temperature = tsens_value; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
76 | } else { |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
77 | temp_state->error = TEMP_ERR_READ; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
78 | temp_state->valid = false; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
79 | temp_state->temperature = 0; |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
80 | } |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
81 | xSemaphoreGive(xSemaphoreTEMP); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
82 | } |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
83 | |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
84 | xEventGroupClearBits(xEventGroupTEMP, TASK_TEMP_REQUEST_TEMP); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
85 | xEventGroupSetBits(xEventGroupTEMP, TASK_TEMP_REQUEST_DONE); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
86 | #if 0 |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
87 | ESP_LOGI(TAG, " TEMP: %.3f C, error: %d", temp_state->temperature, temp_state->error); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
88 | #endif |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
89 | } |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
90 | } |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
91 | } |