From 926869e8491ac57d1dacff6b7cf96556ecb4b455 Mon Sep 17 00:00:00 2001 From: pablozg Date: Thu, 17 Oct 2019 18:22:11 +0200 Subject: [PATCH] Solax X1: Code refactor and optimitation, add as energy driver to get today/yesterday totals and P1 smart meter function --- sonoff/my_user_config.h | 5 +- sonoff/sonoff_post.h | 16 +- sonoff/sonoff_template.h | 14 +- sonoff/xnrg_12_solaxX1.ino | 530 +++++++++++++++++++++++++++++++++++++ sonoff/xsns_49_solaxX1.ino | 391 ++++++++++++--------------- 5 files changed, 727 insertions(+), 229 deletions(-) create mode 100644 sonoff/xnrg_12_solaxX1.ino diff --git a/sonoff/my_user_config.h b/sonoff/my_user_config.h index d205335d5..9a9d70402 100644 --- a/sonoff/my_user_config.h +++ b/sonoff/my_user_config.h @@ -457,13 +457,16 @@ #define DDS2382_SPEED 9600 // Hiking DDS2382 Modbus RS485 serial speed (default: 9600 baud) //#define USE_DDSU666 // Add support for Chint DDSU666 Modbus energy monitor (+0k6 code) #define DDSU666_SPEED 9600 // Chint DDSU666 Modbus RS485 serial speed (default: 9600 baud) +//#define USE_SOLAX_X1_NRG // Add support for Solax X1 series Modbus log info (+3k1 code) + #define SOLAXX1_SPEED 9600 // Solax X1 Modbus RS485 serial speed (default: 9600 baud) + #define SOLAXX1_PV2 // Solax X1 using second PV //#define USE_SDM120 // Add support for Eastron SDM120-Modbus energy meter (+2k4 code) // #define SDM120_SPEED 2400 // SDM120-Modbus RS485 serial speed (default: 2400 baud) #define USE_SDM220 // Add extra parameters for SDM220 (+0k1 code) //#define USE_SDM630 // Add support for Eastron SDM630-Modbus energy meter (+2k code) // #define SDM630_SPEED 9600 // SDM630-Modbus RS485 serial speed (default: 9600 baud) -//#define USE_SOLAX_X1 // Add support for Solax X1 series Modbus log info (+4k1 code) +//#define USE_SOLAX_X1 // Add support for Solax X1 series Modbus log info (+3k6 code) #define SOLAXX1_SPEED 9600 // Solax X1 Modbus RS485 serial speed (default: 9600 baud) #define SOLAXX1_PV2 // Solax X1 using second PV diff --git a/sonoff/sonoff_post.h b/sonoff/sonoff_post.h index d83bb624d..15052711e 100644 --- a/sonoff/sonoff_post.h +++ b/sonoff/sonoff_post.h @@ -189,9 +189,10 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c #define USE_SDM630_2 // Add support for Eastron SDM630-Modbus energy monitor (+0k6 code) #define USE_DDS2382 // Add support for Hiking DDS2382 Modbus energy monitor (+0k6 code) #define USE_DDSU666 // Add support for Chint DDSU666 Modbus energy monitor (+0k6 code) +//#define USE_SOLAX_X1_NRG // Add support for Solax X1 series Modbus log info (+3k1 code) //#define USE_SDM120 // Add support for Eastron SDM120-Modbus energy meter (+1k7 code) //#define USE_SDM630 // Add support for Eastron SDM630-Modbus energy meter (+2k code) -//#define USE_SOLAX_X1 // Add support for Solax X1 series Modbus log info (+4k1 code) +//#define USE_SOLAX_X1 // Add support for Solax X1 series Modbus log info (+3k6 code) #define USE_DHT // Add support for DHT11, AM2301 (DHT21, DHT22, AM2302, AM2321) and SI7021 Temperature and Humidity sensor #define USE_MAX31855 // Add support for MAX31855 K-Type thermocouple sensor using softSPI @@ -294,9 +295,10 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c #undef USE_SDM630_2 // Disable support for Eastron SDM630-Modbus energy monitor (+0k6 code) #undef USE_DDS2382 // Disable support for Hiking DDS2382 Modbus energy monitor (+0k6 code) #undef USE_DDSU666 // Disable support for Chint DDSU666 Modbus energy monitor (+0k6 code) +#undef USE_SOLAX_X1_NRG // Disable support for Solax X1 series Modbus log info (+3k1 code) #undef USE_SDM120 // Disable support for Eastron SDM120-Modbus energy meter #undef USE_SDM630 // Disable support for Eastron SDM630-Modbus energy meter -#undef USE_SOLAX_X1 // Disable support for Solax X1 series Modbus log info (+4k1 code) +#undef USE_SOLAX_X1 // Disable support for Solax X1 series Modbus log info (+3k6 code) #define USE_DHT // Add support for DHT11, AM2301 (DHT21, DHT22, AM2302, AM2321) and SI7021 Temperature and Humidity sensor #undef USE_MAX31855 // Disable MAX31855 K-Type thermocouple sensor using softSPI @@ -376,9 +378,10 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c #undef USE_SDM630_2 // Disable support for Eastron SDM630-Modbus energy monitor (+0k6 code) #undef USE_DDS2382 // Disable support for Hiking DDS2382 Modbus energy monitor (+0k6 code) #undef USE_DDSU666 // Disable support for Chint DDSU666 Modbus energy monitor (+0k6 code) + #undef USE_SOLAX_X1_NRG // Disable support for Solax X1 series Modbus log info (+3k1 code) #undef USE_SDM120 // Disable support for Eastron SDM120-Modbus energy meter #undef USE_SDM630 // Disable support for Eastron SDM630-Modbus energy meter -#undef USE_SOLAX_X1 // Disable support for Solax X1 series Modbus log info (+4k1 code) +#undef USE_SOLAX_X1 // Disable support for Solax X1 series Modbus log info (+3k6 code) #define USE_I2C // I2C using library wire (+10k code, 0k2 mem, 124 iram) #define USE_DISPLAY // Add I2C Display Support (+2k code) @@ -457,9 +460,10 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c #undef USE_SDM630_2 // Disable support for Eastron SDM630-Modbus energy monitor (+0k6 code) #undef USE_DDS2382 // Disable support for Hiking DDS2382 Modbus energy monitor (+0k6 code) #undef USE_DDSU666 // Disable support for Chint DDSU666 Modbus energy monitor (+0k6 code) + #undef USE_SOLAX_X1_NRG // Disable support for Solax X1 series Modbus log info (+3k1 code) #undef USE_SDM120 // Disable support for Eastron SDM120-Modbus energy meter #undef USE_SDM630 // Disable support for Eastron SDM630-Modbus energy meter -#undef USE_SOLAX_X1 // Disable support for Solax X1 series Modbus log info (+4k1 code) +#undef USE_SOLAX_X1 // Disable support for Solax X1 series Modbus log info (+3k6 code) #undef USE_DS18x20 // Disable support for DS18x20 sensors with id sort, single scan and read retry (+1k3 code) @@ -572,6 +576,7 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c #undef USE_SDM630_2 // Disable support for Eastron SDM630-Modbus energy monitor (+0k6 code) #undef USE_DDS2382 // Disable support for Hiking DDS2382 Modbus energy monitor (+0k6 code) #undef USE_DDSU666 // Disable support for Chint DDSU666 Modbus energy monitor (+0k6 code) +#undef USE_SOLAX_X1_NRG // Disable support for Solax X1 series Modbus log info (+3k1 code) #undef USE_SDM120 // Disable support for Eastron SDM120-Modbus energy meter #undef USE_SDM630 // Disable support for Eastron SDM630-Modbus energy meter #undef USE_SOLAX_X1 // Disable support for Solax X1 series Modbus log info (+4k1 code) @@ -674,9 +679,10 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c #undef USE_SDM630_2 // Disable support for Eastron SDM630-Modbus energy monitor (+0k6 code) #undef USE_DDS2382 // Disable support for Hiking DDS2382 Modbus energy monitor (+0k6 code) #undef USE_DDSU666 // Disable support for Chint DDSU666 Modbus energy monitor (+0k6 code) +#undef USE_SOLAX_X1_NRG // Disable support for Solax X1 series Modbus log info (+3k1 code) #undef USE_SDM120 // Disable support for Eastron SDM120-Modbus energy meter #undef USE_SDM630 // Disable support for Eastron SDM630-Modbus energy meter -#undef USE_SOLAX_X1 // Disable support for Solax X1 series Modbus log info (+4k1 code) +#undef USE_SOLAX_X1 // Disable support for Solax X1 series Modbus log info (+3k6 code) #undef USE_DHT // Disable support for DHT11, AM2301 (DHT21, DHT22, AM2302, AM2321) and SI7021 Temperature and Humidity sensor #undef USE_MAX31855 // Disable MAX31855 K-Type thermocouple sensor using softSPI diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index c5a8a4652..81c5f7657 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -646,6 +646,14 @@ const uint8_t kGpioNiceList[] PROGMEM = { GPIO_DDS2382_TX, // DDS2382 Serial interface GPIO_DDS2382_RX, // DDS2382 Serial interface #endif +#ifdef USE_DDSU666 + GPIO_DDSU666_TX, // DDSU666 Serial interface + GPIO_DDSU666_RX, // DDSU666 Serial interface +#endif // USE_DDSU666 +#ifdef USE_SOLAX_X1_NRG + GPIO_SOLAXX1_TX, // Solax Inverter tx pin + GPIO_SOLAXX1_RX, // Solax Inverter rx pin +#endif // USE_SOLAX_X1_NRG #endif // USE_ENERGY_SENSOR #ifndef USE_SDM120_2 #ifdef USE_SDM120 @@ -659,14 +667,12 @@ const uint8_t kGpioNiceList[] PROGMEM = { GPIO_SDM630_RX, // SDM630 Serial interface #endif #endif // USE_SDM630_2 +#ifndef USE_SOLAX_X1_NRG #ifdef USE_SOLAX_X1 GPIO_SOLAXX1_TX, // Solax Inverter tx pin GPIO_SOLAXX1_RX, // Solax Inverter rx pin #endif -#ifdef USE_DDSU666 - GPIO_DDSU666_TX, // DDSU666 Serial interface - GPIO_DDSU666_RX, // DDSU666 Serial interface -#endif // USE_DDSU666 +#endif // USE_SOLAX_X1_NRG #ifdef USE_SERIAL_BRIDGE GPIO_SBR_TX, // Serial Bridge Serial interface diff --git a/sonoff/xnrg_12_solaxX1.ino b/sonoff/xnrg_12_solaxX1.ino new file mode 100644 index 000000000..0bd67ec4d --- /dev/null +++ b/sonoff/xnrg_12_solaxX1.ino @@ -0,0 +1,530 @@ +/* + xnrg_12_solaxX1.ino - Solax X1 inverter RS485 support for Sonoff-Tasmota + + Copyright (C) 2019 Pablo Zerón + + 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 . +*/ + +#ifdef USE_ENERGY_SENSOR +#ifdef USE_SOLAX_X1_NRG +/*********************************************************************************************\ + * Solax X1 Inverter +\*********************************************************************************************/ + +#define XNRG_12 12 + +#ifndef SOLAXX1_SPEED +#define SOLAXX1_SPEED 9600 // default solax rs485 speed +#endif + +#define INVERTER_ADDRESS 0x0A + +#define D_SOLAX_X1 "SolaxX1" + +#include + +enum solaxX1_Error +{ + solaxX1_ERR_NO_ERROR, + solaxX1_ERR_CRC_ERROR +}; + +union { + uint32_t ErrMessage; + struct { + //BYTE0 + uint8_t TzProtectFault:1;//0 + uint8_t MainsLostFault:1;//1 + uint8_t GridVoltFault:1;//2 + uint8_t GridFreqFault:1;//3 + uint8_t PLLLostFault:1;//4 + uint8_t BusVoltFault:1;//5 + uint8_t ErrBit06:1;//6 + uint8_t OciFault:1;//7 + //BYTE1 + uint8_t Dci_OCP_Fault:1;//8 + uint8_t ResidualCurrentFault:1;//9 + uint8_t PvVoltFault:1;//10 + uint8_t Ac10Mins_Voltage_Fault:1;//11 + uint8_t IsolationFault:1;//12 + uint8_t TemperatureOverFault:1;//13 + uint8_t FanFault:1;//14 + uint8_t ErrBit15:1;//15 + //BYTE2 + uint8_t SpiCommsFault:1;//16 + uint8_t SciCommsFault:1;//17 + uint8_t ErrBit18:1;//18 + uint8_t InputConfigFault:1;//19 + uint8_t EepromFault:1;//20 + uint8_t RelayFault:1;//21 + uint8_t SampleConsistenceFault:1;//22 + uint8_t ResidualCurrent_DeviceFault:1;//23 + //BYTE3 + uint8_t ErrBit24:1;//24 + uint8_t ErrBit25:1;//25 + uint8_t ErrBit26:1;//26 + uint8_t ErrBit27:1;//27 + uint8_t ErrBit28:1;//28 + uint8_t DCI_DeviceFault:1;//29 + uint8_t OtherDeviceFault:1;//30 + uint8_t ErrBit31:1;//31 + }; +} ErrCode; + +const char kSolaxMode[] PROGMEM = D_WAITING "|" D_CHECKING "|" D_WORKING "|" D_FAILURE; + +const char kSolaxError[] PROGMEM = + D_SOLAX_ERROR_0 "|" D_SOLAX_ERROR_1 "|" D_SOLAX_ERROR_2 "|" D_SOLAX_ERROR_3 "|" D_SOLAX_ERROR_4 "|" D_SOLAX_ERROR_5 "|" + D_SOLAX_ERROR_6 "|" D_SOLAX_ERROR_7 "|" D_SOLAX_ERROR_8; + +/*********************************************************************************************/ + +TasmotaSerial *solaxX1Serial; + +uint8_t solaxX1_Init = 1; + +struct SOLAXX1 { + float temperature = 0; + float energy_today = 0; + float dc1_voltage = 0; + float dc2_voltage = 0; + float dc1_current = 0; + float dc2_current = 0; + float energy_total = 0; + float runtime_total = 0; + float dc1_power = 0; + float dc2_power = 0; + + uint8_t status = 0; + uint32_t errorCode = 0; +} solaxX1; + +union { + uint8_t status; + struct { + uint8_t freeBit7:1; // Bit7 + uint8_t freeBit6:1; // Bit6 + uint8_t freeBit5:1; // Bit5 + uint8_t queryOffline:1; // Bit4 + uint8_t queryOfflineSend:1; // Bit3 + uint8_t hasAddress:1; // Bit2 + uint8_t inverterAddressSend:1; // Bit1 + uint8_t inverterSnReceived:1; // Bit0 + }; +} protocolStatus; + +uint8_t header[2] = {0xAA, 0x55}; +uint8_t source[2] = {0x00, 0x00}; +uint8_t destination[2] = {0x00, 0x00}; +uint8_t controlCode[1] = {0x00}; +uint8_t functionCode[1] = {0x00}; +uint8_t dataLength[1] = {0x00}; +uint8_t data[16] = {0}; + +uint8_t message[30]; + +/*********************************************************************************************/ + +bool solaxX1_RS485ReceiveReady(void) +{ + return (solaxX1Serial->available() > 1); +} + +void solaxX1_RS485Send(uint16_t msgLen) +{ + memcpy(message, header, 2); + memcpy(message + 2, source, 2); + memcpy(message + 4, destination, 2); + memcpy(message + 6, controlCode, 1); + memcpy(message + 7, functionCode, 1); + memcpy(message + 8, dataLength, 1); + memcpy(message + 9, data, sizeof(data)); + uint16_t crc = solaxX1_calculateCRC(message, msgLen); // calculate out crc bytes + + while (solaxX1Serial->available() > 0) + { // read serial if any old data is available + solaxX1Serial->read(); + } + + solaxX1Serial->flush(); + solaxX1Serial->write(message, msgLen); + solaxX1Serial->write(highByte(crc)); + solaxX1Serial->write(lowByte(crc)); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, message, msgLen); +} + +uint8_t solaxX1_RS485Receive(uint8_t *value) +{ + uint8_t len = 0; + + while (solaxX1Serial->available() > 0) + { + value[len++] = (uint8_t)solaxX1Serial->read(); + } + + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, value, len); + + uint16_t crc = solaxX1_calculateCRC(value, len - 2); // calculate out crc bytes + + if (value[len - 1] == lowByte(crc) && value[len - 2] == highByte(crc)) + { // check calc crc with received crc + return solaxX1_ERR_NO_ERROR; + } + else + { + return solaxX1_ERR_CRC_ERROR; + } +} + +uint16_t solaxX1_calculateCRC(uint8_t *bExternTxPackage, uint8_t bLen) +{ + uint8_t i; + uint16_t wChkSum; + wChkSum = 0; + + for (i = 0; i < bLen; i++) + { + wChkSum = wChkSum + bExternTxPackage[i]; + } + return wChkSum; +} + +void solaxX1_SendInverterAddress() +{ + source[0] = 0x00; + destination[0] = 0x00; + destination[1] = 0x00; + controlCode[0] = 0x10; + functionCode[0] = 0x01; + dataLength[0] = 0x0F; + data[14] = INVERTER_ADDRESS; // Inverter Address, It must be unique in case of more inverters in the same rs485 net. + solaxX1_RS485Send(24); +} + +void solaxX1_QueryLiveData() +{ + source[0] = 0x01; + destination[0] = 0x00; + destination[1] = INVERTER_ADDRESS; + controlCode[0] = 0x11; + functionCode[0] = 0x02; + dataLength[0] = 0x00; + solaxX1_RS485Send(9); +} + +uint8_t solaxX1_ParseErrorCode(uint32_t code){ + ErrCode.ErrMessage = code; + + if (code == 0) return 0; + if (ErrCode.MainsLostFault) return 1; + if (ErrCode.GridVoltFault) return 2; + if (ErrCode.GridFreqFault) return 3; + if (ErrCode.PvVoltFault) return 4; + if (ErrCode.IsolationFault) return 5; + if (ErrCode.TemperatureOverFault) return 6; + if (ErrCode.FanFault) return 7; + if (ErrCode.OtherDeviceFault) return 8; +} + +/*********************************************************************************************/ + +uint8_t solaxX1_send_retry = 0; +uint8_t solaxX1_nodata_count = 0; + +void solaxX1250MSecond(void) // Every Second +{ + uint8_t value[61] = {0}; + bool data_ready = solaxX1_RS485ReceiveReady(); + + if (protocolStatus.hasAddress && (data_ready || solaxX1_send_retry == 0)) + { + if (data_ready) + { + uint8_t error = solaxX1_RS485Receive(value); + if (error) + { + DEBUG_SENSOR_LOG(PSTR("SX1: Data response CRC error")); + } + else + { + solaxX1_nodata_count = 0; + solaxX1_send_retry = 12; + Energy.data_valid[0] = 0; + + solaxX1.temperature = (float)((value[9] << 8) | value[10]); // Temperature + solaxX1.energy_today = (float)((value[11] << 8) | value[12]) * 0.1f; // Energy Today + solaxX1.dc1_voltage = (float)((value[13] << 8) | value[14]) * 0.1f; // PV1 Voltage + solaxX1.dc2_voltage = (float)((value[15] << 8) | value[16]) * 0.1f; // PV2 Voltage + solaxX1.dc1_current = (float)((value[17] << 8) | value[18]) * 0.1f; // PV1 Current + solaxX1.dc2_current = (float)((value[19] << 8) | value[20]) * 0.1f; // PV2 Current + Energy.current[0] = (float)((value[21] << 8) | value[22]) * 0.1f; // AC Current + Energy.voltage[0] = (float)((value[23] << 8) | value[24]) * 0.1f; // AC Voltage + Energy.frequency[0] = (float)((value[25] << 8) | value[26]) * 0.01f; // AC Frequency + Energy.active_power[0] = (float)((value[27] << 8) | value[28]); // AC Power + //temporal = (float)((value[29] << 8) | value[30]) * 0.1f; // Not Used + solaxX1.energy_total = (float)((value[31] << 8) | (value[32] << 8) | (value[33] << 8) | value[34]) * 0.1f; // Energy Total + solaxX1.runtime_total = (float)((value[35] << 8) | (value[36] << 8) | (value[37] << 8) | value[38]); // Work Time Total + solaxX1.status = (uint8_t)((value[39] << 8) | value[40]); // Work mode + //temporal = (float)((value[41] << 8) | value[42]); // Grid voltage fault value 0.1V + //temporal = (float)((value[43] << 8) | value[44]); // Gird frequency fault value 0.01Hz + //temporal = (float)((value[45] << 8) | value[46]); // Dc injection fault value 1mA + //temporal = (float)((value[47] << 8) | value[48]); // Temperature fault value + //temporal = (float)((value[49] << 8) | value[50]); // Pv1 voltage fault value 0.1V + //temporal = (float)((value[51] << 8) | value[52]); // Pv2 voltage fault value 0.1V + //temporal = (float)((value[53] << 8) | value[54]); // GFC fault value + solaxX1.errorCode = (uint32_t)((value[58] << 8) | (value[57] << 8) | (value[56] << 8) | value[55]); // Error Code + + solaxX1.dc1_power = solaxX1.dc1_voltage * solaxX1.dc1_current; + solaxX1.dc2_power = solaxX1.dc2_voltage * solaxX1.dc2_current; + + solaxX1_QueryLiveData(); + EnergyUpdateTotal(solaxX1.energy_total, true); // 484.708 kWh + } + } // End data Ready + + if (0 == solaxX1_send_retry && 255 != solaxX1_nodata_count) { + solaxX1_send_retry = 12; + solaxX1_QueryLiveData(); + } + + // While the inverter has not stable ambient light, will send an address adquired but go offline again, + // so no data will be received when the query is send, then we start the countdown to set the inverter as offline again. + if (255 == solaxX1_nodata_count) { + solaxX1_nodata_count = 0; + solaxX1_send_retry = 12; + } + } // end hasAddress && (data_ready || solaxX1_send_retry == 0) + else + { + if ((solaxX1_nodata_count % 4) == 0) { DEBUG_SENSOR_LOG(PSTR("SX1: No Data count: %d"), solaxX1_nodata_count); } + if (solaxX1_nodata_count < 10 * 4) // max. seconds without data + { + solaxX1_nodata_count++; + } + else if (255 != solaxX1_nodata_count) + { + // no data from RS485, reset values to 0 and set inverter as offline + solaxX1_nodata_count = 255; + solaxX1_send_retry = 12; + protocolStatus.status = 0b00001000; // queryOffline + Energy.data_valid[0] = ENERGY_WATCHDOG; + + solaxX1.temperature = solaxX1.dc1_voltage = solaxX1.dc2_voltage = solaxX1.dc1_current = solaxX1.dc2_current = solaxX1.dc1_power = 0; + solaxX1.dc2_power = solaxX1.status = Energy.current[0] = Energy.voltage[0] = Energy.frequency[0] = Energy.active_power[0] = 0; + //solaxX1.energy_today = solaxX1.energy_total = solaxX1.runtime_total = 0; + } + } + + if (!protocolStatus.hasAddress && (data_ready || solaxX1_send_retry == 0)) + { + if (data_ready) + { + // check address confirmation from inverter + if (protocolStatus.inverterAddressSend) + { + uint8_t error = solaxX1_RS485Receive(value); + if (error) + { + DEBUG_SENSOR_LOG(PSTR("SX1: Address confirmation response CRC error")); + } + else + { + if (value[6] == 0x10 && value[7] == 0x81 && value[9] == 0x06) + { + DEBUG_SENSOR_LOG(PSTR("SX1: Set hasAddress")); + protocolStatus.status = 0b00100000; // hasAddress + } + } + } + + // Check inverter serial number and send the set address request + if (protocolStatus.queryOfflineSend) + { + uint8_t error = solaxX1_RS485Receive(value); + if (error) + { + DEBUG_SENSOR_LOG(PSTR("SX1: Query Offline response CRC error")); + } + else + { + // Serial number from query response + if (value[6] == 0x10 && value[7] == 0x80 && protocolStatus.inverterSnReceived == false) + { + for (uint8_t i = 9; i <= 22; i++) + { + data[i - 9] = value[i]; + } + solaxX1_SendInverterAddress(); + protocolStatus.status = 0b1100000; // inverterSnReceived and inverterAddressSend + DEBUG_SENSOR_LOG(PSTR("SX1: Set inverterSnReceived and inverterAddressSend")); + } + } + } + } // End data ready + + if (solaxX1_send_retry == 0) + { + if (protocolStatus.queryOfflineSend) + { + protocolStatus.status = 0b00001000; // queryOffline + DEBUG_SENSOR_LOG(PSTR("SX1: Set Query Offline")); + } + solaxX1_send_retry = 12; + } + + // request to the inverter the serial number if offline + if (protocolStatus.queryOffline) + { + // We sent the message to query inverters in offline status + source[0] = 0x01; + destination[1] = 0x00; + controlCode[0] = 0x10; + functionCode[0] = 0x00; + dataLength[0] = 0x00; + solaxX1_RS485Send(9); + protocolStatus.status = 0b00010000; // queryOfflineSend + DEBUG_SENSOR_LOG(PSTR("SX1: Query Offline Send")); + } + } // end !hasAddress && (data_ready || solaxX1_send_retry == 0) + + if (!data_ready) + solaxX1_send_retry--; +} + +void solaxX1SnsInit(void) +{ + AddLog_P(LOG_LEVEL_DEBUG, PSTR("SX1: Solax X1 Inverter Init")); + DEBUG_SENSOR_LOG(PSTR("SX1: RX pin: %d, TX pin: %d"), pin[GPIO_SOLAXX1_RX], pin[GPIO_SOLAXX1_TX]); + protocolStatus.status = 0b00100000; // hasAddress + + solaxX1Serial = new TasmotaSerial(pin[GPIO_SOLAXX1_RX], pin[GPIO_SOLAXX1_TX], 1); + if (solaxX1Serial->begin(SOLAXX1_SPEED)) + { + if (solaxX1Serial->hardwareSerial()) { ClaimSerial(); } + }else { + energy_flg = ENERGY_NONE; + } +} + +void solaxX1DrvInit(void) +{ + if ((pin[GPIO_SOLAXX1_RX] < 99) && (pin[GPIO_SOLAXX1_TX] < 99)) { + energy_flg = XNRG_12; + } +} + +#ifdef USE_WEBSERVER +const char HTTP_SNS_solaxX1_DATA1[] PROGMEM = + "{s}" D_SOLAX_X1 " " D_SOLAR_POWER "{m}%s " D_UNIT_WATT "{e}" + "{s}" D_SOLAX_X1 " " D_PV1_VOLTAGE "{m}%s " D_UNIT_VOLT "{e}" + "{s}" D_SOLAX_X1 " " D_PV1_CURRENT "{m}%s " D_UNIT_AMPERE "{e}" + "{s}" D_SOLAX_X1 " " D_PV1_POWER "{m}%s " D_UNIT_WATT "{e}"; +#ifdef SOLAXX1_PV2 +const char HTTP_SNS_solaxX1_DATA2[] PROGMEM = + "{s}" D_SOLAX_X1 " " D_PV2_VOLTAGE "{m}%s " D_UNIT_VOLT "{e}" + "{s}" D_SOLAX_X1 " " D_PV2_CURRENT "{m}%s " D_UNIT_AMPERE "{e}" + "{s}" D_SOLAX_X1 " " D_PV2_POWER "{m}%s " D_UNIT_WATT "{e}"; +#endif +const char HTTP_SNS_solaxX1_DATA3[] PROGMEM = + "{s}" D_SOLAX_X1 " " D_UPTIME "{m}%s " D_UNIT_HOUR "{e}" + "{s}" D_SOLAX_X1 " " D_STATUS "{m}%s" + "{s}" D_SOLAX_X1 " " D_ERROR "{m}%s"; +#endif // USE_WEBSERVER + +void solaxX1Show(bool json) +{ + char solar_power[33]; + dtostrfd(solaxX1.dc1_power + solaxX1.dc2_power, Settings.flag2.wattage_resolution, solar_power); + char pv1_voltage[33]; + dtostrfd(solaxX1.dc1_voltage, Settings.flag2.voltage_resolution, pv1_voltage); + char pv1_current[33]; + dtostrfd(solaxX1.dc1_current, Settings.flag2.current_resolution, pv1_current); + char pv1_power[33]; + dtostrfd(solaxX1.dc1_power, Settings.flag2.wattage_resolution, pv1_power); +#ifdef SOLAXX1_PV2 + char pv2_voltage[33]; + dtostrfd(solaxX1.dc2_voltage, Settings.flag2.voltage_resolution, pv2_voltage); + char pv2_current[33]; + dtostrfd(solaxX1.dc2_current, Settings.flag2.current_resolution, pv2_current); + char pv2_power[33]; + dtostrfd(solaxX1.dc2_power, Settings.flag2.wattage_resolution, pv2_power); +#endif + char temperature[33]; + dtostrfd(solaxX1.temperature, Settings.flag2.temperature_resolution, temperature); + char runtime[33]; + dtostrfd(solaxX1.runtime_total, 0, runtime); + char status[33]; + GetTextIndexed(status, sizeof(status), solaxX1.status, kSolaxMode); + + if (json) + { + ResponseAppend_P(PSTR(",\"" D_JSON_SOLAR_POWER "\":%s,\"" D_JSON_PV1_VOLTAGE "\":%s,\"" D_JSON_PV1_CURRENT "\":%s,\"" D_JSON_PV1_POWER "\":%s"), + solar_power, pv1_voltage, pv1_current, pv1_power); +#ifdef SOLAXX1_PV2 + ResponseAppend_P(PSTR(",\"" D_JSON_PV2_VOLTAGE "\":%s,\"" D_JSON_PV2_CURRENT "\":%s,\"" D_JSON_PV2_POWER "\":%s"), + pv2_voltage, pv2_current, pv2_power); +#endif + ResponseAppend_P(PSTR(",\"" D_JSON_TEMPERATURE "\":%s,\"" D_JSON_RUNTIME "\":%s,\"" D_JSON_STATUS "\":\"%s\",\"" D_JSON_ERROR "\":%d"), + temperature, runtime, status, solaxX1.errorCode); + +#ifdef USE_WEBSERVER + } + else + { + WSContentSend_PD(HTTP_SNS_solaxX1_DATA1, solar_power, pv1_voltage, pv1_current, pv1_power); +#ifdef SOLAXX1_PV2 + WSContentSend_PD(HTTP_SNS_solaxX1_DATA2, pv2_voltage, pv2_current, pv2_power); +#endif + WSContentSend_PD(HTTP_SNS_TEMP, D_SOLAX_X1, temperature, TempUnit()); + char errorCodeString[33]; + WSContentSend_PD(HTTP_SNS_solaxX1_DATA3, runtime, status, + GetTextIndexed(errorCodeString, sizeof(errorCodeString), solaxX1_ParseErrorCode(solaxX1.errorCode), kSolaxError)); +#endif // USE_WEBSERVER + } +} + +/*********************************************************************************************\ + * Interface +\*********************************************************************************************/ + +bool Xnrg12(uint8_t function) +{ + bool result = false; + + switch (function) + { + case FUNC_EVERY_250_MSECOND: + if (uptime > 4) { solaxX1250MSecond(); } + break; + case FUNC_INIT: + solaxX1SnsInit(); + break; + case FUNC_PRE_INIT: + solaxX1DrvInit(); + break; + case FUNC_JSON_APPEND: + solaxX1Show(1); + break; +#ifdef USE_WEBSERVER + case FUNC_WEB_SENSOR: + solaxX1Show(0); + break; +#endif // USE_WEBSERVER + } + return result; +} + +#endif // USE_SOLAX_X1_NRG +#endif // USE_ENERGY_SENSOR \ No newline at end of file diff --git a/sonoff/xsns_49_solaxX1.ino b/sonoff/xsns_49_solaxX1.ino index 72583484f..059017e49 100644 --- a/sonoff/xsns_49_solaxX1.ino +++ b/sonoff/xsns_49_solaxX1.ino @@ -94,30 +94,39 @@ TasmotaSerial *solaxX1Serial; uint8_t solaxX1_Init = 1; -uint8_t solaxX1_status = 0; -uint32_t solaxX1_errorCode = 0; +struct SOLAXX1 { + float temperature = 0; + float energy_today = 0; + float dc1_voltage = 0; + float dc2_voltage = 0; + float dc1_current = 0; + float dc2_current = 0; + float ac_current = 0; + float ac_voltage = 0; + float frequency = 0; + float power = 0; + float energy_total = 0; + float runtime_total = 0; + float dc1_power = 0; + float dc2_power = 0; -float solaxX1_temperature = 0; -float solaxX1_energy_today = 0; -float solaxX1_dc1_voltage = 0; -float solaxX1_dc2_voltage = 0; -float solaxX1_dc1_current = 0; -float solaxX1_dc2_current = 0; -float solaxX1_ac_current = 0; -float solaxX1_ac_voltage = 0; -float solaxX1_frequency = 0; -float solaxX1_power = 0; -float solaxX1_energy_total = 0; -float solaxX1_runtime_total = 0; + uint8_t status = 0; + uint32_t errorCode = 0; +} solaxX1; -float solaxX1_dc1_power = 0; -float solaxX1_dc2_power = 0; - -bool queryOffline = false; -bool queryOfflineSend = false; -bool hasAddress = true; -bool inverterAddressSend = false; -bool inverterSnReceived = false; +union { + uint8_t status; + struct { + uint8_t freeBit7:1; // Bit7 + uint8_t freeBit6:1; // Bit6 + uint8_t freeBit5:1; // Bit5 + uint8_t queryOffline:1; // Bit4 + uint8_t queryOfflineSend:1; // Bit3 + uint8_t hasAddress:1; // Bit2 + uint8_t inverterAddressSend:1; // Bit1 + uint8_t inverterSnReceived:1; // Bit0 + }; +} protocolStatus; uint8_t header[2] = {0xAA, 0x55}; uint8_t source[2] = {0x00, 0x00}; @@ -136,10 +145,16 @@ bool solaxX1_RS485ReceiveReady(void) return (solaxX1Serial->available() > 1); } -void solaxX1_RS485Send(uint8_t *msg, uint16_t msgLen) +void solaxX1_RS485Send(uint16_t msgLen) { - - uint16_t crc = solaxX1_calculateCRC(msg, msgLen - 1); // calculate out crc bytes + memcpy(message, header, 2); + memcpy(message + 2, source, 2); + memcpy(message + 4, destination, 2); + memcpy(message + 6, controlCode, 1); + memcpy(message + 7, functionCode, 1); + memcpy(message + 8, dataLength, 1); + memcpy(message + 9, data, sizeof(data)); + uint16_t crc = solaxX1_calculateCRC(message, msgLen); // calculate out crc bytes while (solaxX1Serial->available() > 0) { // read serial if any old data is available @@ -147,9 +162,10 @@ void solaxX1_RS485Send(uint8_t *msg, uint16_t msgLen) } solaxX1Serial->flush(); - solaxX1Serial->write(msg, msgLen); + solaxX1Serial->write(message, msgLen); solaxX1Serial->write(highByte(crc)); solaxX1Serial->write(lowByte(crc)); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, message, msgLen); } uint8_t solaxX1_RS485Receive(uint8_t *value) @@ -161,7 +177,8 @@ uint8_t solaxX1_RS485Receive(uint8_t *value) value[len++] = (uint8_t)solaxX1Serial->read(); } - uint16_t crc = solaxX1_calculateCRC(value, len - 3); // calculate out crc bytes + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, value, len); + uint16_t crc = solaxX1_calculateCRC(value, len - 2); // calculate out crc bytes if (value[len - 1] == lowByte(crc) && value[len - 2] == highByte(crc)) { // check calc crc with received crc @@ -179,24 +196,13 @@ uint16_t solaxX1_calculateCRC(uint8_t *bExternTxPackage, uint8_t bLen) uint16_t wChkSum; wChkSum = 0; - for (i = 0; i <= bLen; i++) + for (i = 0; i < bLen; i++) { wChkSum = wChkSum + bExternTxPackage[i]; } return wChkSum; } -void solaxX1_setMessage(uint8_t *message) -{ - memcpy(message, header, 2); - memcpy(message + 2, source, 2); - memcpy(message + 4, destination, 2); - memcpy(message + 6, controlCode, 1); - memcpy(message + 7, functionCode, 1); - memcpy(message + 8, dataLength, 1); - memcpy(message + 9, data, sizeof(data)); -} - void solaxX1_SendInverterAddress() { source[0] = 0x00; @@ -205,12 +211,8 @@ void solaxX1_SendInverterAddress() controlCode[0] = 0x10; functionCode[0] = 0x01; dataLength[0] = 0x0F; - - // Inverter Address, It must be unique in case of more inverters in the same rs485 net. - data[14] = INVERTER_ADDRESS; - - solaxX1_setMessage(message); - solaxX1_RS485Send(message, 24); + data[14] = INVERTER_ADDRESS; // Inverter Address, It must be unique in case of more inverters in the same rs485 net. + solaxX1_RS485Send(24); } void solaxX1_QueryLiveData() @@ -221,9 +223,7 @@ void solaxX1_QueryLiveData() controlCode[0] = 0x11; functionCode[0] = 0x02; dataLength[0] = 0x00; - - solaxX1_setMessage(message); - solaxX1_RS485Send(message, 9); + solaxX1_RS485Send(9); } uint8_t solaxX1_ParseErrorCode(uint32_t code){ @@ -245,22 +245,95 @@ uint8_t solaxX1_ParseErrorCode(uint32_t code){ uint8_t solaxX1_send_retry = 0; uint8_t solaxX1_nodata_count = 0; -void solaxX1_Update(void) // Every Second +void solaxX1EverySecond(void) // Every Second { uint8_t value[61] = {0}; - bool data_ready = solaxX1_RS485ReceiveReady(); - DEBUG_SENSOR_LOG(PSTR("SX1: queryOffline: %d , queryOfflineSend: %d, hasAddress: %d, inverterAddressSend: %d, solaxX1_send_retry: %d"), - queryOffline, queryOfflineSend, hasAddress, inverterAddressSend, solaxX1_send_retry); + if (protocolStatus.hasAddress && (data_ready || solaxX1_send_retry == 0)) + { + if (data_ready) + { + uint8_t error = solaxX1_RS485Receive(value); + if (error) + { + DEBUG_SENSOR_LOG(PSTR("SX1: Data response CRC error")); + } + else + { + solaxX1_nodata_count = 0; + solaxX1_send_retry = 3; - if (!hasAddress && (data_ready || solaxX1_send_retry == 0)) + solaxX1.temperature = (float)((value[9] << 8) | value[10]); // Temperature + solaxX1.energy_today = (float)((value[11] << 8) | value[12]) * 0.1f; // Energy Today + solaxX1.dc1_voltage = (float)((value[13] << 8) | value[14]) * 0.1f; // PV1 Voltage + solaxX1.dc2_voltage = (float)((value[15] << 8) | value[16]) * 0.1f; // PV2 Voltage + solaxX1.dc1_current = (float)((value[17] << 8) | value[18]) * 0.1f; // PV1 Current + solaxX1.dc2_current = (float)((value[19] << 8) | value[20]) * 0.1f; // PV2 Current + solaxX1.ac_current = (float)((value[21] << 8) | value[22]) * 0.1f; // AC Current + solaxX1.ac_voltage = (float)((value[23] << 8) | value[24]) * 0.1f; // AC Voltage + solaxX1.frequency = (float)((value[25] << 8) | value[26]) * 0.01f; // AC Frequency + solaxX1.power = (float)((value[27] << 8) | value[28]); // AC Power + //temporal = (float)((value[29] << 8) | value[30]) * 0.1f; // Not Used + solaxX1.energy_total = (float)((value[31] << 8) | (value[32] << 8) | (value[33] << 8) | value[34]) * 0.1f; // Energy Total + solaxX1.runtime_total = (float)((value[35] << 8) | (value[36] << 8) | (value[37] << 8) | value[38]); // Work Time Total + solaxX1.status = (uint8_t)((value[39] << 8) | value[40]); // Work mode + //temporal = (float)((value[41] << 8) | value[42]); // Grid voltage fault value 0.1V + //temporal = (float)((value[43] << 8) | value[44]); // Gird frequency fault value 0.01Hz + //temporal = (float)((value[45] << 8) | value[46]); // Dc injection fault value 1mA + //temporal = (float)((value[47] << 8) | value[48]); // Temperature fault value + //temporal = (float)((value[49] << 8) | value[50]); // Pv1 voltage fault value 0.1V + //temporal = (float)((value[51] << 8) | value[52]); // Pv2 voltage fault value 0.1V + //temporal = (float)((value[53] << 8) | value[54]); // GFC fault value + solaxX1.errorCode = (uint32_t)((value[58] << 8) | (value[57] << 8) | (value[56] << 8) | value[55]); // Error Code + + solaxX1.dc1_power = solaxX1.dc1_voltage * solaxX1.dc1_current; + solaxX1.dc2_power = solaxX1.dc2_voltage * solaxX1.dc2_current; + + solaxX1_QueryLiveData(); + } + } // End data Ready + + if (0 == solaxX1_send_retry && 255 != solaxX1_nodata_count) { + solaxX1_send_retry = 3; + solaxX1_QueryLiveData(); + } + + // While the inverter has not stable ambient light, will send an address adquired but go offline again, + // so no data will be received when the query is send, then we start the countdown to set the inverter as offline again. + if (255 == solaxX1_nodata_count) { + solaxX1_nodata_count = 0; + solaxX1_send_retry = 3; + } + } // end hasAddress && (data_ready || solaxX1_send_retry == 0) + else { + DEBUG_SENSOR_LOG(PSTR("SX1: No Data count: %d"), solaxX1_nodata_count); + + if (solaxX1_nodata_count < 10) // max. seconds without data + { + solaxX1_nodata_count++; + } + else if (255 != solaxX1_nodata_count) + { + // no data from RS485, reset values to 0 and set inverter as offline + solaxX1_nodata_count = 255; + solaxX1_send_retry = 3; + protocolStatus.status = 0b00001000; // queryOffline + + solaxX1.temperature = solaxX1.dc1_voltage = solaxX1.dc2_voltage = solaxX1.dc1_current = solaxX1.dc2_current = solaxX1.ac_current = 0; + solaxX1.ac_voltage = solaxX1.frequency = solaxX1.power = solaxX1.dc1_power = solaxX1.dc2_power = solaxX1.status = 0; + //solaxX1.energy_today = solaxX1.energy_total = solaxX1.runtime_total = 0; + } + } + + if (!protocolStatus.hasAddress && (data_ready || solaxX1_send_retry == 0)) + { if (data_ready) { // check address confirmation from inverter - if (inverterAddressSend) + if (protocolStatus.inverterAddressSend) { uint8_t error = solaxX1_RS485Receive(value); if (error) @@ -271,15 +344,14 @@ void solaxX1_Update(void) // Every Second { if (value[6] == 0x10 && value[7] == 0x81 && value[9] == 0x06) { - inverterAddressSend = false; - queryOfflineSend = false; - hasAddress = true; + DEBUG_SENSOR_LOG(PSTR("SX1: Set hasAddress")); + protocolStatus.status = 0b00100000; // hasAddress } } } // Check inverter serial number and send the set address request - if (queryOfflineSend) + if (protocolStatus.queryOfflineSend) { uint8_t error = solaxX1_RS485Receive(value); if (error) @@ -289,26 +361,32 @@ void solaxX1_Update(void) // Every Second else { // Serial number from query response - if (value[6] == 0x10 && value[7] == 0x80 && inverterSnReceived == false) + if (value[6] == 0x10 && value[7] == 0x80 && protocolStatus.inverterSnReceived == false) { for (uint8_t i = 9; i <= 22; i++) { data[i - 9] = value[i]; } - inverterSnReceived = true; + solaxX1_SendInverterAddress(); + protocolStatus.status = 0b1100000; // inverterSnReceived and inverterAddressSend + DEBUG_SENSOR_LOG(PSTR("SX1: Set inverterSnReceived and inverterAddressSend")); } - - solaxX1_SendInverterAddress(); - - inverterAddressSend = true; - queryOfflineSend = false; - queryOffline = false; } } + } // End data ready + + if (solaxX1_send_retry == 0) + { + if (protocolStatus.queryOfflineSend) + { + protocolStatus.status = 0b00001000; // queryOffline + DEBUG_SENSOR_LOG(PSTR("SX1: Set Query Offline")); + } + solaxX1_send_retry = 3; } // request to the inverter the serial number if offline - if (queryOffline) + if (protocolStatus.queryOffline) { // We sent the message to query inverters in offline status source[0] = 0x01; @@ -316,139 +394,12 @@ void solaxX1_Update(void) // Every Second controlCode[0] = 0x10; functionCode[0] = 0x00; dataLength[0] = 0x00; - - solaxX1_setMessage(message); - solaxX1_RS485Send(message, 9); - - queryOfflineSend = true; - queryOffline = false; + solaxX1_RS485Send(9); + protocolStatus.status = 0b00010000; // queryOfflineSend + DEBUG_SENSOR_LOG(PSTR("SX1: Query Offline Send")); } - - if (solaxX1_send_retry == 0) - { - - if (inverterAddressSend) - { - solaxX1_SendInverterAddress(); - } - if (queryOfflineSend) - { - queryOffline = true; - queryOfflineSend = false; - } - solaxX1_send_retry = 2; - } - } // end !hasAddress && (data_ready || solaxX1_send_retry == 0) - if (hasAddress && (data_ready || solaxX1_send_retry == 0)) - { - - if (data_ready) - { - uint8_t error = solaxX1_RS485Receive(value); - if (error) - { - DEBUG_SENSOR_LOG(PSTR("SX1: Data response CRC error")); - } - else - { -// AddLogBuffer(LOG_LEVEL_DEBUG, value, sizeof(value)); - - solaxX1_nodata_count = 0; - solaxX1_send_retry = 2; - uint32_t temporal = 0; - - temporal = (value[9] << 8) | value[10]; // Temperature - solaxX1_temperature = temporal; - - temporal = (value[11] << 8) | value[12]; // Energy Today - solaxX1_energy_today = temporal * 0.1f; - - temporal = (value[13] << 8) | value[14]; // PV1 Voltage - solaxX1_dc1_voltage = temporal * 0.1f; - - temporal = (value[15] << 8) | value[16]; // PV2 Voltage - solaxX1_dc2_voltage = temporal * 0.1f; - - temporal = (value[17] << 8) | value[18]; // PV1 Current - solaxX1_dc1_current = temporal * 0.1f; - - temporal = (value[19] << 8) | value[20]; // PV2 Current - solaxX1_dc2_current = temporal * 0.1f; - - temporal = (value[21] << 8) | value[22]; // AC Current - solaxX1_ac_current = temporal * 0.1f; - - temporal = (value[23] << 8) | value[24]; // AC Voltage - solaxX1_ac_voltage = temporal * 0.1f; - - temporal = (value[25] << 8) | value[26]; // AC Frequency - solaxX1_frequency = temporal * 0.01f; - - temporal = (value[27] << 8) | value[28]; // AC Power - solaxX1_power = temporal; - - //temporal = (value[29] << 8) | value[30]; // Not Used - //solaxX1_notused = temporal * 0.1f; - - temporal = (value[31] << 8) | (value[32] << 8) | (value[33] << 8) | value[34]; // Energy Total - solaxX1_energy_total = temporal * 0.1f; - - temporal = (value[35] << 8) | (value[36] << 8) | (value[37] << 8) | value[38]; // Work Time Total - solaxX1_runtime_total = temporal; - - temporal = (value[39] << 8) | value[40]; // Work mode - solaxX1_status = (uint8_t)temporal; - - //temporal = (value[41] << 8) | value[42]; // Grid voltage fault value 0.1V - //temporal = (value[43] << 8) | value[44]; // Gird frequency fault value 0.01Hz - //temporal = (value[45] << 8) | value[46]; // Dc injection fault value 1mA - //temporal = (value[47] << 8) | value[48]; // Temperature fault value - //temporal = (value[49] << 8) | value[50]; // Pv1 voltage fault value 0.1V - //temporal = (value[51] << 8) | value[52]; // Pv2 voltage fault value 0.1V - //temporal = (value[53] << 8) | value[54]; // GFC fault value - - temporal = (value[58] << 8) | (value[57] << 8) | (value[56] << 8) | value[55]; // Error Code - solaxX1_errorCode = (uint32_t)temporal; - - solaxX1_dc1_power = solaxX1_dc1_voltage * solaxX1_dc1_current; - solaxX1_dc2_power = solaxX1_dc2_voltage * solaxX1_dc2_current; - - solaxX1_QueryLiveData(); - } // end else no error - } - - if (solaxX1_send_retry == 0) - { - solaxX1_send_retry = 2; - solaxX1_QueryLiveData(); - } - } - else - { // end hasAddress && (data_ready || solaxX1_send_retry == 0) - - if (solaxX1_nodata_count <= 10) // max. 10 sec without data - { - solaxX1_nodata_count++; - } - else if (solaxX1_nodata_count != 255) - { - // no data from RS485, reset values to 0 - solaxX1_nodata_count = 255; - queryOffline = true; - queryOfflineSend = false; - hasAddress = false; - inverterAddressSend = false; - inverterSnReceived = false; - - solaxX1_temperature = solaxX1_dc1_voltage = solaxX1_dc2_voltage = solaxX1_dc1_current = solaxX1_dc2_current = solaxX1_ac_current = 0; - solaxX1_ac_voltage = solaxX1_frequency = solaxX1_power = solaxX1_dc1_power = solaxX1_dc2_power = solaxX1_status = 0; - - //solaxX1_energy_today = solaxX1_energy_total = solaxX1_runtime_total = 0; - } - } - if (!data_ready) solaxX1_send_retry--; } @@ -458,6 +409,8 @@ void solaxX1Init(void) AddLog_P(LOG_LEVEL_DEBUG, PSTR("Solax X1 Inverter Init")); DEBUG_SENSOR_LOG(PSTR("SX1: RX pin: %d, TX pin: %d"), pin[GPIO_SOLAXX1_RX], pin[GPIO_SOLAXX1_TX]); solaxX1_Init = 0; + protocolStatus.status = 0b00100000; // hasAddress + if ((pin[GPIO_SOLAXX1_RX] < 99) && (pin[GPIO_SOLAXX1_TX] < 99)) { solaxX1Serial = new TasmotaSerial(pin[GPIO_SOLAXX1_RX], pin[GPIO_SOLAXX1_TX], 1); @@ -499,39 +452,39 @@ const char HTTP_SNS_solaxX1_DATA3[] PROGMEM = void solaxX1Show(bool json) { char voltage[33]; - dtostrfd(solaxX1_ac_voltage, Settings.flag2.voltage_resolution, voltage); + dtostrfd(solaxX1.ac_voltage, Settings.flag2.voltage_resolution, voltage); char current[33]; - dtostrfd(solaxX1_ac_current, Settings.flag2.current_resolution, current); + dtostrfd(solaxX1.ac_current, Settings.flag2.current_resolution, current); char inverter_power[33]; - dtostrfd(solaxX1_power, Settings.flag2.wattage_resolution, inverter_power); + dtostrfd(solaxX1.power, Settings.flag2.wattage_resolution, inverter_power); char solar_power[33]; - dtostrfd(solaxX1_dc1_power + solaxX1_dc2_power, Settings.flag2.wattage_resolution, solar_power); + dtostrfd(solaxX1.dc1_power + solaxX1.dc2_power, Settings.flag2.wattage_resolution, solar_power); char frequency[33]; - dtostrfd(solaxX1_frequency, Settings.flag2.frequency_resolution, frequency); + dtostrfd(solaxX1.frequency, Settings.flag2.frequency_resolution, frequency); char energy_total[33]; - dtostrfd(solaxX1_energy_total, Settings.flag2.energy_resolution, energy_total); + dtostrfd(solaxX1.energy_total, Settings.flag2.energy_resolution, energy_total); char energy_today[33]; - dtostrfd(solaxX1_energy_today, Settings.flag2.energy_resolution, energy_today); + dtostrfd(solaxX1.energy_today, Settings.flag2.energy_resolution, energy_today); char pv1_voltage[33]; - dtostrfd(solaxX1_dc1_voltage, Settings.flag2.voltage_resolution, pv1_voltage); + dtostrfd(solaxX1.dc1_voltage, Settings.flag2.voltage_resolution, pv1_voltage); char pv1_current[33]; - dtostrfd(solaxX1_dc1_current, Settings.flag2.current_resolution, pv1_current); + dtostrfd(solaxX1.dc1_current, Settings.flag2.current_resolution, pv1_current); char pv1_power[33]; - dtostrfd(solaxX1_dc1_power, Settings.flag2.wattage_resolution, pv1_power); + dtostrfd(solaxX1.dc1_power, Settings.flag2.wattage_resolution, pv1_power); #ifdef SOLAXX1_PV2 char pv2_voltage[33]; - dtostrfd(solaxX1_dc2_voltage, Settings.flag2.voltage_resolution, pv2_voltage); + dtostrfd(solaxX1.dc2_voltage, Settings.flag2.voltage_resolution, pv2_voltage); char pv2_current[33]; - dtostrfd(solaxX1_dc2_current, Settings.flag2.current_resolution, pv2_current); + dtostrfd(solaxX1.dc2_current, Settings.flag2.current_resolution, pv2_current); char pv2_power[33]; - dtostrfd(solaxX1_dc2_power, Settings.flag2.wattage_resolution, pv2_power); + dtostrfd(solaxX1.dc2_power, Settings.flag2.wattage_resolution, pv2_power); #endif char temperature[33]; - dtostrfd(solaxX1_temperature, Settings.flag2.temperature_resolution, temperature); + dtostrfd(solaxX1.temperature, Settings.flag2.temperature_resolution, temperature); char runtime[33]; - dtostrfd(solaxX1_runtime_total, 0, runtime); + dtostrfd(solaxX1.runtime_total, 0, runtime); char status[33]; - GetTextIndexed(status, sizeof(status), solaxX1_status, kSolaxMode); + GetTextIndexed(status, sizeof(status), solaxX1.status, kSolaxMode); if (json) { @@ -546,20 +499,20 @@ void solaxX1Show(bool json) pv2_voltage, pv2_current, pv2_power); #endif ResponseAppend_P(PSTR(",\"" D_JSON_TEMPERATURE "\":%s,\"" D_JSON_RUNTIME "\":%s,\"" D_JSON_STATUS "\":\"%s\",\"" D_JSON_ERROR "\":%d}"), - temperature, runtime, status, solaxX1_errorCode); + temperature, runtime, status, solaxX1.errorCode); #ifdef USE_DOMOTICZ if (0 == tele_period) { char energy_total_chr[33]; - dtostrfd(solaxX1_energy_total * 1000, 1, energy_total_chr); + dtostrfd(solaxX1.energy_total * 1000, 1, energy_total_chr); DomoticzSensor(DZ_VOLTAGE, voltage); DomoticzSensor(DZ_CURRENT, current); // Only do the updates if the values are greater than 0, to avoid wrong data representation in domoticz - if (solaxX1_temperature > 0) DomoticzSensor(DZ_TEMP, temperature); - if (solaxX1_energy_total > 0) DomoticzSensorPowerEnergy((int)solaxX1_power, energy_total_chr); + if (solaxX1.temperature > 0) DomoticzSensor(DZ_TEMP, temperature); + if (solaxX1.energy_total > 0) DomoticzSensorPowerEnergy((int)solaxX1.power, energy_total_chr); } #endif // USE_DOMOTICZ #ifdef USE_WEBSERVER @@ -573,7 +526,7 @@ void solaxX1Show(bool json) WSContentSend_PD(HTTP_SNS_TEMP, D_SOLAX_X1, temperature, TempUnit()); char errorCodeString[33]; WSContentSend_PD(HTTP_SNS_solaxX1_DATA3, runtime, status, - GetTextIndexed(errorCodeString, sizeof(errorCodeString), solaxX1_ParseErrorCode(solaxX1_errorCode), kSolaxError)); + GetTextIndexed(errorCodeString, sizeof(errorCodeString), solaxX1_ParseErrorCode(solaxX1.errorCode), kSolaxError)); #endif // USE_WEBSERVER } } @@ -594,7 +547,7 @@ bool Xsns49(uint8_t function) solaxX1Init(); break; case FUNC_EVERY_SECOND: - solaxX1_Update(); + if (uptime > 4) { solaxX1EverySecond(); } break; case FUNC_JSON_APPEND: solaxX1Show(1);