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.

139 lines
4.1 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 = 4;
  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. if (index == 0) return MAGNITUDE_CURRENT;
  59. if (index == 1) return MAGNITUDE_POWER_APPARENT;
  60. if (index == 2) return MAGNITUDE_ENERGY;
  61. if (index == 3) return MAGNITUDE_ENERGY_DELTA;
  62. _error = SENSOR_ERROR_OUT_OF_RANGE;
  63. return MAGNITUDE_NONE;
  64. }
  65. // Current value for slot # index
  66. double value(unsigned char index) {
  67. _error = SENSOR_ERROR_OK;
  68. // Cache the value
  69. static unsigned long last = 0;
  70. static double current = 0;
  71. static unsigned long energy_delta = 0;
  72. if ((last == 0) || (millis() - last > 1000)) {
  73. current = read(0, _pivot);
  74. energy_delta = current * _voltage * (millis() - last) / 1000;
  75. _energy += energy_delta;
  76. last = millis();
  77. }
  78. if (index == 0) return current;
  79. if (index == 1) return current * _voltage;
  80. if (index == 2) return _energy;
  81. if (index == 3) return energy_delta;
  82. _error = SENSOR_ERROR_OUT_OF_RANGE;
  83. return 0;
  84. }
  85. protected:
  86. unsigned int readADC(unsigned char channel) {
  87. unsigned int value;
  88. #if I2C_USE_BRZO
  89. uint8_t buffer[2];
  90. buffer[0] = ADC121_REG_RESULT;
  91. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  92. brzo_i2c_write(buffer, 1, false);
  93. brzo_i2c_read(buffer, 2, false);
  94. brzo_i2c_end_transaction();
  95. value = (buffer[0] & 0x0F) << 8;
  96. value |= buffer[1];
  97. #else
  98. Wire.beginTransmission(_address);
  99. Wire.write(ADC121_REG_RESULT);
  100. Wire.endTransmission();
  101. Wire.requestFrom(_address, (unsigned char) 2);
  102. value = (Wire.read() & 0x0F) << 8;
  103. value = value + Wire.read();
  104. #endif
  105. return value;
  106. }
  107. unsigned char _address;
  108. unsigned long _energy = 0;
  109. double _pivot = 0;
  110. };