main/task_ds18b20.c

changeset 2
c0184362d48c
parent 0
88d965579617
child 3
cd760fd45271
--- 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);

mercurial