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.

154 lines
4.5 KiB

  1. // -----------------------------------------------------------------------------
  2. // ADS121-based Energy Monitor Sensor over I2C
  3. // Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #pragma once
  6. #include "Arduino.h"
  7. #include "BaseSensor.h"
  8. #include "EmonSensor.h"
  9. #if I2C_USE_BRZO
  10. #include <brzo_i2c.h>
  11. #else
  12. #include <Wire.h>
  13. #endif
  14. // ADC121 Registers
  15. #define ADC121_REG_RESULT 0x00
  16. #define ADC121_REG_ALERT 0x01
  17. #define ADC121_REG_CONFIG 0x02
  18. #define ADC121_REG_LIMITL 0x03
  19. #define ADC121_REG_LIMITH 0x04
  20. #define ADC121_REG_HYST 0x05
  21. #define ADC121_REG_CONVL 0x06
  22. #define ADC121_REG_CONVH 0x07
  23. class EmonADC121Sensor : public EmonSensor {
  24. public:
  25. EmonADC121Sensor(unsigned char address, double voltage, unsigned char bits, double ref, double ratio): EmonSensor(voltage, bits, ref, ratio) {
  26. // Cache
  27. _address = address;
  28. _count = _magnitudes;
  29. // Init sensor
  30. #if I2C_USE_BRZO
  31. uint8_t buffer[2];
  32. buffer[0] = ADC121_REG_CONFIG;
  33. buffer[1] = 0x00;
  34. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  35. brzo_i2c_write(buffer, 2, false);
  36. brzo_i2c_end_transaction();
  37. #else
  38. Wire.beginTransmission(_address);
  39. Wire.write(ADC121_REG_CONFIG);
  40. Wire.write(0x00);
  41. Wire.endTransmission();
  42. #endif
  43. // warmup
  44. read(_address, _pivot);
  45. }
  46. // Descriptive name of the sensor
  47. String name() {
  48. char buffer[30];
  49. snprintf(buffer, sizeof(buffer), "EMON @ ADC121 @ I2C (0x%02X)", _address);
  50. return String(buffer);
  51. }
  52. // Descriptive name of the slot # index
  53. String slot(unsigned char index) {
  54. return name();
  55. }
  56. // Type for slot # index
  57. magnitude_t type(unsigned char index) {
  58. _error = SENSOR_ERROR_OK;
  59. unsigned char i = 0;
  60. #if EMON_REPORT_CURRENT
  61. if (index == i++) return MAGNITUDE_CURRENT;
  62. #endif
  63. #if EMON_REPORT_POWER
  64. if (index == i++) return MAGNITUDE_POWER_APPARENT;
  65. #endif
  66. #if EMON_REPORT_ENERGY
  67. if (index == i) return MAGNITUDE_ENERGY;
  68. #endif
  69. _error = SENSOR_ERROR_OUT_OF_RANGE;
  70. return MAGNITUDE_NONE;
  71. }
  72. // Current value for slot # index
  73. double value(unsigned char index) {
  74. _error = SENSOR_ERROR_OK;
  75. // Cache the value
  76. static unsigned long last = 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. double _current = 0;
  123. #if EMON_REPORT_ENERGY
  124. unsigned long _energy = 0;
  125. #endif
  126. };