From 1677ca4679930ea99559ecad711e8206f943e43d Mon Sep 17 00:00:00 2001 From: Pedro Minatel Date: Fri, 27 Sep 2024 13:05:45 +0100 Subject: [PATCH] feat(ds18b20): Add sensor read example --- .build-test-rules.yml | 5 ++ .../examples/ds18b20-read/CMakeLists.txt | 7 ++ .../ds18b20/examples/ds18b20-read/README.md | 43 ++++++++++ .../examples/ds18b20-read/main/CMakeLists.txt | 2 + .../examples/ds18b20-read/main/ds18b20-read.c | 82 +++++++++++++++++++ .../ds18b20-read/main/idf_component.yml | 4 + 6 files changed, 143 insertions(+) create mode 100644 components/ds18b20/examples/ds18b20-read/CMakeLists.txt create mode 100644 components/ds18b20/examples/ds18b20-read/README.md create mode 100644 components/ds18b20/examples/ds18b20-read/main/CMakeLists.txt create mode 100644 components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c create mode 100644 components/ds18b20/examples/ds18b20-read/main/idf_component.yml diff --git a/.build-test-rules.yml b/.build-test-rules.yml index 3ca05afc..67a1d22a 100644 --- a/.build-test-rules.yml +++ b/.build-test-rules.yml @@ -66,3 +66,8 @@ components/lcd/esp_lcd_ssd1681: components/lcd/esp_lcd_st7796: depends_filepatterns: - "components/lcd/esp_lcd_st7796/**" + +components/ds18b20: + disable: + - if: SOC_RMT_SUPPORTED != 1 + reason: Onewire component depends on RMT peripheral diff --git a/components/ds18b20/examples/ds18b20-read/CMakeLists.txt b/components/ds18b20/examples/ds18b20-read/CMakeLists.txt new file mode 100644 index 00000000..7e8bfd14 --- /dev/null +++ b/components/ds18b20/examples/ds18b20-read/CMakeLists.txt @@ -0,0 +1,7 @@ +# The following five 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) +set(COMPONENTS main) +project(ds18b20-read) diff --git a/components/ds18b20/examples/ds18b20-read/README.md b/components/ds18b20/examples/ds18b20-read/README.md new file mode 100644 index 00000000..ce82df6d --- /dev/null +++ b/components/ds18b20/examples/ds18b20-read/README.md @@ -0,0 +1,43 @@ +# DS18B20 sensor example + +This example shows how to use the 1-Wire temperature sensor [DS18B20](https://www.analog.com/media/en/technical-documentation/data-sheets/ds18b20.pdf) component. + +## How to use the example + +To add the component latest version to a project, you can use the following command: + +```bash +idf.py add-dependency "espressif/ds18b20" +``` + +To create a new project from this example, use: + +```bash +idf.py create-project-from-example "espressif/ds18b20:ds18b20-read" +``` + +### Hardware Required + +* An ESP development board with RMT peripheral (e.g ESP32, ESP32-C3, ESP32-S3, etc) +* An DS18B20 sensor connected to GPIO 18. To use a different pin, modify `EXAMPLE_ONEWIRE_BUS_GPIO` in *ds18b20-read.c* file. +* An USB cable for power supply and programming + +### Example Output + +```bash +... +I (336) DS18B20: Device iterator created, start searching... +I (456) DS18B20: Found a DS18B20[0], address: 990417C1D080FF28 +I (456) DS18B20: Searching done, 1 DS18B20 device(s) found +I (456) main_task: Returned from app_main() +I (1266) DS18B20: temperature read from DS18B20[0]: 27.94C +I (4076) DS18B20: temperature read from DS18B20[0]: 27.81C +I (6886) DS18B20: temperature read from DS18B20[0]: 27.75C +I (9696) DS18B20: temperature read from DS18B20[0]: 27.69C +I (12506) DS18B20: temperature read from DS18B20[0]: 27.62C +I (15316) DS18B20: temperature read from DS18B20[0]: 27.56C +I (18126) DS18B20: temperature read from DS18B20[0]: 27.44C +I (20936) DS18B20: temperature read from DS18B20[0]: 27.38C +I (23746) DS18B20: temperature read from DS18B20[0]: 27.31C +... +``` diff --git a/components/ds18b20/examples/ds18b20-read/main/CMakeLists.txt b/components/ds18b20/examples/ds18b20-read/main/CMakeLists.txt new file mode 100644 index 00000000..ba303e5e --- /dev/null +++ b/components/ds18b20/examples/ds18b20-read/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "ds18b20-read.c" + INCLUDE_DIRS ".") diff --git a/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c b/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c new file mode 100644 index 00000000..d5cf5219 --- /dev/null +++ b/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c @@ -0,0 +1,82 @@ +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + +#include +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "ds18b20.h" +#include "onewire_bus.h" + +#define EXAMPLE_ONEWIRE_BUS_GPIO 18 +#define EXAMPLE_ONEWIRE_MAX_DS18B20 2 + +static int s_ds18b20_device_num = 0; +static float s_temperature = 0.0; +static ds18b20_device_handle_t s_ds18b20s[EXAMPLE_ONEWIRE_MAX_DS18B20]; + +static const char *TAG = "DS18B20"; + +static void sensor_detect(void) +{ + // install 1-wire bus + onewire_bus_handle_t bus = NULL; + onewire_bus_config_t bus_config = { + .bus_gpio_num = EXAMPLE_ONEWIRE_BUS_GPIO, + }; + onewire_bus_rmt_config_t rmt_config = { + .max_rx_bytes = 10, // 1byte ROM command + 8byte ROM number + 1byte device command + }; + ESP_ERROR_CHECK(onewire_new_bus_rmt(&bus_config, &rmt_config, &bus)); + + onewire_device_iter_handle_t iter = NULL; + onewire_device_t next_onewire_device; + esp_err_t search_result = ESP_OK; + + // create 1-wire device iterator, which is used for device search + ESP_ERROR_CHECK(onewire_new_device_iter(bus, &iter)); + ESP_LOGI(TAG, "Device iterator created, start searching..."); + do { + search_result = onewire_device_iter_get_next(iter, &next_onewire_device); + if (search_result == ESP_OK) { // found a new device, let's check if we can upgrade it to a DS18B20 + ds18b20_config_t ds_cfg = {}; + // check if the device is a DS18B20, if so, return the ds18b20 handle + if (ds18b20_new_device(&next_onewire_device, &ds_cfg, &s_ds18b20s[s_ds18b20_device_num]) == ESP_OK) { + ESP_LOGI(TAG, "Found a DS18B20[%d], address: %016llX", s_ds18b20_device_num, next_onewire_device.address); + s_ds18b20_device_num++; + } else { + ESP_LOGI(TAG, "Found an unknown device, address: %016llX", next_onewire_device.address); + } + } + } while (search_result != ESP_ERR_NOT_FOUND); + ESP_ERROR_CHECK(onewire_del_device_iter(iter)); + ESP_LOGI(TAG, "Searching done, %d DS18B20 device(s) found", s_ds18b20_device_num); +} + +void sensor_read(void) +{ + for (int i = 0; i < s_ds18b20_device_num; i ++) { + ESP_ERROR_CHECK(ds18b20_trigger_temperature_conversion(s_ds18b20s[i])); + ESP_ERROR_CHECK(ds18b20_get_temperature(s_ds18b20s[i], &s_temperature)); + ESP_LOGI(TAG, "temperature read from DS18B20[%d]: %.2fC", i, s_temperature); + } +} + +void sensor_readTask(void *pvParameters) +{ + while (1) { + sensor_read(); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } +} + +void app_main(void) +{ + // Detect the DS18B20 sensor in the bus + sensor_detect(); + // Start task to read the temperature from DS18B20 sensor + xTaskCreate(&sensor_readTask, "sensor_readTask", 4096, NULL, 5, NULL); +} diff --git a/components/ds18b20/examples/ds18b20-read/main/idf_component.yml b/components/ds18b20/examples/ds18b20-read/main/idf_component.yml new file mode 100644 index 00000000..af117e14 --- /dev/null +++ b/components/ds18b20/examples/ds18b20-read/main/idf_component.yml @@ -0,0 +1,4 @@ +dependencies: + espressif/ds18b20: + version: "*" + override_path: '../../../' \ No newline at end of file