From 9776f821d38a8ed79d113d20cd3abe7bc17887ab Mon Sep 17 00:00:00 2001 From: Mit4el Date: Thu, 21 Mar 2024 22:22:54 +0300 Subject: [PATCH] BrokerMqtt v2, SIM800 upd --- src/modules/exec/BrokerMQTT/BrokerMQTT.cpp | 97 ++++++++++++++++++---- src/modules/exec/BrokerMQTT/modinfo.json | 15 +++- src/modules/exec/SIM800/SIM800.cpp | 16 ++-- 3 files changed, 103 insertions(+), 25 deletions(-) diff --git a/src/modules/exec/BrokerMQTT/BrokerMQTT.cpp b/src/modules/exec/BrokerMQTT/BrokerMQTT.cpp index f3595345..9d62d553 100644 --- a/src/modules/exec/BrokerMQTT/BrokerMQTT.cpp +++ b/src/modules/exec/BrokerMQTT/BrokerMQTT.cpp @@ -7,16 +7,26 @@ namespace _Broker { #define DEF_PORT 1883 + // MqttBroker broker(1883); + class myPicoMQTT : public PicoMQTT::Server { private: bool _debug; + String _user; + String _pass; public: myPicoMQTT(int port) : PicoMQTT::Server(port) { } + void setAuth(String user, String pass) + { + _user = user; + _pass = pass; + } + void setDebug(bool debug) { _debug = debug; @@ -65,19 +75,36 @@ namespace _Broker Serial.println(topic); } } - }; - // MqttBroker broker(1883); - myPicoMQTT *picoMqtt = nullptr; - - myPicoMQTT *instanceBroker(int port) - { - if (!picoMqtt) + PicoMQTT::ConnectReturnCode auth(const char *client_id, const char *username, const char *password) { - picoMqtt = new myPicoMQTT(port); - // ot->begin(); + if (String(client_id).length() < 3) + { + return PicoMQTT::CRC_IDENTIFIER_REJECTED; + } + if (!username && !password) + { + return PicoMQTT::CRC_NOT_AUTHORIZED; + } + if (String(username) == _user && String(password) == _pass) + { + return PicoMQTT::CRC_ACCEPTED; + } + Serial.print("[BrokerMQTT], Client: "); + Serial.print(client_id); + Serial.print(", NOT Authorized: "); + Serial.print(username); + Serial.print(" != "); + Serial.print(_user); + Serial.print(" ,pass: "); + Serial.print(password); + Serial.print(" != "); + Serial.println(_pass); + return PicoMQTT::CRC_BAD_USERNAME_OR_PASSWORD; } - return picoMqtt; - } + }; + + myPicoMQTT *picoMqtt = nullptr; + PicoMQTT::Client *clientMqtt = nullptr; TaskHandle_t brokerTask; // void Task2code( void * pvParameters ); @@ -89,7 +116,10 @@ namespace _Broker Serial.println(xPortGetCoreID()); for (;;) { - instanceBroker(DEF_PORT)->loop(); + if (clientMqtt) + clientMqtt->loop(); + if (picoMqtt) + picoMqtt->loop(); // picoMqtt.loop(); // vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(5)); } @@ -100,14 +130,41 @@ namespace _Broker private: unsigned long ts = 0; int _port = 0; + String _user; + String _pass; bool _debug; + bool _brige; + String _server; + String _srvUser; + String _srvPass; + int _srvPort; public: BrokerMQTT(String parameters) : IoTItem(parameters) { SerialPrint("i", F("BrokerMQTT"), " START... "); jsonRead(parameters, "port", _port); + jsonRead(parameters, "user", _user); + jsonRead(parameters, "pass", _pass); jsonRead(parameters, "debug", _debug); + jsonRead(parameters, "brige", _brige); + jsonRead(parameters, "server", _server); + jsonRead(parameters, "srvUser", _srvUser); + jsonRead(parameters, "srvPass", _srvPass); + jsonRead(parameters, "srvPort", _srvPort); + + if (_brige) + { + clientMqtt = new PicoMQTT::Client(_server.c_str(), _srvPort, nullptr, _srvUser.c_str(), _srvPass.c_str()); + if (_debug) + { + SerialPrint("i", F("BrigeMQTT"), "Bridge mode : ON"); + SerialPrint("i", F("BrigeMQTT"), "Bridge server: " + _server); + SerialPrint("i", F("BrigeMQTT"), "Bridge port: " + String(_srvPort)); + SerialPrint("i", F("BrigeMQTT"), "Bridge user: " + _srvUser); + SerialPrint("i", F("BrigeMQTT"), "Bridge pass: " + _srvPass); + } + } } void doByInterval() @@ -117,8 +174,16 @@ namespace _Broker { if (!_port) _port = DEF_PORT; - instanceBroker(_port)->begin(); - instanceBroker(_port)->setDebug(_debug); + picoMqtt = new myPicoMQTT(_port); + picoMqtt->begin(); + picoMqtt->setDebug(_debug); + picoMqtt->setAuth(_user, _pass); + if (_brige && picoMqtt && clientMqtt) + { + picoMqtt->subscribe("#", [](const char *topic, const char *message) + { clientMqtt->publish(topic, message); + SerialPrint("i", F("BrigeMQTT"), "client publish, topic: " + String(topic) + " msg: " + String(message) ); }); + } // picoMqtt.begin(); xTaskCreatePinnedToCore( tBrokerMQTT, // Функция задачи. @@ -140,7 +205,9 @@ namespace _Broker ~BrokerMQTT() { - // delete picoMqtt; + vTaskDelete(brokerTask); + delete picoMqtt; + delete clientMqtt; } }; } diff --git a/src/modules/exec/BrokerMQTT/modinfo.json b/src/modules/exec/BrokerMQTT/modinfo.json index 3a5725f4..fac52f02 100644 --- a/src/modules/exec/BrokerMQTT/modinfo.json +++ b/src/modules/exec/BrokerMQTT/modinfo.json @@ -13,6 +13,13 @@ "int": 10, "value": "", "port": 1883, + "user": "root", + "pass": "4321", + "brige":1, + "server":"http://iotmanager.org", + "srvUser": "rise", + "srvPass": "3hostel3", + "srvPort": 1883, "debug": 1 } ], @@ -22,7 +29,7 @@ "authorGit": "https://github.com/Mit4el", "specialThanks": "Андрей Душин", "moduleName": "BrokerMQTT", - "moduleVersion": "0.1", + "moduleVersion": "2.0", "usedRam": { "esp32_4mb": 15, "esp8266_4mb": 15 @@ -30,7 +37,11 @@ "title": "BrokerMQTT", "moduleDesc": "MQTT Брокер на основе Pico Mqtt", "propInfo": { - "port":"Порт, по умолчанию 1883" + "port":"Порт, по умолчанию 1883", + "brige":"1 - Использовать режим моста, Брокер будет дублировать все топики в указанные сервер", + "server":"Адрес внешнего MQTT брокера/сервера для режима моста", + "srvUser": "Пользователь внешнего MQTT брокера", + "srvPass": "Пароль внешнего MQTT брокера" } }, "defActive": false, diff --git a/src/modules/exec/SIM800/SIM800.cpp b/src/modules/exec/SIM800/SIM800.cpp index 5a4edfbe..d5334c3f 100644 --- a/src/modules/exec/SIM800/SIM800.cpp +++ b/src/modules/exec/SIM800/SIM800.cpp @@ -38,7 +38,9 @@ public: void sendSms(String sms, String num) { _printUart(1, "AT+CMGF=1"); // переводим в текстовый режим + delay(2); _printUart(1, "AT+CMGS=\"" + num + "\""); + delay(2); //_printUart(1, sms + "\r\n" + String((char)26)); _myUART->println(sms + "\r\n" + String((char)26)); if (_debug) @@ -71,8 +73,12 @@ public: if (_inc == '\r') { - _inStr += _inc; - if (_debug && _inStr != "\r") + return; + } + if (_inc == '\n') + { + _inStr += "";//_inc; + if (_debug && _inStr != "") SerialPrint("I", F("SIM800"), "-> " + _inStr); if (_inStr.indexOf("CPAS") != -1) @@ -81,16 +87,10 @@ public: setValue("OK"); else setValue("NO"); - return; } _inStr = ""; return; } - - if (_inc == '\n') - { - // SerialPrint("I", F("SIM800"), "-> " + _inStr); - } else _inStr += _inc; }