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.

141 lines
3.5 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(ADC121_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. // POWER API
  45. // -----------------------------------------------------------------------------
  46. double _powerCurrent() {
  47. static unsigned long last = 0;
  48. static double current = 0;
  49. if (millis() - last > 1000) {
  50. last = millis();
  51. current = _emon.getCurrent(EMON_SAMPLES);
  52. current -= EMON_CURRENT_OFFSET;
  53. if (current < 0) current = 0;
  54. }
  55. return current;
  56. }
  57. double _powerVoltage() {
  58. return _power_voltage;
  59. }
  60. double _powerActivePower() {
  61. return _powerApparentPower();
  62. }
  63. double _powerApparentPower() {
  64. return _powerCurrent() * _powerVoltage();
  65. }
  66. double _powerReactivePower() {
  67. return 0;
  68. }
  69. double _powerPowerFactor() {
  70. return 1;
  71. }
  72. void _powerEnabledProvider() {
  73. // Nothing to do
  74. }
  75. void _powerConfigureProvider() {
  76. _emon.setCurrentRatio(getSetting("powerRatioC", EMON_CURRENT_RATIO).toFloat());
  77. _power_voltage = getSetting("powerVoltage", POWER_VOLTAGE).toFloat();
  78. }
  79. void _powerSetupProvider() {
  80. _emon.initCurrent(currentCallback, EMON_ADC_BITS, EMON_REFERENCE_VOLTAGE, EMON_CURRENT_RATIO);
  81. #if POWER_PROVIDER == POWER_PROVIDER_EMON_ADC121
  82. uint8_t buffer[2];
  83. buffer[0] = ADC121_REG_CONFIG;
  84. buffer[1] = 0x00;
  85. brzo_i2c_start_transaction(ADC121_I2C_ADDRESS, I2C_SCL_FREQUENCY);
  86. brzo_i2c_write(buffer, 2, false);
  87. brzo_i2c_end_transaction();
  88. #endif
  89. powerConfigureProvider();
  90. _emon.warmup();
  91. }
  92. void _powerLoopProvider(bool before) {
  93. if (before) {
  94. static unsigned long last = 0;
  95. if (millis() - last > POWER_READ_INTERVAL) {
  96. last = millis();
  97. _power_newdata = true;
  98. }
  99. }
  100. }
  101. #endif // (POWER_PROVIDER & POWER_PROVIDER_EMON == POWER_PROVIDER_EMON)