2021-10-23 12:54:03 +01:00
|
|
|
/*
|
2022-01-15 14:02:45 +00:00
|
|
|
xnrg_21_sdm230.ino - Eastron SDM230-Modbus energy meter support for Tasmota
|
2021-10-23 12:54:03 +01:00
|
|
|
|
2022-01-15 14:02:45 +00:00
|
|
|
Copyright (C) 2022 dad401 and Theo Arends
|
2021-10-23 12:54:03 +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
|
|
|
|
#ifdef USE_SDM230
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Eastron SDM230 Modbus energy meter
|
|
|
|
*
|
2021-11-02 17:17:07 +00:00
|
|
|
* Based on: existing tasmota drivers for SDM120 and SDM630,
|
|
|
|
* https://github.com/reaper7/SDM_Energy_Meter and https://github.com/nmakel/sdm_modbus
|
2022-01-15 14:02:45 +00:00
|
|
|
|
2021-11-02 17:17:07 +00:00
|
|
|
* manuals can be found here:
|
|
|
|
* German: https://bg-etech.de/download/manual/SDM230-Modbus.pdf
|
|
|
|
* English: https://www.eastroneurope.com/images/uploads/products/manuals/SDM230_Sereis_Manual.pdf
|
|
|
|
* Protocol Spec: https://www.eastroneurope.com/images/uploads/products/protocol/Correct_SDM230_Protocol.pdf
|
2021-10-23 12:54:03 +01:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XNRG_21 21
|
|
|
|
|
2022-03-15 16:19:34 +00:00
|
|
|
//#define SDM230_MORE_REGS
|
|
|
|
|
2021-10-23 12:54:03 +01:00
|
|
|
// can be user defined in my_user_config.h
|
|
|
|
#ifndef SDM230_SPEED
|
2021-11-01 22:03:57 +00:00
|
|
|
#define SDM230_SPEED 9600 // default SDM230 Modbus baudrate
|
2021-10-23 12:54:03 +01:00
|
|
|
#endif
|
|
|
|
// can be user defined in my_user_config.h
|
|
|
|
#ifndef SDM230_ADDR
|
|
|
|
#define SDM230_ADDR 1 // default SDM120 Modbus address
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <TasmotaModbus.h>
|
|
|
|
TasmotaModbus *Sdm230Modbus;
|
|
|
|
|
2021-11-03 21:33:59 +00:00
|
|
|
// read all default registers for generic energy driver
|
2021-10-23 12:54:03 +01:00
|
|
|
const uint16_t sdm230_start_addresses[] {
|
|
|
|
0x0000, // SDM230_VOLTAGE [V]
|
|
|
|
0x0006, // SDM230_CURRENT [A]
|
|
|
|
0x000C, // SDM230_POWER (Active) [W]
|
|
|
|
0x0012, // SDM230_POWER (Apparent) G [VA]
|
2024-11-08 16:15:03 +00:00
|
|
|
0x0018, // SDM230_POWER (Reactive) [var]
|
2022-01-15 14:02:45 +00:00
|
|
|
0x001E, // SDM230_POWER_FACTOR
|
2021-10-23 12:54:03 +01:00
|
|
|
0x0046, // SDM230_FREQUENCY [Hz]
|
2021-11-03 21:33:59 +00:00
|
|
|
0X0048, // SDM230_IMPORT_ACTIVE [kWh]
|
2021-10-23 12:54:03 +01:00
|
|
|
0X004A, // SDM230_EXPORT_ACTIVE [kWh]
|
2023-01-24 15:54:03 +00:00
|
|
|
// 0X0156, // SDM230_TOTAL_ENERGY_ACTIVE [kWh] // = SDM230_IMPORT_ACTIVE = Energy->total
|
2021-11-03 21:33:59 +00:00
|
|
|
|
|
|
|
// read more registers offered by SDM230 (may cause timing issues)
|
|
|
|
#ifdef SDM230_MORE_REGS
|
2021-10-23 12:54:03 +01:00
|
|
|
0x0024, // SDM230_PHASE ANGLE [Degree]
|
2021-11-03 21:33:59 +00:00
|
|
|
0X0056, // SDM230_MAXIMUM_TOTAL_DEMAND_POWER_ACTIVE [W]
|
|
|
|
0X0180, // SDM230_RESETTABLE_TOTAL_ENERGY_ACTIVE [kWh]
|
|
|
|
#endif // SDM230_MORE_REGS
|
2021-10-23 12:54:03 +01:00
|
|
|
|
2021-11-03 21:33:59 +00:00
|
|
|
// for documentation / further use or implementation
|
2024-11-08 16:15:03 +00:00
|
|
|
// 0X0158, // SDM230_TOTAL_ENERGY_REACTIVE [kvarh]
|
|
|
|
// 0X0182, // SDM230_RESETTABLE_TOTAL_ENERGY_REACTIVE [kvarh]
|
|
|
|
// 0X004C, // SDM230_IMPORT_REACTIVE [kvarh]
|
|
|
|
// 0X004E, // SDM230_EXPORT_REACTIVE [kvarh]
|
2021-10-23 12:54:03 +01:00
|
|
|
// 0X0054, // SDM230_TOTAL_DEMAND_POWER_ACTIVE [W]
|
|
|
|
// 0X0058, // SDM230_IMPORT_DEMAND_POWER_ACTIVE [W]
|
|
|
|
// 0X005A, // SDM230_MAXIMUM_IMPORT_DEMAND_POWER_ACTIVE [W]
|
|
|
|
// 0X005C, // SDM230_EXPORT_DEMAND_POWER_ACTIVE [W]
|
|
|
|
// 0X005E, // SDM230_MAXIMUM_EXPORT_DEMAND_POWER_ACTIVE [W]
|
|
|
|
// 0X0102, // SDM230_TOTAL_DEMAND_CURRENT [A]
|
|
|
|
// 0X0108, // SDM230_MAXIMUM_TOTAL_DEMAND_CURRENT [A]
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SDM230 {
|
2021-11-03 21:33:59 +00:00
|
|
|
#ifdef SDM230_MORE_REGS
|
2021-10-23 12:54:03 +01:00
|
|
|
float phase_angle = 0;
|
2021-11-03 21:33:59 +00:00
|
|
|
float maximum_total_demand_power_active = 0;
|
|
|
|
float resettable_total_energy = 0;
|
|
|
|
#endif // SDM230_MORE_REGS
|
2021-10-23 12:54:03 +01:00
|
|
|
uint8_t read_state = 0;
|
|
|
|
uint8_t send_retry = 0;
|
|
|
|
} Sdm230;
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
void SDM230Every250ms(void)
|
|
|
|
{
|
|
|
|
bool data_ready = Sdm230Modbus->ReceiveReady();
|
|
|
|
|
|
|
|
if (data_ready) {
|
|
|
|
uint8_t buffer[14]; // At least 5 + (2 * 2) = 9
|
|
|
|
|
|
|
|
uint32_t error = Sdm230Modbus->ReceiveBuffer(buffer, 2);
|
|
|
|
AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, Sdm230Modbus->ReceiveCount());
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SDM: SDM230 error %d"), error);
|
|
|
|
} else {
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->data_valid[0] = 0;
|
2021-10-23 12:54:03 +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
|
|
|
|
float value;
|
|
|
|
((uint8_t*)&value)[3] = buffer[3]; // Get float values
|
|
|
|
((uint8_t*)&value)[2] = buffer[4];
|
|
|
|
((uint8_t*)&value)[1] = buffer[5];
|
|
|
|
((uint8_t*)&value)[0] = buffer[6];
|
|
|
|
|
|
|
|
switch(Sdm230.read_state) {
|
|
|
|
case 0:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->voltage[0] = value; // 230.2 V
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->current[0] = value; // 1.260 A
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->active_power[0] = value; // -196.3 W
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->apparent_power[0] = value; // 223.4 VA
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->reactive_power[0] = value; // 92.2
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->power_factor[0] = value; // -0.91
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->frequency[0] = value; // 50.0 Hz
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->import_active[0] = value; // 6.216 kWh => used in EnergyUpdateTotal()
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->export_active[0] = value; // 478.492 kWh
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
2022-01-15 14:02:45 +00:00
|
|
|
#ifdef SDM230_MORE_REGS
|
2021-10-23 12:54:03 +01:00
|
|
|
case 9:
|
2021-11-03 21:33:59 +00:00
|
|
|
Sdm230.phase_angle = value; // 0.00 Deg
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 10:
|
2022-01-15 14:02:45 +00:00
|
|
|
Sdm230.maximum_total_demand_power_active = value;
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 11:
|
2022-01-15 14:02:45 +00:00
|
|
|
Sdm230.resettable_total_energy = value;
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
2021-11-03 21:33:59 +00:00
|
|
|
#endif // SDM230_MORE_REGS
|
2021-10-23 12:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Sdm230.read_state++;
|
|
|
|
if (sizeof(sdm230_start_addresses)/2 == Sdm230.read_state) {
|
|
|
|
Sdm230.read_state = 0;
|
2022-01-15 14:02:45 +00:00
|
|
|
EnergyUpdateTotal(); // update every cycle after all registers have been read
|
2021-10-23 12:54:03 +01:00
|
|
|
}
|
2022-01-15 14:02:45 +00:00
|
|
|
|
2021-10-23 12:54:03 +01:00
|
|
|
}
|
|
|
|
} // end data ready
|
|
|
|
|
|
|
|
if (0 == Sdm230.send_retry || data_ready) {
|
|
|
|
Sdm230.send_retry = 5;
|
|
|
|
Sdm230Modbus->Send(SDM230_ADDR, 0x04, sdm230_start_addresses[Sdm230.read_state], 2);
|
|
|
|
} else {
|
|
|
|
Sdm230.send_retry--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sdm230SnsInit(void)
|
|
|
|
{
|
2022-12-03 11:33:42 +00:00
|
|
|
Sdm230Modbus = new TasmotaModbus(Pin(GPIO_SDM230_RX), Pin(GPIO_SDM230_TX), Pin(GPIO_NRG_MBS_TX_ENA));
|
2021-10-23 12:54:03 +01:00
|
|
|
uint8_t result = Sdm230Modbus->Begin(SDM230_SPEED);
|
|
|
|
if (result) {
|
|
|
|
if (2 == result) { ClaimSerial(); }
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->phase_count = 1;
|
|
|
|
Energy->frequency_common = true; // Use common frequency
|
2021-10-23 12:54:03 +01:00
|
|
|
} else {
|
|
|
|
TasmotaGlobal.energy_driver = ENERGY_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sdm230DrvInit(void)
|
|
|
|
{
|
|
|
|
if (PinUsed(GPIO_SDM230_RX) && PinUsed(GPIO_SDM230_TX)) {
|
|
|
|
TasmotaGlobal.energy_driver = XNRG_21;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sdm230Reset(void)
|
|
|
|
{
|
2021-11-03 21:33:59 +00:00
|
|
|
#ifdef SDM230_MORE_REGS
|
2021-10-23 12:54:03 +01:00
|
|
|
Sdm230.phase_angle = 0;
|
2021-11-03 21:33:59 +00:00
|
|
|
Sdm230.maximum_total_demand_power_active = 0;
|
|
|
|
Sdm230.resettable_total_energy = 0;
|
|
|
|
#endif // SDM230_MORE_REGS
|
2021-10-23 12:54:03 +01:00
|
|
|
}
|
|
|
|
|
2021-11-03 21:33:59 +00:00
|
|
|
#ifdef SDM230_MORE_REGS
|
2022-03-15 16:19:34 +00:00
|
|
|
void Sdm230Show(bool json) {
|
|
|
|
if (json) {
|
2023-03-18 17:26:43 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_PHASE_ANGLE "\":%s"), EnergyFmt(&Sdm230.phase_angle, 2));
|
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_POWERMAX "\":%s"), EnergyFmt(&Sdm230.maximum_total_demand_power_active, Settings->flag2.wattage_resolution));
|
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_RESETTABLE_TOTAL_ACTIVE "\":%s"), EnergyFmt(&Sdm230.resettable_total_energy, Settings->flag2.energy_resolution));
|
2022-03-15 16:19:34 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2023-03-18 17:26:43 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_PHASE_ANGLE, WebEnergyFmt(&Sdm230.phase_angle, 2));
|
|
|
|
WSContentSend_PD(HTTP_SNS_MAX_POWER, WebEnergyFmt(&Sdm230.maximum_total_demand_power_active, Settings->flag2.wattage_resolution));
|
|
|
|
WSContentSend_PD(HTTP_SNS_RSTTBL_TOTAL_ACTIVE, WebEnergyFmt(&Sdm230.resettable_total_energy, Settings->flag2.energy_resolution));
|
2022-03-15 16:19:34 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
2021-11-03 21:33:59 +00:00
|
|
|
#endif // SDM230_MORE_REGS
|
2021-10-23 12:54:03 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xnrg21(uint32_t function)
|
2021-10-23 12:54:03 +01:00
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_250_MSECOND:
|
|
|
|
SDM230Every250ms();
|
|
|
|
break;
|
2021-11-03 21:33:59 +00:00
|
|
|
#ifdef SDM230_MORE_REGS
|
2021-10-23 12:54:03 +01:00
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Sdm230Show(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
2022-03-15 16:19:34 +00:00
|
|
|
case FUNC_WEB_COL_SENSOR:
|
2021-10-23 12:54:03 +01:00
|
|
|
Sdm230Show(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
2021-11-03 21:33:59 +00:00
|
|
|
#endif // SDM230_MORE_REGS
|
2021-10-23 12:54:03 +01:00
|
|
|
case FUNC_ENERGY_RESET:
|
|
|
|
Sdm230Reset();
|
|
|
|
break;
|
|
|
|
case FUNC_INIT:
|
|
|
|
Sdm230SnsInit();
|
|
|
|
break;
|
|
|
|
case FUNC_PRE_INIT:
|
|
|
|
Sdm230DrvInit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_SDM230
|
|
|
|
#endif // USE_ENERGY_SENSOR
|