# HG changeset patch # User Michiel Broek # Date 1570542454 -7200 # Node ID c0184362d48cbb42912fd8861e2470b33d92ea92 # Parent 1082183cd6bb8f27ace062e23fe388c7ea08d005 Prepare ds18b20 sensors for multiple sensors on the onewire bus. diff -r 1082183cd6bb -r c0184362d48c main/co2meter.c --- a/main/co2meter.c Tue Oct 08 12:55:23 2019 +0200 +++ b/main/co2meter.c Tue Oct 08 15:47:34 2019 +0200 @@ -282,9 +282,11 @@ /* Get global temperature, use for all units. */ uint32_t temp = 0; int state = 0; + char rom_code[17]; if (xSemaphoreTake(xSemaphoreDS18B20, 10) == pdTRUE) { - temp = (ds18b20_state->bottle_temperature * 1000); - state = (ds18b20_state->bottle_error == 0) ? 0:1; + temp = (ds18b20_state->sensor[0].temperature * 1000); + state = (ds18b20_state->sensor[0].error == 0) ? 0:1; + strncpy(rom_code, ds18b20_state->sensor[0].rom_code, strlen(ds18b20_state->sensor[0].rom_code)); xSemaphoreGive(xSemaphoreDS18B20); } @@ -292,6 +294,7 @@ for (int i = 0; i < 3; i++) { units[i].temperature = temp; units[i].temperature_state = state; + strncpy(units[i].temperature_rom_code, rom_code, strlen(rom_code)); if (xSemaphoreTake(xSemaphoreADC, 10) == pdTRUE) { units[i].pressure_state = adc_state->Pressure[i].error; units[i].pressure_channel = adc_state->Pressure[i].channel; diff -r 1082183cd6bb -r c0184362d48c main/config.c --- a/main/config.c Tue Oct 08 12:55:23 2019 +0200 +++ b/main/config.c Tue Oct 08 15:47:34 2019 +0200 @@ -79,7 +79,7 @@ void read_units() { uint8_t *dst; uint8_t mac_addr[8] = {0}; - FILE *f = fopen("/spiffs/etc/units.conf", "r"); + FILE *f = fopen("/spiffs/etc/units.conft", "r"); if (f == NULL) { // No units yet, create them. diff -r 1082183cd6bb -r c0184362d48c main/config.h --- a/main/config.h Tue Oct 08 12:55:23 2019 +0200 +++ b/main/config.h Tue Oct 08 15:47:34 2019 +0200 @@ -170,7 +170,7 @@ char product_name[129]; ///< Beer product name char alias[33]; ///< Alias name 'unit1' int temperature_state; ///< Reading status - char temperature_address[33]; ///< DS18B20 address + char temperature_rom_code[17]; ///< DS18B20 address uint32_t temperature; ///< Temperature in C * 1000 int pressure_state; ///< Reading status uint8_t pressure_channel; ///< ADC channel diff -r 1082183cd6bb -r c0184362d48c main/task_ds18b20.c --- a/main/task_ds18b20.c Tue Oct 08 12:55:23 2019 +0200 +++ b/main/task_ds18b20.c Tue Oct 08 15:47:34 2019 +0200 @@ -61,9 +61,14 @@ ESP_LOGI(TAG, "Starting DS18B20 sensors"); ds18b20_state = malloc(sizeof(DS18B20_State)); - ds18b20_state->bottle_valid = false; - ds18b20_state->bottle_temperature = 0.0; - ds18b20_state->bottle_error = DS18B20_ERR_NOSENSOR; + ds18b20_state->valid = false; + ds18b20_state->num_sensors = 0; + + for (int i = 0; i < DS18B20_MAX; i++) { + ds18b20_state->sensor[i].temperature = 0.0; + ds18b20_state->sensor[i].rom_code[0] = '\0'; + ds18b20_state->sensor[i].error = DS18B20_ERR_READ; + } /* event handler and event group for the wifi driver */ xEventGroupDS18B20 = xEventGroupCreate(); @@ -71,10 +76,10 @@ /* * Initialize the MLT and HLT one-wire busses. */ - OneWireBus *owb_bottle; + OneWireBus *owb; owb_rmt_driver_info rmt_driver_info_bottle; - owb_bottle = owb_rmt_initialize(&rmt_driver_info_bottle, GPIO_DS18B20_BUS, RMT_CHANNEL_1, RMT_CHANNEL_0); - owb_use_crc(owb_bottle, true); // enable CRC check for ROM code + owb = owb_rmt_initialize(&rmt_driver_info_bottle, GPIO_DS18B20_BUS, RMT_CHANNEL_1, RMT_CHANNEL_0); + owb_use_crc(owb, true); // enable CRC check for ROM code DS18B20_Info * ds18b20_info = ds18b20_malloc(); EventBits_t uxBits; @@ -88,39 +93,45 @@ if (uxBits & TASK_DS18B20_REQUEST_TEMPS) { num_devices = 0; - OneWireBus_SearchState bottle_search_state = {0}; + OneWireBus_SearchState search_state = {0}; found = false; - owb_search_first(owb_bottle, &bottle_search_state, &found); + 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)); + rom_code_s[16] = '\0'; + printf(" %d : %s %d\n", num_devices + 1, rom_code_s, strlen(rom_code_s)); + sprintf(ds18b20_state->sensor[num_devices].rom_code, "%s", rom_code_s); ++num_devices; - owb_search_next(owb_bottle, &bottle_search_state, &found); + owb_search_next(owb, &search_state, &found); } if (num_devices == 1) { - ds18b20_init_solo(ds18b20_info, owb_bottle); // only one device on bus + ds18b20_init_solo(ds18b20_info, owb); // only one device on bus ds18b20_use_crc(ds18b20_info, true); // enable CRC check for temperature readings ds18b20_set_resolution(ds18b20_info, DS18B20_RESOLUTION); // returns true if ok. // Read temperatures more efficiently by starting conversions on all devices at the same time - ds18b20_convert_all(owb_bottle); + ds18b20_convert_all(owb); ds18b20_wait_for_conversion(ds18b20_info); errors = ds18b20_read_temp(ds18b20_info, &readings); if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) { + ds18b20_state->num_sensors = 1; if (errors == DS18B20_OK) { - ds18b20_state->bottle_error = DS18B20_ERR_NONE; - ds18b20_state->bottle_valid = true; - ds18b20_state->bottle_temperature = readings; + ds18b20_state->sensor[0].error = DS18B20_ERR_NONE; + ds18b20_state->valid = true; + ds18b20_state->sensor[0].temperature = readings; } else { if (errors == DS18B20_ERROR_CRC) - ds18b20_state->bottle_error = DS18B20_ERR_CRC; + ds18b20_state->sensor[0].error = DS18B20_ERR_CRC; if (errors == DS18B20_ERROR_OWB) - ds18b20_state->bottle_error = DS18B20_ERR_READ; + ds18b20_state->sensor[0].error = DS18B20_ERR_READ; if (errors == DS18B20_ERROR_DEVICE) - ds18b20_state->bottle_error = DS18B20_ERR_READ; - ds18b20_state->bottle_valid = false; - ds18b20_state->bottle_temperature = 0.0; + ds18b20_state->sensor[0].error = DS18B20_ERR_READ; + ds18b20_state->valid = false; + ds18b20_state->sensor[0].temperature = 0.0; } xSemaphoreGive(xSemaphoreDS18B20); } @@ -131,11 +142,11 @@ */ if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) { if (num_devices == 0) - ds18b20_state->bottle_error = DS18B20_ERR_NOSENSOR; + ds18b20_state->sensor[0].error = DS18B20_ERR_READ; else - ds18b20_state->bottle_error = DS18B20_ERR_TOOMANY; - ds18b20_state->bottle_valid = false; - ds18b20_state->bottle_temperature = 0.0; + ds18b20_state->sensor[0].error = DS18B20_ERR_READ; + ds18b20_state->valid = false; + ds18b20_state->num_sensors = 0; xSemaphoreGive(xSemaphoreDS18B20); } } // if num_devices == 1 @@ -143,7 +154,7 @@ xEventGroupClearBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_TEMPS); xEventGroupSetBits(xEventGroupDS18B20, TASK_DS18B20_REQUEST_DONE); #if 1 - ESP_LOGI(TAG, "Temperature %.3f %s", ds18b20_state->bottle_temperature, dsErrors[ds18b20_state->bottle_error]); + ESP_LOGI(TAG, "Temperature %.3f %s", ds18b20_state->sensor[0].temperature, dsErrors[ds18b20_state->sensor[0].error]); #endif } vTaskDelay( (TickType_t)10); diff -r 1082183cd6bb -r c0184362d48c main/task_ds18b20.h --- a/main/task_ds18b20.h Tue Oct 08 12:55:23 2019 +0200 +++ b/main/task_ds18b20.h Tue Oct 08 15:47:34 2019 +0200 @@ -14,19 +14,29 @@ * Error codes in this task */ #define DS18B20_ERR_NONE 0 ///< No errors -#define DS18B20_ERR_NOSENSOR 1 ///< No sensor detected -#define DS18B20_ERR_TOOMANY 2 ///< Too many sensors -#define DS18B20_ERR_CRC 3 ///< CRC read error -#define DS18B20_ERR_READ 4 ///< Generic read error +#define DS18B20_ERR_CRC 1 ///< CRC read error +#define DS18B20_ERR_READ 2 ///< Generic read error + +#define DS18B20_MAX 4 + +/** + * @brief Structure for a temperature sensor. + */ +typedef struct strTempsensor { + double temperature; ///< Current temperature. + char rom_code[17]; ///< ROM code of this device. + int error; ///< Error number. +} tempsensor_t; + /** * @brief Structure containing the variables for the DS18B20 task. */ typedef struct { - bool bottle_valid; ///< Bottle sensor valid reading. - double bottle_temperature; ///< Current bottle temperature. - int bottle_error; ///< Bottle error number. + bool valid; ///< Sensor valid reading. + int num_sensors; ///< Number of sensors + tempsensor_t sensor[DS18B20_MAX]; ///< Temperature sensors } DS18B20_State; diff -r 1082183cd6bb -r c0184362d48c main/task_mqtt.c --- a/main/task_mqtt.c Tue Oct 08 12:55:23 2019 +0200 +++ b/main/task_mqtt.c Tue Oct 08 15:47:34 2019 +0200 @@ -108,7 +108,7 @@ payload = xstrcat(payload, (char *)"\",\"temperature\":{\"state\":\""); payload = xstrcat(payload, (char *)sensState[units[i].temperature_state]); payload = xstrcat(payload, (char *)"\",\"address\":\""); - payload = xstrcat(payload, (char *)units[i].temperature_address); + payload = xstrcat(payload, (char *)units[i].temperature_rom_code); payload = xstrcat(payload, (char *)"\",\"temperature\":"); sprintf(buf, "%.3f", units[i].temperature / 1000.0); payload = xstrcat(payload, buf); @@ -126,7 +126,7 @@ sprintf(buf, "%.3f", units[i].pressure_zero / 1000.0); payload = xstrcat(payload, buf); payload = xstrcat(payload, (char *)",\"bar\":"); - sprintf(buf, "%.3f", units[i].pressure / 1000.0); + sprintf(buf, "%.2f", units[i].pressure / 1000.0); payload = xstrcat(payload, buf); payload = xstrcat(payload, (char *)"},\"mode\":\""); payload = xstrcat(payload, (char *)unitMode[units[i].mode]); @@ -186,7 +186,7 @@ if (xSemaphoreTake(xSemaphoreDS18B20, 10) == pdTRUE) { payload = xstrcat(payload, (char *)",\"THB\":{\"temperature\":"); - sprintf(buf, "%.3f", ds18b20_state->bottle_temperature); + sprintf(buf, "%.3f", ds18b20_state->sensor[0].temperature); payload = xstrcat(payload, buf); payload = xstrcat(payload, (char *)"}"); xSemaphoreGive(xSemaphoreDS18B20);