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.

153 lines
4.4 KiB

  1. // -----------------------------------------------------------------------------
  2. // Energy monitor sensor
  3. // -----------------------------------------------------------------------------
  4. #pragma once
  5. #include "Arduino.h"
  6. #include "BaseSensor.h"
  7. #include "EmonSensor.h"
  8. #if I2C_USE_BRZO
  9. #include <brzo_i2c.h>
  10. #else
  11. #include <Wire.h>
  12. #endif
  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. class EmonADC121Sensor : public EmonSensor {
  23. public:
  24. EmonADC121Sensor(unsigned char address, double voltage, unsigned char bits, double ref, double ratio): EmonSensor(voltage, bits, ref, ratio) {
  25. // Cache
  26. _address = address;
  27. _count = _magnitudes;
  28. // Init sensor
  29. #if I2C_USE_BRZO
  30. uint8_t buffer[2];
  31. buffer[0] = ADC121_REG_CONFIG;
  32. buffer[1] = 0x00;
  33. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  34. brzo_i2c_write(buffer, 2, false);
  35. brzo_i2c_end_transaction();
  36. #else
  37. Wire.beginTransmission(_address);
  38. Wire.write(ADC121_REG_CONFIG);
  39. Wire.write(0x00);
  40. Wire.endTransmission();
  41. #endif
  42. // warmup
  43. read(_address, _pivot);
  44. }
  45. // Descriptive name of the sensor
  46. String name() {
  47. char buffer[30];
  48. snprintf(buffer, sizeof(buffer), "EMON @ ADC121 @ I2C (0x%02X)", _address);
  49. return String(buffer);
  50. }
  51. // Descriptive name of the slot # index
  52. String slot(unsigned char index) {
  53. return name();
  54. }
  55. // Type for slot # index
  56. magnitude_t type(unsigned char index) {
  57. _error = SENSOR_ERROR_OK;
  58. unsigned char i = 0;
  59. #if EMON_REPORT_CURRENT
  60. if (index == i++) return MAGNITUDE_CURRENT;
  61. #endif
  62. #if EMON_REPORT_POWER
  63. if (index == i++) return MAGNITUDE_POWER_APPARENT;
  64. #endif
  65. #if EMON_REPORT_ENERGY
  66. if (index == i) return MAGNITUDE_ENERGY;
  67. #endif
  68. _error = SENSOR_ERROR_OUT_OF_RANGE;
  69. return MAGNITUDE_NONE;
  70. }
  71. // Current value for slot # index
  72. double value(unsigned char index) {
  73. _error = SENSOR_ERROR_OK;
  74. // Cache the value
  75. static unsigned long last = 0;
  76. if ((last == 0) || (millis() - last > 1000)) {
  77. _current = read(0, _pivot);
  78. #if EMON_REPORT_ENERGY
  79. _energy += (_current * _voltage * (millis() - last) / 1000);
  80. #endif
  81. last = millis();
  82. }
  83. // Report
  84. unsigned char i = 0;
  85. #if EMON_REPORT_CURRENT
  86. if (index == i++) return _current;
  87. #endif
  88. #if EMON_REPORT_POWER
  89. if (index == i++) return _current * _voltage;
  90. #endif
  91. #if EMON_REPORT_ENERGY
  92. if (index == i) return _energy;
  93. #endif
  94. _error = SENSOR_ERROR_OUT_OF_RANGE;
  95. return 0;
  96. }
  97. protected:
  98. unsigned int readADC(unsigned char channel) {
  99. unsigned int value;
  100. #if I2C_USE_BRZO
  101. uint8_t buffer[2];
  102. buffer[0] = ADC121_REG_RESULT;
  103. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  104. brzo_i2c_write(buffer, 1, false);
  105. brzo_i2c_read(buffer, 2, false);
  106. brzo_i2c_end_transaction();
  107. value = (buffer[0] & 0x0F) << 8;
  108. value |= buffer[1];
  109. #else
  110. Wire.beginTransmission(_address);
  111. Wire.write(ADC121_REG_RESULT);
  112. Wire.endTransmission();
  113. Wire.requestFrom(_address, (unsigned char) 2);
  114. value = (Wire.read() & 0x0F) << 8;
  115. value = value + Wire.read();
  116. #endif
  117. return value;
  118. }
  119. unsigned char _address;
  120. double _pivot = 0;
  121. double _current = 0;
  122. #if EMON_REPORT_ENERGY
  123. unsigned long _energy = 0;
  124. #endif
  125. };