Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added example for the DS18B20 #397

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
pedrominatel marked this conversation as resolved.
Show resolved Hide resolved
31 changes: 31 additions & 0 deletions components/ds18b20/examples/ds18b20-read/README.md
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it would be nice to mention that the ESP chip should be one with an RMT peripheral (e.g. not ESP32-C2)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @igrr for your review. I'll do it.

* 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

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