Skip to content

Commit

Permalink
Added example
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrominatel committed Sep 23, 2024
1 parent 064e0e4 commit aca0115
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
6 changes: 6 additions & 0 deletions components/ds18b20/examples/ds18b20-read/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
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
* 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
...
```
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 ".")
76 changes: 76 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,76 @@
#include <stdio.h>
#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);
}

0 comments on commit aca0115

Please sign in to comment.