BrokerMqtt v2, SIM800 upd

This commit is contained in:
Mit4el
2024-03-21 22:22:54 +03:00
parent 8c98a387a2
commit 9776f821d3
3 changed files with 103 additions and 25 deletions

View File

@@ -7,16 +7,26 @@ namespace _Broker
{ {
#define DEF_PORT 1883 #define DEF_PORT 1883
// MqttBroker broker(1883);
class myPicoMQTT : public PicoMQTT::Server class myPicoMQTT : public PicoMQTT::Server
{ {
private: private:
bool _debug; bool _debug;
String _user;
String _pass;
public: public:
myPicoMQTT(int port) : PicoMQTT::Server(port) myPicoMQTT(int port) : PicoMQTT::Server(port)
{ {
} }
void setAuth(String user, String pass)
{
_user = user;
_pass = pass;
}
void setDebug(bool debug) void setDebug(bool debug)
{ {
_debug = debug; _debug = debug;
@@ -65,19 +75,36 @@ namespace _Broker
Serial.println(topic); Serial.println(topic);
} }
} }
}; PicoMQTT::ConnectReturnCode auth(const char *client_id, const char *username, const char *password)
// MqttBroker broker(1883);
myPicoMQTT *picoMqtt = nullptr;
myPicoMQTT *instanceBroker(int port)
{
if (!picoMqtt)
{ {
picoMqtt = new myPicoMQTT(port); if (String(client_id).length() < 3)
// ot->begin(); {
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; TaskHandle_t brokerTask;
// void Task2code( void * pvParameters ); // void Task2code( void * pvParameters );
@@ -89,7 +116,10 @@ namespace _Broker
Serial.println(xPortGetCoreID()); Serial.println(xPortGetCoreID());
for (;;) for (;;)
{ {
instanceBroker(DEF_PORT)->loop(); if (clientMqtt)
clientMqtt->loop();
if (picoMqtt)
picoMqtt->loop();
// picoMqtt.loop(); // picoMqtt.loop();
// vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(5)); // vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(5));
} }
@@ -100,14 +130,41 @@ namespace _Broker
private: private:
unsigned long ts = 0; unsigned long ts = 0;
int _port = 0; int _port = 0;
String _user;
String _pass;
bool _debug; bool _debug;
bool _brige;
String _server;
String _srvUser;
String _srvPass;
int _srvPort;
public: public:
BrokerMQTT(String parameters) : IoTItem(parameters) BrokerMQTT(String parameters) : IoTItem(parameters)
{ {
SerialPrint("i", F("BrokerMQTT"), " START... "); SerialPrint("i", F("BrokerMQTT"), " START... ");
jsonRead(parameters, "port", _port); jsonRead(parameters, "port", _port);
jsonRead(parameters, "user", _user);
jsonRead(parameters, "pass", _pass);
jsonRead(parameters, "debug", _debug); 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() void doByInterval()
@@ -117,8 +174,16 @@ namespace _Broker
{ {
if (!_port) if (!_port)
_port = DEF_PORT; _port = DEF_PORT;
instanceBroker(_port)->begin(); picoMqtt = new myPicoMQTT(_port);
instanceBroker(_port)->setDebug(_debug); 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(); // picoMqtt.begin();
xTaskCreatePinnedToCore( xTaskCreatePinnedToCore(
tBrokerMQTT, // Функция задачи. tBrokerMQTT, // Функция задачи.
@@ -140,7 +205,9 @@ namespace _Broker
~BrokerMQTT() ~BrokerMQTT()
{ {
// delete picoMqtt; vTaskDelete(brokerTask);
delete picoMqtt;
delete clientMqtt;
} }
}; };
} }

View File

@@ -13,6 +13,13 @@
"int": 10, "int": 10,
"value": "", "value": "",
"port": 1883, "port": 1883,
"user": "root",
"pass": "4321",
"brige":1,
"server":"http://iotmanager.org",
"srvUser": "rise",
"srvPass": "3hostel3",
"srvPort": 1883,
"debug": 1 "debug": 1
} }
], ],
@@ -22,7 +29,7 @@
"authorGit": "https://github.com/Mit4el", "authorGit": "https://github.com/Mit4el",
"specialThanks": "Андрей Душин", "specialThanks": "Андрей Душин",
"moduleName": "BrokerMQTT", "moduleName": "BrokerMQTT",
"moduleVersion": "0.1", "moduleVersion": "2.0",
"usedRam": { "usedRam": {
"esp32_4mb": 15, "esp32_4mb": 15,
"esp8266_4mb": 15 "esp8266_4mb": 15
@@ -30,7 +37,11 @@
"title": "BrokerMQTT", "title": "BrokerMQTT",
"moduleDesc": "MQTT Брокер на основе Pico Mqtt", "moduleDesc": "MQTT Брокер на основе Pico Mqtt",
"propInfo": { "propInfo": {
"port":"Порт, по умолчанию 1883" "port":"Порт, по умолчанию 1883",
"brige":"1 - Использовать режим моста, Брокер будет дублировать все топики в указанные сервер",
"server":"Адрес внешнего MQTT брокера/сервера для режима моста",
"srvUser": "Пользователь внешнего MQTT брокера",
"srvPass": "Пароль внешнего MQTT брокера"
} }
}, },
"defActive": false, "defActive": false,

View File

@@ -38,7 +38,9 @@ public:
void sendSms(String sms, String num) void sendSms(String sms, String num)
{ {
_printUart(1, "AT+CMGF=1"); // переводим в текстовый режим _printUart(1, "AT+CMGF=1"); // переводим в текстовый режим
delay(2);
_printUart(1, "AT+CMGS=\"" + num + "\""); _printUart(1, "AT+CMGS=\"" + num + "\"");
delay(2);
//_printUart(1, sms + "\r\n" + String((char)26)); //_printUart(1, sms + "\r\n" + String((char)26));
_myUART->println(sms + "\r\n" + String((char)26)); _myUART->println(sms + "\r\n" + String((char)26));
if (_debug) if (_debug)
@@ -71,8 +73,12 @@ public:
if (_inc == '\r') if (_inc == '\r')
{ {
_inStr += _inc; return;
if (_debug && _inStr != "\r") }
if (_inc == '\n')
{
_inStr += "";//_inc;
if (_debug && _inStr != "")
SerialPrint("I", F("SIM800"), "-> " + _inStr); SerialPrint("I", F("SIM800"), "-> " + _inStr);
if (_inStr.indexOf("CPAS") != -1) if (_inStr.indexOf("CPAS") != -1)
@@ -81,16 +87,10 @@ public:
setValue("OK"); setValue("OK");
else else
setValue("NO"); setValue("NO");
return;
} }
_inStr = ""; _inStr = "";
return; return;
} }
if (_inc == '\n')
{
// SerialPrint("I", F("SIM800"), "-> " + _inStr);
}
else else
_inStr += _inc; _inStr += _inc;
} }