# HG changeset patch # User Michiel Broek # Date 1681741258 -7200 # Node ID 84e54b14e7dbe4ba85fccee2fb287e2bd9cdc132 # Parent ec5c7794dcd65979319f455c89050b4bd5ea6c82 Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload. diff -r ec5c7794dcd6 -r 84e54b14e7db CMakeLists.txt --- a/CMakeLists.txt Mon Apr 17 14:54:35 2023 +0200 +++ b/CMakeLists.txt Mon Apr 17 16:20:58 2023 +0200 @@ -4,7 +4,7 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -set(PROJECT_VER "0.4.0") +set(PROJECT_VER "0.4.1") set(PROJECT_NAME "iotbalkon") set(EXTRA_COMPONENT_DIRS esp-idf-lib/components) diff -r ec5c7794dcd6 -r 84e54b14e7db main/CMakeLists.txt --- a/main/CMakeLists.txt Mon Apr 17 14:54:35 2023 +0200 +++ b/main/CMakeLists.txt Mon Apr 17 16:20:58 2023 +0200 @@ -1,7 +1,7 @@ if(CONFIG_ENABLE_BLE_GATT) - set(srcs config.c iotbalkon.c task_bmp280.c task_ina219.c task_apds9930.c task_wifi.c task_mqtt.c task_ble.c task_out.c xutil.c nvsio.c ble_adv.c ble_gap.c ble_gatts.c) + set(srcs config.c iotbalkon.c task_bmp280.c task_ina219.c task_apds9930.c task_wifi.c task_mqtt.c task_temp.c task_ble.c task_out.c xutil.c nvsio.c ble_adv.c ble_gap.c ble_gatts.c) else() - set(srcs config.c iotbalkon.c task_bmp280.c task_ina219.c task_apds9930.c task_wifi.c task_mqtt.c task_out.c xutil.c nvsio.c) + set(srcs config.c iotbalkon.c task_bmp280.c task_ina219.c task_apds9930.c task_wifi.c task_mqtt.c task_temp.c task_out.c xutil.c nvsio.c) endif() idf_component_register(SRCS "${srcs}" diff -r ec5c7794dcd6 -r 84e54b14e7db main/config.h --- a/main/config.h Mon Apr 17 14:54:35 2023 +0200 +++ b/main/config.h Mon Apr 17 16:20:58 2023 +0200 @@ -26,6 +26,7 @@ #include "driver/gpio.h" #include "driver/i2c.h" #include "driver/ledc.h" +#include "driver/temperature_sensor.h" #include "esp_log.h" #include "esp_system.h" #include "esp_sleep.h" @@ -70,6 +71,7 @@ #include "task_wifi.h" #include "task_mqtt.h" #include "task_out.h" +#include "task_temp.h" #include "xutil.h" #include "nvsio.h" diff -r ec5c7794dcd6 -r 84e54b14e7db main/iotbalkon.c --- a/main/iotbalkon.c Mon Apr 17 14:54:35 2023 +0200 +++ b/main/iotbalkon.c Mon Apr 17 16:20:58 2023 +0200 @@ -33,6 +33,8 @@ static TaskHandle_t xTaskBLE = NULL; #endif static TaskHandle_t xTaskOUT = NULL; +static TaskHandle_t xTaskTEMP = NULL; + #define DS_TIME 60 #define DS_CURRENT 6.6 @@ -59,6 +61,8 @@ int DisCounter = 0; +extern TEMP_State *temp_state; +extern SemaphoreHandle_t xSemaphoreTEMP; extern BMP280_State *bmp280_state; ///< I2C state extern SemaphoreHandle_t xSemaphoreBMP280; ///< I2C lock semaphore extern bmp280_params_t bmp280_params; @@ -399,7 +403,7 @@ xSemaphoreBMP280 = xSemaphoreCreateMutex(); xSemaphoreINA219 = xSemaphoreCreateMutex(); xSemaphoreAPDS9930 = xSemaphoreCreateMutex(); - + xSemaphoreTEMP = xSemaphoreCreateMutex(); xTaskCreate(&task_bmp280, "task_bmp280", 2560, NULL, 8, &xTaskBMP280); xTaskCreate(&task_ina219, "task_ina219", 2560, NULL, 8, &xTaskINA219); @@ -410,6 +414,7 @@ #ifdef CONFIG_ENABLE_BLE_GATT xTaskCreate(&task_ble, "task_ble", 4096, NULL, 3, &xTaskBLE); #endif + xTaskCreate(&task_temp, "task_temp", 2560, NULL, 9, &xTaskTEMP); vTaskDelay(10 / portTICK_PERIOD_MS); @@ -421,7 +426,8 @@ uint8_t ds_time = DS_Time; - while (1) { + while (0) { + request_temp(); // request_bmp280(); // request_ina219(); // request_apds9930(); @@ -555,6 +561,7 @@ #endif } getTempBaro(); + request_temp(); vTaskDelay(2000 / portTICK_PERIOD_MS); publish(); State = State_WorkDone; diff -r ec5c7794dcd6 -r 84e54b14e7db main/task_mqtt.c --- a/main/task_mqtt.c Mon Apr 17 14:54:35 2023 +0200 +++ b/main/task_mqtt.c Mon Apr 17 16:20:58 2023 +0200 @@ -22,6 +22,8 @@ const char *sensState[] = { "OK", "ERROR" }; ///< Sensor state strings const char *unitMode[] = { "OFF", "ON" }; ///< Units state strings +extern TEMP_State *temp_state; ///< Internal temperature state +extern SemaphoreHandle_t xSemaphoreTEMP; ///< Internal temperature semaphore extern BMP280_State *bmp280_state; ///< BMP280 state extern SemaphoreHandle_t xSemaphoreBMP280; ///< BMP280 lock semaphore extern INA219_State *ina219_state; ///< INA219 state @@ -172,6 +174,12 @@ payload = xstrcat(payload, buf); xSemaphoreGive(xSemaphoreWiFi); } + if (xSemaphoreTake(xSemaphoreTEMP, 25) == pdTRUE) { + payload = xstrcat(payload, (char *)",\"temperature\":"); + sprintf(buf, "%.1f", temp_state->temperature); + payload = xstrcat(payload, buf); + xSemaphoreGive(xSemaphoreTEMP); + } payload = xstrcat(payload, (char *)",\"light\":{\"lux\":"); if (xSemaphoreTake(xSemaphoreAPDS9930, 25) == pdTRUE) { sprintf(buf, "%.1f", apds9930_state->ambient_light); diff -r ec5c7794dcd6 -r 84e54b14e7db main/task_temp.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/task_temp.c Mon Apr 17 16:20:58 2023 +0200 @@ -0,0 +1,91 @@ +/** + * @file task_temp.c + * @brief The FreeRTOS task to query the temperature sensor. + */ + + +#include "config.h" + + +static const char *TAG = "task_temp"; + +SemaphoreHandle_t xSemaphoreTEMP = NULL; ///< Semaphore TEMP task +EventGroupHandle_t xEventGroupTEMP; ///< Events TEMP task +TEMP_State *temp_state; ///< Public state for other tasks + +const int TASK_TEMP_REQUEST_DONE = BIT0; ///< All requests are done. +const int TASK_TEMP_REQUEST_TEMP = BIT1; ///< Request Temperature and Barometer + + + +void request_temp(void) +{ + xEventGroupClearBits(xEventGroupTEMP, TASK_TEMP_REQUEST_DONE); + xEventGroupSetBits(xEventGroupTEMP, TASK_TEMP_REQUEST_TEMP); +} + + + +bool ready_temp(void) +{ + if (xEventGroupGetBits(xEventGroupTEMP) & TASK_TEMP_REQUEST_DONE) + return true; + return false; +} + + +/* + * Task to read the chip temperature sensor on request. + */ +void task_temp(void *pvParameter) +{ + float tsens_value; + int error = 0; + + ESP_LOGI(TAG, "Starting task temperature"); + temp_state = malloc(sizeof(TEMP_State)); + + temp_state->valid = false; + temp_state->error = TEMP_ERR_NONE; + + temperature_sensor_handle_t temp_sensor = NULL; + temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80); + ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor)); + + /* event handler and event group for this task */ + xEventGroupTEMP = xEventGroupCreate(); + EventBits_t uxBits; + + /* + * Task loop forever. + */ + while (1) { + + uxBits = xEventGroupWaitBits(xEventGroupTEMP, TASK_TEMP_REQUEST_TEMP, pdFALSE, pdFALSE, portMAX_DELAY ); + if (uxBits & TASK_TEMP_REQUEST_TEMP) { + + ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor)); + error = temperature_sensor_get_celsius(temp_sensor, &tsens_value); + ESP_ERROR_CHECK(temperature_sensor_disable(temp_sensor)); + + if (xSemaphoreTake(xSemaphoreTEMP, 25) == pdTRUE) { + if (error == ESP_OK) { + temp_state->error = TEMP_ERR_NONE; + temp_state->valid = true; + temp_state->temperature = tsens_value; + } else { + temp_state->error = TEMP_ERR_READ; + temp_state->valid = false; + temp_state->temperature = 0; + } + xSemaphoreGive(xSemaphoreTEMP); + } + + xEventGroupClearBits(xEventGroupTEMP, TASK_TEMP_REQUEST_TEMP); + xEventGroupSetBits(xEventGroupTEMP, TASK_TEMP_REQUEST_DONE); +#if 0 + ESP_LOGI(TAG, " TEMP: %.3f C, error: %d", temp_state->temperature, temp_state->error); +#endif + } + } +} diff -r ec5c7794dcd6 -r 84e54b14e7db main/task_temp.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/task_temp.h Mon Apr 17 16:20:58 2023 +0200 @@ -0,0 +1,49 @@ +/** + * @file task_temp.h + * @brief The FreeRTOS task to query the chip temperature sensor. + * The task will update the sensor state structures. + */ + +#ifndef _TASK_TEMP_H +#define _TASK_TEMP_H + +/* + * Error codes in this task + */ +#define TEMP_ERR_NONE 0 ///< No errors +#define TEMP_ERR_READ 1 + + +/** + * @brief Structure containing the variables for the TEMP task. + */ +typedef struct { + bool valid; ///< Valid measurement + float temperature; ///< Temperature in celsius + int error; ///< Error result +} TEMP_State; + + + +/** + * @brief Request a new measurement from the temperature sensor. + */ +void request_temp(void); + + +/** + * @brief Check if results are ready + * @return true of results are ready, else false. + */ +bool ready_temp(void); + + +/** + * @brief The FreeRTOS task to update the temperature on request. + * @param pvParameters Parameters for the task. + */ +void task_temp(void *pvParameters); + + +#endif +