mirror of https://github.com/arendst/Tasmota.git
Remove obsolete SDM120 and SDM630 drivers
This commit is contained in:
parent
5ab85a9825
commit
b361d0cc46
|
@ -1,397 +0,0 @@
|
|||
/*
|
||||
xsns_23_sdm120.ino - Eastron SDM120-Modbus energy meter support for Sonoff-Tasmota
|
||||
|
||||
Copyright (C) 2019 Gennaro Tortone
|
||||
|
||||
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_SDM120
|
||||
|
||||
/*********************************************************************************************\
|
||||
* Eastron SDM120-Modbus energy meter
|
||||
*
|
||||
* Based on: https://github.com/reaper7/SDM_Energy_Meter
|
||||
\*********************************************************************************************/
|
||||
|
||||
#define XSNS_23 23
|
||||
|
||||
// can be user defined in my_user_config.h
|
||||
#ifndef SDM120_SPEED
|
||||
#define SDM120_SPEED 2400 // default SDM120 Modbus address
|
||||
#endif
|
||||
// can be user defined in my_user_config.h
|
||||
#ifndef SDM120_ADDR
|
||||
#define SDM120_ADDR 1 // default SDM120 Modbus address
|
||||
#endif
|
||||
|
||||
|
||||
#include <TasmotaSerial.h>
|
||||
|
||||
enum SDM120_Error {SDM120_ERR_NO_ERROR=0, SDM120_ERR_CRC_ERROR, SDM120_ERR_WRONG_BYTES, SDM120_ERR_NOT_ENOUGHT_BYTES};
|
||||
|
||||
TasmotaSerial *SDM120Serial;
|
||||
|
||||
uint8_t sdm120_type = 1;
|
||||
//uint8_t sdm120_state = 0;
|
||||
|
||||
float sdm120_voltage = 0;
|
||||
float sdm120_current = 0;
|
||||
float sdm120_active_power = 0;
|
||||
float sdm120_apparent_power = 0;
|
||||
float sdm120_reactive_power = 0;
|
||||
float sdm120_power_factor = 0;
|
||||
float sdm120_frequency = 0;
|
||||
float sdm120_energy_total = 0;
|
||||
float sdm120_phase_angle = 0;
|
||||
float sdm120_import_active = 0;
|
||||
float sdm120_export_active = 0;
|
||||
float sdm120_import_reactive = 0;
|
||||
float sdm120_export_reactive = 0;
|
||||
float sdm120_total_reactive = 0;
|
||||
|
||||
bool SDM120_ModbusReceiveReady(void)
|
||||
{
|
||||
return (SDM120Serial->available() > 1);
|
||||
}
|
||||
|
||||
void SDM120_ModbusSend(uint8_t function_code, uint16_t start_address, uint16_t register_count)
|
||||
{
|
||||
uint8_t frame[8];
|
||||
|
||||
frame[0] = SDM120_ADDR;
|
||||
frame[1] = function_code;
|
||||
frame[2] = (uint8_t)(start_address >> 8);
|
||||
frame[3] = (uint8_t)(start_address);
|
||||
frame[4] = (uint8_t)(register_count >> 8);
|
||||
frame[5] = (uint8_t)(register_count);
|
||||
|
||||
uint16_t crc = SDM120_calculateCRC(frame, 6); // calculate out crc only from first 6 bytes
|
||||
frame[6] = lowByte(crc);
|
||||
frame[7] = highByte(crc);
|
||||
|
||||
while (SDM120Serial->available() > 0) { // read serial if any old data is available
|
||||
SDM120Serial->read();
|
||||
}
|
||||
|
||||
SDM120Serial->flush();
|
||||
SDM120Serial->write(frame, sizeof(frame));
|
||||
}
|
||||
|
||||
uint8_t SDM120_ModbusReceive(float *value)
|
||||
{
|
||||
uint8_t buffer[9];
|
||||
|
||||
*value = NAN;
|
||||
uint8_t len = 0;
|
||||
while (SDM120Serial->available() > 0) {
|
||||
buffer[len++] = (uint8_t)SDM120Serial->read();
|
||||
}
|
||||
|
||||
if (len < 9) {
|
||||
return SDM120_ERR_NOT_ENOUGHT_BYTES;
|
||||
}
|
||||
|
||||
if (9 == len) {
|
||||
if (0x01 == buffer[0] && 0x04 == buffer[1] && 4 == buffer[2]) { // check node number, op code and reply bytes count
|
||||
if((SDM120_calculateCRC(buffer, 7)) == ((buffer[8] << 8) | buffer[7])) { //calculate crc from first 7 bytes and compare with received crc (bytes 7 & 8)
|
||||
|
||||
((uint8_t*)value)[3] = buffer[3];
|
||||
((uint8_t*)value)[2] = buffer[4];
|
||||
((uint8_t*)value)[1] = buffer[5];
|
||||
((uint8_t*)value)[0] = buffer[6];
|
||||
|
||||
} else {
|
||||
return SDM120_ERR_CRC_ERROR;
|
||||
}
|
||||
|
||||
} else {
|
||||
return SDM120_ERR_WRONG_BYTES;
|
||||
}
|
||||
}
|
||||
|
||||
return SDM120_ERR_NO_ERROR;
|
||||
}
|
||||
|
||||
uint16_t SDM120_calculateCRC(uint8_t *frame, uint8_t num)
|
||||
{
|
||||
uint16_t crc, flag;
|
||||
crc = 0xFFFF;
|
||||
for (uint32_t i = 0; i < num; i++) {
|
||||
crc ^= frame[i];
|
||||
for (uint32_t j = 8; j; j--) {
|
||||
if ((crc & 0x0001) != 0) { // If the LSB is set
|
||||
crc >>= 1; // Shift right and XOR 0xA001
|
||||
crc ^= 0xA001;
|
||||
} else { // Else LSB is not set
|
||||
crc >>= 1; // Just shift right
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
/*********************************************************************************************/
|
||||
|
||||
const uint16_t sdm120_start_addresses[] {
|
||||
0x0000, // SDM120C_VOLTAGE [V]
|
||||
0x0006, // SDM120C_CURRENT [A]
|
||||
0x000C, // SDM120C_POWER [W]
|
||||
0x0012, // SDM120C_APPARENT_POWER [VA]
|
||||
0x0018, // SDM120C_REACTIVE_POWER [VAR]
|
||||
0x001E, // SDM120C_POWER_FACTOR
|
||||
0x0046, // SDM120C_FREQUENCY [Hz]
|
||||
#ifdef USE_SDM220
|
||||
0x0156, // SDM120C_TOTAL_ACTIVE_ENERGY [Wh]
|
||||
0X0024, // SDM220_PHASE_ANGLE [Degre]
|
||||
0X0048, // SDM220_IMPORT_ACTIVE [kWh]
|
||||
0X004A, // SDM220_EXPORT_ACTIVE [kWh]
|
||||
0X004C, // SDM220_IMPORT_REACTIVE [kVArh]
|
||||
0X004E, // SDM220_EXPORT_REACTIVE [kVArh]
|
||||
0X0158 // SDM220 TOTAL_REACTIVE [kVArh]
|
||||
#else // USE_SDM220
|
||||
0x0156 // SDM120C_TOTAL_ACTIVE_ENERGY [Wh]
|
||||
#endif // USE_SDM220
|
||||
};
|
||||
|
||||
uint8_t sdm120_read_state = 0;
|
||||
uint8_t sdm120_send_retry = 0;
|
||||
uint8_t sdm120_nodata_count = 0;
|
||||
|
||||
void SDM120250ms(void) // Every 250 mSec
|
||||
{
|
||||
// sdm120_state++;
|
||||
// if (6 == sdm120_state) { // Every 300 mSec
|
||||
// sdm120_state = 0;
|
||||
|
||||
float value = 0;
|
||||
bool data_ready = SDM120_ModbusReceiveReady();
|
||||
|
||||
if (data_ready) {
|
||||
sdm120_nodata_count = 0;
|
||||
uint8_t error = SDM120_ModbusReceive(&value);
|
||||
if (error) {
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "SDM120 response error %d"), error);
|
||||
} else {
|
||||
switch(sdm120_read_state) {
|
||||
case 0:
|
||||
sdm120_voltage = value;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
sdm120_current = value;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
sdm120_active_power = value;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
sdm120_apparent_power = value;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
sdm120_reactive_power = value;
|
||||
break;
|
||||
|
||||
case 5:
|
||||
sdm120_power_factor = value;
|
||||
break;
|
||||
|
||||
case 6:
|
||||
sdm120_frequency = value;
|
||||
break;
|
||||
|
||||
case 7:
|
||||
sdm120_energy_total = value;
|
||||
break;
|
||||
#ifdef USE_SDM220
|
||||
case 8:
|
||||
sdm120_phase_angle = value;
|
||||
break;
|
||||
|
||||
case 9:
|
||||
sdm120_import_active = value;
|
||||
break;
|
||||
|
||||
case 10:
|
||||
sdm120_export_active = value;
|
||||
break;
|
||||
|
||||
case 11:
|
||||
sdm120_import_reactive = value;
|
||||
break;
|
||||
|
||||
case 12:
|
||||
sdm120_export_reactive = value;
|
||||
break;
|
||||
|
||||
case 13:
|
||||
sdm120_total_reactive = value;
|
||||
break;
|
||||
#endif // USE_SDM220
|
||||
} // end switch
|
||||
|
||||
sdm120_read_state++;
|
||||
|
||||
if (sizeof(sdm120_start_addresses)/2 == sdm120_read_state) {
|
||||
sdm120_read_state = 0;
|
||||
}
|
||||
}
|
||||
} // end data ready
|
||||
else {
|
||||
if (sdm120_nodata_count <= (1000/250) * 4) { // max. 4 sec without data
|
||||
sdm120_nodata_count++;
|
||||
} else if (sdm120_nodata_count != 255) {
|
||||
// no data from modbus, reset values to 0
|
||||
sdm120_nodata_count = 255;
|
||||
sdm120_voltage = sdm120_current = sdm120_active_power = sdm120_apparent_power = sdm120_reactive_power = sdm120_power_factor = sdm120_frequency = sdm120_energy_total = 0;
|
||||
#ifdef USE_SDM220
|
||||
sdm120_phase_angle = sdm120_import_active = sdm120_export_active = sdm120_import_reactive = sdm120_export_reactive = sdm120_total_reactive = 0;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (0 == sdm120_send_retry || data_ready) {
|
||||
sdm120_send_retry = 5;
|
||||
SDM120_ModbusSend(0x04, sdm120_start_addresses[sdm120_read_state], 2);
|
||||
} else {
|
||||
sdm120_send_retry--;
|
||||
}
|
||||
// } // end 300 ms
|
||||
}
|
||||
|
||||
void SDM120Init(void)
|
||||
{
|
||||
sdm120_type = 0;
|
||||
if ((pin[GPIO_SDM120_RX] < 99) && (pin[GPIO_SDM120_TX] < 99)) {
|
||||
SDM120Serial = new TasmotaSerial(pin[GPIO_SDM120_RX], pin[GPIO_SDM120_TX], 1);
|
||||
if (SDM120Serial->begin(SDM120_SPEED)) {
|
||||
if (SDM120Serial->hardwareSerial()) { ClaimSerial(); }
|
||||
sdm120_type = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
const char HTTP_SNS_SDM120_DATA[] PROGMEM =
|
||||
"{s}SDM120 " D_VOLTAGE "{m}%s " D_UNIT_VOLT "{e}"
|
||||
"{s}SDM120 " D_CURRENT "{m}%s " D_UNIT_AMPERE "{e}"
|
||||
"{s}SDM120 " D_POWERUSAGE_ACTIVE "{m}%s " D_UNIT_WATT "{e}"
|
||||
"{s}SDM120 " D_POWERUSAGE_APPARENT "{m}%s " D_UNIT_VA "{e}"
|
||||
"{s}SDM120 " D_POWERUSAGE_REACTIVE "{m}%s " D_UNIT_VAR "{e}"
|
||||
"{s}SDM120 " D_POWER_FACTOR "{m}%s{e}"
|
||||
"{s}SDM120 " D_FREQUENCY "{m}%s " D_UNIT_HERTZ "{e}"
|
||||
"{s}SDM120 " D_ENERGY_TOTAL "{m}%s " D_UNIT_KILOWATTHOUR "{e}"
|
||||
#ifdef USE_SDM220
|
||||
"{s}SDM120 " D_PHASE_ANGLE "{m}%s " D_UNIT_ANGLE "{e}"
|
||||
"{s}SDM120 " D_IMPORT_ACTIVE "{m}%s " D_UNIT_KILOWATTHOUR "{e}"
|
||||
"{s}SDM120 " D_EXPORT_ACTIVE "{m}%s " D_UNIT_KILOWATTHOUR "{e}"
|
||||
"{s}SDM120 " D_IMPORT_REACTIVE "{m}%s " D_UNIT_KWARH "{e}"
|
||||
"{s}SDM120 " D_EXPORT_REACTIVE "{m}%s " D_UNIT_KWARH "{e}"
|
||||
"{s}SDM120 " D_TOTAL_REACTIVE "{m}%s " D_UNIT_KWARH "{e}"
|
||||
#endif // USE_SDM220
|
||||
;
|
||||
#endif // USE_WEBSERVER
|
||||
|
||||
void SDM120Show(bool json)
|
||||
{
|
||||
char voltage[33];
|
||||
dtostrfd(sdm120_voltage, Settings.flag2.voltage_resolution, voltage);
|
||||
char current[33];
|
||||
dtostrfd(sdm120_current, Settings.flag2.current_resolution, current);
|
||||
char active_power[33];
|
||||
dtostrfd(sdm120_active_power, Settings.flag2.wattage_resolution, active_power);
|
||||
char apparent_power[33];
|
||||
dtostrfd(sdm120_apparent_power, Settings.flag2.wattage_resolution, apparent_power);
|
||||
char reactive_power[33];
|
||||
dtostrfd(sdm120_reactive_power, Settings.flag2.wattage_resolution, reactive_power);
|
||||
char power_factor[33];
|
||||
dtostrfd(sdm120_power_factor, 2, power_factor);
|
||||
char frequency[33];
|
||||
dtostrfd(sdm120_frequency, Settings.flag2.frequency_resolution, frequency);
|
||||
char energy_total[33];
|
||||
dtostrfd(sdm120_energy_total, Settings.flag2.energy_resolution, energy_total);
|
||||
#ifdef USE_SDM220
|
||||
char phase_angle[33];
|
||||
dtostrfd(sdm120_phase_angle, 2, phase_angle);
|
||||
char import_active[33];
|
||||
dtostrfd(sdm120_import_active, Settings.flag2.wattage_resolution, import_active);
|
||||
char export_active[33];
|
||||
dtostrfd(sdm120_export_active, Settings.flag2.wattage_resolution, export_active);
|
||||
char import_reactive[33];
|
||||
dtostrfd(sdm120_import_reactive,Settings.flag2.wattage_resolution, import_reactive);
|
||||
char export_reactive[33];
|
||||
dtostrfd(sdm120_export_reactive,Settings.flag2.wattage_resolution, export_reactive);
|
||||
char total_reactive[33];
|
||||
dtostrfd(sdm120_total_reactive, Settings.flag2.wattage_resolution, total_reactive);
|
||||
#endif // USE_SDM220
|
||||
if (json) {
|
||||
#ifdef USE_SDM220
|
||||
ResponseAppend_P(PSTR(",\"" D_RSLT_ENERGY "\":{\"" D_JSON_TOTAL "\":%s,\"" D_JSON_ACTIVE_POWERUSAGE "\":%s,\"" D_JSON_APPARENT_POWERUSAGE "\":%s,\"" D_JSON_REACTIVE_POWERUSAGE "\":%s,\"" D_JSON_FREQUENCY "\":%s,\"" D_JSON_POWERFACTOR "\":%s,\"" D_JSON_VOLTAGE "\":%s,\"" D_JSON_CURRENT "\":%s,\"" D_JSON_PHASE_ANGLE "\":%s,\"" D_JSON_IMPORT_ACTIVE "\":%s,\"" D_JSON_EXPORT_ACTIVE "\":%s,\"" D_JSON_IMPORT_REACTIVE "\":%s,\"" D_JSON_EXPORT_REACTIVE "\":%s,\"" D_JSON_TOTAL_REACTIVE "\":%s}"),
|
||||
energy_total, active_power, apparent_power, reactive_power, frequency, power_factor, voltage, current, phase_angle, import_active, export_active, import_reactive, export_reactive, total_reactive);
|
||||
#else
|
||||
ResponseAppend_P(PSTR(",\"" D_RSLT_ENERGY "\":{\"" D_JSON_TOTAL "\":%s,\"" D_JSON_ACTIVE_POWERUSAGE "\":%s,\"" D_JSON_APPARENT_POWERUSAGE "\":%s,\"" D_JSON_REACTIVE_POWERUSAGE "\":%s,\"" D_JSON_FREQUENCY "\":%s,\"" D_JSON_POWERFACTOR "\":%s,\"" D_JSON_VOLTAGE "\":%s,\"" D_JSON_CURRENT "\":%s}"),
|
||||
energy_total, active_power, apparent_power, reactive_power, frequency, power_factor, voltage, current);
|
||||
#endif // USE_SDM220
|
||||
#ifdef USE_DOMOTICZ
|
||||
if (0 == tele_period) {
|
||||
char energy_total_chr[33];
|
||||
dtostrfd(sdm120_energy_total * 1000, 1, energy_total_chr);
|
||||
DomoticzSensor(DZ_VOLTAGE, voltage);
|
||||
DomoticzSensor(DZ_CURRENT, current);
|
||||
DomoticzSensorPowerEnergy((int)sdm120_active_power, energy_total_chr);
|
||||
}
|
||||
#endif // USE_DOMOTICZ
|
||||
#ifdef USE_WEBSERVER
|
||||
} else {
|
||||
#ifdef USE_SDM220
|
||||
WSContentSend_PD(HTTP_SNS_SDM120_DATA, voltage, current, active_power, apparent_power, reactive_power, power_factor, frequency, energy_total, phase_angle,import_active,export_active,import_reactive,export_reactive,total_reactive);
|
||||
#else
|
||||
WSContentSend_PD(HTTP_SNS_SDM120_DATA, voltage, current, active_power, apparent_power, reactive_power, power_factor, frequency, energy_total);
|
||||
#endif // USE_SDM220
|
||||
#endif // USE_WEBSERVER
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************\
|
||||
* Interface
|
||||
\*********************************************************************************************/
|
||||
|
||||
bool Xsns23(uint8_t function)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (sdm120_type) {
|
||||
switch (function) {
|
||||
case FUNC_INIT:
|
||||
SDM120Init();
|
||||
break;
|
||||
case FUNC_EVERY_250_MSECOND:
|
||||
SDM120250ms();
|
||||
break;
|
||||
case FUNC_JSON_APPEND:
|
||||
SDM120Show(1);
|
||||
break;
|
||||
#ifdef USE_WEBSERVER
|
||||
case FUNC_WEB_SENSOR:
|
||||
SDM120Show(0);
|
||||
break;
|
||||
#endif // USE_WEBSERVER
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif // USE_SDM120
|
|
@ -1,362 +0,0 @@
|
|||
/*
|
||||
xsns_25_sdm630.ino - Eastron SDM630-Modbus energy meter support for Sonoff-Tasmota
|
||||
|
||||
Copyright (C) 2019 Gennaro Tortone
|
||||
|
||||
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_SDM630
|
||||
|
||||
/*********************************************************************************************\
|
||||
* Eastron SDM630-Modbus energy meter
|
||||
*
|
||||
* Based on: https://github.com/reaper7/SDM_Energy_Meter
|
||||
\*********************************************************************************************/
|
||||
|
||||
#define XSNS_25 25
|
||||
|
||||
#include <TasmotaSerial.h>
|
||||
|
||||
TasmotaSerial *SDM630Serial;
|
||||
|
||||
uint8_t sdm630_type = 1;
|
||||
//uint8_t sdm630_state = 0;
|
||||
|
||||
float sdm630_voltage[] = {0,0,0};
|
||||
float sdm630_current[] = {0,0,0};
|
||||
float sdm630_active_power[] = {0,0,0};
|
||||
float sdm630_reactive_power[] = {0,0,0};
|
||||
float sdm630_power_factor[] = {0,0,0};
|
||||
float sdm630_energy_total = 0;
|
||||
|
||||
bool SDM630_ModbusReceiveReady(void)
|
||||
{
|
||||
return (SDM630Serial->available() > 1);
|
||||
}
|
||||
|
||||
void SDM630_ModbusSend(uint8_t function_code, uint16_t start_address, uint16_t register_count)
|
||||
{
|
||||
uint8_t frame[8];
|
||||
|
||||
frame[0] = 0x01; // default SDM630 Modbus address
|
||||
frame[1] = function_code;
|
||||
frame[2] = (uint8_t)(start_address >> 8);
|
||||
frame[3] = (uint8_t)(start_address);
|
||||
frame[4] = (uint8_t)(register_count >> 8);
|
||||
frame[5] = (uint8_t)(register_count);
|
||||
|
||||
uint16_t crc = SDM630_calculateCRC(frame, 6); // calculate out crc only from first 6 bytes
|
||||
frame[6] = lowByte(crc);
|
||||
frame[7] = highByte(crc);
|
||||
|
||||
while (SDM630Serial->available() > 0) { // read serial if any old data is available
|
||||
SDM630Serial->read();
|
||||
}
|
||||
|
||||
SDM630Serial->flush();
|
||||
SDM630Serial->write(frame, sizeof(frame));
|
||||
}
|
||||
|
||||
uint8_t SDM630_ModbusReceive(float *value)
|
||||
{
|
||||
uint8_t buffer[9];
|
||||
|
||||
*value = NAN;
|
||||
uint8_t len = 0;
|
||||
while (SDM630Serial->available() > 0) {
|
||||
buffer[len++] = (uint8_t)SDM630Serial->read();
|
||||
}
|
||||
|
||||
if (len < 9)
|
||||
return 3; // SDM_ERR_NOT_ENOUGHT_BYTES
|
||||
|
||||
if (len == 9) {
|
||||
|
||||
if (buffer[0] == 0x01 && buffer[1] == 0x04 && buffer[2] == 4) { // check node number, op code and reply bytes count
|
||||
|
||||
if((SDM630_calculateCRC(buffer, 7)) == ((buffer[8] << 8) | buffer[7])) { //calculate crc from first 7 bytes and compare with received crc (bytes 7 & 8)
|
||||
|
||||
((uint8_t*)value)[3] = buffer[3];
|
||||
((uint8_t*)value)[2] = buffer[4];
|
||||
((uint8_t*)value)[1] = buffer[5];
|
||||
((uint8_t*)value)[0] = buffer[6];
|
||||
|
||||
} else return 1; // SDM_ERR_CRC_ERROR
|
||||
|
||||
} else return 2; // SDM_ERR_WRONG_BYTES
|
||||
}
|
||||
|
||||
return 0; // SDM_ERR_NO_ERROR
|
||||
}
|
||||
|
||||
uint16_t SDM630_calculateCRC(uint8_t *frame, uint8_t num)
|
||||
{
|
||||
uint16_t crc, flag;
|
||||
crc = 0xFFFF;
|
||||
for (uint32_t i = 0; i < num; i++) {
|
||||
crc ^= frame[i];
|
||||
for (uint32_t j = 8; j; j--) {
|
||||
if ((crc & 0x0001) != 0) { // If the LSB is set
|
||||
crc >>= 1; // Shift right and XOR 0xA001
|
||||
crc ^= 0xA001;
|
||||
} else { // Else LSB is not set
|
||||
crc >>= 1; // Just shift right
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
/*********************************************************************************************/
|
||||
|
||||
const uint16_t sdm630_start_addresses[] {
|
||||
0x0000, // L1 - SDM630_VOLTAGE [V]
|
||||
0x0002, // L2 - SDM630_VOLTAGE [V]
|
||||
0x0004, // L3 - SDM630_VOLTAGE [V]
|
||||
0x0006, // L1 - SDM630_CURRENT [A]
|
||||
0x0008, // L2 - SDM630_CURRENT [A]
|
||||
0x000A, // L3 - SDM630_CURRENT [A]
|
||||
0x000C, // L1 - SDM630_POWER [W]
|
||||
0x000E, // L2 - SDM630_POWER [W]
|
||||
0x0010, // L3 - SDM630_POWER [W]
|
||||
0x0018, // L1 - SDM630_REACTIVE_POWER [VAR]
|
||||
0x001A, // L2 - SDM630_REACTIVE_POWER [VAR]
|
||||
0x001C, // L3 - SDM630_REACTIVE_POWER [VAR]
|
||||
0x001E, // L1 - SDM630_POWER_FACTOR
|
||||
0x0020, // L2 - SDM630_POWER_FACTOR
|
||||
0x0022, // L3 - SDM630_POWER_FACTOR
|
||||
0x0156 // Total - SDM630_TOTAL_ACTIVE_ENERGY [Wh]
|
||||
};
|
||||
|
||||
uint8_t sdm630_read_state = 0;
|
||||
uint8_t sdm630_send_retry = 0;
|
||||
|
||||
void SDM630250ms(void) // Every 250 mSec
|
||||
{
|
||||
// sdm630_state++;
|
||||
// if (6 == sdm630_state) { // Every 300 mSec
|
||||
// sdm630_state = 0;
|
||||
|
||||
float value = 0;
|
||||
bool data_ready = SDM630_ModbusReceiveReady();
|
||||
|
||||
if (data_ready) {
|
||||
uint8_t error = SDM630_ModbusReceive(&value);
|
||||
if (error) {
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "SDM630 response error %d"), error);
|
||||
} else {
|
||||
switch(sdm630_read_state) {
|
||||
case 0:
|
||||
sdm630_voltage[0] = value;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
sdm630_voltage[1] = value;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
sdm630_voltage[2] = value;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
sdm630_current[0] = value;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
sdm630_current[1] = value;
|
||||
break;
|
||||
|
||||
case 5:
|
||||
sdm630_current[2] = value;
|
||||
break;
|
||||
|
||||
case 6:
|
||||
sdm630_active_power[0] = value;
|
||||
break;
|
||||
|
||||
case 7:
|
||||
sdm630_active_power[1] = value;
|
||||
break;
|
||||
|
||||
case 8:
|
||||
sdm630_active_power[2] = value;
|
||||
break;
|
||||
|
||||
case 9:
|
||||
sdm630_reactive_power[0] = value;
|
||||
break;
|
||||
|
||||
case 10:
|
||||
sdm630_reactive_power[1] = value;
|
||||
break;
|
||||
|
||||
case 11:
|
||||
sdm630_reactive_power[2] = value;
|
||||
break;
|
||||
|
||||
case 12:
|
||||
sdm630_power_factor[0] = value;
|
||||
break;
|
||||
|
||||
case 13:
|
||||
sdm630_power_factor[1] = value;
|
||||
break;
|
||||
|
||||
case 14:
|
||||
sdm630_power_factor[2] = value;
|
||||
break;
|
||||
|
||||
case 15:
|
||||
sdm630_energy_total = value;
|
||||
break;
|
||||
} // end switch
|
||||
|
||||
sdm630_read_state++;
|
||||
|
||||
if (sizeof(sdm630_start_addresses)/2 == sdm630_read_state) {
|
||||
sdm630_read_state = 0;
|
||||
}
|
||||
}
|
||||
} // end data ready
|
||||
|
||||
if (0 == sdm630_send_retry || data_ready) {
|
||||
sdm630_send_retry = 5;
|
||||
SDM630_ModbusSend(0x04, sdm630_start_addresses[sdm630_read_state], 2);
|
||||
} else {
|
||||
sdm630_send_retry--;
|
||||
}
|
||||
// } // end 300 ms
|
||||
}
|
||||
|
||||
void SDM630Init(void)
|
||||
{
|
||||
sdm630_type = 0;
|
||||
if ((pin[GPIO_SDM630_RX] < 99) && (pin[GPIO_SDM630_TX] < 99)) {
|
||||
SDM630Serial = new TasmotaSerial(pin[GPIO_SDM630_RX], pin[GPIO_SDM630_TX], 1);
|
||||
#ifdef SDM630_SPEED
|
||||
if (SDM630Serial->begin(SDM630_SPEED)) {
|
||||
#else
|
||||
if (SDM630Serial->begin(2400)) {
|
||||
#endif
|
||||
if (SDM630Serial->hardwareSerial()) { ClaimSerial(); }
|
||||
sdm630_type = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
const char HTTP_SNS_SDM630_DATA[] PROGMEM =
|
||||
"{s}SDM630 " D_VOLTAGE "{m}%s/%s/%s " D_UNIT_VOLT "{e}"
|
||||
"{s}SDM630 " D_CURRENT "{m}%s/%s/%s " D_UNIT_AMPERE "{e}"
|
||||
"{s}SDM630 " D_POWERUSAGE_ACTIVE "{m}%s/%s/%s " D_UNIT_WATT "{e}"
|
||||
"{s}SDM630 " D_POWERUSAGE_REACTIVE "{m}%s/%s/%s " D_UNIT_VAR "{e}"
|
||||
"{s}SDM630 " D_POWER_FACTOR "{m}%s/%s/%s{e}"
|
||||
"{s}SDM630 " D_ENERGY_TOTAL "{m}%s " D_UNIT_KILOWATTHOUR "{e}";
|
||||
#endif // USE_WEBSERVER
|
||||
|
||||
void SDM630Show(bool json)
|
||||
{
|
||||
char voltage_l1[33];
|
||||
dtostrfd(sdm630_voltage[0], Settings.flag2.voltage_resolution, voltage_l1);
|
||||
char voltage_l2[33];
|
||||
dtostrfd(sdm630_voltage[1], Settings.flag2.voltage_resolution, voltage_l2);
|
||||
char voltage_l3[33];
|
||||
dtostrfd(sdm630_voltage[2], Settings.flag2.voltage_resolution, voltage_l3);
|
||||
char current_l1[33];
|
||||
dtostrfd(sdm630_current[0], Settings.flag2.current_resolution, current_l1);
|
||||
char current_l2[33];
|
||||
dtostrfd(sdm630_current[1], Settings.flag2.current_resolution, current_l2);
|
||||
char current_l3[33];
|
||||
dtostrfd(sdm630_current[2], Settings.flag2.current_resolution, current_l3);
|
||||
char active_power_l1[33];
|
||||
dtostrfd(sdm630_active_power[0], Settings.flag2.wattage_resolution, active_power_l1);
|
||||
char active_power_l2[33];
|
||||
dtostrfd(sdm630_active_power[1], Settings.flag2.wattage_resolution, active_power_l2);
|
||||
char active_power_l3[33];
|
||||
dtostrfd(sdm630_active_power[2], Settings.flag2.wattage_resolution, active_power_l3);
|
||||
char reactive_power_l1[33];
|
||||
dtostrfd(sdm630_reactive_power[0], Settings.flag2.wattage_resolution, reactive_power_l1);
|
||||
char reactive_power_l2[33];
|
||||
dtostrfd(sdm630_reactive_power[1], Settings.flag2.wattage_resolution, reactive_power_l2);
|
||||
char reactive_power_l3[33];
|
||||
dtostrfd(sdm630_reactive_power[2], Settings.flag2.wattage_resolution, reactive_power_l3);
|
||||
char power_factor_l1[33];
|
||||
dtostrfd(sdm630_power_factor[0], 2, power_factor_l1);
|
||||
char power_factor_l2[33];
|
||||
dtostrfd(sdm630_power_factor[1], 2, power_factor_l2);
|
||||
char power_factor_l3[33];
|
||||
dtostrfd(sdm630_power_factor[2], 2, power_factor_l3);
|
||||
char energy_total[33];
|
||||
dtostrfd(sdm630_energy_total, Settings.flag2.energy_resolution, energy_total);
|
||||
|
||||
if (json) {
|
||||
ResponseAppend_P(PSTR(",\"" D_RSLT_ENERGY "\":{\"" D_JSON_TOTAL "\":%s,\""
|
||||
D_JSON_ACTIVE_POWERUSAGE "\":[%s,%s,%s],\"" D_JSON_REACTIVE_POWERUSAGE "\":[%s,%s,%s],\""
|
||||
D_JSON_POWERFACTOR "\":[%s,%s,%s],\"" D_JSON_VOLTAGE "\":[%s,%s,%s],\"" D_JSON_CURRENT "\":[%s,%s,%s]}"),
|
||||
energy_total, active_power_l1, active_power_l2, active_power_l3,
|
||||
reactive_power_l1, reactive_power_l2, reactive_power_l3,
|
||||
power_factor_l1, power_factor_l2, power_factor_l3,
|
||||
voltage_l1, voltage_l2, voltage_l3,
|
||||
current_l1, current_l2, current_l3);
|
||||
#ifdef USE_DOMOTICZ
|
||||
if (0 == tele_period) {
|
||||
char energy_total_chr[33];
|
||||
dtostrfd(sdm630_energy_total * 1000, 1, energy_total_chr);
|
||||
DomoticzSensor(DZ_VOLTAGE, voltage_l1);
|
||||
DomoticzSensor(DZ_CURRENT, current_l1);
|
||||
DomoticzSensorPowerEnergy((int)sdm630_active_power[0], energy_total_chr);
|
||||
}
|
||||
#endif // USE_DOMOTICZ
|
||||
#ifdef USE_WEBSERVER
|
||||
} else {
|
||||
WSContentSend_PD(HTTP_SNS_SDM630_DATA,
|
||||
voltage_l1, voltage_l2, voltage_l3, current_l1, current_l2, current_l3,
|
||||
active_power_l1, active_power_l2, active_power_l3,
|
||||
reactive_power_l1, reactive_power_l2, reactive_power_l3,
|
||||
power_factor_l1, power_factor_l2, power_factor_l3, energy_total);
|
||||
#endif // USE_WEBSERVER
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************\
|
||||
* Interface
|
||||
\*********************************************************************************************/
|
||||
|
||||
bool Xsns25(uint8_t function)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (sdm630_type) {
|
||||
switch (function) {
|
||||
case FUNC_INIT:
|
||||
SDM630Init();
|
||||
break;
|
||||
case FUNC_EVERY_250_MSECOND:
|
||||
SDM630250ms();
|
||||
break;
|
||||
case FUNC_JSON_APPEND:
|
||||
SDM630Show(1);
|
||||
break;
|
||||
#ifdef USE_WEBSERVER
|
||||
case FUNC_WEB_SENSOR:
|
||||
SDM630Show(0);
|
||||
break;
|
||||
#endif // USE_WEBSERVER
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue