diff --git a/include/ESPConfiguration.h b/include/ESPConfiguration.h index d2196696..7f22d53c 100644 --- a/include/ESPConfiguration.h +++ b/include/ESPConfiguration.h @@ -5,4 +5,5 @@ extern std::vector IoTItems; // вектор ссылок базового класса IoTItem - список всех запущенных сенсоров extern void configure(String path); +void clearConfigure(); extern IoTItem* myIoTItem; diff --git a/include/Global.h b/include/Global.h index dc991068..56fdd1ab 100644 --- a/include/Global.h +++ b/include/Global.h @@ -49,11 +49,13 @@ #include "Utils/SerialPrint.h" #include "Utils/StringUtils.h" #include "PeriodicTasks.h" +#include "classes/IoTGpio.h" /********************************************************************************************************************* *****************************************глобальные объекты классов*************************************************** **********************************************************************************************************************/ + extern TickerScheduler ts; extern WiFiClient espClient; extern PubSubClient mqtt; diff --git a/include/WsServer.h b/include/WsServer.h index ed4788fe..f31c041d 100644 --- a/include/WsServer.h +++ b/include/WsServer.h @@ -2,6 +2,7 @@ #include "Global.h" #include "Utils/WiFiUtils.h" #include "DeviceList.h" +#include "ESPConfiguration.h" #ifdef STANDARD_WEB_SOCKETS extern void standWebSocketsInit(); diff --git a/include/classes/IoTGpio.h b/include/classes/IoTGpio.h new file mode 100644 index 00000000..ed817b66 --- /dev/null +++ b/include/classes/IoTGpio.h @@ -0,0 +1,21 @@ +#pragma once +#include + +class IoTGpio { + public: + IoTGpio(int pins); + ~IoTGpio(); + + void pinMode(uint8_t pin, uint8_t mode); + void digitalWrite(uint8_t pin, uint8_t val); + int digitalRead(uint8_t pin); + int analogRead(uint8_t pin); + void analogWrite(uint8_t pin, int val); + + int pinNums; // база для определения диапазона номеров пинов. pinNums + используемый + void regDriver(IoTGpio* newDriver); + + private: + IoTGpio* _drivers[5]; //ссылки на объекты доступа к портам более 100, 200, 300, 400. Нулевой элемент используется как маркер - и возвращается nullptr при обращении + +}; \ No newline at end of file diff --git a/include/classes/IoTItem.h b/include/classes/IoTItem.h index ef02379c..d7f65cdf 100644 --- a/include/classes/IoTItem.h +++ b/include/classes/IoTItem.h @@ -1,4 +1,5 @@ #pragma once +#include "classes/IoTGpio.h" struct IoTValue { float valD = 0; @@ -27,6 +28,8 @@ class IoTItem { IoTValue value; // хранение основного значения, котрое обновляется из сценария, execute(), loop() или doByInterval() + IoTGpio* getGpioDriver(); + protected: String _subtype; String _id; diff --git a/src/ESPConfiguration.cpp b/src/ESPConfiguration.cpp index 9e976008..436f8a82 100644 --- a/src/ESPConfiguration.cpp +++ b/src/ESPConfiguration.cpp @@ -1,4 +1,7 @@ #include "ESPConfiguration.h" +#include "classes/IoTGpio.h" + +extern IoTGpio IoTgpio; std::vector IoTItems; void* getAPI(String subtype, String params); @@ -19,9 +22,19 @@ void configure(String path) { } else { myIoTItem = (IoTItem*)getAPI(subtype, jsonArrayElement); if (myIoTItem) { + IoTGpio* tmp = myIoTItem->getGpioDriver(); + if (tmp) IoTgpio.regDriver(tmp); IoTItems.push_back(myIoTItem); } } } file.close(); +} + +void clearConfigure() { + Serial.printf("Start clearing config\n"); + for (unsigned int i = 0; i < IoTItems.size(); i++) { + if (IoTItems[i]) delete IoTItems[i]; + } + IoTItems.clear(); } \ No newline at end of file diff --git a/src/Global.cpp b/src/Global.cpp index 9edeee54..5667cf67 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -29,6 +29,7 @@ WebSocketsServer standWebSocket = WebSocketsServer(81); /********************************************************************************************************************* ***********************************************глобальные переменные************************************************** **********************************************************************************************************************/ +IoTGpio IoTgpio(0); String settingsFlashJson = "{}"; //переменная в которой хранятся все настройки, находится в оперативной памяти и синхронизированна с flash памятью String errorsHeapJson = "{}"; //переменная в которой хранятся все ошибки, находится в оперативной памяти только diff --git a/src/WsServer.cpp b/src/WsServer.cpp index 1038e690..a619ea34 100644 --- a/src/WsServer.cpp +++ b/src/WsServer.cpp @@ -71,6 +71,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) //**сохранение**// if (headerStr == "/gifnoc|") { writeFileUint8tByFrames("config.json", payload, length, headerLenth, 256); + clearConfigure(); + configure("/config.json"); } // page connection=================================================================== //**отправка**// diff --git a/src/classes/IoTGpio.cpp b/src/classes/IoTGpio.cpp new file mode 100644 index 00000000..96faeac2 --- /dev/null +++ b/src/classes/IoTGpio.cpp @@ -0,0 +1,43 @@ +#include "classes/IoTGpio.h" + + +IoTGpio::IoTGpio(int pins) { // инициируем стартовым числом диапазона доступных пинов для создаваемого экземпляра 0 - системный, далее 100, 200, 300, 400 или иной + pinNums = pins; + _drivers[0] = nullptr; +} + +IoTGpio::~IoTGpio(){ + +} + + +void IoTGpio::pinMode(uint8_t pin, uint8_t mode) { + if (_drivers[pin/100]) _drivers[pin/100]->pinMode(pin, mode); + else ::pinMode(pin, mode); +} + +void IoTGpio::digitalWrite(uint8_t pin, uint8_t val) { + if (_drivers[pin/100]) _drivers[pin/100]->digitalWrite(pin, val); + else ::digitalWrite(pin, val); +} + +int IoTGpio::digitalRead(uint8_t pin) { + if (_drivers[pin/100]) return _drivers[pin/100]->digitalRead(pin); + else return ::digitalRead(pin); +} + + +int IoTGpio::analogRead(uint8_t pin) { + if (_drivers[pin/100]) return _drivers[pin/100]->analogRead(pin); + else return ::analogRead(pin); +} + +void IoTGpio::analogWrite(uint8_t pin, int val) { + if (_drivers[pin/100]) _drivers[pin/100]->analogWrite(pin, val); + else ::analogWrite(pin, val); +} + + +void IoTGpio::regDriver(IoTGpio* newDriver) { + _drivers[newDriver->pinNums/100] = newDriver; +} \ No newline at end of file diff --git a/src/classes/IoTItem.cpp b/src/classes/IoTItem.cpp index db69a31f..ab9ab61d 100644 --- a/src/classes/IoTItem.cpp +++ b/src/classes/IoTItem.cpp @@ -83,4 +83,8 @@ IoTItem* findIoTItem(String name) { // поиск элемента модуля return nullptr; } -IoTItem* myIoTItem; \ No newline at end of file +IoTItem* myIoTItem; + +IoTGpio* IoTItem::getGpioDriver() { + return nullptr; +} \ No newline at end of file diff --git a/src/modules/AnalogAdc.cpp b/src/modules/AnalogAdc.cpp index ff274289..5167ef81 100644 --- a/src/modules/AnalogAdc.cpp +++ b/src/modules/AnalogAdc.cpp @@ -1,6 +1,8 @@ #include "Global.h" #include "Classes/IoTItem.h" +extern IoTGpio IoTgpio; + //Это файл сенсора, в нем осуществляется чтение сенсора. //для добавления сенсора вам нужно скопировать этот файл и заменить в нем текст AnalogAdc на название вашего сенсора //Название должно быть уникальным, коротким и отражать суть сенсора. @@ -32,7 +34,7 @@ class AnalogAdc : public IoTItem { //не используйте delay - помните, что данный loop общий для всех модулей. Если у вас планируется длительная операция, постарайтесь разбить ее на порции //и выполнить за несколько тактов void doByInterval() { - value.valD = analogRead(_pin); + value.valD = IoTgpio.analogRead(_pin); regEvent(value.valD, "AnalogAdc"); //обязательный вызов хотяб один } diff --git a/src/modules/Bme280.cpp b/src/modules/Bme280.cpp index 0d14e38a..1849c818 100644 --- a/src/modules/Bme280.cpp +++ b/src/modules/Bme280.cpp @@ -24,8 +24,8 @@ class Bme280t : public IoTItem { } void doByInterval() { - float value = _bme->readTemperature(); - if (value < 145) regEvent(value, "Bme280t"); + value.valD = _bme->readTemperature(); + if (value.valD < 145) regEvent(value.valD, "Bme280t"); else SerialPrint("E", "Sensor Bme280t", "Error"); } @@ -43,8 +43,8 @@ class Bme280h : public IoTItem { } void doByInterval() { - float value = _bme->readHumidity(); - if (value < 100) regEvent(value, "Bme280h"); + value.valD = _bme->readHumidity(); + if (value.valD < 100) regEvent(value.valD, "Bme280h"); else SerialPrint("E", "Sensor Bme280h", "Error"); } @@ -62,10 +62,10 @@ class Bme280p : public IoTItem { } void doByInterval() { - float value = _bme->readPressure(); - if (value > 0) { - value = value / 1.333224 / 100; - regEvent(value, "Bme280p"); + value.valD = _bme->readPressure(); + if (value.valD > 0) { + value.valD = value.valD / 1.333224 / 100; + regEvent(value.valD, "Bme280p"); } else SerialPrint("E", "Sensor Bme280p", "Error"); } diff --git a/src/modules/Bmp280.cpp b/src/modules/Bmp280.cpp index d474edc2..c25fe3c3 100644 --- a/src/modules/Bmp280.cpp +++ b/src/modules/Bmp280.cpp @@ -24,8 +24,8 @@ class Bmp280t : public IoTItem { } void doByInterval() { - float value = _bmp->readTemperature(); - if (String(value) != "nan") regEvent(value, "Bmp280t"); + value.valD = _bmp->readTemperature(); + if (String(value.valD) != "nan") regEvent(value.valD, "Bmp280t"); else SerialPrint("E", "Sensor DHTt", "Error"); } @@ -43,10 +43,10 @@ class Bmp280p : public IoTItem { } void doByInterval() { - float value = _bmp->readPressure(); - if (String(value) != "nan") { - value = value / 1.333224 / 100; - regEvent(value, "Bmp280p"); + value.valD = _bmp->readPressure(); + if (String(value.valD) != "nan") { + value.valD = value.valD / 1.333224 / 100; + regEvent(value.valD, "Bmp280p"); } else SerialPrint("E", "Sensor DHTh", "Error"); } diff --git a/src/modules/Dht1122.cpp b/src/modules/Dht1122.cpp index 927861a5..0ff9e134 100644 --- a/src/modules/Dht1122.cpp +++ b/src/modules/Dht1122.cpp @@ -24,8 +24,8 @@ class Dht1122t : public IoTItem { } void doByInterval() { - float value = _dht->getTemperature(); - if (String(value) != "nan") regEvent(value, "Dht1122t"); + value.valD = _dht->getTemperature(); + if (String(value.valD) != "nan") regEvent(value.valD, "Dht1122t"); else SerialPrint("E", "Sensor DHTt", "Error"); } @@ -43,8 +43,8 @@ class Dht1122h : public IoTItem { } void doByInterval() { - float value = _dht->getHumidity(); - if (String(value) != "nan") regEvent(value, "Dht1122h"); + value.valD = _dht->getHumidity(); + if (String(value.valD) != "nan") regEvent(value.valD, "Dht1122h"); else SerialPrint("E", "Sensor DHTh", "Error"); } diff --git a/src/modules/Sht20.cpp b/src/modules/Sht20.cpp index 4381e9b4..70488f43 100644 --- a/src/modules/Sht20.cpp +++ b/src/modules/Sht20.cpp @@ -13,8 +13,8 @@ class Sht20t : public IoTItem { void doByInterval() { sht->read(); - float value = sht->getTemperature(); - if (value > -46.85F) regEvent(value, "Sht20t"); + value.valD = sht->getTemperature(); + if (value.valD > -46.85F) regEvent(value.valD, "Sht20t"); else SerialPrint("E", "Sensor Sht20t", "Error"); } @@ -27,8 +27,8 @@ class Sht20h : public IoTItem { void doByInterval() { sht->read(); - float value = sht->getHumidity(); - if (value != -6) regEvent(value, "Sht20h"); + value.valD = sht->getHumidity(); + if (value.valD != -6) regEvent(value.valD, "Sht20h"); else SerialPrint("E", "Sensor Sht20h", "Error"); } diff --git a/src/modules/ds18b20.cpp b/src/modules/ds18b20.cpp index e51103f6..2e09aa48 100644 --- a/src/modules/ds18b20.cpp +++ b/src/modules/ds18b20.cpp @@ -69,12 +69,12 @@ class Ds18b20 : public IoTItem { string2hex(_addr.c_str(), deviceAddress); } //получаем температуру по адресу - float value = sensors->getTempC(deviceAddress); + value.valD = sensors->getTempC(deviceAddress); char addrStr[20] = ""; hex2string(deviceAddress, 8, addrStr); - if (value != -127) regEvent(value, "addr: " + String(addrStr)); //обязательный вызов для отправки результата работы + if (value.valD != -127) regEvent(value.valD, "addr: " + String(addrStr)); //обязательный вызов для отправки результата работы else SerialPrint("E", "Sensor Ds18b20", "Error"); } //=======================================================================================================