Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.6 KiB

  1. /*
  2. POWER EMON MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if (POWER_PROVIDER & POWER_PROVIDER_EMON == POWER_PROVIDER_EMON)
  6. // -----------------------------------------------------------------------------
  7. // MODULE GLOBALS AND CACHE
  8. // -----------------------------------------------------------------------------
  9. #include <EmonLiteESP.h>
  10. EmonLiteESP _emon;
  11. #if POWER_PROVIDER == POWER_PROVIDER_EMON_ADC121
  12. #include "brzo_i2c.h"
  13. // ADC121 Registers
  14. #define ADC121_REG_RESULT 0x00
  15. #define ADC121_REG_ALERT 0x01
  16. #define ADC121_REG_CONFIG 0x02
  17. #define ADC121_REG_LIMITL 0x03
  18. #define ADC121_REG_LIMITH 0x04
  19. #define ADC121_REG_HYST 0x05
  20. #define ADC121_REG_CONVL 0x06
  21. #define ADC121_REG_CONVH 0x07
  22. #endif // POWER_PROVIDER == POWER_PROVIDER_EMON_ADC121
  23. // -----------------------------------------------------------------------------
  24. // HAL
  25. // -----------------------------------------------------------------------------
  26. unsigned int currentCallback() {
  27. #if POWER_PROVIDER == POWER_PROVIDER_EMON_ANALOG
  28. return analogRead(0);
  29. #endif // POWER_PROVIDER == POWER_PROVIDER_EMON_ANALOG
  30. #if POWER_PROVIDER == POWER_PROVIDER_EMON_ADC121
  31. uint8_t buffer[2];
  32. brzo_i2c_start_transaction(POWER_I2C_ADDRESS, I2C_SCL_FREQUENCY);
  33. buffer[0] = ADC121_REG_RESULT;
  34. brzo_i2c_write(buffer, 1, false);
  35. brzo_i2c_read(buffer, 2, false);
  36. brzo_i2c_end_transaction();
  37. unsigned int value;
  38. value = (buffer[0] & 0x0F) << 8;
  39. value |= buffer[1];
  40. return value;
  41. #endif // POWER_PROVIDER == POWER_PROVIDER_EMON_ADC121
  42. }
  43. // -----------------------------------------------------------------------------
  44. double _powerCurrent() {
  45. static unsigned long last = 0;
  46. static double current = 0;
  47. if (millis() - last > 1000) {
  48. last = millis();
  49. current = _emon.getCurrent(POWER_SAMPLES);
  50. current -= POWER_CURRENT_OFFSET;
  51. if (current < 0) current = 0;
  52. }
  53. return current;
  54. }
  55. double _powerVoltage() {
  56. return _power_voltage;
  57. }
  58. double _powerActivePower() {
  59. return _powerApparentPower();
  60. }
  61. double _powerApparentPower() {
  62. return _powerCurrent() * _powerVoltage();
  63. }
  64. double _powerReactivePower() {
  65. return 0;
  66. }
  67. double _powerPowerFactor() {
  68. return 1;
  69. }
  70. // -----------------------------------------------------------------------------
  71. // PUBLIC API
  72. // -----------------------------------------------------------------------------
  73. void powerEnabledProvider() {
  74. // Nothing to do
  75. }
  76. void powerConfigureProvider() {
  77. _emon.setCurrentRatio(getSetting("powerRatioC", POWER_CURRENT_RATIO).toFloat());
  78. _power_voltage = getSetting("powerVoltage", POWER_VOLTAGE).toFloat();
  79. }
  80. void powerSetupProvider() {
  81. _emon.initCurrent(currentCallback, POWER_ADC_BITS, POWER_REFERENCE_VOLTAGE, POWER_CURRENT_RATIO);
  82. #if POWER_PROVIDER == POWER_PROVIDER_EMON_ADC121
  83. uint8_t buffer[2];
  84. buffer[0] = ADC121_REG_CONFIG;
  85. buffer[1] = 0x00;
  86. brzo_i2c_start_transaction(POWER_I2C_ADDRESS, I2C_SCL_FREQUENCY);
  87. brzo_i2c_write(buffer, 2, false);
  88. brzo_i2c_end_transaction();
  89. #endif
  90. powerConfigureProvider();
  91. _emon.warmup();
  92. }
  93. void powerLoopProvider(bool before) {
  94. if (before) {
  95. static unsigned long last = 0;
  96. if (millis() - last > POWER_READ_INTERVAL) {
  97. last = millis();
  98. _power_newdata = true;
  99. }
  100. }
  101. }
  102. #endif // (POWER_PROVIDER & POWER_PROVIDER_EMON == POWER_PROVIDER_EMON)