readTemps.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/task.h"
  3. #include "driver/gpio.h"
  4. #include "esp32/rom/ets_sys.h"
  5. #include "esp_log.h"
  6. #include "config.h"
  7. #include "readTemps.h"
  8. #include "mqtt.h"
  9. #ifdef ENABLE_DS18B20
  10. #include "owb.h"
  11. #include "owb_rmt.h"
  12. #include "ds18b20.h"
  13. #define GPIO_DS18B20_0 (ONE_WIRE_BUS_IO)
  14. #define MAX_DEVICES (8)
  15. #define DS18B20_RESOLUTION (DS18B20_RESOLUTION_12_BIT)
  16. #define SAMPLE_PERIOD (1000) // milliseconds
  17. DS18B20_Info * devices[MAX_DEVICES] = {0};
  18. int num_devices = 0;
  19. OneWireBus * owb;
  20. owb_rmt_driver_info rmt_driver_info;
  21. void initTempReadings() {
  22. ESP_LOGI("TEMPS", "Init temp readings.");
  23. owb = owb_rmt_initialize(&rmt_driver_info, GPIO_DS18B20_0, RMT_CHANNEL_1, RMT_CHANNEL_0);
  24. owb_use_crc(owb, true); // enable CRC check for ROM code
  25. // Find all connected devices
  26. ESP_LOGI("TEMPS", "Find devices:");
  27. OneWireBus_ROMCode device_rom_codes[MAX_DEVICES] = {0};
  28. OneWireBus_SearchState search_state = {0};
  29. bool found = false;
  30. owb_search_first(owb, &search_state, &found);
  31. while (found)
  32. {
  33. char rom_code_s[17];
  34. owb_string_from_rom_code(search_state.rom_code, rom_code_s, sizeof(rom_code_s));
  35. ESP_LOGI("TEMPS", " %d : %s", num_devices, rom_code_s);
  36. device_rom_codes[num_devices] = search_state.rom_code;
  37. ++num_devices;
  38. owb_search_next(owb, &search_state, &found);
  39. }
  40. ESP_LOGI("TEMPS", "Found %d device%s", num_devices, num_devices == 1 ? "" : "s");
  41. // Create DS18B20 devices on the 1-Wire bus
  42. for (int i = 0; i < num_devices; ++i)
  43. {
  44. DS18B20_Info * ds18b20_info = ds18b20_malloc(); // heap allocation
  45. devices[i] = ds18b20_info;
  46. ds18b20_init(ds18b20_info, owb, device_rom_codes[i]); // associate with bus and device
  47. ds18b20_use_crc(ds18b20_info, true); // enable CRC check on all reads
  48. ds18b20_set_resolution(ds18b20_info, DS18B20_RESOLUTION);
  49. }
  50. }
  51. void readAndSendTemps(void *pvParameters) {
  52. ESP_LOGI("TEMPS", "Read temperature task. Core:%d",xPortGetCoreID());
  53. // Read temperatures more efficiently by starting conversions on all devices at the same time
  54. int errors_count[MAX_DEVICES] = {0};
  55. if (num_devices > 0)
  56. {
  57. ds18b20_convert_all(owb);
  58. // In this application all devices use the same resolution,
  59. // so use the first device to determine the delay
  60. const float sampleTime = ds18b20_wait_for_conversion(devices[0]);
  61. // Read the results immediately after conversion otherwise it may fail
  62. // (using printf before reading may take too long)
  63. float readings[MAX_DEVICES] = { 0 };
  64. DS18B20_ERROR errors[MAX_DEVICES] = { 0 };
  65. for (int i = 0; i < num_devices; ++i)
  66. {
  67. errors[i] = ds18b20_read_temp(devices[i], &readings[i]);
  68. }
  69. ESP_LOGI("TEMPS","Sample time:%.0fms",sampleTime);
  70. // Print results in a separate loop, after all have been read
  71. for (int i = 0; i < num_devices; ++i)
  72. {
  73. if (errors[i] != DS18B20_OK)
  74. {
  75. ++errors_count[i];
  76. }
  77. char rom_code_s[25];
  78. char mqtt_s[50];
  79. char value_s[10];
  80. owb_string_from_rom_code(devices[i]->rom_code, rom_code_s, sizeof(rom_code_s));
  81. sprintf(mqtt_s,"basement/boiler/temps/%s", rom_code_s);
  82. sprintf(value_s,"%.1f",readings[i]);
  83. ESP_LOGI("TEMPS","%s %s", mqtt_s, value_s);
  84. sendMQTTMessage(mqtt_s, value_s);
  85. }
  86. }
  87. else
  88. {
  89. ESP_LOGE("TEMPS", "No DS18B20 devices detected!");
  90. }
  91. vTaskDelete(NULL);
  92. }
  93. #endif