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; |
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)); |
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 /* |
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; |