123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "driver/gpio.h"
- #include "esp32/rom/ets_sys.h"
- #include "esp_log.h"
- #include "config.h"
- #include "readTemps.h"
- #include "mqtt.h"
- #ifdef ENABLE_DS18B20
- #include "owb.h"
- #include "owb_rmt.h"
- #include "ds18b20.h"
- #define GPIO_DS18B20_0 (ONE_WIRE_BUS_IO)
- #define MAX_DEVICES (8)
- #define DS18B20_RESOLUTION (DS18B20_RESOLUTION_12_BIT)
- #define SAMPLE_PERIOD (1000) // milliseconds
- DS18B20_Info * devices[MAX_DEVICES] = {0};
- int num_devices = 0;
- OneWireBus * owb;
- owb_rmt_driver_info rmt_driver_info;
- void initTempReadings() {
- ESP_LOGI("TEMPS", "Init temp readings.");
-
- owb = owb_rmt_initialize(&rmt_driver_info, GPIO_DS18B20_0, RMT_CHANNEL_1, RMT_CHANNEL_0);
- owb_use_crc(owb, true); // enable CRC check for ROM code
- // Find all connected devices
- ESP_LOGI("TEMPS", "Find devices:");
- OneWireBus_ROMCode device_rom_codes[MAX_DEVICES] = {0};
- OneWireBus_SearchState search_state = {0};
- bool found = false;
- owb_search_first(owb, &search_state, &found);
- while (found)
- {
- char rom_code_s[17];
- owb_string_from_rom_code(search_state.rom_code, rom_code_s, sizeof(rom_code_s));
- ESP_LOGI("TEMPS", " %d : %s", num_devices, rom_code_s);
- device_rom_codes[num_devices] = search_state.rom_code;
- ++num_devices;
- owb_search_next(owb, &search_state, &found);
- }
- ESP_LOGI("TEMPS", "Found %d device%s", num_devices, num_devices == 1 ? "" : "s");
- // Create DS18B20 devices on the 1-Wire bus
- for (int i = 0; i < num_devices; ++i)
- {
- DS18B20_Info * ds18b20_info = ds18b20_malloc(); // heap allocation
- devices[i] = ds18b20_info;
- ds18b20_init(ds18b20_info, owb, device_rom_codes[i]); // associate with bus and device
- ds18b20_use_crc(ds18b20_info, true); // enable CRC check on all reads
- ds18b20_set_resolution(ds18b20_info, DS18B20_RESOLUTION);
- }
- }
- void readAndSendTemps(void *pvParameters) {
- ESP_LOGI("TEMPS", "Read temperature task. Core:%d",xPortGetCoreID());
- // Read temperatures more efficiently by starting conversions on all devices at the same time
- int errors_count[MAX_DEVICES] = {0};
- if (num_devices > 0)
- {
- ds18b20_convert_all(owb);
- // In this application all devices use the same resolution,
- // so use the first device to determine the delay
- const float sampleTime = ds18b20_wait_for_conversion(devices[0]);
- // Read the results immediately after conversion otherwise it may fail
- // (using printf before reading may take too long)
- float readings[MAX_DEVICES] = { 0 };
- DS18B20_ERROR errors[MAX_DEVICES] = { 0 };
- for (int i = 0; i < num_devices; ++i)
- {
- errors[i] = ds18b20_read_temp(devices[i], &readings[i]);
- }
- ESP_LOGI("TEMPS","Sample time:%.0fms",sampleTime);
- // Print results in a separate loop, after all have been read
- for (int i = 0; i < num_devices; ++i)
- {
- if (errors[i] != DS18B20_OK)
- {
- ++errors_count[i];
- }
-
- char rom_code_s[25];
- char mqtt_s[50];
- char value_s[10];
- owb_string_from_rom_code(devices[i]->rom_code, rom_code_s, sizeof(rom_code_s));
- sprintf(mqtt_s,"basement/boiler/temps/%s", rom_code_s);
- sprintf(value_s,"%.1f",readings[i]);
- ESP_LOGI("TEMPS","%s %s", mqtt_s, value_s);
- sendMQTTMessage(mqtt_s, value_s);
- }
- }
- else
- {
- ESP_LOGE("TEMPS", "No DS18B20 devices detected!");
- }
- vTaskDelete(NULL);
- }
- #endif
|