|
@@ -0,0 +1,138 @@
|
|
|
|
+#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"
|
|
|
|
+
|
|
|
|
+#include "owb.h"
|
|
|
|
+#include "owb_rmt.h"
|
|
|
|
+#include "ds18b20.h"
|
|
|
|
+
|
|
|
|
+#ifdef ENABLE_DS18B20
|
|
|
|
+
|
|
|
|
+#define GPIO_DS18B20_0 (CONFIG_ONE_WIRE_GPIO)
|
|
|
|
+#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);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Start the TEMPS task
|
|
|
|
+ xTaskCreatePinnedToCore(readAndSendTemps, "TEMPS-Task", 1024*10, NULL, 2, NULL,0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void readAndSendTemps(void *pvParameters) {
|
|
|
|
+
|
|
|
|
+ ESP_LOGI("TEMPS", "Read temperature task. Core:%d",xPortGetCoreID());
|
|
|
|
+
|
|
|
|
+ TickType_t vLastWakeTime = xTaskGetTickCount();
|
|
|
|
+
|
|
|
|
+ while(true)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // 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]);
|
|
|
|
+
|
|
|
|
+ vTaskDelay(500 / portTICK_PERIOD_MS);
|
|
|
|
+
|
|
|
|
+ // 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,"garage/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!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ vTaskDelayUntil( &vLastWakeTime, 20000 / portTICK_PERIOD_MS );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ vTaskDelete(NULL);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#endif
|