2020-07-12 16:52:24 +01:00
|
|
|
|
/*
|
2020-07-14 16:37:14 +01:00
|
|
|
|
xnrg_16_iem3000.ino - Schneider Electric iEM3000 series Modbus energy meter support for Tasmota
|
2020-07-12 16:52:24 +01:00
|
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
|
Copyright (C) 2021 Marius Bezuidenhout
|
2020-07-12 16:52:24 +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_IEM3000
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
|
* Schneider Electric iEM3000 series Modbus energy meter
|
|
|
|
|
* iEM3150 / iEM3155 / iEM3250 / iEM3255 / iEM3350 / iEM3355 / iEM3455 / iEM3555
|
|
|
|
|
* Important! Set meter Commnication -> Parity to None
|
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
2020-07-14 16:37:14 +01:00
|
|
|
|
#define XNRG_16 16
|
2020-07-12 16:52:24 +01:00
|
|
|
|
|
|
|
|
|
// can be user defined in my_user_config.h
|
|
|
|
|
#ifndef IEM3000_SPEED
|
|
|
|
|
#define IEM3000_SPEED 19200 // default IEM3000 Modbus address
|
|
|
|
|
#endif
|
|
|
|
|
// can be user defined in my_user_config.h
|
|
|
|
|
#ifndef IEM3000_ADDR
|
|
|
|
|
#define IEM3000_ADDR 1 // default IEM3000 Modbus address
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <TasmotaModbus.h>
|
|
|
|
|
TasmotaModbus *Iem3000Modbus;
|
|
|
|
|
|
|
|
|
|
const uint16_t Iem3000_start_addresses[] {
|
|
|
|
|
// ID (reg count/datatype) [unit] Description
|
|
|
|
|
0x0bb7, // 0 . IEM3000_I1_CURRENT (2/Float32) [A] I1: phase 1 current
|
|
|
|
|
0x0bb9, // 1 . IEM3000_I2_CURRENT (2/Float32) [A] I2: phase 2 current
|
|
|
|
|
0x0bbb, // 2 . IEM3000_I3_CURRENT (2/Float32) [A] I3: phase 3 current
|
|
|
|
|
0x0bd3, // 3 . IEM3000_L1_VOLTAGE (2/Float32) [V] Voltage L1–N
|
|
|
|
|
0x0bd5, // 4 . IEM3000_L2_VOLTAGE (2/Float32) [V] Voltage L2–N
|
|
|
|
|
0x0bd7, // 5 . IEM3000_L3_VOLTAGE (2/Float32) [V] Voltage L3–N
|
|
|
|
|
0x0bed, // 6 . IEM3000_P1_POWER (2/Float32) [KW] Active Power Phase 1
|
|
|
|
|
0x0bef, // 7 . IEM3000_P2_POWER (2/Float32) [KW] Active Power Phase 2
|
|
|
|
|
0x0bf1, // 8 . IEM3000_P3_POWER (2/Float32) [KW] Active Power Phase 3
|
|
|
|
|
0x0c25, // 9 . IEM3000_FREQUENCY (2/Float32) [Hz] Frequency
|
2021-08-22 10:09:44 +01:00
|
|
|
|
#ifdef IEM3000_IEM3155
|
|
|
|
|
0xb02b, // 10 . IEM3000_TOTAL_ACTIVE (2/Float32) [Wh] Total Active Energy Import
|
|
|
|
|
#else
|
2020-07-12 16:52:24 +01:00
|
|
|
|
0xb02b, // 10 . IEM3000_TOTAL_ACTIVE (4/Int64) [Wh] Total Active Energy Import
|
2021-08-22 10:09:44 +01:00
|
|
|
|
#endif
|
2020-07-12 16:52:24 +01:00
|
|
|
|
};
|
|
|
|
|
|
2021-08-22 10:09:44 +01:00
|
|
|
|
#ifdef IEM3000_IEM3155
|
|
|
|
|
#define FLOAT_ParamLimit 11
|
|
|
|
|
#else
|
|
|
|
|
#define FLOAT_ParamLimit 10
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-07-12 16:52:24 +01:00
|
|
|
|
struct IEM3000 {
|
|
|
|
|
uint8_t read_state = 0;
|
|
|
|
|
uint8_t send_retry = 0;
|
|
|
|
|
} Iem3000;
|
|
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
|
|
void IEM3000Every250ms(void)
|
|
|
|
|
{
|
|
|
|
|
bool data_ready = Iem3000Modbus->ReceiveReady();
|
|
|
|
|
uint8_t reg_count = 4;
|
2021-08-22 10:09:44 +01:00
|
|
|
|
if (Iem3000.read_state < FLOAT_ParamLimit) {
|
2020-07-12 16:52:24 +01:00
|
|
|
|
reg_count = 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data_ready) {
|
2021-08-22 10:09:44 +01:00
|
|
|
|
uint8_t buffer[16]; // At least 5 + sizeof(int64_t) = 13
|
2020-07-12 16:52:24 +01:00
|
|
|
|
|
|
|
|
|
uint32_t error = Iem3000Modbus->ReceiveBuffer(buffer, reg_count);
|
|
|
|
|
AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, Iem3000Modbus->ReceiveCount());
|
|
|
|
|
|
|
|
|
|
if (error) {
|
2021-01-23 16:10:06 +00:00
|
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SDM: Iem3000 error %d"), error);
|
2020-07-12 16:52:24 +01:00
|
|
|
|
} 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;
|
|
|
|
|
int64_t value64;
|
2021-08-22 10:09:44 +01:00
|
|
|
|
if(Iem3000.read_state >= 0 && Iem3000.read_state < FLOAT_ParamLimit) {
|
2020-07-12 16:52:24 +01:00
|
|
|
|
((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];
|
|
|
|
|
} else {
|
|
|
|
|
((uint8_t*)&value64)[7] = buffer[3]; // Get int values
|
|
|
|
|
((uint8_t*)&value64)[6] = buffer[4];
|
|
|
|
|
((uint8_t*)&value64)[5] = buffer[5];
|
|
|
|
|
((uint8_t*)&value64)[4] = buffer[6];
|
|
|
|
|
((uint8_t*)&value64)[3] = buffer[7];
|
|
|
|
|
((uint8_t*)&value64)[2] = buffer[8];
|
|
|
|
|
((uint8_t*)&value64)[1] = buffer[9];
|
|
|
|
|
((uint8_t*)&value64)[0] = buffer[10];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(Iem3000.read_state) {
|
|
|
|
|
case 0:
|
|
|
|
|
Energy.current[0] = value;
|
|
|
|
|
break;
|
2020-07-14 16:37:14 +01:00
|
|
|
|
|
2020-07-12 16:52:24 +01:00
|
|
|
|
case 1:
|
|
|
|
|
Energy.current[1] = value;
|
|
|
|
|
break;
|
2020-07-14 16:37:14 +01:00
|
|
|
|
|
2020-07-12 16:52:24 +01:00
|
|
|
|
case 2:
|
|
|
|
|
Energy.current[2] = value;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
|
Energy.voltage[0] = value;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
|
Energy.voltage[1] = value;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
|
Energy.voltage[2] = value;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 6:
|
2021-08-22 10:09:44 +01:00
|
|
|
|
#ifdef IEM3000_IEM3155
|
|
|
|
|
Energy.active_power[0] = value*1000;
|
|
|
|
|
#else
|
2020-07-12 16:52:24 +01:00
|
|
|
|
Energy.active_power[0] = value;
|
2021-08-22 10:09:44 +01:00
|
|
|
|
#endif
|
2020-07-12 16:52:24 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 7:
|
2021-08-22 10:09:44 +01:00
|
|
|
|
#ifdef IEM3000_IEM3155
|
|
|
|
|
Energy.active_power[1] = value*1000;
|
|
|
|
|
#else
|
2020-07-12 16:52:24 +01:00
|
|
|
|
Energy.active_power[1] = value;
|
2021-08-22 10:09:44 +01:00
|
|
|
|
#endif
|
2020-07-12 16:52:24 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 8:
|
2021-08-22 10:09:44 +01:00
|
|
|
|
#ifdef IEM3000_IEM3155
|
|
|
|
|
Energy.active_power[2] = value*1000;
|
|
|
|
|
#else
|
2020-07-12 16:52:24 +01:00
|
|
|
|
Energy.active_power[2] = value;
|
2021-08-22 10:09:44 +01:00
|
|
|
|
#endif
|
2020-07-12 16:52:24 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 9:
|
|
|
|
|
Energy.frequency[0] = value;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 10:
|
2021-08-22 10:09:44 +01:00
|
|
|
|
#ifdef IEM3000_IEM3155
|
2021-10-02 17:29:05 +01:00
|
|
|
|
Energy.import_active[0] = value;
|
2021-08-22 10:09:44 +01:00
|
|
|
|
#else
|
2021-10-02 17:29:05 +01:00
|
|
|
|
Energy.import_active[0] = value64 * 0.001f; // 1125 => 1.125
|
2021-08-22 10:09:44 +01:00
|
|
|
|
#endif
|
2021-10-02 17:29:05 +01:00
|
|
|
|
EnergyUpdateTotal();
|
2020-07-12 16:52:24 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Iem3000.read_state++;
|
|
|
|
|
if (sizeof(Iem3000_start_addresses)/2 == Iem3000.read_state) {
|
|
|
|
|
Iem3000.read_state = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} // end data ready
|
|
|
|
|
|
|
|
|
|
if (0 == Iem3000.send_retry || data_ready) {
|
|
|
|
|
Iem3000.send_retry = 5;
|
|
|
|
|
Iem3000Modbus->Send(IEM3000_ADDR, 0x03, Iem3000_start_addresses[Iem3000.read_state], reg_count);
|
|
|
|
|
} else {
|
|
|
|
|
Iem3000.send_retry--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Iem3000SnsInit(void)
|
|
|
|
|
{
|
|
|
|
|
Iem3000Modbus = new TasmotaModbus(Pin(GPIO_IEM3000_RX), Pin(GPIO_IEM3000_TX));
|
|
|
|
|
uint8_t result = Iem3000Modbus->Begin(IEM3000_SPEED);
|
|
|
|
|
if (result) {
|
|
|
|
|
if (2 == result) { ClaimSerial(); }
|
|
|
|
|
Energy.phase_count = 3;
|
|
|
|
|
Energy.frequency_common = true; // Use common frequency
|
|
|
|
|
} else {
|
2020-10-30 11:29:48 +00:00
|
|
|
|
TasmotaGlobal.energy_driver = ENERGY_NONE;
|
2020-07-12 16:52:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Iem3000DrvInit(void)
|
|
|
|
|
{
|
|
|
|
|
if (PinUsed(GPIO_IEM3000_RX) && PinUsed(GPIO_IEM3000_TX)) {
|
2020-10-30 11:29:48 +00:00
|
|
|
|
TasmotaGlobal.energy_driver = XNRG_16;
|
2020-07-12 16:52:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
|
* Interface
|
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
2020-07-14 16:37:14 +01:00
|
|
|
|
bool Xnrg16(uint8_t function)
|
2020-07-12 16:52:24 +01:00
|
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
|
|
switch (function) {
|
|
|
|
|
case FUNC_EVERY_250_MSECOND:
|
2020-10-30 11:45:34 +00:00
|
|
|
|
IEM3000Every250ms();
|
2020-07-12 16:52:24 +01:00
|
|
|
|
break;
|
|
|
|
|
case FUNC_INIT:
|
|
|
|
|
Iem3000SnsInit();
|
|
|
|
|
break;
|
|
|
|
|
case FUNC_PRE_INIT:
|
|
|
|
|
Iem3000DrvInit();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // USE_IEM3000
|
|
|
|
|
#endif // USE_ENERGY_SENSOR
|