|
@@ -18,21 +18,23 @@
|
|
|
#define DS18B20_RESOLUTION (DS18B20_RESOLUTION_12_BIT)
|
|
|
#define SAMPLE_PERIOD (1000) // milliseconds
|
|
|
|
|
|
+DS18B20_Info * devices[MAX_DEVICES] = {0};
|
|
|
+int num_devices = 0;
|
|
|
|
|
|
-// 0x28,0x41,0x2e,0x7b,0x0d,0x00,0x00,0x2a
|
|
|
+OneWireBus * owb;
|
|
|
+owb_rmt_driver_info rmt_driver_info;
|
|
|
|
|
|
-void readTemps() {
|
|
|
+void initTempReadings() {
|
|
|
|
|
|
- // Create a 1-Wire bus, using the RMT timeslot driver
|
|
|
- OneWireBus * owb;
|
|
|
- owb_rmt_driver_info rmt_driver_info;
|
|
|
+ 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
|
|
|
- printf("Find devices:\n");
|
|
|
+ ESP_LOGI("TEMPS", "Find devices:");
|
|
|
OneWireBus_ROMCode device_rom_codes[MAX_DEVICES] = {0};
|
|
|
- int num_devices = 0;
|
|
|
OneWireBus_SearchState search_state = {0};
|
|
|
bool found = false;
|
|
|
owb_search_first(owb, &search_state, &found);
|
|
@@ -41,133 +43,81 @@ void readTemps() {
|
|
|
{
|
|
|
char rom_code_s[17];
|
|
|
owb_string_from_rom_code(search_state.rom_code, rom_code_s, sizeof(rom_code_s));
|
|
|
- printf(" %d : %s\n", num_devices, 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);
|
|
|
}
|
|
|
- printf("Found %d device%s\n", num_devices, num_devices == 1 ? "" : "s");
|
|
|
-
|
|
|
- if (num_devices == 1)
|
|
|
- {
|
|
|
- // For a single device only:
|
|
|
- OneWireBus_ROMCode rom_code;
|
|
|
- owb_status status = owb_read_rom(owb, &rom_code);
|
|
|
- if (status == OWB_STATUS_OK)
|
|
|
- {
|
|
|
- char rom_code_s[OWB_ROM_CODE_STRING_LENGTH];
|
|
|
- owb_string_from_rom_code(rom_code, rom_code_s, sizeof(rom_code_s));
|
|
|
- printf("Single device %s present\n", rom_code_s);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- printf("An error occurred reading ROM code: %d", status);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- // Search for a known ROM code (LSB first):
|
|
|
- // For example: 0x1502162ca5b2ee28
|
|
|
- OneWireBus_ROMCode known_device = {
|
|
|
- .fields.family = { 0x28 },
|
|
|
- .fields.serial_number = { 0xee, 0xb2, 0xa5, 0x2c, 0x16, 0x02 },
|
|
|
- .fields.crc = { 0x15 },
|
|
|
- };
|
|
|
- char rom_code_s[OWB_ROM_CODE_STRING_LENGTH];
|
|
|
- owb_string_from_rom_code(known_device, rom_code_s, sizeof(rom_code_s));
|
|
|
- bool is_present = false;
|
|
|
-
|
|
|
- owb_status search_status = owb_verify_rom(owb, known_device, &is_present);
|
|
|
- if (search_status == OWB_STATUS_OK)
|
|
|
- {
|
|
|
- printf("Device %s is %s\n", rom_code_s, is_present ? "present" : "not present");
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- printf("An error occurred searching for known device: %d", search_status);
|
|
|
- }
|
|
|
- }
|
|
|
+ ESP_LOGI("TEMPS", "Found %d device%s", num_devices, num_devices == 1 ? "" : "s");
|
|
|
|
|
|
// Create DS18B20 devices on the 1-Wire bus
|
|
|
- DS18B20_Info * devices[MAX_DEVICES] = {0};
|
|
|
for (int i = 0; i < num_devices; ++i)
|
|
|
{
|
|
|
DS18B20_Info * ds18b20_info = ds18b20_malloc(); // heap allocation
|
|
|
devices[i] = ds18b20_info;
|
|
|
|
|
|
- if (num_devices == 1)
|
|
|
- {
|
|
|
- printf("Single device optimisations enabled\n");
|
|
|
- ds18b20_init_solo(ds18b20_info, owb); // only one device on bus
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- ds18b20_init(ds18b20_info, owb, device_rom_codes[i]); // associate with bus and device
|
|
|
- }
|
|
|
+ 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() {
|
|
|
|
|
|
+ ESP_LOGI("TEMPS", "Read temperatures.... %d", num_devices);
|
|
|
|
|
|
// Read temperatures more efficiently by starting conversions on all devices at the same time
|
|
|
int errors_count[MAX_DEVICES] = {0};
|
|
|
- int sample_count = 0;
|
|
|
if (num_devices > 0)
|
|
|
{
|
|
|
- TickType_t last_wake_time = xTaskGetTickCount();
|
|
|
+ ds18b20_convert_all(owb);
|
|
|
|
|
|
- while (1)
|
|
|
- {
|
|
|
- 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]);
|
|
|
|
|
|
- // In this application all devices use the same resolution,
|
|
|
- // so use the first device to determine the delay
|
|
|
- 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 };
|
|
|
|
|
|
- // 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]);
|
|
|
+ }
|
|
|
|
|
|
- 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
|
|
|
- printf("\nTemperature readings (degrees C): sample %d\n", ++sample_count);
|
|
|
- for (int i = 0; i < num_devices; ++i)
|
|
|
+ // Print results in a separate loop, after all have been read
|
|
|
+ for (int i = 0; i < num_devices; ++i)
|
|
|
+ {
|
|
|
+ if (errors[i] != DS18B20_OK)
|
|
|
{
|
|
|
- if (errors[i] != DS18B20_OK)
|
|
|
- {
|
|
|
- ++errors_count[i];
|
|
|
- }
|
|
|
-
|
|
|
- printf(" %d: %.1f %d errors\n", i, readings[i], errors_count[i]);
|
|
|
+ ++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));
|
|
|
|
|
|
- vTaskDelayUntil(&last_wake_time, SAMPLE_PERIOD / portTICK_PERIOD_MS);
|
|
|
+ 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
|
|
|
{
|
|
|
- printf("\nNo DS18B20 devices detected!\n");
|
|
|
+ ESP_LOGE("TEMPS", "No DS18B20 devices detected!");
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- // clean up dynamically allocated data
|
|
|
- for (int i = 0; i < num_devices; ++i)
|
|
|
- {
|
|
|
- ds18b20_free(&devices[i]);
|
|
|
- }
|
|
|
- owb_uninitialize(owb);
|
|
|
-
|
|
|
- printf("Restarting now.\n");
|
|
|
- fflush(stdout);
|
|
|
- vTaskDelay(10000 / portTICK_PERIOD_MS);
|
|
|
- esp_restart();
|
|
|
}
|
|
|
|
|
|
+/*void readAndSendTemps() {
|
|
|
+}*/
|
|
|
+
|
|
|
|
|
|
|
|
|
|