From aca0115f22b9ca72786acfcb7b5f5c620d660932 Mon Sep 17 00:00:00 2001 From: pedrominatel Date: Mon, 23 Sep 2024 16:07:16 +0100 Subject: [PATCH 1/9] Added example --- .../examples/ds18b20-read/CMakeLists.txt | 6 ++ .../ds18b20/examples/ds18b20-read/README.md | 31 ++++++++ .../examples/ds18b20-read/main/CMakeLists.txt | 2 + .../examples/ds18b20-read/main/ds18b20-read.c | 76 +++++++++++++++++++ 4 files changed, 115 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 diff --git a/components/ds18b20/examples/ds18b20-read/CMakeLists.txt b/components/ds18b20/examples/ds18b20-read/CMakeLists.txt new file mode 100644 index 00000000..17563508 --- /dev/null +++ b/components/ds18b20/examples/ds18b20-read/CMakeLists.txt @@ -0,0 +1,6 @@ +# 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) +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..1acbdb43 --- /dev/null +++ b/components/ds18b20/examples/ds18b20-read/README.md @@ -0,0 +1,31 @@ +# DS18B20 sensor example + +This example shows how to use the DS18B20 component. + +## How to use the example + +### Hardware Required + +* An ESP development board +* An DS18B20 sensor +* 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..7dfdfb37 --- /dev/null +++ b/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c @@ -0,0 +1,76 @@ +#include +#include "ds18b20.h" +#include "onewire_bus.h" +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +#define EXAMPLE_ONEWIRE_BUS_GPIO 18 +#define EXAMPLE_ONEWIRE_MAX_DS18B20 2 + +int ds18b20_device_num = 0; +float temperature = 0.0; +ds18b20_device_handle_t ds18b20s[EXAMPLE_ONEWIRE_MAX_DS18B20]; + +static const char *TAG = "DS18B20"; + +void sensorDetect(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, &ds18b20s[ds18b20_device_num]) == ESP_OK) { + ESP_LOGI(TAG, "Found a DS18B20[%d], address: %016llX", ds18b20_device_num, next_onewire_device.address); + 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", ds18b20_device_num); +} + +void readSensor(void) +{ + for (int i = 0; i < ds18b20_device_num; i ++) { + ESP_ERROR_CHECK(ds18b20_trigger_temperature_conversion(ds18b20s[i])); + ESP_ERROR_CHECK(ds18b20_get_temperature(ds18b20s[i], &temperature)); + ESP_LOGI(TAG, "temperature read from DS18B20[%d]: %.2fC", i, temperature); + } +} + +void readSensorTask(void *pvParameters) +{ + while (1) { + readSensor(); + vTaskDelay(2000 / portTICK_PERIOD_MS); + } +} + +void app_main(void) +{ + // Detect the DS18B20 sensor in the bus + sensorDetect(); + // Start task to read the temperature from DS18B20 sensor + xTaskCreate(&readSensorTask, "readSensorTask", 4096, NULL, 5, NULL); +} From 6304c5356dd646724dcac7769296a191f52630d2 Mon Sep 17 00:00:00 2001 From: pedrominatel Date: Mon, 23 Sep 2024 16:21:36 +0100 Subject: [PATCH 2/9] Added YML file and license --- .../ds18b20/examples/ds18b20-read/main/ds18b20-read.c | 6 ++++++ .../ds18b20/examples/ds18b20-read/main/idf_component.yml | 4 ++++ 2 files changed, 10 insertions(+) create mode 100644 components/ds18b20/examples/ds18b20-read/main/idf_component.yml diff --git a/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c b/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c index 7dfdfb37..e391eaa0 100644 --- a/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c +++ b/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c @@ -1,3 +1,9 @@ +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + #include #include "ds18b20.h" #include "onewire_bus.h" 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 From 35d7473badb5b1c8b4b65a3833c9f7061d3b593e Mon Sep 17 00:00:00 2001 From: Pedro Minatel Date: Tue, 24 Sep 2024 08:53:37 +0100 Subject: [PATCH 3/9] Update components/ds18b20/examples/ds18b20-read/CMakeLists.txt Co-authored-by: Ivan Grokhotkov --- components/ds18b20/examples/ds18b20-read/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/components/ds18b20/examples/ds18b20-read/CMakeLists.txt b/components/ds18b20/examples/ds18b20-read/CMakeLists.txt index 17563508..7e8bfd14 100644 --- a/components/ds18b20/examples/ds18b20-read/CMakeLists.txt +++ b/components/ds18b20/examples/ds18b20-read/CMakeLists.txt @@ -3,4 +3,5 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) +set(COMPONENTS main) project(ds18b20-read) From 7505af10b1bc13407f1219b5baf9913890856eb1 Mon Sep 17 00:00:00 2001 From: Pedro Minatel Date: Tue, 24 Sep 2024 08:53:53 +0100 Subject: [PATCH 4/9] Update components/ds18b20/examples/ds18b20-read/README.md Co-authored-by: Ivan Grokhotkov --- components/ds18b20/examples/ds18b20-read/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/ds18b20/examples/ds18b20-read/README.md b/components/ds18b20/examples/ds18b20-read/README.md index 1acbdb43..bec1c171 100644 --- a/components/ds18b20/examples/ds18b20-read/README.md +++ b/components/ds18b20/examples/ds18b20-read/README.md @@ -7,7 +7,7 @@ This example shows how to use the DS18B20 component. ### Hardware Required * An ESP development board -* An DS18B20 sensor +* An DS18B20 sensor connected to GPIO 18. To use a different pin, modify `EXAMPLE_ONEWIRE_BUS_GPIO` in ds18b20-read.c. * An USB cable for power supply and programming ### Example Output From 13cd572e351228c0e3d1115b036f9df6844c168d Mon Sep 17 00:00:00 2001 From: Pedro Minatel Date: Tue, 24 Sep 2024 08:54:01 +0100 Subject: [PATCH 5/9] Update components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c Co-authored-by: Ivan Grokhotkov --- components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c b/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c index e391eaa0..c5a3af46 100644 --- a/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c +++ b/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c @@ -5,11 +5,11 @@ */ #include -#include "ds18b20.h" -#include "onewire_bus.h" #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 From 7e4c468ca057a343d3db4b219efa5953d415d33f Mon Sep 17 00:00:00 2001 From: Pedro Minatel Date: Tue, 24 Sep 2024 08:54:07 +0100 Subject: [PATCH 6/9] Update components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c Co-authored-by: Ivan Grokhotkov --- .../ds18b20/examples/ds18b20-read/main/ds18b20-read.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c b/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c index c5a3af46..27391d3b 100644 --- a/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c +++ b/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c @@ -14,9 +14,9 @@ #define EXAMPLE_ONEWIRE_BUS_GPIO 18 #define EXAMPLE_ONEWIRE_MAX_DS18B20 2 -int ds18b20_device_num = 0; -float temperature = 0.0; -ds18b20_device_handle_t ds18b20s[EXAMPLE_ONEWIRE_MAX_DS18B20]; +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"; From 82eadf5f412c8f6e899c6a8bad6248a6d837c0dc Mon Sep 17 00:00:00 2001 From: Pedro Minatel Date: Tue, 24 Sep 2024 08:54:12 +0100 Subject: [PATCH 7/9] Update components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c Co-authored-by: Ivan Grokhotkov --- components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c b/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c index 27391d3b..49a5d7d9 100644 --- a/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c +++ b/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c @@ -20,7 +20,7 @@ static ds18b20_device_handle_t s_ds18b20s[EXAMPLE_ONEWIRE_MAX_DS18B20]; static const char *TAG = "DS18B20"; -void sensorDetect(void) +static void sensor_detect(void) { // install 1-wire bus onewire_bus_handle_t bus = NULL; From 2ee178abb27dc498fe627c3ca5b70bef6fc945a5 Mon Sep 17 00:00:00 2001 From: pedrominatel Date: Wed, 25 Sep 2024 11:46:08 +0100 Subject: [PATCH 8/9] README file updated --- .../ds18b20/examples/ds18b20-read/README.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/components/ds18b20/examples/ds18b20-read/README.md b/components/ds18b20/examples/ds18b20-read/README.md index bec1c171..ce82df6d 100644 --- a/components/ds18b20/examples/ds18b20-read/README.md +++ b/components/ds18b20/examples/ds18b20-read/README.md @@ -1,13 +1,25 @@ # DS18B20 sensor example -This example shows how to use the DS18B20 component. +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 -* An DS18B20 sensor connected to GPIO 18. To use a different pin, modify `EXAMPLE_ONEWIRE_BUS_GPIO` in ds18b20-read.c. +* 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 From 3e85db3fd84c88b8fb893a3a1c59365326d8edd7 Mon Sep 17 00:00:00 2001 From: pedrominatel Date: Wed, 25 Sep 2024 11:54:18 +0100 Subject: [PATCH 9/9] Code review/renaming and static vars fixed --- .../examples/ds18b20-read/main/ds18b20-read.c | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c b/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c index 49a5d7d9..d5cf5219 100644 --- a/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c +++ b/components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c @@ -44,39 +44,39 @@ static void sensor_detect(void) 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, &ds18b20s[ds18b20_device_num]) == ESP_OK) { - ESP_LOGI(TAG, "Found a DS18B20[%d], address: %016llX", ds18b20_device_num, next_onewire_device.address); - ds18b20_device_num++; + 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", ds18b20_device_num); + ESP_LOGI(TAG, "Searching done, %d DS18B20 device(s) found", s_ds18b20_device_num); } -void readSensor(void) +void sensor_read(void) { - for (int i = 0; i < ds18b20_device_num; i ++) { - ESP_ERROR_CHECK(ds18b20_trigger_temperature_conversion(ds18b20s[i])); - ESP_ERROR_CHECK(ds18b20_get_temperature(ds18b20s[i], &temperature)); - ESP_LOGI(TAG, "temperature read from DS18B20[%d]: %.2fC", i, temperature); + 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 readSensorTask(void *pvParameters) +void sensor_readTask(void *pvParameters) { while (1) { - readSensor(); - vTaskDelay(2000 / portTICK_PERIOD_MS); + sensor_read(); + vTaskDelay(1000 / portTICK_PERIOD_MS); } } void app_main(void) { // Detect the DS18B20 sensor in the bus - sensorDetect(); + sensor_detect(); // Start task to read the temperature from DS18B20 sensor - xTaskCreate(&readSensorTask, "readSensorTask", 4096, NULL, 5, NULL); + xTaskCreate(&sensor_readTask, "sensor_readTask", 4096, NULL, 5, NULL); }