diff --git a/myProfile.json b/myProfile.json index 7e303157..9044ff30 100644 --- a/myProfile.json +++ b/myProfile.json @@ -242,6 +242,10 @@ "path": "src/modules/sensors/Emon", "active": false }, + { + "path": "src/modules/sensors/EnergyMon485", + "active": true + }, { "path": "src/modules/sensors/ExampleModule", "active": false diff --git a/src/modules/sensors/EnergyMon485/Energy485Header.h b/src/modules/sensors/EnergyMon485/Energy485Header.h new file mode 100644 index 00000000..c2349103 --- /dev/null +++ b/src/modules/sensors/EnergyMon485/Energy485Header.h @@ -0,0 +1,48 @@ +#pragma once + +namespace _Electro485 +{ + + unsigned char mass_to_send[16]; + unsigned char mass_read[16]; + unsigned short int checkSum; + + unsigned char auchCRCLo[] = { + 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, + 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, + 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, + 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, + 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, + 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, + 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, + 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, + 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, + 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, + 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, + 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, + 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, + 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, + 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, + 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, + 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, + 0x40}; + unsigned char auchCRCHi[] = { + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, + 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, + 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, + 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, + 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, + 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, + 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, + 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, + 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, + 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, + 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, + 0x40}; +} \ No newline at end of file diff --git a/src/modules/sensors/EnergyMon485/EnergyMon485.cpp b/src/modules/sensors/EnergyMon485/EnergyMon485.cpp new file mode 100644 index 00000000..c1fbe9b9 --- /dev/null +++ b/src/modules/sensors/EnergyMon485/EnergyMon485.cpp @@ -0,0 +1,340 @@ + +#include "Global.h" +#include "classes/IoTItem.h" + +#include "classes/IoTUart.h" +#include "Energy485Header.h" + +// PZEMContainer _pzemCntr; +Stream *_myUART_Gran = nullptr; + +namespace _Electro485 +{ + + unsigned short int sdm120_mbCRC16(unsigned char *puchMsg, unsigned char usDataLen) + { + unsigned char uchCRCHi = 0xFF; /* инициализация старшего байта контрольной суммы */ + unsigned char uchCRCLo = 0xFF; /* инициализация младшего байта контрольной суммы */ + unsigned char uIndex; + + uchCRCHi = 0xFF; + uchCRCLo = 0xFF; + + while (usDataLen--) + { + uIndex = uchCRCHi ^ *puchMsg++; + uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex]; + uchCRCLo = auchCRCLo[uIndex]; + } + + return (uchCRCLo << 8 | uchCRCHi); + } + + unsigned short int gran485_mbCRC16(unsigned char *puchMsg, unsigned char usDataLen) + { + unsigned char uchCRCHi = 0xFF; /* инициализация старшего байта контрольной суммы */ + unsigned char uchCRCLo = 0xFF; /* инициализация младшего байта контрольной суммы */ + unsigned char uIndex; + + uchCRCHi = 0xFF; + uchCRCLo = 0xFF; + + while (usDataLen--) + { + uIndex = (uchCRCHi ^ *puchMsg++) & 0xFF; + uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex]; + uchCRCLo = auchCRCLo[uIndex]; + } + + return (uchCRCHi << 8 | uchCRCLo); + } + + void uart485_send(unsigned char *str, unsigned char bytes_to_send) + { + // delay(20); + if (_myUART_Gran) + { + for (int i = 0; i < bytes_to_send; i++) + { + _myUART_Gran->write(str[i]); + } + } + // delay(100); + } + + int8_t gran485_read(byte addr, String ¶m, float &res) + { + int8_t ret; + if (!_myUART_Gran) + return 3; + unsigned short int param_hex; + mass_to_send[4] = 0x00; + if (param == "v") + param_hex = 0x0A00; + else if (param == "a") + param_hex = 0x0B00; + else if (param == "w") + param_hex = 0x0800; + else if (param == "r") + param_hex = 0x0900; + else if (param == "f") + param_hex = 0x0D00; + else if (param == "k") + { + param_hex = 0x0100; + mass_to_send[4] = 0x01; + } + else if (param == "p") + { + param_hex = 0x0C00; + } + SerialPrint("i", "Gran", "param: " + param + ", param_hex: " + String(param_hex, HEX)); + mass_to_send[0] = addr; + mass_to_send[1] = 0x03; + mass_to_send[2] = param_hex >> 8; + mass_to_send[3] = param_hex; + + mass_to_send[5] = 0x01; + checkSum = gran485_mbCRC16(mass_to_send, 6); + mass_to_send[6] = checkSum; + mass_to_send[7] = checkSum >> 8; + uart485_send(mass_to_send, 8); + + // Считываем первые 3 байта из ответа + int s = _myUART_Gran->readBytes(mass_read, 10); + // uart_send(mass_read, 3); + // Serial.println("Count read byte: " + String(s)); + + // Если вернулся правильный адрес и команда + if (mass_read[0] == addr && mass_read[1] == 0x03) + { + // Проверяем контрольную сумму + checkSum = 0; + checkSum = gran485_mbCRC16(mass_read, 8); + /* + Serial.print("HEX: "); + Serial.print(String(mass_read[4], HEX)); + Serial.print(String(mass_read[5], HEX)); + Serial.print(String(mass_read[6], HEX)); + Serial.println(String(mass_read[7], HEX)); + */ + uint32_t x = mass_read[7] << 24 | mass_read[6] << 16 | mass_read[5] << 8 | mass_read[4]; + float y = *(float *)&x; + Serial.println(String(y)); + if ((byte)checkSum == mass_read[8] && (checkSum >> 8) == mass_read[9]) + { + res = y; + ret = 0; + } + else + { + // Serial.println("ERROR_CRC"); + ret = 1; + } + } + else + { + // Serial.println("ERROR_NO_RESPONSE"); + ret = 2; + } + + // очистка массива + mass_read[0] = 0; + mass_read[1] = 0; + mass_read[2] = 0; + mass_read[3] = 0; + mass_read[4] = 0; + mass_read[5] = 0; + mass_read[6] = 0; + // очистка буфера serial + while (_myUART_Gran->available()) + _myUART_Gran->read(); + return ret; + } + + int8_t sdm120_read(byte addr, String ¶m, float &res) + { + int8_t ret; + if (!_myUART_Gran) + return 3; + unsigned short int param_hex; + if (param == "v") + param_hex = 0x0000; + else if (param == "a") + param_hex = 0x0006; + else if (param == "w") + param_hex = 0x000C; + else if (param == "r") + param_hex = 0x0018; + else if (param == "f") + param_hex = 0x0046; + else if (param == "k") + param_hex = 0x0156; + else if (param == "p") + param_hex = 0x001E; + SerialPrint("i", "SMD120", "param: " + param + ", param_hex: " + String(param_hex, HEX)); + + mass_to_send[0] = addr; + mass_to_send[1] = 0x04; + mass_to_send[2] = param_hex >> 8; + mass_to_send[3] = param_hex; + mass_to_send[4] = 0x00; + mass_to_send[5] = 0x02; + checkSum = sdm120_mbCRC16(mass_to_send, 6); + mass_to_send[6] = checkSum; + mass_to_send[7] = checkSum >> 8; + uart485_send(mass_to_send, 8); + + // Считываем первые 3 байта из ответа + _myUART_Gran->readBytes(mass_read, 3); + // uart_send(mass_read, 3); + + // Если вернулся правильный адрес и команда + if (mass_read[0] == addr && mass_read[1] == 0x04) + { + // то считываем данные (их количество было в тетьем байте) +2 байта на контрольную сумму +3 байта чтобы не затереть начало массива + for (int i = 3; i < mass_read[2] + 5; i++) + { + mass_read[i] = _myUART_Gran->read(); + } + + // Проверяем контрольную сумму + checkSum = 0; + checkSum = sdm120_mbCRC16(mass_read, mass_read[2] + 3); + if ((byte)checkSum == mass_read[mass_read[2] + 3] && (checkSum >> 8) == mass_read[mass_read[2] + 4]) + { + // преобразуем результат во float + + uint32_t x = mass_read[3] << 24 | mass_read[4] << 16 | mass_read[5] << 8 | mass_read[6]; + float y = *(float *)&x; + res = y; + + ret = 0; + } + else + { + // Serial.println("ERROR_CRC"); + ret = 1; + } + } + else + { + // Serial.println("ERROR_NO_RESPONSE"); + ret = 2; + } + + // очистка массива + mass_read[0] = 0; + mass_read[1] = 0; + mass_read[2] = 0; + mass_read[3] = 0; + mass_read[4] = 0; + mass_read[5] = 0; + mass_read[6] = 0; + + // очистка буфера serial + while (_myUART_Gran->available()) + _myUART_Gran->read(); + return ret; + } + + class GranItem : public IoTItem + { + private: + String _sensor; + + public: + GranItem(String parameters) : IoTItem(parameters) + { + _sensor = jsonReadStr(parameters, "sensor"); + } + + void doByInterval() + { + + byte addr = 00; + float val = 0; + int8_t result = gran485_read(addr, _sensor, val); + + if (result == 0) // OK + regEvent(val, "Gran"); + else + { + regEvent(NAN, "Gran"); + if (result == 1) // ERROR_CRC + SerialPrint("E", "Gran", "ERROR_CRC", _id); + else if (result == 2) // ERROR_NO_RESPONSE + SerialPrint("E", "Gran", "ERROR_NO_RESPONSE", _id); + else if (result == 3) // (!_myUART_Gran) + SerialPrint("E", "Gran", "gran_uart not found", _id); + } + } + + ~GranItem(){}; + }; + + class SDM120Item : public IoTItem + { + private: + String _sensor; + + public: + SDM120Item(String parameters) : IoTItem(parameters) + { + _sensor = jsonReadStr(parameters, "sensor"); + } + + void doByInterval() + { + + byte addr = 00; + float val = 0; + int8_t result = sdm120_read(addr, _sensor, val); + + if (result == 0) // OK + regEvent(val, "SDM120"); + else + { + regEvent(NAN, "SDM120"); + if (result == 1) // ERROR_CRC + SerialPrint("E", "SDM120", "ERROR_CRC", _id); + else if (result == 2) // ERROR_NO_RESPONSE + SerialPrint("E", "SDM120", "ERROR_NO_RESPONSE", _id); + else if (result == 3) // (!_myUART_Gran) + SerialPrint("E", "SDM120", "gran_uart not found", _id); + } + } + + ~SDM120Item(){}; + }; + + class EnergyUART : public IoTUart + { + public: + EnergyUART(String parameters) : IoTUart(parameters) + { + _myUART_Gran = _myUART; + } + + ~EnergyUART(){}; + }; +} +void *getAPI_EnergyMon485(String subtype, String param) +{ + if (subtype == F("gran485")) + { + return new _Electro485::GranItem(param); + } + else if (subtype == F("sdm120")) + { + return new _Electro485::SDM120Item(param); + } + else if (subtype == F("energy_uart")) + { + return new _Electro485::EnergyUART(param); + } + else + { + return nullptr; + } +} diff --git a/src/modules/sensors/EnergyMon485/modinfo.json b/src/modules/sensors/EnergyMon485/modinfo.json new file mode 100644 index 00000000..233bfabc --- /dev/null +++ b/src/modules/sensors/EnergyMon485/modinfo.json @@ -0,0 +1,83 @@ +{ + "menuSection": "sensors", + "configItem": [ + { + "global": 0, + "name": "Gran данные", + "type": "Reading", + "subtype": "gran485", + "id": "gran", + "widget": "anydataVlt", + "page": "Гран-Электро", + "descr": "Потребление", + "sensor": "k", + "int": 15, + "round": 1 + }, + { + "global": 0, + "name": "SDM120 данные", + "type": "Reading", + "subtype": "sdm120", + "id": "sdm120", + "widget": "anydataVlt", + "page": "Счётчик SDM120", + "descr": "Потребление", + "sensor": "k", + "int": 15, + "round": 1 + }, + { + "global": 0, + "name": "Energy UART", + "type": "Reading", + "subtype": "energy_uart", + "id": "enrg_uart", + "widget": "nil", + "page": "", + "descr": "", + "tx": 17, + "rx": 16, + "line": 2, + "speed": 9600 + } + ], + "about": { + "authorName": "Bubnov Mikhail", + "authorContact": "https://t.me/Mit4bmw", + "authorGit": "https://github.com/Mit4el", + "specialThanks": "", + "moduleName": "EnergyMon485", + "moduleVersion": "1.0", + "usedRam": { + "esp32_4mb": 15, + "esp8266_4mb": 15 + }, + "title": "Счетчик электроэнергии Гран-Электро или SDM120", + "moduleDesc": "Счетчик электроэнергии Гран-Электро (Беларусь) или SDM120 (Китай) с интерфейсом rs-485. Energy UART - обязателен, настройки UART интерфейса модуля", + "propInfo": { + "tx": "TX пин", + "rx": "RX пин", + "speed": "Скорость UART", + "line": "Актуально только для ESP32: номер линии hardUART. =2 rx=16 tx=17, для SoftwarwSerial в ESP32 line = -1", + "sensor": "Тип данных: v - напряжение, a - ток, w - активная мощность, r - реактивная мощность, f - частота, k - общее потребление, p - косинус фи", + "int": "Количество секунд между опросами датчика. Желателно устанавливать одинаковые интервалы для параметров (для одного адреса Pzem) что опрос происходил один раз, остальные из 1000мс буфера." + } + }, + "defActive": false, + "usedLibs": { + "esp32*": [], + "esp82*": [], + "esp32_4mb": [], + "esp32_4mb3f": [], + "esp32cam_4mb": [], + "esp32c3m_4mb": [], + "esp8266_4mb": [], + "esp8266_1mb": [], + "esp8266_1mb_ota": [], + "esp8285_1mb": [], + "esp8285_1mb_ota": [], + "esp8266_2mb": [], + "esp8266_2mb_ota": [] + } +} \ No newline at end of file