1
6
12
13
14
15
16
17
18
19
20
21
22
25
28
29
30
31
32
33
34
35
36
37
38
39
40
56
57
58
59
60
64
65
66
67
76
/* ... */
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_check.h"
#include "onewire_bus.h"
#include "ds18b20.h"6 includes
static const char *TAG = "example";
#define EXAMPLE_ONEWIRE_BUS_GPIO 0
#define EXAMPLE_ONEWIRE_MAX_DS18B20 2
void app_main(void)
{
onewire_bus_handle_t bus;
onewire_bus_config_t bus_config = {
.bus_gpio_num = EXAMPLE_ONEWIRE_BUS_GPIO,
}{...};
onewire_bus_rmt_config_t rmt_config = {
.max_rx_bytes = 10,
}{...};
ESP_ERROR_CHECK(onewire_new_bus_rmt(&bus_config, &rmt_config, &bus));
ESP_LOGI(TAG, "1-Wire bus installed on GPIO%d", EXAMPLE_ONEWIRE_BUS_GPIO);
int ds18b20_device_num = 0;
ds18b20_device_handle_t ds18b20s[EXAMPLE_ONEWIRE_MAX_DS18B20];
onewire_device_iter_handle_t iter = NULL;
onewire_device_t next_onewire_device;
esp_err_t search_result = ESP_OK;
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) {
ds18b20_config_t ds_cfg = {};
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_device_num >= EXAMPLE_ONEWIRE_MAX_DS18B20) {
ESP_LOGI(TAG, "Max DS18B20 number reached, stop searching...");
break;
}{...}
}{...} 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);
for (int i = 0; i < ds18b20_device_num; i++) {
ESP_ERROR_CHECK(ds18b20_set_resolution(ds18b20s[i], DS18B20_RESOLUTION_12B));
}{...}
float temperature;
while (1) {
vTaskDelay(pdMS_TO_TICKS(200));
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);
}{...}
}{...}
}{ ... }