2021-10-23 12:54:03 +01:00
|
|
|
/*
|
|
|
|
xnrg_08_sdm230.ino - Eastron SDM230-Modbus energy meter support for Tasmota
|
|
|
|
|
|
|
|
Copyright (C) 2021 Gennaro Tortone and Theo Arends
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
* 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
|
|
|
|
|
|
|
|
// 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]
|
|
|
|
0x0018, // SDM230_POWER (Reactive) [VAr]
|
|
|
|
0x001E, // SDM230_POWER_FACTOR
|
|
|
|
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]
|
2021-11-03 21:33:59 +00:00
|
|
|
// 0X0156, // SDM230_TOTAL_ENERGY_ACTIVE [kWh] // = SDM230_IMPORT_ACTIVE = Energy.total
|
|
|
|
|
|
|
|
// 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
|
2021-10-23 12:54:03 +01:00
|
|
|
// 0X0158, // SDM230_TOTAL_ENERGY_REACTIVE [kVArh]
|
|
|
|
// 0X0182, // SDM230_RESETTABLE_TOTAL_ENERGY_REACTIVE [kVArh]
|
|
|
|
// 0X004C, // SDM230_IMPORT_REACTIVE [kVArh]
|
|
|
|
// 0X004E, // SDM230_EXPORT_REACTIVE [kVArh]
|
|
|
|
// 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 {
|
|
|
|
Energy.data_valid[0] = 0;
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
Energy.voltage[0] = value; // 230.2 V
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
Energy.current[0] = value; // 1.260 A
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
Energy.active_power[0] = value; // -196.3 W
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
Energy.apparent_power[0] = value; // 223.4 VA
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
Energy.reactive_power[0] = value; // 92.2
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
Energy.power_factor[0] = value; // -0.91
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
Energy.frequency[0] = value; // 50.0 Hz
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
2021-11-03 21:33:59 +00:00
|
|
|
Energy.import_active[0] = value; // 6.216 kWh => used in EnergyUpdateTotal()
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
2021-11-03 21:33:59 +00:00
|
|
|
Energy.export_active[0] = value; // 478.492 kWh
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
2021-11-03 21:33:59 +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:
|
2021-11-03 21:33:59 +00:00
|
|
|
Sdm230.maximum_total_demand_power_active = value;
|
2021-10-23 12:54:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 11:
|
2021-11-03 21:33:59 +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;
|
2021-11-03 21:33:59 +00:00
|
|
|
EnergyUpdateTotal(); // update every cycle after all registers have been read
|
2021-10-23 12:54:03 +01:00
|
|
|
}
|
2021-11-03 21:33:59 +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)
|
|
|
|
{
|
|
|
|
Sdm230Modbus = new TasmotaModbus(Pin(GPIO_SDM230_RX), Pin(GPIO_SDM230_TX));
|
|
|
|
uint8_t result = Sdm230Modbus->Begin(SDM230_SPEED);
|
|
|
|
if (result) {
|
|
|
|
if (2 == result) { ClaimSerial(); }
|
2021-11-06 13:02:54 +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
|
2021-10-23 12:54:03 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
const char HTTP_ENERGY_SDM230[] PROGMEM =
|
2021-11-03 21:33:59 +00:00
|
|
|
"{s}" D_PHASE_ANGLE "{m}%s " D_UNIT_ANGLE "{e}"
|
2021-10-23 12:54:03 +01:00
|
|
|
"{s}" D_MAX_POWER "{m}%s " D_UNIT_WATT "{e}"
|
2021-11-03 21:33:59 +00:00
|
|
|
"{s}" D_RESETTABLE_TOTAL_ACTIVE "{m}%s " D_UNIT_KILOWATTHOUR "{e}";
|
2021-10-23 12:54:03 +01:00
|
|
|
#endif // USE_WEBSERVER
|
2021-11-03 21:33:59 +00:00
|
|
|
#endif // SDM230_MORE_REGS
|
2021-10-23 12:54:03 +01:00
|
|
|
|
2021-11-03 21:33:59 +00:00
|
|
|
#ifdef SDM230_MORE_REGS
|
2021-10-23 12:54:03 +01:00
|
|
|
void Sdm230Show(bool json)
|
|
|
|
{
|
|
|
|
char phase_angle_chr[FLOATSZ];
|
|
|
|
dtostrfd(Sdm230.phase_angle, 2, phase_angle_chr);
|
2021-11-03 21:33:59 +00:00
|
|
|
char maximum_demand_chr[FLOATSZ];
|
|
|
|
dtostrfd(Sdm230.maximum_total_demand_power_active, Settings->flag2.wattage_resolution, maximum_demand_chr);
|
|
|
|
char resettable_energy_chr[FLOATSZ];
|
|
|
|
dtostrfd(Sdm230.resettable_total_energy, Settings->flag2.energy_resolution, resettable_energy_chr);
|
2021-10-23 12:54:03 +01:00
|
|
|
|
|
|
|
if (json) {
|
2021-11-03 21:33:59 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_PHASE_ANGLE "\":%s,\"" D_JSON_POWERMAX "\":%s,\"" D_JSON_RESETTABLE_TOTAL_ACTIVE "\":%s"),
|
|
|
|
phase_angle_chr, maximum_demand_chr, resettable_energy_chr);
|
2021-10-23 12:54:03 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2021-11-03 21:33:59 +00:00
|
|
|
WSContentSend_PD(HTTP_ENERGY_SDM230, phase_angle_chr, maximum_demand_chr, resettable_energy_chr);
|
2021-10-23 12:54:03 +01: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
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
bool Xnrg21(uint8_t function)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
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
|