Skip to content

Commit

Permalink
Added example
Browse files Browse the repository at this point in the history
Added YML file and license

Update components/ds18b20/examples/ds18b20-read/CMakeLists.txt

Co-authored-by: Ivan Grokhotkov <[email protected]>

Update components/ds18b20/examples/ds18b20-read/README.md

Co-authored-by: Ivan Grokhotkov <[email protected]>

Update components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c

Co-authored-by: Ivan Grokhotkov <[email protected]>

Update components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c

Co-authored-by: Ivan Grokhotkov <[email protected]>

Update components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c

Co-authored-by: Ivan Grokhotkov <[email protected]>

README file updated

Code review/renaming and static vars fixed

Added RMT skip as a build test rule
  • Loading branch information
pedrominatel committed Sep 25, 2024
1 parent 064e0e4 commit 9cb86de
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .build-test-rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions components/ds18b20/examples/ds18b20-read/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
43 changes: 43 additions & 0 deletions components/ds18b20/examples/ds18b20-read/README.md
Original file line number Diff line number Diff line change
@@ -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
...
```
2 changes: 2 additions & 0 deletions components/ds18b20/examples/ds18b20-read/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
idf_component_register(SRCS "ds18b20-read.c"
INCLUDE_DIRS ".")
82 changes: 82 additions & 0 deletions components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/

#include <stdio.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

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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies:
espressif/ds18b20:
version: "*"
override_path: '../../../'

0 comments on commit 9cb86de

Please sign in to comment.