2019-09-01 16:51:25 +01:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xnrg_08_sdm120.ino - Eastron SDM120-Modbus energy meter support for Tasmota
|
2019-09-01 16:51:25 +01:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Gennaro Tortone and Theo Arends
|
2019-09-01 16:51:25 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_ENERGY_SENSOR
|
2019-10-18 11:28:29 +01:00
|
|
|
#ifdef USE_SDM120
|
2019-09-01 16:51:25 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Eastron SDM120 or SDM220 Modbus energy meter
|
|
|
|
*
|
|
|
|
* Based on: https://github.com/reaper7/SDM_Energy_Meter
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-09-08 15:57:56 +01:00
|
|
|
#define XNRG_08 8
|
2019-09-01 16:51:25 +01:00
|
|
|
|
|
|
|
// can be user defined in my_user_config.h
|
|
|
|
#ifndef SDM120_SPEED
|
2021-10-27 07:51:52 +01:00
|
|
|
#define SDM120_SPEED 2400 // default SDM120 Modbus baud rate
|
2019-09-01 16:51:25 +01:00
|
|
|
#endif
|
|
|
|
// can be user defined in my_user_config.h
|
|
|
|
#ifndef SDM120_ADDR
|
|
|
|
#define SDM120_ADDR 1 // default SDM120 Modbus address
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <TasmotaModbus.h>
|
|
|
|
TasmotaModbus *Sdm120Modbus;
|
|
|
|
|
2019-09-08 14:07:28 +01:00
|
|
|
const uint8_t sdm120_table = 8;
|
2019-09-08 14:14:48 +01:00
|
|
|
const uint8_t sdm220_table = 13;
|
2019-09-08 14:07:28 +01:00
|
|
|
|
2019-09-01 16:51:25 +01:00
|
|
|
const uint16_t sdm120_start_addresses[] {
|
2019-09-08 14:07:28 +01:00
|
|
|
0x0000, // SDM120C_VOLTAGE [V]
|
|
|
|
0x0006, // SDM120C_CURRENT [A]
|
|
|
|
0x000C, // SDM120C_POWER [W]
|
|
|
|
0x0012, // SDM120C_APPARENT_POWER [VA]
|
|
|
|
0x0018, // SDM120C_REACTIVE_POWER [VAR]
|
2019-09-01 16:51:25 +01:00
|
|
|
0x001E, // SDM120C_POWER_FACTOR
|
2019-09-08 14:07:28 +01:00
|
|
|
0x0046, // SDM120C_FREQUENCY [Hz]
|
|
|
|
0x0156, // SDM120C_TOTAL_ACTIVE_ENERGY [kWh]
|
|
|
|
|
|
|
|
0X0048, // SDM220_IMPORT_ACTIVE [kWh]
|
|
|
|
0X004A, // SDM220_EXPORT_ACTIVE [kWh]
|
|
|
|
0X004C, // SDM220_IMPORT_REACTIVE [kVArh]
|
|
|
|
0X004E, // SDM220_EXPORT_REACTIVE [kVArh]
|
|
|
|
0X0024 // SDM220_PHASE_ANGLE [Degree]
|
2019-09-01 16:51:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SDM120 {
|
2019-09-08 14:07:28 +01:00
|
|
|
float total_active = 0;
|
|
|
|
float import_active = NAN;
|
2019-09-01 16:51:25 +01:00
|
|
|
float import_reactive = 0;
|
|
|
|
float export_reactive = 0;
|
2019-09-08 14:07:28 +01:00
|
|
|
float phase_angle = 0;
|
|
|
|
uint8_t read_state = 0;
|
|
|
|
uint8_t send_retry = 0;
|
|
|
|
uint8_t start_address_count = sdm220_table;
|
|
|
|
} Sdm120;
|
2019-09-01 16:51:25 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2019-09-07 15:31:39 +01:00
|
|
|
void SDM120Every250ms(void)
|
2019-09-01 16:51:25 +01:00
|
|
|
{
|
|
|
|
bool data_ready = Sdm120Modbus->ReceiveReady();
|
|
|
|
|
|
|
|
if (data_ready) {
|
2019-09-10 14:14:51 +01:00
|
|
|
uint8_t buffer[14]; // At least 5 + (2 * 2) = 9
|
2019-09-01 16:51:25 +01:00
|
|
|
|
|
|
|
uint32_t error = Sdm120Modbus->ReceiveBuffer(buffer, 2);
|
2019-09-16 16:11:38 +01:00
|
|
|
AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, Sdm120Modbus->ReceiveCount());
|
2019-09-01 16:51:25 +01:00
|
|
|
|
|
|
|
if (error) {
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SDM: SDM120 error %d"), error);
|
2019-09-01 16:51:25 +01:00
|
|
|
} else {
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->data_valid[0] = 0;
|
2019-09-01 16:51:25 +01:00
|
|
|
|
2019-09-05 17:02:36 +01:00
|
|
|
// 0 1 2 3 4 5 6 7 8
|
|
|
|
// SA FC BC Fh Fl Sh Sl Cl Ch
|
|
|
|
// 01 04 04 43 66 33 34 1B 38 = 230.2 Volt
|
2019-09-01 16:51:25 +01:00
|
|
|
float value;
|
2019-09-03 20:53:20 +01:00
|
|
|
((uint8_t*)&value)[3] = buffer[3]; // Get float values
|
2019-09-01 16:51:25 +01:00
|
|
|
((uint8_t*)&value)[2] = buffer[4];
|
|
|
|
((uint8_t*)&value)[1] = buffer[5];
|
|
|
|
((uint8_t*)&value)[0] = buffer[6];
|
|
|
|
|
|
|
|
switch(Sdm120.read_state) {
|
|
|
|
case 0:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->voltage[0] = value; // 230.2 V
|
2019-09-01 16:51:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->current[0] = value; // 1.260 A
|
2019-09-01 16:51:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->active_power[0] = value; // -196.3 W
|
2019-09-01 16:51:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->apparent_power[0] = value; // 223.4 VA
|
2019-09-01 16:51:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->reactive_power[0] = value; // 92.2
|
2019-09-01 16:51:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->power_factor[0] = value; // -0.91
|
2019-09-01 16:51:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->frequency[0] = value; // 50.0 Hz
|
2019-09-01 16:51:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
2019-09-08 14:07:28 +01:00
|
|
|
Sdm120.total_active = value; // 484.708 kWh = import_active + export_active
|
2019-09-01 16:51:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
2019-09-08 14:07:28 +01:00
|
|
|
Sdm120.import_active = value; // 478.492 kWh
|
2019-09-01 16:51:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 9:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->export_active[0] = value; // 6.216 kWh
|
2019-09-01 16:51:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 10:
|
2019-09-08 14:07:28 +01:00
|
|
|
Sdm120.import_reactive = value; // 172.750 kVArh
|
2019-09-01 16:51:25 +01:00
|
|
|
break;
|
|
|
|
|
2019-09-08 14:14:48 +01:00
|
|
|
case 11:
|
2019-09-08 14:07:28 +01:00
|
|
|
Sdm120.export_reactive = value; // 2.844 kVArh
|
2019-09-01 16:51:25 +01:00
|
|
|
break;
|
|
|
|
|
2019-09-08 14:14:48 +01:00
|
|
|
case 12:
|
2019-09-08 14:07:28 +01:00
|
|
|
Sdm120.phase_angle = value; // 0.00 Deg
|
2019-09-01 16:51:25 +01:00
|
|
|
break;
|
2019-09-02 15:41:18 +01:00
|
|
|
}
|
2019-09-01 16:51:25 +01:00
|
|
|
|
|
|
|
Sdm120.read_state++;
|
2019-09-08 14:07:28 +01:00
|
|
|
if (Sdm120.read_state == Sdm120.start_address_count) {
|
2019-09-01 16:51:25 +01:00
|
|
|
Sdm120.read_state = 0;
|
2019-09-08 14:07:28 +01:00
|
|
|
|
|
|
|
if (Sdm120.start_address_count > sdm120_table) {
|
|
|
|
if (!isnan(Sdm120.import_active)) {
|
|
|
|
Sdm120.total_active = Sdm120.import_active;
|
|
|
|
} else {
|
|
|
|
Sdm120.start_address_count = sdm120_table; // No extended registers available
|
|
|
|
}
|
|
|
|
}
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->import_active[0] = Sdm120.total_active; // 484.708 kWh
|
2021-10-02 17:29:05 +01:00
|
|
|
EnergyUpdateTotal(); // 484.708 kWh
|
2019-09-01 16:51:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end data ready
|
|
|
|
|
|
|
|
if (0 == Sdm120.send_retry || data_ready) {
|
|
|
|
Sdm120.send_retry = 5;
|
|
|
|
Sdm120Modbus->Send(SDM120_ADDR, 0x04, sdm120_start_addresses[Sdm120.read_state], 2);
|
|
|
|
} else {
|
|
|
|
Sdm120.send_retry--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sdm120SnsInit(void)
|
|
|
|
{
|
2022-12-03 11:33:42 +00:00
|
|
|
Sdm120Modbus = new TasmotaModbus(Pin(GPIO_SDM120_RX), Pin(GPIO_SDM120_TX), Pin(GPIO_NRG_MBS_TX_ENA));
|
2019-09-01 16:51:25 +01:00
|
|
|
uint8_t result = Sdm120Modbus->Begin(SDM120_SPEED);
|
|
|
|
if (result) {
|
|
|
|
if (2 == result) { ClaimSerial(); }
|
|
|
|
} else {
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.energy_driver = ENERGY_NONE;
|
2019-09-01 16:51:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sdm120DrvInit(void)
|
|
|
|
{
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_SDM120_RX) && PinUsed(GPIO_SDM120_TX)) {
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.energy_driver = XNRG_08;
|
2019-09-01 16:51:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sdm220Reset(void)
|
|
|
|
{
|
2019-09-08 14:07:28 +01:00
|
|
|
if (isnan(Sdm120.import_active)) { return; }
|
|
|
|
|
|
|
|
Sdm120.import_active = 0;
|
|
|
|
Sdm120.import_reactive = 0;
|
|
|
|
Sdm120.export_reactive = 0;
|
|
|
|
Sdm120.phase_angle = 0;
|
2019-09-01 16:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
const char HTTP_ENERGY_SDM220[] PROGMEM =
|
|
|
|
"{s}" D_IMPORT_REACTIVE "{m}%s " D_UNIT_KWARH "{e}"
|
|
|
|
"{s}" D_EXPORT_REACTIVE "{m}%s " D_UNIT_KWARH "{e}"
|
2019-09-02 15:41:18 +01:00
|
|
|
"{s}" D_PHASE_ANGLE "{m}%s " D_UNIT_ANGLE "{e}";
|
2019-09-01 16:51:25 +01:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|
2022-03-15 14:43:23 +00:00
|
|
|
/*
|
|
|
|
void Sdm220Show(bool json) {
|
2019-09-08 14:07:28 +01:00
|
|
|
if (isnan(Sdm120.import_active)) { return; }
|
|
|
|
|
2019-09-01 16:51:25 +01:00
|
|
|
char import_active_chr[FLOATSZ];
|
2021-06-11 17:14:12 +01:00
|
|
|
dtostrfd(Sdm120.import_active, Settings->flag2.energy_resolution, import_active_chr);
|
2019-09-01 16:51:25 +01:00
|
|
|
char import_reactive_chr[FLOATSZ];
|
2021-06-11 17:14:12 +01:00
|
|
|
dtostrfd(Sdm120.import_reactive, Settings->flag2.energy_resolution, import_reactive_chr);
|
2019-09-01 16:51:25 +01:00
|
|
|
char export_reactive_chr[FLOATSZ];
|
2021-06-11 17:14:12 +01:00
|
|
|
dtostrfd(Sdm120.export_reactive, Settings->flag2.energy_resolution, export_reactive_chr);
|
2019-09-02 15:41:18 +01:00
|
|
|
char phase_angle_chr[FLOATSZ];
|
2019-09-08 14:07:28 +01:00
|
|
|
dtostrfd(Sdm120.phase_angle, 2, phase_angle_chr);
|
2019-09-01 16:51:25 +01:00
|
|
|
|
|
|
|
if (json) {
|
2019-09-10 11:31:08 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_IMPORT_ACTIVE "\":%s,\"" D_JSON_IMPORT_REACTIVE "\":%s,\"" D_JSON_EXPORT_REACTIVE "\":%s,\"" D_JSON_PHASE_ANGLE "\":%s"),
|
|
|
|
import_active_chr, import_reactive_chr, export_reactive_chr, phase_angle_chr);
|
2019-09-01 16:51:25 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2019-09-10 11:31:08 +01:00
|
|
|
WSContentSend_PD(HTTP_ENERGY_SDM220, import_reactive_chr, export_reactive_chr, phase_angle_chr);
|
2019-09-01 16:51:25 +01:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 14:43:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void Sdm220Show(bool json) {
|
|
|
|
if (isnan(Sdm120.import_active)) { return; }
|
|
|
|
|
2022-10-11 10:10:47 +01:00
|
|
|
char value_chr[GUISZ];
|
|
|
|
char value2_chr[GUISZ];
|
|
|
|
char value3_chr[GUISZ];
|
|
|
|
char value4_chr[GUISZ];
|
2022-03-15 14:43:23 +00:00
|
|
|
|
|
|
|
if (json) {
|
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_IMPORT_ACTIVE "\":%s,\"" D_JSON_IMPORT_REACTIVE "\":%s,\"" D_JSON_EXPORT_REACTIVE "\":%s,\"" D_JSON_PHASE_ANGLE "\":%s"),
|
|
|
|
EnergyFormat(value_chr, &Sdm120.import_active, Settings->flag2.energy_resolution),
|
|
|
|
EnergyFormat(value2_chr, &Sdm120.import_reactive, Settings->flag2.energy_resolution),
|
|
|
|
EnergyFormat(value3_chr, &Sdm120.export_reactive, Settings->flag2.energy_resolution),
|
|
|
|
EnergyFormat(value4_chr, &Sdm120.phase_angle, 2));
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
|
|
|
WSContentSend_PD(HTTP_ENERGY_SDM220, WebEnergyFormat(value_chr, &Sdm120.import_reactive, Settings->flag2.energy_resolution, 2),
|
|
|
|
WebEnergyFormat(value2_chr, &Sdm120.export_reactive, Settings->flag2.energy_resolution, 2),
|
|
|
|
WebEnergyFormat(value3_chr, &Sdm120.phase_angle, 2));
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
2019-09-01 16:51:25 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xnrg08(uint32_t function)
|
2019-09-01 16:51:25 +01:00
|
|
|
{
|
2019-09-08 15:57:56 +01:00
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_250_MSECOND:
|
2020-10-30 11:45:34 +00:00
|
|
|
SDM120Every250ms();
|
2019-09-08 15:57:56 +01:00
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Sdm220Show(1);
|
|
|
|
break;
|
2019-09-01 16:51:25 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
2022-03-15 14:43:23 +00:00
|
|
|
#ifdef USE_ENERGY_COLUMN_GUI
|
|
|
|
case FUNC_WEB_COL_SENSOR:
|
2022-03-26 15:58:44 +00:00
|
|
|
#else // not USE_ENERGY_COLUMN_GUI
|
2019-09-08 15:57:56 +01:00
|
|
|
case FUNC_WEB_SENSOR:
|
2022-03-26 15:58:44 +00:00
|
|
|
#endif // USE_ENERGY_COLUMN_GUI
|
2019-09-08 15:57:56 +01:00
|
|
|
Sdm220Show(0);
|
|
|
|
break;
|
2019-09-01 16:51:25 +01:00
|
|
|
#endif // USE_WEBSERVER
|
2019-09-08 15:57:56 +01:00
|
|
|
case FUNC_ENERGY_RESET:
|
|
|
|
Sdm220Reset();
|
|
|
|
break;
|
|
|
|
case FUNC_INIT:
|
|
|
|
Sdm120SnsInit();
|
|
|
|
break;
|
|
|
|
case FUNC_PRE_INIT:
|
|
|
|
Sdm120DrvInit();
|
|
|
|
break;
|
2019-09-01 16:51:25 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-10-18 11:28:29 +01:00
|
|
|
#endif // USE_SDM120
|
2019-09-01 16:51:25 +01:00
|
|
|
#endif // USE_ENERGY_SENSOR
|