2020-10-02 15:37:43 +01:00
|
|
|
/*
|
|
|
|
xnrg_17_ornowe512.ino - Orno WE517-Modbus energy meter support for Tasmota
|
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Maxime Vincent - based on the work of Gennaro Tortone and Theo Arends
|
2020-10-02 15:37:43 +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_WE517
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Orno WE517-Modbus energy meter
|
|
|
|
*
|
|
|
|
* [SetOption72: Set reference used for total energy]
|
|
|
|
* This driver supports SetOption72 = 1, which enables the use of Hardware Energy Totals,
|
|
|
|
* (as apposed to software energy totals kept in Tasmota flash memory)
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XNRG_17 17
|
|
|
|
|
|
|
|
// can be user defined in my_user_config.h
|
|
|
|
#ifndef WE517_SPEED
|
|
|
|
#define WE517_SPEED 9600 // default WE517 Modbus address
|
|
|
|
#endif
|
|
|
|
// can be user defined in my_user_config.h
|
|
|
|
#ifndef WE517_ADDR
|
|
|
|
#define WE517_ADDR 1 // default WE517 Modbus address
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define FUNCTION_CODE_READ_HOLDING_REGISTERS (0x03)
|
|
|
|
|
|
|
|
#include <TasmotaModbus.h>
|
|
|
|
TasmotaModbus *We517Modbus;
|
|
|
|
|
|
|
|
const uint16_t we517_start_addresses[] {
|
|
|
|
/* */ // 3P4 3P3 1P2 Unit Description
|
|
|
|
/* 0 */ 0x000E, // + - + V Phase 1 line to neutral volts
|
|
|
|
/* 1 */ 0x0010, // + - - V Phase 2 line to neutral volts
|
|
|
|
/* 2 */ 0x0012, // + - - V Phase 3 line to neutral volts
|
|
|
|
/* 3 */ 0x0016, // + + + A Phase 1 current
|
|
|
|
/* 4 */ 0x0018, // + + - A Phase 2 current
|
|
|
|
/* 5 */ 0x001A, // + + - A Phase 3 current
|
|
|
|
/* 6 */ 0x001E, // + - + kW Phase 1 power
|
|
|
|
/* 7 */ 0x0020, // + - + kW Phase 2 power
|
|
|
|
/* 8 */ 0x0022, // + - - kW Phase 3 power
|
|
|
|
/* 9 */ 0x0026, // + - + VAr Phase 1 volt amps reactive
|
2022-02-14 07:40:32 +00:00
|
|
|
/* 10 */ 0x0028, // + - - VAr Phase 2 volt amps reactive
|
2020-10-02 15:37:43 +01:00
|
|
|
/* 11 */ 0x002A, // + - - VAr Phase 3 volt amps reactive
|
|
|
|
/* 12 */ 0x0036, // + - + Phase 1 power factor
|
|
|
|
/* 13 */ 0x0038, // + - - Phase 2 power factor
|
|
|
|
/* 14 */ 0x003A, // + - - Phase 3 power factor
|
|
|
|
/* 15 */ 0x0014, // + + + Hz Frequency of supply voltages
|
|
|
|
/* 16 */ 0x0100 // + + + kWh Total active energy
|
|
|
|
};
|
|
|
|
|
|
|
|
struct WE517 {
|
|
|
|
uint8_t read_state = 0;
|
|
|
|
uint8_t send_retry = 0;
|
|
|
|
} We517;
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
void WE517Every250ms(void)
|
|
|
|
{
|
|
|
|
bool data_ready = We517Modbus->ReceiveReady();
|
|
|
|
|
|
|
|
if (data_ready) {
|
|
|
|
uint8_t buffer[14]; // At least 5 + (2 * 2) = 9
|
|
|
|
|
|
|
|
uint32_t error = We517Modbus->ReceiveBuffer(buffer, 2);
|
|
|
|
AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, We517Modbus->ReceiveCount());
|
|
|
|
|
|
|
|
if (error) {
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR("ORNO: WE517 error %d"), error);
|
2020-10-02 15:37:43 +01:00
|
|
|
} else {
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->data_valid[0] = 0;
|
|
|
|
Energy->data_valid[1] = 0;
|
|
|
|
Energy->data_valid[2] = 0;
|
2020-10-02 15:37:43 +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(We517.read_state) {
|
|
|
|
case 0:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->voltage[0] = value;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->voltage[1] = value;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->voltage[2] = value;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->current[0] = value;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->current[1] = value;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->current[2] = value;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->active_power[0] = value * 1000;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->active_power[1] = value * 1000;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->active_power[2] = value * 1000;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 9:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->reactive_power[0] = value;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 10:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->reactive_power[1] = value;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 11:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->reactive_power[2] = value;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 12:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->power_factor[0] = value;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 13:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->power_factor[1] = value;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 14:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->power_factor[2] = value;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 15:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->frequency[0] = value;
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 16:
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->import_active[0] = value;
|
2021-10-02 17:29:05 +01:00
|
|
|
EnergyUpdateTotal();
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
We517.read_state++;
|
|
|
|
if (sizeof(we517_start_addresses)/2 == We517.read_state) {
|
|
|
|
We517.read_state = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end data ready
|
|
|
|
|
|
|
|
if (0 == We517.send_retry || data_ready) {
|
|
|
|
We517.send_retry = 5;
|
|
|
|
We517Modbus->Send(WE517_ADDR, FUNCTION_CODE_READ_HOLDING_REGISTERS, we517_start_addresses[We517.read_state], 2);
|
|
|
|
} else {
|
|
|
|
We517.send_retry--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 08:50:21 +00:00
|
|
|
void We517SnsInit(void) {
|
2022-12-03 11:33:42 +00:00
|
|
|
We517Modbus = new TasmotaModbus(Pin(GPIO_WE517_RX), Pin(GPIO_WE517_TX), Pin(GPIO_NRG_MBS_TX_ENA));
|
2023-01-02 08:50:21 +00:00
|
|
|
uint8_t result = We517Modbus->Begin(WE517_SPEED, SERIAL_8E1);
|
2020-10-02 15:37:43 +01:00
|
|
|
if (result) {
|
|
|
|
if (2 == result) {
|
2023-01-02 08:50:21 +00:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("ORNO: WE517 HW serial init 8E1 at %d baud"), WE517_SPEED);
|
2023-01-09 10:04:52 +00:00
|
|
|
Serial.begin(WE517_SPEED, SERIAL_8E1);
|
2020-10-02 15:37:43 +01:00
|
|
|
ClaimSerial();
|
|
|
|
}
|
2023-01-24 15:54:03 +00:00
|
|
|
Energy->phase_count = 3;
|
|
|
|
Energy->frequency_common = true; // Use common frequency
|
2020-10-02 15:37:43 +01:00
|
|
|
} else {
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.energy_driver = ENERGY_NONE;
|
2020-10-02 15:37:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void We517DrvInit(void)
|
|
|
|
{
|
|
|
|
if (PinUsed(GPIO_WE517_RX) && PinUsed(GPIO_WE517_TX)) {
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.energy_driver = XNRG_17;
|
2020-10-02 15:37:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xnrg17(uint32_t function)
|
2020-10-02 15:37:43 +01:00
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_250_MSECOND:
|
2020-10-30 11:45:34 +00:00
|
|
|
WE517Every250ms();
|
2020-10-02 15:37:43 +01:00
|
|
|
break;
|
|
|
|
case FUNC_INIT:
|
|
|
|
We517SnsInit();
|
|
|
|
break;
|
|
|
|
case FUNC_PRE_INIT:
|
|
|
|
We517DrvInit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_WE517
|
2022-02-14 07:40:32 +00:00
|
|
|
#endif // USE_ENERGY_SENSOR
|