onewire_example_main.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "esp_log.h"
  9. #include "esp_check.h"
  10. #include "onewire_bus.h"
  11. #include "ds18b20.h"
  12. static const char *TAG = "example";
  13. #define EXAMPLE_ONEWIRE_BUS_GPIO 0
  14. #define EXAMPLE_ONEWIRE_MAX_DS18B20 2
  15. void app_main(void)
  16. {
  17. // install new 1-wire bus
  18. onewire_bus_handle_t bus;
  19. onewire_bus_config_t bus_config = {
  20. .bus_gpio_num = EXAMPLE_ONEWIRE_BUS_GPIO,
  21. };
  22. onewire_bus_rmt_config_t rmt_config = {
  23. .max_rx_bytes = 10, // 1byte ROM command + 8byte ROM number + 1byte device command
  24. };
  25. ESP_ERROR_CHECK(onewire_new_bus_rmt(&bus_config, &rmt_config, &bus));
  26. ESP_LOGI(TAG, "1-Wire bus installed on GPIO%d", EXAMPLE_ONEWIRE_BUS_GPIO);
  27. int ds18b20_device_num = 0;
  28. ds18b20_device_handle_t ds18b20s[EXAMPLE_ONEWIRE_MAX_DS18B20];
  29. onewire_device_iter_handle_t iter = NULL;
  30. onewire_device_t next_onewire_device;
  31. esp_err_t search_result = ESP_OK;
  32. // create 1-wire device iterator, which is used for device search
  33. ESP_ERROR_CHECK(onewire_new_device_iter(bus, &iter));
  34. ESP_LOGI(TAG, "Device iterator created, start searching...");
  35. do {
  36. search_result = onewire_device_iter_get_next(iter, &next_onewire_device);
  37. if (search_result == ESP_OK) { // found a new device, let's check if we can upgrade it to a DS18B20
  38. ds18b20_config_t ds_cfg = {};
  39. if (ds18b20_new_device(&next_onewire_device, &ds_cfg, &ds18b20s[ds18b20_device_num]) == ESP_OK) {
  40. ESP_LOGI(TAG, "Found a DS18B20[%d], address: %016llX", ds18b20_device_num, next_onewire_device.address);
  41. ds18b20_device_num++;
  42. if (ds18b20_device_num >= EXAMPLE_ONEWIRE_MAX_DS18B20) {
  43. ESP_LOGI(TAG, "Max DS18B20 number reached, stop searching...");
  44. break;
  45. }
  46. } else {
  47. ESP_LOGI(TAG, "Found an unknown device, address: %016llX", next_onewire_device.address);
  48. }
  49. }
  50. } while (search_result != ESP_ERR_NOT_FOUND);
  51. ESP_ERROR_CHECK(onewire_del_device_iter(iter));
  52. ESP_LOGI(TAG, "Searching done, %d DS18B20 device(s) found", ds18b20_device_num);
  53. // set resolution for all DS18B20s
  54. for (int i = 0; i < ds18b20_device_num; i++) {
  55. // set resolution
  56. ESP_ERROR_CHECK(ds18b20_set_resolution(ds18b20s[i], DS18B20_RESOLUTION_12B));
  57. }
  58. // get temperature from sensors one by one
  59. float temperature;
  60. while (1) {
  61. vTaskDelay(pdMS_TO_TICKS(200));
  62. for (int i = 0; i < ds18b20_device_num; i ++) {
  63. ESP_ERROR_CHECK(ds18b20_trigger_temperature_conversion(ds18b20s[i]));
  64. ESP_ERROR_CHECK(ds18b20_get_temperature(ds18b20s[i], &temperature));
  65. ESP_LOGI(TAG, "temperature read from DS18B20[%d]: %.2fC", i, temperature);
  66. }
  67. }
  68. }