diff --git a/src/modules/sensors/Ds2423/Ds2423.cpp b/src/modules/sensors/Ds2423/Ds2423.cpp new file mode 100644 index 00000000..1ec384f7 --- /dev/null +++ b/src/modules/sensors/Ds2423/Ds2423.cpp @@ -0,0 +1,178 @@ + +#include "Global.h" +#include "classes/IoTItem.h" + +#include +#include +#include + +typedef uint8_t DeviceAddressDS2423[8]; +// глобальные списки необходимы для хранения объектов об активных линиях 1-wire используемых разными датчиками из модуля. Ключ - номер пина +std::map oneWireDS2423Array; + +// Функция инициализации библиотечного класса, возвращает Единстрвенный указать на библиотеку +OneWire *instanceOneWire_DS2423(uint8_t ONE_WIRE_PIN) +{ + // учитываем, что библиотека может работать с несколькими линиями на разных пинах, поэтому инициируем библиотеку, если линия ранее не использовалась + if (oneWireDS2423Array.find(ONE_WIRE_PIN) == oneWireDS2423Array.end()) + oneWireDS2423Array[ONE_WIRE_PIN] = new OneWire((uint8_t)ONE_WIRE_PIN); + return oneWireDS2423Array[ONE_WIRE_PIN]; +} + +// Определяем адрес. +bool getDeviceAddressDS2423(uint8_t pin, uint8_t *deviceAddress, int index) +{ + OneWire *_wire = instanceOneWire_DS2423(pin); + uint8_t depth = 0; + _wire->reset_search(); + while (depth <= index && _wire->search(deviceAddress)) + { + if (depth == index && _wire->crc8((uint8_t *)deviceAddress, 7) == deviceAddress[7]) + return true; + depth++; + } + return false; +} + + +class Ds2423a : public IoTItem +{ +private: + // описание параметров передаваемых из настроек датчика из веба + String _addr; + int _pin; + int _index; + DS2423 *ds2423; + DeviceAddressDS2423 _deviceAddress; + +public: + Ds2423a(String parameters) : IoTItem(parameters) + { + + jsonRead(parameters, "pin", _pin); + jsonRead(parameters, "index", _index, false); + jsonRead(parameters, "addr", _addr, false); + + // Определяем адрес. Если параметр addr не установлен, то узнаем адрес по индексу + if (_addr == "") + { + if (getDeviceAddressDS2423(_pin, _deviceAddress, _index)) + { + char addrStr[20] = ""; + hex2string(_deviceAddress, 8, addrStr); + SerialPrint("I", "Sensor " + (String)_id, "index: " + (String)_index + " addr: " + String(addrStr)); + } + else + { + SerialPrint("E", "Sensor " + (String)_id, "index: " + (String)_index + " addres not search"); + } + } + else + { + string2hex(_addr.c_str(), _deviceAddress); + } + + ds2423 = new DS2423(instanceOneWire_DS2423(_pin), _deviceAddress); + ds2423->begin(DS2423_COUNTER_A | DS2423_COUNTER_B); + + } + + void doByInterval() + { + ds2423->update(); + if (ds2423->isError()) + { + Serial.println("Error reading counter"); + } + else + { + value.valD = ds2423->getCount(DS2423_COUNTER_A); + // if (value.valD != -127) + regEvent(value.valD, "Ds2423a"); // обязательный вызов для отправки результата работы + // else + // SerialPrint("E", "Sensor Ds2423a", "Error"); + } + } + //======================================================================================================= + + ~Ds2423a(){}; +}; + +class Ds2423b : public IoTItem +{ +private: + // описание параметров передаваемых из настроек датчика из веба + String _addr; + int _pin; + int _index; + DS2423 *ds2423; + DeviceAddressDS2423 _deviceAddress; + +public: + Ds2423b(String parameters) : IoTItem(parameters) + { + jsonRead(parameters, "pin", _pin); + jsonRead(parameters, "index", _index, false); + jsonRead(parameters, "addr", _addr, false); + + // Определяем адрес. Если параметр addr не установлен, то узнаем адрес по индексу + if (_addr == "") + { + if (getDeviceAddressDS2423(_pin, _deviceAddress, _index)) + { + char addrStr[20] = ""; + hex2string(_deviceAddress, 8, addrStr); + SerialPrint("I", "Sensor " + (String)_id, "index: " + (String)_index + " addr: " + String(addrStr)); + } + else + { + SerialPrint("E", "Sensor " + (String)_id, "index: " + (String)_index + " addres not search"); + } + } + else + { + string2hex(_addr.c_str(), _deviceAddress); + } + + ds2423 = new DS2423(instanceOneWire_DS2423(_pin), _deviceAddress); + ds2423->begin(DS2423_COUNTER_A | DS2423_COUNTER_B); + } + + void doByInterval() + { + ds2423->update(); + + if (ds2423->isError()) + { + Serial.println("Error reading counter"); + } + else + { + // запускаем опрос измерений у всех датчиков на линии + value.valD = ds2423->getCount(DS2423_COUNTER_B); + // if (value.valD != -127) + regEvent(value.valD, "Ds2423b"); // обязательный вызов для отправки результата работы + // else + // SerialPrint("E", "Sensor Ds2423b", "Error"); + } + } + //======================================================================================================= + + ~Ds2423b(){}; +}; + +void *getAPI_Ds2423(String subtype, String param) +{ + if (subtype == F("Ds2423a")) + { + return new Ds2423a(param); + } + else if (subtype == F("Ds2423b")) + { + return new Ds2423b(param); + } + else + { + return nullptr; + } +} diff --git a/src/modules/sensors/Ds2423/modinfo.json b/src/modules/sensors/Ds2423/modinfo.json new file mode 100644 index 00000000..dffda1dc --- /dev/null +++ b/src/modules/sensors/Ds2423/modinfo.json @@ -0,0 +1,72 @@ +{ + "menuSection": "Сенсоры", + "configItem": [ + { + "global": 0, + "name": "DS2423 счетчик 1", + "type": "Reading", + "subtype": "Ds2423a", + "id": "dscounta", + "widget": "anydataDef", + "page": "Счетчики", + "descr": "DS1 V", + "plus": 0, + "multiply": 1, + "pin": 4, + "index": 0, + "addr": "", + "int": 10, + "round": 0, + "needSave": 0 + }, + { + "global": 0, + "name": "DS2423 счетчик 2", + "type": "Reading", + "subtype": "Ds2423b", + "id": "dscountb", + "widget": "anydataDef", + "page": "Счетчики", + "descr": "DS2 V", + "plus": 0, + "multiply": 1, + "pin": 4, + "index": 0, + "addr": "", + "int": 10, + "round": 0, + "needSave": 0 + } + ], + "about": { + "authorName": "Serghei Crasnicov", + "authorContact": "https://t.me/Serghei63", + "authorGit": "https://github.com/Serghei63", + "specialThanks": "Bubnov Mikhail @Mit4bmw", + "moduleName": "Ds2423", + "moduleVersion": "1.0", + "usedRam": { + "esp32_4mb": 15, + "esp8266_4mb": 15 + }, + "title": "Cчетчик ds2423", + "moduleDesc": "Позволяет получить значения с Ds2423.", + "propInfo": { + "pin": "GPIO номер, к которому подключена шина данных датчиков.", + "index": "Порядковый номер датчика на шине.", + "addr": "Адрес датчика на шине для точной идентификации. Если оставить пустым, то попробует найти по индексу и пину и Можно скопировать из консоли.", + "int": "Количество секунд между опросами датчика." + } + }, + "defActive": true, + "usedLibs": { + "esp32_4mb": [ + "https://github.com/jbechter/arduino-onewire-DS2423", + "paulstoffregen/OneWire @ ^2.3.7" + ], + "esp8266_4mb": [ + "https://github.com/jbechter/arduino-onewire-DS2423", + "paulstoffregen/OneWire @ ^2.3.7" + ] + } +} \ No newline at end of file