189 if (ready_ds18b20() && ready_adc()) { |
189 if (ready_ds18b20() && ready_adc()) { |
190 connect_mqtt(true); |
190 connect_mqtt(true); |
191 Main_Loop1 = ML1_WAITCON; |
191 Main_Loop1 = ML1_WAITCON; |
192 ESP_LOGI(TAG, "Loop timer: Wait MQTT"); |
192 ESP_LOGI(TAG, "Loop timer: Wait MQTT"); |
193 |
193 |
194 /* Get global temperature, use for all units. */ |
194 uint32_t temp[DS18B20_MAX]; |
195 uint32_t temp = 0; |
195 int state[DS18B20_MAX], i, num_sensors = 0; |
196 int state = 0; |
196 char rom_code[DS18B20_MAX][17]; |
197 char rom_code[17]; |
197 for (i = 0; i < DS18B20_MAX; i++) { |
|
198 temp[i] = 0; |
|
199 state[i] = 0; |
|
200 rom_code[i][0] = '\0'; |
|
201 } |
|
202 /* Copy results from all connected DS18B20 sensors */ |
198 if (xSemaphoreTake(xSemaphoreDS18B20, 10) == pdTRUE) { |
203 if (xSemaphoreTake(xSemaphoreDS18B20, 10) == pdTRUE) { |
199 temp = (ds18b20_state->sensor[0].temperature * 1000); |
204 num_sensors = ds18b20_state->num_sensors; |
200 state = (ds18b20_state->sensor[0].error == 0) ? 0:1; |
205 for (i = 0; i < num_sensors; i++) { |
201 strncpy(rom_code, ds18b20_state->sensor[0].rom_code, 17); |
206 temp[i] = (ds18b20_state->sensor[i].temperature * 1000); |
202 rom_code[16] = '\0'; |
207 state[i] = (ds18b20_state->sensor[i].error == 0) ? 0:1; |
|
208 strncpy(rom_code[i], ds18b20_state->sensor[i].rom_code, 17); |
|
209 rom_code[i][16] = '\0'; |
|
210 } |
203 xSemaphoreGive(xSemaphoreDS18B20); |
211 xSemaphoreGive(xSemaphoreDS18B20); |
204 } else { |
212 } else { |
205 ESP_LOGE(TAG, "ML1_MQTT_CONNECT DS18B20 lock error"); |
213 ESP_LOGE(TAG, "ML1_MQTT_CONNECT DS18B20 lock error"); |
206 } |
214 } |
207 |
215 |
208 /* Copy measured data and calculate results */ |
216 /* Copy measured data and calculate results */ |
209 for (int i = 0; i < 3; i++) { |
217 for (i = 0; i < 3; i++) { |
210 if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) { |
218 if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) { |
211 units[i].temperature = temp; |
219 /* Search configured temperature sensor for this unit */ |
212 units[i].temperature_state = state; |
220 for (int j = 0; j < num_sensors; j++) { |
213 units[i].alarm = 0; |
221 if (strcmp(rom_code[j], units[i].temperature_rom_code) == 0) { |
214 if (state) |
222 units[i].temperature = temp[j]; |
215 units[i].alarm |= ALARM_SYS_TEMPERATURE & ALARM_UNIT_TEMPERATURE; |
223 units[i].temperature_state = state[j]; |
216 strncpy(units[i].temperature_rom_code, rom_code, 17); |
224 units[i].alarm = 0; |
217 |
225 if (state[j]) |
|
226 units[i].alarm |= ALARM_SYS_TEMPERATURE & ALARM_UNIT_TEMPERATURE; |
|
227 break; |
|
228 } |
|
229 } |
|
230 |
|
231 /* Get the ADC results */ |
218 if (xSemaphoreTake(xSemaphoreADC, 10) == pdTRUE) { |
232 if (xSemaphoreTake(xSemaphoreADC, 10) == pdTRUE) { |
219 units[i].pressure_state = adc_state->Pressure[i].error; |
233 units[i].pressure_state = adc_state->Pressure[i].error; |
220 units[i].pressure_channel = adc_state->Pressure[i].channel; |
234 units[i].pressure_channel = adc_state->Pressure[i].channel; |
221 units[i].pressure_voltage = adc_state->Pressure[i].voltage; |
235 units[i].pressure_voltage = adc_state->Pressure[i].voltage; |
222 if (units[i].pressure_state || units[i].pressure_voltage < 80) |
236 if (units[i].pressure_state || units[i].pressure_voltage < 80) |
225 if (P < 0) |
239 if (P < 0) |
226 P = 0; |
240 P = 0; |
227 units[i].pressure = P; |
241 units[i].pressure = P; |
228 printf("%d volt: %d batt: %d scale: %d mbar: %d\n", i, units[i].pressure_voltage, adc_state->Batt_voltage, |
242 printf("%d volt: %d batt: %d scale: %d mbar: %d\n", i, units[i].pressure_voltage, adc_state->Batt_voltage, |
229 units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero, P); |
243 units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero, P); |
230 // Moet die echt op 5 volt? |
|
231 // Verbruik 10 mA |
244 // Verbruik 10 mA |
232 // Setup tijd max 2 mS |
245 // Setup tijd max 2 mS |
233 xSemaphoreGive(xSemaphoreADC); |
246 xSemaphoreGive(xSemaphoreADC); |
234 } else { |
247 } else { |
235 ESP_LOGE(TAG, "ML1_MQTT_CONNECT ADC[%d] lock error", i); |
248 ESP_LOGE(TAG, "ML1_MQTT_CONNECT ADC[%d] lock error", i); |