Search DS18B20 sensor for the right pressure unit.

Tue, 12 Nov 2019 16:26:51 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 12 Nov 2019 16:26:51 +0100
changeset 31
1f507aba6561
parent 30
8b630bf52092
child 32
7717ac1d2f7f

Search DS18B20 sensor for the right pressure unit.

main/co2meter.c file | annotate | diff | comparison | revisions
--- a/main/co2meter.c	Tue Nov 12 10:36:53 2019 +0100
+++ b/main/co2meter.c	Tue Nov 12 16:26:51 2019 +0100
@@ -191,30 +191,44 @@
 			Main_Loop1 = ML1_WAITCON;
 			ESP_LOGI(TAG, "Loop timer: Wait MQTT");
 
-			/* Get global temperature, use for all units. */
-			uint32_t temp = 0;
-			int state = 0;
-			char rom_code[17];
+			uint32_t temp[DS18B20_MAX];
+			int state[DS18B20_MAX], i, num_sensors = 0;
+			char rom_code[DS18B20_MAX][17];
+			for (i = 0; i < DS18B20_MAX; i++) {
+			    temp[i] = 0;
+			    state[i] = 0;
+			    rom_code[i][0] = '\0';
+			}
+			/* Copy results from all connected DS18B20 sensors */
 			if (xSemaphoreTake(xSemaphoreDS18B20, 10) == pdTRUE) {
-			    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, 17);
-			    rom_code[16] = '\0';
+			    num_sensors = ds18b20_state->num_sensors;
+			    for (i = 0; i < num_sensors; i++) {
+			    	temp[i] = (ds18b20_state->sensor[i].temperature * 1000);
+			    	state[i] = (ds18b20_state->sensor[i].error == 0) ? 0:1;
+			    	strncpy(rom_code[i], ds18b20_state->sensor[i].rom_code, 17);
+			    	rom_code[i][16] = '\0';
+			    }
         		    xSemaphoreGive(xSemaphoreDS18B20);
     			} else {
 			    ESP_LOGE(TAG, "ML1_MQTT_CONNECT DS18B20 lock error");
 			}
 
 			/* Copy measured data and calculate results */
-			for (int i = 0; i < 3; i++) {
+			for (i = 0; i < 3; i++) {
 			    if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) {
-			    	units[i].temperature = temp;
-			    	units[i].temperature_state = state;
-				units[i].alarm = 0;
-				if (state)
-				    units[i].alarm |= ALARM_SYS_TEMPERATURE & ALARM_UNIT_TEMPERATURE;
-			    	strncpy(units[i].temperature_rom_code, rom_code, 17);
+				/* Search configured temperature sensor for this unit */
+				for (int j = 0; j < num_sensors; j++) {
+				    if (strcmp(rom_code[j], units[i].temperature_rom_code) == 0) {
+					units[i].temperature = temp[j];
+					units[i].temperature_state = state[j];
+					units[i].alarm = 0;
+					if (state[j])
+                                    	    units[i].alarm |= ALARM_SYS_TEMPERATURE & ALARM_UNIT_TEMPERATURE;
+					break;
+				    }
+				}
 
+				/* Get the ADC results */
 			    	if (xSemaphoreTake(xSemaphoreADC, 10) == pdTRUE) {
 				    units[i].pressure_state = adc_state->Pressure[i].error;
 				    units[i].pressure_channel = adc_state->Pressure[i].channel;
@@ -227,7 +241,6 @@
 				    units[i].pressure = P;
 printf("%d volt: %d batt: %d scale: %d  mbar: %d\n", i, units[i].pressure_voltage, adc_state->Batt_voltage, 
 		units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero, P);
-// Moet die echt op 5 volt?
 // Verbruik 10 mA
 // Setup tijd max 2 mS
 				    xSemaphoreGive(xSemaphoreADC);

mercurial