xsns_13_ina219.ino 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. xsns_13_ina219.ino - INA219 Current Sensor support for Sonoff-Tasmota
  3. Copyright (C) 2018 Stefan Bode and Theo Arends
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifdef USE_I2C
  16. #ifdef USE_INA219
  17. /*********************************************************************************************\
  18. * INA219 - Low voltage (max 32V!) Current sensor
  19. *
  20. * Source: Adafruit Industries
  21. *
  22. * I2C Address: 0x40, 0x41 0x44 or 0x45
  23. \*********************************************************************************************/
  24. #define XSNS_13 13
  25. #define INA219_ADDRESS1 (0x40) // 1000000 (A0+A1=GND)
  26. #define INA219_ADDRESS2 (0x41) // 1000000 (A0=Vcc, A1=GND)
  27. #define INA219_ADDRESS3 (0x44) // 1000000 (A0=GND, A1=Vcc)
  28. #define INA219_ADDRESS4 (0x45) // 1000000 (A0+A1=Vcc)
  29. #define INA219_READ (0x01)
  30. #define INA219_REG_CONFIG (0x00)
  31. #define INA219_CONFIG_RESET (0x8000) // Reset Bit
  32. #define INA219_CONFIG_BVOLTAGERANGE_MASK (0x2000) // Bus Voltage Range Mask
  33. #define INA219_CONFIG_BVOLTAGERANGE_16V (0x0000) // 0-16V Range
  34. #define INA219_CONFIG_BVOLTAGERANGE_32V (0x2000) // 0-32V Range
  35. #define INA219_CONFIG_GAIN_MASK (0x1800) // Gain Mask
  36. #define INA219_CONFIG_GAIN_1_40MV (0x0000) // Gain 1, 40mV Range
  37. #define INA219_CONFIG_GAIN_2_80MV (0x0800) // Gain 2, 80mV Range
  38. #define INA219_CONFIG_GAIN_4_160MV (0x1000) // Gain 4, 160mV Range
  39. #define INA219_CONFIG_GAIN_8_320MV (0x1800) // Gain 8, 320mV Range
  40. #define INA219_CONFIG_BADCRES_MASK (0x0780) // Bus ADC Resolution Mask
  41. #define INA219_CONFIG_BADCRES_9BIT (0x0080) // 9-bit bus res = 0..511
  42. #define INA219_CONFIG_BADCRES_10BIT (0x0100) // 10-bit bus res = 0..1023
  43. #define INA219_CONFIG_BADCRES_11BIT (0x0200) // 11-bit bus res = 0..2047
  44. #define INA219_CONFIG_BADCRES_12BIT (0x0400) // 12-bit bus res = 0..4097
  45. #define INA219_CONFIG_SADCRES_MASK (0x0078) // Shunt ADC Resolution and Averaging Mask
  46. #define INA219_CONFIG_SADCRES_9BIT_1S_84US (0x0000) // 1 x 9-bit shunt sample
  47. #define INA219_CONFIG_SADCRES_10BIT_1S_148US (0x0008) // 1 x 10-bit shunt sample
  48. #define INA219_CONFIG_SADCRES_11BIT_1S_276US (0x0010) // 1 x 11-bit shunt sample
  49. #define INA219_CONFIG_SADCRES_12BIT_1S_532US (0x0018) // 1 x 12-bit shunt sample
  50. #define INA219_CONFIG_SADCRES_12BIT_2S_1060US (0x0048) // 2 x 12-bit shunt samples averaged together
  51. #define INA219_CONFIG_SADCRES_12BIT_4S_2130US (0x0050) // 4 x 12-bit shunt samples averaged together
  52. #define INA219_CONFIG_SADCRES_12BIT_8S_4260US (0x0058) // 8 x 12-bit shunt samples averaged together
  53. #define INA219_CONFIG_SADCRES_12BIT_16S_8510US (0x0060) // 16 x 12-bit shunt samples averaged together
  54. #define INA219_CONFIG_SADCRES_12BIT_32S_17MS (0x0068) // 32 x 12-bit shunt samples averaged together
  55. #define INA219_CONFIG_SADCRES_12BIT_64S_34MS (0x0070) // 64 x 12-bit shunt samples averaged together
  56. #define INA219_CONFIG_SADCRES_12BIT_128S_69MS (0x0078) // 128 x 12-bit shunt samples averaged together
  57. #define INA219_CONFIG_MODE_MASK (0x0007) // Operating Mode Mask
  58. #define INA219_CONFIG_MODE_POWERDOWN (0x0000)
  59. #define INA219_CONFIG_MODE_SVOLT_TRIGGERED (0x0001)
  60. #define INA219_CONFIG_MODE_BVOLT_TRIGGERED (0x0002)
  61. #define INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED (0x0003)
  62. #define INA219_CONFIG_MODE_ADCOFF (0x0004)
  63. #define INA219_CONFIG_MODE_SVOLT_CONTINUOUS (0x0005)
  64. #define INA219_CONFIG_MODE_BVOLT_CONTINUOUS (0x0006)
  65. #define INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS (0x0007)
  66. #define INA219_REG_SHUNTVOLTAGE (0x01)
  67. #define INA219_REG_BUSVOLTAGE (0x02)
  68. #define INA219_REG_POWER (0x03)
  69. #define INA219_REG_CURRENT (0x04)
  70. #define INA219_REG_CALIBRATION (0x05)
  71. uint8_t ina219_type = 0;
  72. uint8_t ina219_address;
  73. uint8_t ina219_addresses[] = { INA219_ADDRESS1, INA219_ADDRESS2, INA219_ADDRESS3, INA219_ADDRESS4 };
  74. uint32_t ina219_cal_value = 0;
  75. // The following multiplier is used to convert raw current values to mA, taking into account the current config settings
  76. uint32_t ina219_current_divider_ma = 0;
  77. uint8_t ina219_valid = 0;
  78. float ina219_voltage = 0;
  79. float ina219_current = 0;
  80. char ina219_types[] = "INA219";
  81. bool Ina219SetCalibration(uint8_t mode)
  82. {
  83. uint16_t config = 0;
  84. switch (mode &3) {
  85. case 0: // 32V 2A
  86. case 3:
  87. ina219_cal_value = 4096;
  88. ina219_current_divider_ma = 10; // Current LSB = 100uA per bit (1000/100 = 10)
  89. config = INA219_CONFIG_BVOLTAGERANGE_32V | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BADCRES_12BIT | INA219_CONFIG_SADCRES_12BIT_1S_532US | INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
  90. break;
  91. case 1: // 32V 1A
  92. ina219_cal_value = 10240;
  93. ina219_current_divider_ma = 25; // Current LSB = 40uA per bit (1000/40 = 25)
  94. config |= INA219_CONFIG_BVOLTAGERANGE_32V | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BADCRES_12BIT | INA219_CONFIG_SADCRES_12BIT_1S_532US | INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
  95. break;
  96. case 2: // 16V 0.4A
  97. ina219_cal_value = 8192;
  98. ina219_current_divider_ma = 20; // Current LSB = 50uA per bit (1000/50 = 20)
  99. config |= INA219_CONFIG_BVOLTAGERANGE_16V | INA219_CONFIG_GAIN_1_40MV | INA219_CONFIG_BADCRES_12BIT | INA219_CONFIG_SADCRES_12BIT_1S_532US | INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
  100. break;
  101. }
  102. // Set Calibration register to 'Cal' calculated above
  103. bool success = I2cWrite16(ina219_address, INA219_REG_CALIBRATION, ina219_cal_value);
  104. if (success) {
  105. // Set Config register to take into account the settings above
  106. I2cWrite16(ina219_address, INA219_REG_CONFIG, config);
  107. }
  108. return success;
  109. }
  110. float Ina219GetShuntVoltage_mV(void)
  111. {
  112. // raw shunt voltage (16-bit signed integer, so +-32767)
  113. int16_t value = I2cReadS16(ina219_address, INA219_REG_SHUNTVOLTAGE);
  114. // shunt voltage in mV (so +-327mV)
  115. return value * 0.01;
  116. }
  117. float Ina219GetBusVoltage_V(void)
  118. {
  119. // Shift to the right 3 to drop CNVR and OVF and multiply by LSB
  120. // raw bus voltage (16-bit signed integer, so +-32767)
  121. int16_t value = (int16_t)(((uint16_t)I2cReadS16(ina219_address, INA219_REG_BUSVOLTAGE) >> 3) * 4);
  122. // bus voltage in volts
  123. return value * 0.001;
  124. }
  125. float Ina219GetCurrent_mA(void)
  126. {
  127. // Sometimes a sharp load will reset the INA219, which will reset the cal register,
  128. // meaning CURRENT and POWER will not be available ... avoid this by always setting
  129. // a cal value even if it's an unfortunate extra step
  130. I2cWrite16(ina219_address, INA219_REG_CALIBRATION, ina219_cal_value);
  131. // Now we can safely read the CURRENT register!
  132. // raw current value (16-bit signed integer, so +-32767)
  133. float value = I2cReadS16(ina219_address, INA219_REG_CURRENT);
  134. value /= ina219_current_divider_ma;
  135. // current value in mA, taking into account the config settings and current LSB
  136. return value;
  137. }
  138. bool Ina219Read(void)
  139. {
  140. ina219_voltage = Ina219GetBusVoltage_V() + (Ina219GetShuntVoltage_mV() / 1000);
  141. ina219_current = Ina219GetCurrent_mA() / 1000;
  142. ina219_valid = SENSOR_MAX_MISS;
  143. return true;
  144. }
  145. /*********************************************************************************************\
  146. * Command Sensor13
  147. *
  148. * 0 - Max 32V 2A range
  149. * 1 - Max 32V 1A range
  150. * 2 - Max 16V 0.4A range
  151. \*********************************************************************************************/
  152. bool Ina219CommandSensor(void)
  153. {
  154. boolean serviced = true;
  155. if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
  156. Settings.ina219_mode = XdrvMailbox.payload;
  157. restart_flag = 2;
  158. }
  159. snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_SENSOR_INDEX_NVALUE, XSNS_13, Settings.ina219_mode);
  160. return serviced;
  161. }
  162. /********************************************************************************************/
  163. void Ina219Detect(void)
  164. {
  165. if (ina219_type) { return; }
  166. for (byte i = 0; i < sizeof(ina219_addresses); i++) {
  167. ina219_address = ina219_addresses[i];
  168. if (Ina219SetCalibration(Settings.ina219_mode)) {
  169. ina219_type = 1;
  170. snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, ina219_types, ina219_address);
  171. AddLog(LOG_LEVEL_DEBUG);
  172. break;
  173. }
  174. }
  175. }
  176. void Ina219EverySecond(void)
  177. {
  178. if (87 == (uptime %100)) {
  179. // 2mS
  180. Ina219Detect();
  181. }
  182. else {
  183. // 3mS
  184. if (ina219_type) {
  185. if (!Ina219Read()) {
  186. AddLogMissed(ina219_types, ina219_valid);
  187. // if (!ina219_valid) { ina219_type = 0; }
  188. }
  189. }
  190. }
  191. }
  192. #ifdef USE_WEBSERVER
  193. const char HTTP_SNS_INA219_DATA[] PROGMEM = "%s"
  194. "{s}INA219 " D_VOLTAGE "{m}%s " D_UNIT_VOLT "{e}"
  195. "{s}INA219 " D_CURRENT "{m}%s " D_UNIT_AMPERE "{e}"
  196. "{s}INA219 " D_POWERUSAGE "{m}%s " D_UNIT_WATT "{e}";
  197. #endif // USE_WEBSERVER
  198. void Ina219Show(boolean json)
  199. {
  200. if (ina219_valid) {
  201. float fpower = ina219_voltage * ina219_current;
  202. char voltage[33];
  203. dtostrfd(ina219_voltage, Settings.flag2.voltage_resolution, voltage);
  204. char power[33];
  205. dtostrfd(fpower, Settings.flag2.wattage_resolution, power);
  206. char current[33];
  207. dtostrfd(ina219_current, Settings.flag2.current_resolution, current);
  208. if (json) {
  209. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"" D_JSON_VOLTAGE "\":%s,\"" D_JSON_CURRENT "\":%s,\"" D_JSON_POWERUSAGE "\":%s}"),
  210. mqtt_data, ina219_types, voltage, current, power);
  211. #ifdef USE_DOMOTICZ
  212. if (0 == tele_period) {
  213. DomoticzSensor(DZ_VOLTAGE, voltage);
  214. DomoticzSensor(DZ_CURRENT, current);
  215. }
  216. #endif // USE_DOMOTICZ
  217. #ifdef USE_WEBSERVER
  218. } else {
  219. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_INA219_DATA, mqtt_data, voltage, current, power);
  220. #endif // USE_WEBSERVER
  221. }
  222. }
  223. }
  224. /*********************************************************************************************\
  225. * Interface
  226. \*********************************************************************************************/
  227. boolean Xsns13(byte function)
  228. {
  229. boolean result = false;
  230. if (i2c_flg) {
  231. switch (function) {
  232. case FUNC_COMMAND:
  233. if ((XSNS_13 == XdrvMailbox.index) && (ina219_type)) {
  234. result = Ina219CommandSensor();
  235. }
  236. break;
  237. case FUNC_INIT:
  238. Ina219Detect();
  239. break;
  240. case FUNC_EVERY_SECOND:
  241. Ina219EverySecond();
  242. break;
  243. case FUNC_JSON_APPEND:
  244. Ina219Show(1);
  245. break;
  246. #ifdef USE_WEBSERVER
  247. case FUNC_WEB_APPEND:
  248. Ina219Show(0);
  249. break;
  250. #endif // USE_WEBSERVER
  251. }
  252. }
  253. return result;
  254. }
  255. #endif // USE_INA219
  256. #endif // USE_I2C