main/co2meter.c

changeset 37
358bbd5b608e
parent 34
495b7eafbf5a
child 38
46e2c385e9de
equal deleted inserted replaced
36:e2a43e7c4035 37:358bbd5b608e
160 /* 160 /*
161 * Main application loop. 161 * Main application loop.
162 */ 162 */
163 while (1) { 163 while (1) {
164 164
165 ESP_LOGI(TAG, "Entered app loop"); 165 ESP_LOGI(TAG, "Entered Main loop");
166 166
167 /* Measure process */ 167 /* Measure process */
168 while (1) { 168 while (1) {
169 switch (Main_Loop1) { 169 switch (Main_Loop1) {
170 case ML1_INIT: 170 case ML1_INIT:
171 ESP_LOGI(TAG, "Loop timer: Init");
172 // If configured do ML1_CONNECT
173 Main_Loop1 = ML1_CONNECT; 171 Main_Loop1 = ML1_CONNECT;
174 requestWiFi_system(true); 172 requestWiFi_system(true);
175 request_ds18b20(); 173 request_ds18b20();
176 request_adc(); 174 request_adc();
177 break; 175 break;
185 183
186 case ML1_MQTT_CONNECT: 184 case ML1_MQTT_CONNECT:
187 if (ready_ds18b20() && ready_adc()) { 185 if (ready_ds18b20() && ready_adc()) {
188 connect_mqtt(true); 186 connect_mqtt(true);
189 Main_Loop1 = ML1_WAITCON; 187 Main_Loop1 = ML1_WAITCON;
190 ESP_LOGI(TAG, "Loop timer: Wait MQTT");
191 188
192 uint32_t temp[DS18B20_MAX]; 189 uint32_t temp[DS18B20_MAX];
193 int state[DS18B20_MAX], i; 190 int state[DS18B20_MAX], i;
194 char rom_code[DS18B20_MAX][17]; 191 char rom_code[DS18B20_MAX][17];
195 for (i = 0; i < DS18B20_MAX; i++) { 192 for (i = 0; i < DS18B20_MAX; i++) {
236 units[i].alarm |= ALARM_UNIT_PRESSURE; 233 units[i].alarm |= ALARM_UNIT_PRESSURE;
237 int P = (units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero) * 14; // in bar 234 int P = (units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero) * 14; // in bar
238 if (P < 0) 235 if (P < 0)
239 P = 0; 236 P = 0;
240 units[i].pressure = P; 237 units[i].pressure = P;
241 ESP_LOGI(TAG, "%d vb:%.3f vp:%.3f zero:%d scale:%3d mbar:%4d t:%6.3f %s", 238 ESP_LOGI(TAG, "%d vb:%.3f vp:%.3f zero:%d scale:%3d mbar:%4d alm: %d t:%6.3f %s",
242 i, adc_state->Batt_voltage / 1000.0, units[i].pressure_voltage / 1000.0, units[i].pressure_zero, 239 i, adc_state->Batt_voltage / 1000.0, units[i].pressure_voltage / 1000.0, units[i].pressure_zero,
243 units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero, P, 240 units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero, P, units[i].alarm,
244 units[i].temperature / 1000.0, units[i].temperature_rom_code); 241 units[i].temperature / 1000.0, units[i].temperature_rom_code);
245 xSemaphoreGive(xSemaphoreADC); 242 xSemaphoreGive(xSemaphoreADC);
246 } else { 243 } else {
247 ESP_LOGE(TAG, "ML1_MQTT_CONNECT ADC[%d] lock error", i); 244 ESP_LOGE(TAG, "ML1_MQTT_CONNECT ADC[%d] lock error", i);
248 } 245 }
265 if (ready_mqtt()) 262 if (ready_mqtt())
266 Main_Loop1 = ML1_SEND; 263 Main_Loop1 = ML1_SEND;
267 break; 264 break;
268 265
269 case ML1_SEND: 266 case ML1_SEND:
270 ESP_LOGI(TAG, "Loop timer: Send MQTT");
271 publishNode(); 267 publishNode();
272 publishUnits(); 268 publishUnits();
273 publishLogs(); 269 publishLogs();
274 Main_Loop1 = ML1_WAITACK; 270 Main_Loop1 = ML1_WAITACK;
275 break; 271 break;
278 if (count_pub == 0) // Wait until all published messages are sent. 274 if (count_pub == 0) // Wait until all published messages are sent.
279 Main_Loop1 = ML1_MQTT_DISCONNECT; 275 Main_Loop1 = ML1_MQTT_DISCONNECT;
280 break; 276 break;
281 277
282 case ML1_MQTT_DISCONNECT: 278 case ML1_MQTT_DISCONNECT:
283 ESP_LOGI(TAG, "Loop timer: Disconnect MQTT");
284 connect_mqtt(false); // Doesn't really disconnect. 279 connect_mqtt(false); // Doesn't really disconnect.
285 Main_Loop1 = ML1_DISCONNECT; 280 Main_Loop1 = ML1_DISCONNECT;
286 break; 281 break;
287 282
288 case ML1_DISCONNECT: 283 case ML1_DISCONNECT:
289 if (! ready_mqtt()) { 284 if (! ready_mqtt()) {
290 ESP_LOGI(TAG, "Loop timer: WiFi off");
291 requestWiFi_system(false); 285 requestWiFi_system(false);
292 Main_Loop1 = ML1_WIFI_OFF; 286 Main_Loop1 = ML1_WIFI_OFF;
293 } 287 }
294 break; 288 break;
295 289
296 case ML1_WIFI_OFF: 290 case ML1_WIFI_OFF:
297 if (! ready_WiFi()) { 291 if (! ready_WiFi()) {
298 ESP_LOGI(TAG, "Loop timer: Done %s", user_busy() ? "true":"false"); 292 ESP_LOGI(TAG, "Main loop: Done, user busy: %s", user_busy() ? "true":"false");
299 Main_Loop1 = ML1_DONE; 293 Main_Loop1 = ML1_DONE;
300 user_refresh(); 294 user_refresh();
301 } 295 }
302 break; 296 break;
303 297

mercurial