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.

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