xsns_23_sdm120.ino 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. xsns_23_sdm120.ino - Eastron SDM120-Modbus energy meter support for Sonoff-Tasmota
  3. Copyright (C) 2018 Gennaro Tortone
  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_SDM120
  16. /*********************************************************************************************\
  17. * Eastron SDM120-Modbus energy meter
  18. *
  19. * Based on: https://github.com/reaper7/SDM_Energy_Meter
  20. \*********************************************************************************************/
  21. #define XSNS_23 23
  22. #include <TasmotaSerial.h>
  23. TasmotaSerial *SDM120Serial;
  24. uint8_t sdm120_type = 1;
  25. //uint8_t sdm120_state = 0;
  26. float sdm120_voltage = 0;
  27. float sdm120_current = 0;
  28. float sdm120_active_power = 0;
  29. float sdm120_apparent_power = 0;
  30. float sdm120_reactive_power = 0;
  31. float sdm120_power_factor = 0;
  32. float sdm120_frequency = 0;
  33. float sdm120_energy_total = 0;
  34. float sdm120_phase_angle = 0;
  35. float sdm120_import_active = 0;
  36. float sdm120_export_active = 0;
  37. float sdm120_import_reactive = 0;
  38. float sdm120_export_reactive = 0;
  39. float sdm120_total_reactive = 0;
  40. bool SDM120_ModbusReceiveReady(void)
  41. {
  42. return (SDM120Serial->available() > 1);
  43. }
  44. void SDM120_ModbusSend(uint8_t function_code, uint16_t start_address, uint16_t register_count)
  45. {
  46. uint8_t frame[8];
  47. frame[0] = 0x01; // default SDM120 Modbus address
  48. frame[1] = function_code;
  49. frame[2] = (uint8_t)(start_address >> 8);
  50. frame[3] = (uint8_t)(start_address);
  51. frame[4] = (uint8_t)(register_count >> 8);
  52. frame[5] = (uint8_t)(register_count);
  53. uint16_t crc = SDM120_calculateCRC(frame, 6); // calculate out crc only from first 6 bytes
  54. frame[6] = lowByte(crc);
  55. frame[7] = highByte(crc);
  56. while (SDM120Serial->available() > 0) { // read serial if any old data is available
  57. SDM120Serial->read();
  58. }
  59. SDM120Serial->flush();
  60. SDM120Serial->write(frame, sizeof(frame));
  61. }
  62. uint8_t SDM120_ModbusReceive(float *value)
  63. {
  64. uint8_t buffer[9];
  65. *value = NAN;
  66. uint8_t len = 0;
  67. while (SDM120Serial->available() > 0) {
  68. buffer[len++] = (uint8_t)SDM120Serial->read();
  69. }
  70. if (len < 9)
  71. return 3; // SDM_ERR_NOT_ENOUGHT_BYTES
  72. if (len == 9) {
  73. if (buffer[0] == 0x01 && buffer[1] == 0x04 && buffer[2] == 4) { // check node number, op code and reply bytes count
  74. if((SDM120_calculateCRC(buffer, 7)) == ((buffer[8] << 8) | buffer[7])) { //calculate crc from first 7 bytes and compare with received crc (bytes 7 & 8)
  75. ((uint8_t*)value)[3] = buffer[3];
  76. ((uint8_t*)value)[2] = buffer[4];
  77. ((uint8_t*)value)[1] = buffer[5];
  78. ((uint8_t*)value)[0] = buffer[6];
  79. } else return 1; // SDM_ERR_CRC_ERROR
  80. } else return 2; // SDM_ERR_WRONG_BYTES
  81. }
  82. return 0; // SDM_ERR_NO_ERROR
  83. }
  84. uint16_t SDM120_calculateCRC(uint8_t *frame, uint8_t num)
  85. {
  86. uint16_t crc, flag;
  87. crc = 0xFFFF;
  88. for (uint8_t i = 0; i < num; i++) {
  89. crc ^= frame[i];
  90. for (uint8_t j = 8; j; j--) {
  91. if ((crc & 0x0001) != 0) { // If the LSB is set
  92. crc >>= 1; // Shift right and XOR 0xA001
  93. crc ^= 0xA001;
  94. } else { // Else LSB is not set
  95. crc >>= 1; // Just shift right
  96. }
  97. }
  98. }
  99. return crc;
  100. }
  101. /*********************************************************************************************/
  102. const uint16_t sdm120_start_addresses[] {
  103. 0x0000, // SDM120C_VOLTAGE [V]
  104. 0x0006, // SDM120C_CURRENT [A]
  105. 0x000C, // SDM120C_POWER [W]
  106. 0x0012, // SDM120C_APPARENT_POWER [VA]
  107. 0x0018, // SDM120C_REACTIVE_POWER [VAR]
  108. 0x001E, // SDM120C_POWER_FACTOR
  109. 0x0046, // SDM120C_FREQUENCY [Hz]
  110. #ifdef USE_SDM220
  111. 0x0156, // SDM120C_TOTAL_ACTIVE_ENERGY [Wh]
  112. 0X0024, // SDM220_PHASE_ANGLE [Degre]
  113. 0X0048, // SDM220_IMPORT_ACTIVE [kWh]
  114. 0X004A, // SDM220_EXPORT_ACTIVE [kWh]
  115. 0X004C, // SDM220_IMPORT_REACTIVE [kVArh]
  116. 0X004E, // SDM220_EXPORT_REACTIVE [kVArh]
  117. 0X0158 // SDM220 TOTAL_REACTIVE [kVArh]
  118. #else // USE_SDM220
  119. 0x0156 // SDM120C_TOTAL_ACTIVE_ENERGY [Wh]
  120. #endif // USE_SDM220
  121. };
  122. uint8_t sdm120_read_state = 0;
  123. uint8_t sdm120_send_retry = 0;
  124. void SDM120250ms(void) // Every 250 mSec
  125. {
  126. // sdm120_state++;
  127. // if (6 == sdm120_state) { // Every 300 mSec
  128. // sdm120_state = 0;
  129. float value = 0;
  130. bool data_ready = SDM120_ModbusReceiveReady();
  131. if (data_ready) {
  132. uint8_t error = SDM120_ModbusReceive(&value);
  133. if (error) {
  134. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "SDM120 response error %d"), error);
  135. AddLog(LOG_LEVEL_DEBUG);
  136. } else {
  137. switch(sdm120_read_state) {
  138. case 0:
  139. sdm120_voltage = value;
  140. break;
  141. case 1:
  142. sdm120_current = value;
  143. break;
  144. case 2:
  145. sdm120_active_power = value;
  146. break;
  147. case 3:
  148. sdm120_apparent_power = value;
  149. break;
  150. case 4:
  151. sdm120_reactive_power = value;
  152. break;
  153. case 5:
  154. sdm120_power_factor = value;
  155. break;
  156. case 6:
  157. sdm120_frequency = value;
  158. break;
  159. case 7:
  160. sdm120_energy_total = value;
  161. break;
  162. #ifdef USE_SDM220
  163. case 8:
  164. sdm120_phase_angle = value;
  165. break;
  166. case 9:
  167. sdm120_import_active = value;
  168. break;
  169. case 10:
  170. sdm120_export_active = value;
  171. break;
  172. case 11:
  173. sdm120_import_reactive = value;
  174. break;
  175. case 12:
  176. sdm120_export_reactive = value;
  177. break;
  178. case 13:
  179. sdm120_total_reactive = value;
  180. break;
  181. #endif // USE_SDM220
  182. } // end switch
  183. sdm120_read_state++;
  184. if (sizeof(sdm120_start_addresses)/2 == sdm120_read_state) {
  185. sdm120_read_state = 0;
  186. }
  187. }
  188. } // end data ready
  189. if (0 == sdm120_send_retry || data_ready) {
  190. sdm120_send_retry = 5;
  191. SDM120_ModbusSend(0x04, sdm120_start_addresses[sdm120_read_state], 2);
  192. } else {
  193. sdm120_send_retry--;
  194. }
  195. // } // end 300 ms
  196. }
  197. void SDM120Init(void)
  198. {
  199. sdm120_type = 0;
  200. if ((pin[GPIO_SDM120_RX] < 99) && (pin[GPIO_SDM120_TX] < 99)) {
  201. SDM120Serial = new TasmotaSerial(pin[GPIO_SDM120_RX], pin[GPIO_SDM120_TX], 1);
  202. #ifdef SDM120_SPEED
  203. if (SDM120Serial->begin(SDM120_SPEED)) {
  204. #else
  205. if (SDM120Serial->begin(2400)) {
  206. #endif
  207. if (SDM120Serial->hardwareSerial()) { ClaimSerial(); }
  208. sdm120_type = 1;
  209. }
  210. }
  211. }
  212. #ifdef USE_WEBSERVER
  213. const char HTTP_SNS_SDM120_DATA[] PROGMEM = "%s"
  214. "{s}SDM120 " D_VOLTAGE "{m}%s " D_UNIT_VOLT "{e}"
  215. "{s}SDM120 " D_CURRENT "{m}%s " D_UNIT_AMPERE "{e}"
  216. "{s}SDM120 " D_POWERUSAGE_ACTIVE "{m}%s " D_UNIT_WATT "{e}"
  217. "{s}SDM120 " D_POWERUSAGE_APPARENT "{m}%s " D_UNIT_VA "{e}"
  218. "{s}SDM120 " D_POWERUSAGE_REACTIVE "{m}%s " D_UNIT_VAR "{e}"
  219. "{s}SDM120 " D_POWER_FACTOR "{m}%s{e}"
  220. "{s}SDM120 " D_FREQUENCY "{m}%s " D_UNIT_HERTZ "{e}"
  221. "{s}SDM120 " D_ENERGY_TOTAL "{m}%s " D_UNIT_KILOWATTHOUR "{e}"
  222. #ifdef USE_SDM220
  223. "{s}SDM120 " D_PHASE_ANGLE "{m}%s " D_UNIT_ANGLE "{e}"
  224. "{s}SDM120 " D_IMPORT_ACTIVE "{m}%s " D_UNIT_KILOWATTHOUR "{e}"
  225. "{s}SDM120 " D_EXPORT_ACTIVE "{m}%s " D_UNIT_KILOWATTHOUR "{e}"
  226. "{s}SDM120 " D_IMPORT_REACTIVE "{m}%s " D_UNIT_KWARH "{e}"
  227. "{s}SDM120 " D_EXPORT_REACTIVE "{m}%s " D_UNIT_KWARH "{e}"
  228. "{s}SDM120 " D_TOTAL_REACTIVE "{m}%s " D_UNIT_KWARH "{e}"
  229. #endif // USE_SDM220
  230. ;
  231. #endif // USE_WEBSERVER
  232. void SDM120Show(boolean json)
  233. {
  234. char voltage[33];
  235. dtostrfd(sdm120_voltage, Settings.flag2.voltage_resolution, voltage);
  236. char current[33];
  237. dtostrfd(sdm120_current, Settings.flag2.current_resolution, current);
  238. char active_power[33];
  239. dtostrfd(sdm120_active_power, Settings.flag2.wattage_resolution, active_power);
  240. char apparent_power[33];
  241. dtostrfd(sdm120_apparent_power, Settings.flag2.wattage_resolution, apparent_power);
  242. char reactive_power[33];
  243. dtostrfd(sdm120_reactive_power, Settings.flag2.wattage_resolution, reactive_power);
  244. char power_factor[33];
  245. dtostrfd(sdm120_power_factor, 2, power_factor);
  246. char frequency[33];
  247. dtostrfd(sdm120_frequency, Settings.flag2.frequency_resolution, frequency);
  248. char energy_total[33];
  249. dtostrfd(sdm120_energy_total, Settings.flag2.energy_resolution, energy_total);
  250. #ifdef USE_SDM220
  251. char phase_angle[33];
  252. dtostrfd(sdm120_phase_angle, 2, phase_angle);
  253. char import_active[33];
  254. dtostrfd(sdm120_import_active, Settings.flag2.wattage_resolution, import_active);
  255. char export_active[33];
  256. dtostrfd(sdm120_export_active, Settings.flag2.wattage_resolution, export_active);
  257. char import_reactive[33];
  258. dtostrfd(sdm120_import_reactive,Settings.flag2.wattage_resolution, import_reactive);
  259. char export_reactive[33];
  260. dtostrfd(sdm120_export_reactive,Settings.flag2.wattage_resolution, export_reactive);
  261. char total_reactive[33];
  262. dtostrfd(sdm120_total_reactive, Settings.flag2.wattage_resolution, total_reactive);
  263. #endif // USE_SDM220
  264. if (json) {
  265. #ifdef USE_SDM220
  266. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_RSLT_ENERGY "\":{\"" D_JSON_TOTAL "\":%s,\"" D_JSON_ACTIVE_POWERUSAGE "\":%s,\"" D_JSON_APPARENT_POWERUSAGE "\":%s,\"" D_JSON_REACTIVE_POWERUSAGE "\":%s,\"" D_JSON_FREQUENCY "\":%s,\"" D_JSON_POWERFACTOR "\":%s,\"" D_JSON_VOLTAGE "\":%s,\"" D_JSON_CURRENT "\":%s,\"" D_JSON_PHASE_ANGLE "\":%s,\"" D_JSON_IMPORT_ACTIVE "\":%s,\"" D_JSON_EXPORT_ACTIVE "\":%s,\"" D_JSON_IMPORT_REACTIVE "\":%s,\"" D_JSON_EXPORT_REACTIVE "\":%s,\"" D_JSON_TOTAL_REACTIVE "\":%s}"),
  267. mqtt_data, energy_total, active_power, apparent_power, reactive_power, frequency, power_factor, voltage, current, phase_angle, import_active, export_active, import_reactive, export_reactive, total_reactive);
  268. #else
  269. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_RSLT_ENERGY "\":{\"" D_JSON_TOTAL "\":%s,\"" D_JSON_ACTIVE_POWERUSAGE "\":%s,\"" D_JSON_APPARENT_POWERUSAGE "\":%s,\"" D_JSON_REACTIVE_POWERUSAGE "\":%s,\"" D_JSON_FREQUENCY "\":%s,\"" D_JSON_POWERFACTOR "\":%s,\"" D_JSON_VOLTAGE "\":%s,\"" D_JSON_CURRENT "\":%s}"),
  270. mqtt_data, energy_total, active_power, apparent_power, reactive_power, frequency, power_factor, voltage, current);
  271. #endif // USE_SDM220
  272. #ifdef USE_DOMOTICZ
  273. if (0 == tele_period) {
  274. DomoticzSensor(DZ_VOLTAGE, voltage);
  275. DomoticzSensor(DZ_CURRENT, current);
  276. DomoticzSensorPowerEnergy((int)sdm120_active_power, energy_total);
  277. }
  278. #endif // USE_DOMOTICZ
  279. #ifdef USE_WEBSERVER
  280. } else {
  281. #ifdef USE_SDM220
  282. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_SDM120_DATA, mqtt_data, voltage, current, active_power, apparent_power, reactive_power, power_factor, frequency, energy_total, phase_angle,import_active,export_active,import_reactive,export_reactive,total_reactive);
  283. #else
  284. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_SDM120_DATA, mqtt_data, voltage, current, active_power, apparent_power, reactive_power, power_factor, frequency, energy_total);
  285. #endif // USE_SDM220
  286. #endif // USE_WEBSERVER
  287. }
  288. }
  289. /*********************************************************************************************\
  290. * Interface
  291. \*********************************************************************************************/
  292. boolean Xsns23(byte function)
  293. {
  294. boolean result = false;
  295. if (sdm120_type) {
  296. switch (function) {
  297. case FUNC_INIT:
  298. SDM120Init();
  299. break;
  300. case FUNC_EVERY_250_MSECOND:
  301. SDM120250ms();
  302. break;
  303. case FUNC_JSON_APPEND:
  304. SDM120Show(1);
  305. break;
  306. #ifdef USE_WEBSERVER
  307. case FUNC_WEB_APPEND:
  308. SDM120Show(0);
  309. break;
  310. #endif // USE_WEBSERVER
  311. }
  312. }
  313. return result;
  314. }
  315. #endif // USE_SDM120