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);
  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. static double current = 0;
  77. if ((last == 0) || (millis() - last > 1000)) {
  78. current = read(0, _pivot);
  79. #if EMON_REPORT_ENERGY
  80. _energy += (current * _voltage * (millis() - last) / 1000);
  81. #endif
  82. last = millis();
  83. }
  84. // Report
  85. unsigned char i = 0;
  86. #if EMON_REPORT_CURRENT
  87. if (index == i++) return current;
  88. #endif
  89. #if EMON_REPORT_POWER
  90. if (index == i++) return current * _voltage;
  91. #endif
  92. #if EMON_REPORT_ENERGY
  93. if (index == i) return _energy;
  94. #endif
  95. _error = SENSOR_ERROR_OUT_OF_RANGE;
  96. return 0;
  97. }
  98. protected:
  99. unsigned int readADC(unsigned char channel) {
  100. unsigned int value;
  101. #if I2C_USE_BRZO
  102. uint8_t buffer[2];
  103. buffer[0] = ADC121_REG_RESULT;
  104. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  105. brzo_i2c_write(buffer, 1, false);
  106. brzo_i2c_read(buffer, 2, false);
  107. brzo_i2c_end_transaction();
  108. value = (buffer[0] & 0x0F) << 8;
  109. value |= buffer[1];
  110. #else
  111. Wire.beginTransmission(_address);
  112. Wire.write(ADC121_REG_RESULT);
  113. Wire.endTransmission();
  114. Wire.requestFrom(_address, (unsigned char) 2);
  115. value = (Wire.read() & 0x0F) << 8;
  116. value = value + Wire.read();
  117. #endif
  118. return value;
  119. }
  120. unsigned char _address;
  121. double _pivot = 0;
  122. #if EMON_REPORT_ENERGY
  123. unsigned long _energy = 0;
  124. #endif
  125. };