main/iotbalkon.c

changeset 7
2b337dd92f25
parent 6
bad3414f7bc4
child 8
115e93bf8796
equal deleted inserted replaced
6:bad3414f7bc4 7:2b337dd92f25
21 static TaskHandle_t xTaskINA219 = NULL; 21 static TaskHandle_t xTaskINA219 = NULL;
22 static TaskHandle_t xTaskMQTT = NULL; 22 static TaskHandle_t xTaskMQTT = NULL;
23 static TaskHandle_t xTaskWifi = NULL; 23 static TaskHandle_t xTaskWifi = NULL;
24 24
25 #define MAX_LOOPS 32 25 #define MAX_LOOPS 32
26 #define SUB_TIME 1000
26 27
27 float temperature; 28 float temperature;
28 float pressure; 29 float pressure;
29 RTC_DATA_ATTR float solarVolts, solarCurrent, solarPower; 30 float solarVolts, solarCurrent, solarPower;
30 RTC_DATA_ATTR float batteryVolts, batteryCurrent, batteryPower; 31 float batteryVolts, batteryCurrent, batteryPower;
31 RTC_DATA_ATTR int batteryState; 32 int batteryState;
32 RTC_DATA_ATTR float s_Volts[MAX_LOOPS + 1]; 33 float s_Volts[MAX_LOOPS + 1];
33 RTC_DATA_ATTR float s_Current[MAX_LOOPS + 1]; 34 float s_Current[MAX_LOOPS + 1];
34 RTC_DATA_ATTR float b_Volts[MAX_LOOPS + 1]; 35 float b_Volts[MAX_LOOPS + 1];
35 RTC_DATA_ATTR float b_Current[MAX_LOOPS + 1]; 36 float b_Current[MAX_LOOPS + 1];
36 RTC_DATA_ATTR bool m_Valid[MAX_LOOPS + 1]; 37 bool m_Valid[MAX_LOOPS + 1];
37 RTC_DATA_ATTR unsigned long m_Time[MAX_LOOPS + 1]; 38 uint64_t m_Time[MAX_LOOPS + 1];
38 RTC_DATA_ATTR unsigned long gLastTime; // millis() 39 uint64_t gLastTime;
40 uint64_t gTimeNext;
39 41
40 uint8_t loopno = 0; 42 uint8_t loopno = 0;
41 uint8_t loops = 0; 43 uint8_t loops = 0;
42 44 uint8_t ST_LOOPS = 6;
45
46 bool WorkAgain = false;
43 int DisCounter = 0; 47 int DisCounter = 0;
44 48
45 extern BMP280_State *bmp280_state; ///< I2C state 49 extern BMP280_State *bmp280_state; ///< I2C state
46 extern SemaphoreHandle_t xSemaphoreBMP280; ///< I2C lock semaphore 50 extern SemaphoreHandle_t xSemaphoreBMP280; ///< I2C lock semaphore
47 extern bmp280_params_t bmp280_params; 51 extern bmp280_params_t bmp280_params;
54 extern WIFI_State *wifi_state; ///< WiFi state 58 extern WIFI_State *wifi_state; ///< WiFi state
55 59
56 uint32_t Alarm = 0; 60 uint32_t Alarm = 0;
57 61
58 /* 62 /*
59 Alarm bits 63 * Alarm bits
60 */ 64 */
61 #define AL_ACCULOW 0x01 65 #define AL_ACCULOW 0x01
62 #define AL_NOWIFI 0x02 66 #define AL_NOWIFI 0x02
63 67
64 68
65 #define ST_INTERVAL 10000 69 #define ST_INTERVAL 10000
169 } 173 }
170 } 174 }
171 175
172 176
173 177
178 uint64_t millis(void) {
179 return esp_timer_get_time() / 1000;
180 }
181
182
183
174 /* 184 /*
175 * Read the temperature and pressure from the BMP280. 185 * Read the temperature and pressure from the BMP280.
176 */ 186 */
177 void getTempBaro() { 187 void getTempBaro() {
178 temperature = 20; 188 temperature = 20;
238 if (s_Current[loopno] > 2000) { 248 if (s_Current[loopno] > 2000) {
239 m_Valid[loopno] = false; 249 m_Valid[loopno] = false;
240 } 250 }
241 } 251 }
242 252
243 // m_Time[loopno] = millis() - gLastTime; 253 uint64_t ms = millis();
244 //gLastTime = millis(); 254 m_Time[loopno] = ms - gLastTime;
245 255 gLastTime = ms;
246 ESP_LOGI(TAG, "Measure: %d Valid: %s Solar: %.4fV %.4fmA Battery: %.4fV %.4fmA time %ld", 256
257 ESP_LOGI(TAG, "Measure: %d Valid: %s Solar: %.4fV %.4fmA Battery: %.4fV %.4fmA time %llu",
247 loopno, (m_Valid[loopno]) ? "true":"false", s_Volts[loopno], s_Current[loopno], b_Volts[loopno], b_Current[loopno], m_Time[loopno]); 258 loopno, (m_Valid[loopno]) ? "true":"false", s_Volts[loopno], s_Current[loopno], b_Volts[loopno], b_Current[loopno], m_Time[loopno]);
248 259
249 if (loopno < (MAX_LOOPS - 1)) 260 if (loopno < (MAX_LOOPS - 1))
250 loopno++; 261 loopno++;
251 } 262 }
252 263
253 264
254 265
255 void app_main(void) 266 void app_main(void)
256 { 267 {
268 uint64_t totalTime, gTimeInMillis;
269
257 #ifdef CONFIG_CODE_PRODUCTION 270 #ifdef CONFIG_CODE_PRODUCTION
258 ESP_LOGI(TAG, "Starting production"); 271 ESP_LOGI(TAG, "Starting production");
259 #endif 272 #endif
260 #ifdef CONFIG_CODE_TESTING 273 #ifdef CONFIG_CODE_TESTING
261 ESP_LOGI(TAG, "Starting testing"); 274 ESP_LOGI(TAG, "Starting testing");
262 #endif 275 #endif
263 276
277 gLastTime = millis();
264 ESP_ERROR_CHECK(i2cdev_init()); 278 ESP_ERROR_CHECK(i2cdev_init());
265 279
266 bmp280_init_default_params(&bmp280_params); 280 bmp280_init_default_params(&bmp280_params);
267 memset(&bmp280_dev, 0, sizeof(bmp280_t)); 281 memset(&bmp280_dev, 0, sizeof(bmp280_t));
268 memset(&ina219_b_dev, 0, sizeof(ina219_t)); 282 memset(&ina219_b_dev, 0, sizeof(ina219_t));
328 while (1) { 342 while (1) {
329 if (OldState != State) { 343 if (OldState != State) {
330 ESP_LOGI(TAG, "Switch to state %d", State); 344 ESP_LOGI(TAG, "Switch to state %d", State);
331 OldState = State; 345 OldState = State;
332 } 346 }
347
348 gTimeInMillis = millis();
349
333 switch (State) { 350 switch (State) {
334 case State_Init: getTempBaro(); 351 case State_Init: getTempBaro();
335 // getLightValues(); 352 // getLightValues();
336 getVoltsCurrent(); 353 getVoltsCurrent();
337 State = State_Connect; 354 State = State_Connect;
340 break; 357 break;
341 358
342 case State_Connect: if (ready_WiFi() && ready_mqtt()) { 359 case State_Connect: if (ready_WiFi() && ready_mqtt()) {
343 State = State_Working; 360 State = State_Working;
344 Alarm &= ~AL_NOWIFI; 361 Alarm &= ~AL_NOWIFI;
362 ESP_LOGI(TAG, "Connected counter %d", DisCounter);
345 DisCounter = 0; 363 DisCounter = 0;
346 } else { 364 } else {
347 DisCounter++; 365 DisCounter++;
348 if (DisCounter > 30) { 366 if (DisCounter > 30) {
349 Alarm |= AL_NOWIFI; 367 Alarm |= AL_NOWIFI;
352 } 370 }
353 vTaskDelay(2000 / portTICK_PERIOD_MS); 371 vTaskDelay(2000 / portTICK_PERIOD_MS);
354 } 372 }
355 break; 373 break;
356 374
357 case State_Working: // WorkAgain = false; 375 case State_Working: WorkAgain = false;
358 376
359 // Measure 377 // Measure
360 getVoltsCurrent(); 378 getVoltsCurrent();
361 solarVolts = solarCurrent = solarPower = batteryVolts = batteryCurrent = batteryPower = 0; 379 solarVolts = solarCurrent = solarPower = batteryVolts = batteryCurrent = batteryPower = 0;
362 // loops = 0; 380 loops = 0;
363 // totalTime = 0; 381 totalTime = 0;
364 // for (int i = 0; i < loopno; i++) { 382 for (int i = 0; i < loopno; i++) {
365 /* 383 /*
366 * If there are only 2 loops, and the flag that we came from deep-sleep is set 384 * If there are only 2 loops, and the flag that we came from deep-sleep is set
367 * then assume the following: 385 * then assume the following:
368 * 1. No current (or very low for the dc converter) during DS_TIME seconds. 386 * 1. No current (or very low for the dc converter) during DS_TIME seconds.
369 * 2. Low power during 0.5 second (no WiFi yet). 387 * 2. Low power during 0.5 second (no WiFi yet).
373 * If there are more loops and we came from continues running do the following: 391 * If there are more loops and we came from continues running do the following:
374 * 1. Take the current use from all exept the last one, weight loops * 10 seconds. 392 * 1. Take the current use from all exept the last one, weight loops * 10 seconds.
375 * 2. Take the last one, and weight for 5 seconds. 393 * 2. Take the last one, and weight for 5 seconds.
376 * Calculate the average battery current from this. 394 * Calculate the average battery current from this.
377 */ 395 */
378 // if (m_Valid[i]) { 396 if (m_Valid[i]) {
379 // solarVolts += s_Volts[i]; 397 solarVolts += s_Volts[i];
380 // solarCurrent += s_Current[i]; 398 solarCurrent += s_Current[i];
381 // batteryVolts += b_Volts[i]; 399 batteryVolts += b_Volts[i];
382 // if (i == (loopno - 1)) { 400 if (i == (loopno - 1)) {
383 // // Add the extra time 401 // Add the extra time
384 // m_Time[i] += SUB_TIME; 402 m_Time[i] += SUB_TIME;
385 // } 403 }
386 // batteryCurrent += b_Current[i] * m_Time[i]; 404 batteryCurrent += b_Current[i] * m_Time[i];
387 // totalTime += m_Time[i]; 405 totalTime += m_Time[i];
388 // loops++; 406 loops++;
389 // } 407 }
390 // } 408 }
391 409
392 // if (EEPROM.read(EM_DS_Active)) { 410 // if (EEPROM.read(EM_DS_Active)) {
393 // totalTime += EEPROM.read(EM_DS_Time) * 1000; 411 // totalTime += EEPROM.read(EM_DS_Time) * 1000;
394 // batteryCurrent += DS_CURRENT * EEPROM.read(EM_DS_Time) * 1000; 412 // batteryCurrent += DS_CURRENT * EEPROM.read(EM_DS_Time) * 1000;
395 // // Serial.printf("Added %d totalTime %d\n", EEPROM.read(EM_DS_Time) * 1000, totalTime); 413 // // Serial.printf("Added %d totalTime %d\n", EEPROM.read(EM_DS_Time) * 1000, totalTime);
396 // } 414 // }
397 415
398 // If valid measurements 416 // If valid measurements
399 // if (loops) { 417 if (loops) {
400 // solarVolts = solarVolts / loops; 418 solarVolts = solarVolts / loops;
401 // solarCurrent = solarCurrent / loops; 419 solarCurrent = solarCurrent / loops;
402 // solarPower = solarVolts * solarCurrent; 420 solarPower = solarVolts * solarCurrent;
403 // batteryVolts = batteryVolts / loops; 421 batteryVolts = batteryVolts / loops;
404 // batteryCurrent = batteryCurrent / totalTime; 422 batteryCurrent = batteryCurrent / totalTime;
405 // batteryPower = batteryVolts * batteryCurrent; 423 batteryPower = batteryVolts * batteryCurrent;
406 BatteryState(batteryVolts, (0 - batteryCurrent) + solarCurrent); 424 BatteryState(batteryVolts, (0 - batteryCurrent) + solarCurrent);
407 425
408 ESP_LOGI(TAG, " Solar Volts: %.4fV Current %.4fmA Power %.4fmW", solarVolts, solarCurrent, solarPower); 426 ESP_LOGI(TAG, " Solar Volts: %.4fV Current %.4fmA Power %.4fmW", solarVolts, solarCurrent, solarPower);
409 ESP_LOGI(TAG, "Battery Volts: %.4fV Current %.4fmA Power %.4fmW", batteryVolts, batteryCurrent, batteryPower); 427 ESP_LOGI(TAG, "Battery Volts: %.4fV Current %.4fmA Power %.4fmW", batteryVolts, batteryCurrent, batteryPower);
410 428
411 /* Check alarm conditions */ 429 /* Check alarm conditions */
412 if (batteryState <= 10) { 430 if (batteryState <= 10) {
413 Alarm |= AL_ACCULOW; 431 Alarm |= AL_ACCULOW;
414 } else { 432 } else {
415 Alarm &= ~AL_ACCULOW; 433 Alarm &= ~AL_ACCULOW;
434 }
435 }
436 getTempBaro();
437 publish();
438 State = State_WorkDone;
439 gTimeNext = millis() + SUB_TIME;
440 break;
441
442 case State_WorkDone: vTaskDelay(2 / portTICK_PERIOD_MS);
443 // Hang around for a while to process the subscriptions.
444 if (WorkAgain) {
445 // Some command was executed.
446 State = State_Working;
416 } 447 }
417 // } 448 if (gTimeInMillis > gTimeNext) {
418 getTempBaro(); 449 State = State_Stop;
419 // Publish(); 450 }
420 // State = State_WorkDone;
421 // gTimeNext = millis() + SUB_TIME;
422 break;
423
424 case State_WorkDone: // Hang around for a while to process the subscriptions.
425 // client.loop();
426 // delay(1);
427 // if (WorkAgain) {
428 // // Some command was executed.
429 // State = State_Working;
430 // }
431 // if (gTimeInMillis > gTimeNext) {
432 // State = State_Stop;
433 // }
434 break; 451 break;
435 452
436 case State_Stop: request_WiFi(false); 453 case State_Stop: request_WiFi(false);
437 // // Reset values for average current measurement. 454 // // Reset values for average current measurement.
438 // HaveIP = false; 455 // HaveIP = false;
439 loopno = 0; 456 loopno = 0;
440 // gLastTime = millis(); 457 gLastTime = millis();
441 // delay(10); 458 vTaskDelay(10 / portTICK_PERIOD_MS);
442 // #if defined(ARDUINO_ESP8266_WEMOS_D1MINI) 459 // #if defined(ARDUINO_ESP8266_WEMOS_D1MINI)
443 // WiFi.forceSleepBegin(0); // 0 == forever 460 // WiFi.forceSleepBegin(0); // 0 == forever
444 // #endif 461 // #endif
445 462
446 /* 463 /*
452 // EEPROM.write(EM_DS_Active, 0); 469 // EEPROM.write(EM_DS_Active, 0);
453 // EEPROM.commit(); 470 // EEPROM.commit();
454 // } 471 // }
455 472
456 // // Active mode, 60 seconds loop 473 // // Active mode, 60 seconds loop
457 // ST_LOOPS = 6; 474 ST_LOOPS = 6;
458 // gTimeNext = millis() + ST_INTERVAL; 475 gTimeNext = millis() + ST_INTERVAL;
476 ESP_LOGI(TAG, "Start sleeploops");
477 State = State_Wait;
459 // #if Debug == true 478 // #if Debug == true
460 // Serial.println(F("Start sleeploops")); 479 // Serial.println(F("Start sleeploops"));
461 // #endif 480 // #endif
462 // State = State_Wait; 481 // State = State_Wait;
463 // } else { 482 // } else {
488 // Serial.println("Write error rtcData"); 507 // Serial.println("Write error rtcData");
489 // } 508 // }
490 // #endif 509 // #endif
491 break; 510 break;
492 511
493 case State_Wait: // if (gTimeInMillis > gTimeNext) { 512 case State_Wait: if (gTimeInMillis > gTimeNext) {
494 // State = State_Measure; 513 State = State_Measure;
495 // } 514 }
515 vTaskDelay(10 / portTICK_PERIOD_MS);
496 break; 516 break;
497 517
498 case State_Measure: getVoltsCurrent(); 518 case State_Measure: getVoltsCurrent();
499 // if (isnan(Temperature)) { 519 // if (isnan(Temperature)) {
500 // getTempHumi(); 520 // getTempHumi();
501 // } 521 // }
502 // gTimeNext = millis() + ST_INTERVAL; 522 gTimeNext = millis() + ST_INTERVAL;
503 // if (loopno >= ST_LOOPS) { 523 if (loopno >= ST_LOOPS) {
524 ESP_LOGI(TAG, "Enough loops, do connect");
504 // getLightValues(); 525 // getLightValues();
505 // State = State_Connect; 526 State = State_Connect;
506 // } else { 527 DisCounter = 0;
507 // State = State_Wait; 528 request_WiFi(true);
508 // } 529 } else {
509 break; 530 State = State_Wait;
510 531 }
511 case State_GoSleep: // ds_time = EEPROM.read(EM_DS_Time); 532 break;
533
534 case State_GoSleep: ESP_LOGE(TAG, "Entered GoSleep -- not supported");
535 // ds_time = EEPROM.read(EM_DS_Time);
512 // #if Debug == true 536 // #if Debug == true
513 // Serial.printf("Going to deep-sleep for %d seconds\n", ds_time); 537 // Serial.printf("Going to deep-sleep for %d seconds\n", ds_time);
514 // #endif 538 // #endif
515 // ESP.deepSleep(ds_time * 1e6); 539 // ESP.deepSleep(ds_time * 1e6);
516 break; 540 break;

mercurial