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.

194 lines
5.7 KiB

  1. // -----------------------------------------------------------------------------
  2. // ADS121-based Energy Monitor Sensor over I2C
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && EMON_ADC121_SUPPORT
  6. #pragma once
  7. #include "Arduino.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. #define ADC121_RESOLUTION 12
  24. #define ADC121_CHANNELS 1
  25. class EmonADC121Sensor : public EmonSensor {
  26. public:
  27. // ---------------------------------------------------------------------
  28. // Public
  29. // ---------------------------------------------------------------------
  30. EmonADC121Sensor(): EmonSensor() {
  31. _channels = ADC121_CHANNELS;
  32. _sensor_id = SENSOR_EMON_ADC121_ID;
  33. init();
  34. }
  35. // ---------------------------------------------------------------------
  36. // Sensor API
  37. // ---------------------------------------------------------------------
  38. // Initialization method, must be idempotent
  39. void begin() {
  40. if (!_dirty) return;
  41. _dirty = false;
  42. // Discover
  43. unsigned char addresses[] = {0x50, 0x51, 0x52, 0x54, 0x55, 0x56, 0x58, 0x59, 0x5A};
  44. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  45. if (_address == 0) return;
  46. // Init sensor
  47. #if I2C_USE_BRZO
  48. uint8_t buffer[2];
  49. buffer[0] = ADC121_REG_CONFIG;
  50. buffer[1] = 0x00;
  51. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  52. brzo_i2c_write(buffer, 2, false);
  53. brzo_i2c_end_transaction();
  54. #else
  55. Wire.beginTransmission(_address);
  56. Wire.write(ADC121_REG_CONFIG);
  57. Wire.write(0x00);
  58. Wire.endTransmission();
  59. #endif
  60. // Just one channel
  61. _count = _magnitudes;
  62. // Bit depth
  63. _resolution = ADC121_RESOLUTION;
  64. // Call the parent class method
  65. EmonSensor::begin();
  66. // warmup channel 0 (the only one)
  67. read(0);
  68. }
  69. // Descriptive name of the sensor
  70. String description() {
  71. char buffer[30];
  72. snprintf(buffer, sizeof(buffer), "EMON @ ADC121 @ I2C (0x%02X)", _address);
  73. return String(buffer);
  74. }
  75. // Pre-read hook (usually to populate registers with up-to-date data)
  76. void pre() {
  77. if (_address == 0) {
  78. _error = SENSOR_ERROR_UNKNOWN_ID;
  79. return;
  80. }
  81. _current[0] = read(0);
  82. #if EMON_REPORT_ENERGY
  83. static unsigned long last = 0;
  84. if (last > 0) {
  85. _energy[0] += (_current[0] * _voltage * (millis() - last) / 1000);
  86. }
  87. last = millis();
  88. #endif
  89. _error = SENSOR_ERROR_OK;
  90. }
  91. // Type for slot # index
  92. unsigned char type(unsigned char index) {
  93. _error = SENSOR_ERROR_OK;
  94. unsigned char i=0;
  95. #if EMON_REPORT_CURRENT
  96. if (index == i++) return MAGNITUDE_CURRENT;
  97. #endif
  98. #if EMON_REPORT_POWER
  99. if (index == i++) return MAGNITUDE_POWER_APPARENT;
  100. #endif
  101. #if EMON_REPORT_ENERGY
  102. if (index == i) return MAGNITUDE_ENERGY;
  103. #endif
  104. _error = SENSOR_ERROR_OUT_OF_RANGE;
  105. return MAGNITUDE_NONE;
  106. }
  107. // Current value for slot # index
  108. double value(unsigned char index) {
  109. _error = SENSOR_ERROR_OK;
  110. unsigned char channel = index / _magnitudes;
  111. unsigned char i=0;
  112. #if EMON_REPORT_CURRENT
  113. if (index == i++) return _current[channel];
  114. #endif
  115. #if EMON_REPORT_POWER
  116. if (index == i++) return _current[channel] * _voltage;
  117. #endif
  118. #if EMON_REPORT_ENERGY
  119. if (index == i) return _energy[channel];
  120. #endif
  121. _error = SENSOR_ERROR_OUT_OF_RANGE;
  122. return 0;
  123. }
  124. protected:
  125. // ---------------------------------------------------------------------
  126. // Protected
  127. // ---------------------------------------------------------------------
  128. unsigned int readADC(unsigned char channel) {
  129. (void) channel;
  130. unsigned int value;
  131. #if I2C_USE_BRZO
  132. uint8_t buffer[2];
  133. buffer[0] = ADC121_REG_RESULT;
  134. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  135. brzo_i2c_write(buffer, 1, false);
  136. brzo_i2c_read(buffer, 2, false);
  137. brzo_i2c_end_transaction();
  138. value = (buffer[0] & 0x0F) << 8;
  139. value |= buffer[1];
  140. #else
  141. Wire.beginTransmission(_address);
  142. Wire.write(ADC121_REG_RESULT);
  143. Wire.endTransmission();
  144. Wire.requestFrom(_address, (unsigned char) 2);
  145. value = (Wire.read() & 0x0F) << 8;
  146. value = value + Wire.read();
  147. #endif
  148. return value;
  149. }
  150. };
  151. #endif // SENSOR_SUPPORT && EMON_ADC121_SUPPORT