diff --git a/README.md b/README.md index d9a81fb..e7d16d6 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # esp32-ds18b20-example +[![Build Status](https://travis-ci.org/DavidAntliff/esp32-ds18b20-example.svg?branch=master)](https://travis-ci.org/DavidAntliff/esp32-ds18b20-example) +[![license](https://img.shields.io/github/license/mashape/apistatus.svg)]() + ## Introduction This is an example application for the Maxim Integrated DS18B20 Programmable Resolution 1-Wire Digital Thermometer device. diff --git a/main/app_main.c b/main/app_main.c index 593fcec..88f997f 100644 --- a/main/app_main.c +++ b/main/app_main.c @@ -145,13 +145,15 @@ void app_main() // } // Read temperatures more efficiently by starting conversions on all devices at the same time - int crc_errors[MAX_DEVICES] = {0}; + int errors_count[MAX_DEVICES] = {0}; int sample_count = 0; if (num_devices > 0) { + TickType_t last_wake_time = xTaskGetTickCount(); + while (1) { - TickType_t start_ticks = xTaskGetTickCount(); + last_wake_time = xTaskGetTickCount(); ds18b20_convert_all(owb); @@ -161,29 +163,27 @@ void app_main() // Read the results immediately after conversion otherwise it may fail // (using printf before reading may take too long) - float temps[MAX_DEVICES] = { 0 }; + float readings[MAX_DEVICES] = { 0 }; + DS18B20_ERROR errors[MAX_DEVICES] = { 0 }; + for (int i = 0; i < num_devices; ++i) { - temps[i] = ds18b20_read_temp(devices[i]); + errors[i] = ds18b20_read_temp(devices[i], &readings[i]); } // 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) { - if (temps[i] == DS18B20_INVALID_READING) + if (errors[i] != DS18B20_OK) { - ++crc_errors[i]; + ++errors_count[i]; } - printf(" %d: %.1f %d errors\n", i, temps[i], crc_errors[i]); + printf(" %d: %.1f %d errors\n", i, readings[i], errors_count[i]); } - // Make up periodic delay to approximately one sample period per measurement - if ((xTaskGetTickCount() - start_ticks) < (SAMPLE_PERIOD / portTICK_PERIOD_MS)) - { - vTaskDelay(SAMPLE_PERIOD / portTICK_PERIOD_MS - (xTaskGetTickCount() - start_ticks)); - } + vTaskDelayUntil(&last_wake_time, SAMPLE_PERIOD / portTICK_PERIOD_MS); } }