temperature_sensor: Add temperature monitor interrupt example

This commit is contained in:
Cao Sen Miao 2023-03-03 10:40:47 +08:00
parent 840ff4f865
commit 3526ff3c6a
11 changed files with 152 additions and 1 deletions

View File

@ -210,10 +210,14 @@ examples/peripherals/spi_slave_hd/segment_mode/seg_slave:
temporary: true temporary: true
reason: not supported reason: not supported
examples/peripherals/temp_sensor: examples/peripherals/temperature_sensor/temp_sensor:
disable: disable:
- if: SOC_TEMP_SENSOR_SUPPORTED != 1 - if: SOC_TEMP_SENSOR_SUPPORTED != 1
examples/peripherals/temperature_sensor/temp_sensor_monitor:
disable:
- if: SOC_TEMPERATURE_SENSOR_INTR_SUPPORT != 1
examples/peripherals/timer_group: examples/peripherals/timer_group:
disable: disable:
- if: SOC_GPTIMER_SUPPORTED != 1 - if: SOC_GPTIMER_SUPPORTED != 1

View File

@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(temp_sensor_monitor)

View File

@ -0,0 +1,67 @@
| Supported Targets | ESP32-C6 | ESP32-H2 |
| ----------------- | -------- | -------- |
# Temperature Sensor Interrupt Example
(Refer to README.md file in path peripherals/temperature_sensor/temp_sensor to get some basic information about temperature sensor)
The ESP32-C6/H2 supports automatically monitor the temperature value continuously and gives interrupt when reaches specific value, which means user can register their callback functions when interrupt happens.
The interrupt happens in two ways.
* Absolute mode, which means when temperature reaches to a specific value, then interrupt triggered.
* Delta mode, which means the change between two consecutive samplings is larger/smaller than the settings.
## How to use example
Before project configuration and build, be sure to set the correct chip target using `idf.py set-target <chip_name>`.
If you want to see clearer what interrupt did. You may need a heat gun or other heaters, when temperature reaches specific value you can see interrupt message.
### Hardware Required
* A development board with a supported target listed in the above table.
* A USB cable for power supply and programming
### Build and Flash
Build the project and flash it to the board, then run monitor tool to view serial output:
Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
(To exit the serial monitor, type ``Ctrl-]``.)
See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
## Example Output
```
I (312) example: Install temperature sensor, expected temp ranger range: 10~50 ℃
I (322) temperature_sensor: Range [-10°C ~ 80°C], error < 1°C
I (332) example: Enable temperature sensor
I (332) example: Read temperature
I (342) example: Temperature value 20.27 ℃
I (1342) example: Temperature value 20.71 ℃
I (2342) example: Temperature value 22.46 ℃
I (3342) example: Temperature value 26.85 ℃
I (4342) example: Temperature value 29.92 ℃
I (5342) example: Temperature value 33.87 ℃
I (6342) example: Temperature value 41.76 ℃
I (7342) example: Temperature value 48.78 ℃
I tsens: Temperature value is higher or lower than threshold, value is 50
...
I tsens: Temperature value is higher or lower than threshold, value is 50
...
I tsens: Temperature value is higher or lower than threshold, value is 51
...
```
## Troubleshooting
For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.

View File

@ -0,0 +1,2 @@
idf_component_register(SRCS "temp_sensor_monitor_main.c"
INCLUDE_DIRS ".")

View File

@ -0,0 +1,53 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/temperature_sensor.h"
#include "esp_attr.h"
static const char *TAG = "example";
IRAM_ATTR static bool temp_sensor_monitor_cbs(temperature_sensor_handle_t tsens, const temperature_sensor_threshold_event_data_t *edata, void *user_data)
{
ESP_DRAM_LOGI("tsens", "Temperature value is higher or lower than threshold, value is %d\n...\n\n", edata->celsius_value);
return false;
}
void app_main(void)
{
ESP_LOGI(TAG, "Install temperature sensor, expected temp ranger range: 10~50 ℃");
temperature_sensor_handle_t temp_sensor = NULL;
temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(10, 50);
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor));
temperature_sensor_event_callbacks_t cbs = {
.on_threshold = temp_sensor_monitor_cbs,
};
temperature_sensor_abs_threshold_config_t threshold_cfg = {
.high_threshold = 50,
.low_threshold = -10,
};
ESP_ERROR_CHECK(temperature_sensor_set_absolute_threshold(temp_sensor, &threshold_cfg));
ESP_ERROR_CHECK(temperature_sensor_register_callbacks(temp_sensor, &cbs, NULL));
ESP_LOGI(TAG, "Enable temperature sensor");
ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor));
ESP_LOGI(TAG, "Read temperature");
int cnt = 20;
float tsens_value;
while (cnt--) {
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_sensor, &tsens_value));
ESP_LOGI(TAG, "Temperature value %.02f ℃", tsens_value);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

View File

@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0
import pytest
from pytest_embedded.dut import Dut
@pytest.mark.esp32c6
@pytest.mark.esp32h2
@pytest.mark.generic
def test_temp_sensor_monitor_example(dut: Dut) -> None:
dut.expect_exact('Install temperature sensor')
dut.expect_exact('Enable temperature sensor')
dut.expect_exact('Read temperature')
temp_value = dut.expect(r'Temperature value (\d+\.\d+) .*', timeout=5)
# Because the example test only run in the normal temperature environment. So this assert range is meaningful
assert 0 < float(temp_value.group(1).decode('utf8')) < 50
temp_value = dut.expect(r'Temperature value (\d+\.\d+) .*', timeout=5)
assert 0 < float(temp_value.group(1).decode('utf8')) < 50