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.

248 lines
8.9 KiB

  1. // -----------------------------------------------------------------------------
  2. // ADE7853 Sensor over I2C
  3. // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // Implemented by Antonio López <tonilopezmr at gmail dot com>
  5. // -----------------------------------------------------------------------------
  6. #if SENSOR_SUPPORT && ADE7953_SUPPORT
  7. #pragma once
  8. #undef I2C_SUPPORT
  9. #define I2C_SUPPORT 1 // Explicitly request I2C support.
  10. #include "Arduino.h"
  11. #include "I2CSensor.h"
  12. #include <Wire.h>
  13. // -----------------------------------------------------------------------------
  14. // ADE7953 - Energy (Shelly 2.5)
  15. //
  16. // Based on datasheet from https://www.analog.com/en/products/ade7953.html
  17. // Based on Tasmota code https://github.com/arendst/Sonoff-Tasmota/blob/development/sonoff/xnrg_07_ade7953.ino
  18. //
  19. // I2C Address: 0x38
  20. // -----------------------------------------------------------------------------
  21. #define ADE7953_PREF 1540
  22. #define ADE7953_UREF 26000
  23. #define ADE7953_IREF 10000
  24. #define ADE7953_ALL_RELAYS 0
  25. #define ADE7953_RELAY_1 1
  26. #define ADE7953_RELAY_2 2
  27. #define ADE7953_VOLTAGE 1
  28. #define ADE7953_TOTAL_DEVICES 3
  29. class ADE7953Sensor : public I2CSensor {
  30. protected:
  31. struct reading_t {
  32. float current = 0.0;
  33. float power = 0.0;
  34. float energy = 0.0;
  35. };
  36. public:
  37. // ---------------------------------------------------------------------
  38. // Public
  39. // ---------------------------------------------------------------------
  40. ADE7953Sensor(): I2CSensor() {
  41. _sensor_id = SENSOR_ADE7953_ID;
  42. _readings.resize(ADE7953_TOTAL_DEVICES);
  43. _energy_offsets.resize(ADE7953_TOTAL_DEVICES);
  44. _count = _readings.size() * ADE7953_TOTAL_DEVICES + ADE7953_VOLTAGE; //10
  45. }
  46. // ---------------------------------------------------------------------
  47. // Sensors API
  48. // ---------------------------------------------------------------------
  49. // Initialization method, must be idempotent
  50. void begin() {
  51. if (!_dirty) return;
  52. _init();
  53. _dirty = !_ready;
  54. }
  55. // Descriptive name of the sensor
  56. String description() {
  57. char buffer[25];
  58. snprintf(buffer, sizeof(buffer), "ADE7953 @ I2C (0x%02X)", _address);
  59. return String(buffer);
  60. }
  61. // Descriptive name of the slot # index
  62. String slot(unsigned char index) {
  63. return description();
  64. };
  65. // Type for slot # index
  66. unsigned char type(unsigned char index) {
  67. if (index == 0) return MAGNITUDE_VOLTAGE;
  68. index = index % ADE7953_TOTAL_DEVICES;
  69. if (index == 0) return MAGNITUDE_ENERGY;
  70. if (index == 1) return MAGNITUDE_CURRENT;
  71. if (index == 2) return MAGNITUDE_POWER_ACTIVE;
  72. return MAGNITUDE_NONE;
  73. }
  74. // Pre-read hook (usually to populate registers with up-to-date data)
  75. void pre() {
  76. uint32_t active_power1 = 0;
  77. uint32_t active_power2 = 0;
  78. uint32_t current_rms1 = 0;
  79. uint32_t current_rms2 = 0;
  80. uint32_t voltage_rms = 0;
  81. voltage_rms = read(_address, 0x31C); // Both relays
  82. current_rms1 = read(_address, 0x31B); // Relay 1
  83. if (current_rms1 < 2000) { // No load threshold (20mA)
  84. current_rms1 = 0;
  85. active_power1 = 0;
  86. } else {
  87. active_power1 = (int32_t)read(_address, 0x313) * -1; // Relay 1
  88. active_power1 = (active_power1 > 0) ? active_power1 : 0;
  89. }
  90. current_rms2 = read(_address, 0x31A); // Relay 2
  91. if (current_rms2 < 2000) { // No load threshold (20mA)
  92. current_rms2 = 0;
  93. active_power2 = 0;
  94. } else {
  95. active_power2 = (int32_t)read(_address, 0x312); // Relay 2
  96. active_power2 = (active_power2 > 0) ? active_power2 : 0;
  97. }
  98. _voltage = (float) voltage_rms / ADE7953_UREF;
  99. storeReading(
  100. ADE7953_ALL_RELAYS,
  101. (float)(current_rms1 + current_rms2) / (ADE7953_IREF * 10),
  102. (float)(active_power1 + active_power2) / (ADE7953_PREF / 10)
  103. );
  104. storeReading(
  105. ADE7953_RELAY_1,
  106. (float) current_rms1 / (ADE7953_IREF * 10),
  107. (float) active_power1 / (ADE7953_PREF / 10)
  108. );
  109. storeReading(
  110. ADE7953_RELAY_2,
  111. (float)current_rms2 / (ADE7953_IREF * 10),
  112. (float)active_power2 / (ADE7953_PREF / 10)
  113. );
  114. }
  115. inline void storeReading(unsigned int relay, float current, float power) {
  116. auto& reading_ref = _readings.at(relay);
  117. reading_ref.current = current;
  118. reading_ref.power = power;
  119. static unsigned long last = 0;
  120. if (last > 0) {
  121. reading_ref.energy += (power * (millis() - last) / 1000);
  122. }
  123. last = millis();
  124. }
  125. // Current value for slot # index
  126. double value(unsigned char index) {
  127. if (index == 0) return _voltage;
  128. int relay = (index - 1) / ADE7953_TOTAL_DEVICES;
  129. index = index % ADE7953_TOTAL_DEVICES;
  130. if (index == 0) return _energy_offsets[relay] + _readings[relay].energy;
  131. if (index == 1) return _readings[relay].current;
  132. if (index == 2) return _readings[relay].power;
  133. return 0;
  134. }
  135. unsigned int getTotalDevices() {
  136. return ADE7953_TOTAL_DEVICES;
  137. }
  138. void resetEnergy(int relay, double value = 0) {
  139. _energy_offsets[relay] = value;
  140. }
  141. protected:
  142. void _init() {
  143. nice_delay(100); // Need 100mS to init ADE7953
  144. write(_address, 0x102, 0x0004); // Locking the communication interface (Clear bit COMM_LOCK), Enable HPF
  145. write(_address, 0x0FE, 0x00AD); // Unlock register 0x120
  146. write(_address, 0x120, 0x0030); // Configure optimum setting
  147. _ready = true;
  148. }
  149. #if 0
  150. static int reg_size(uint16_t reg) {
  151. int size = 0;
  152. switch ((reg >> 8) & 0x0F) {
  153. case 0x03:
  154. size++;
  155. case 0x02:
  156. size++;
  157. case 0x01:
  158. size++;
  159. case 0x00:
  160. case 0x07:
  161. case 0x08:
  162. size++;
  163. }
  164. return size;
  165. }
  166. #else
  167. // Optimized version of the function above, -80 bytes of code
  168. // Use the known property of register addresses to calculate their size
  169. static const int reg_size(const uint16_t reg) {
  170. const uint8_t mask = ((reg >> 8) & 0b1111);
  171. if (!mask || (mask & 0b1100)) {
  172. return 1;
  173. } else if (mask & 0b0011) {
  174. return mask + 1;
  175. }
  176. return 0;
  177. }
  178. #endif
  179. void write(unsigned char address, uint16_t reg, uint32_t val) {
  180. int size = reg_size(reg);
  181. if (size) {
  182. Wire.beginTransmission(address);
  183. Wire.write((reg >> 8) & 0xFF);
  184. Wire.write(reg & 0xFF);
  185. while (size--) {
  186. Wire.write((val >> (8 * size)) & 0xFF); // Write data, MSB first
  187. }
  188. Wire.endTransmission();
  189. delayMicroseconds(5); // Bus-free time minimum 4.7us
  190. }
  191. }
  192. static uint32_t read(int address, uint16_t reg) {
  193. uint32_t response = 0;
  194. const int size = reg_size(reg);
  195. if (size) {
  196. Wire.beginTransmission(address);
  197. Wire.write((reg >> 8) & 0xFF);
  198. Wire.write(reg & 0xFF);
  199. Wire.endTransmission(0);
  200. Wire.requestFrom(address, size);
  201. if (size <= Wire.available()) {
  202. for (int i = 0; i < size; i++) {
  203. response = response << 8 | Wire.read(); // receive DATA (MSB first)
  204. }
  205. }
  206. }
  207. return response;
  208. }
  209. std::vector<reading_t> _readings;
  210. float _voltage = 0;
  211. std::vector<double> _energy_offsets;
  212. };
  213. #endif // SENSOR_SUPPORT && ADE7953_SUPPORT