{"id":356145,"date":"2024-05-20T23:23:26","date_gmt":"2024-05-20T23:23:26","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=356145"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=356145","title":{"rendered":"<span>\u041f\u0440\u043e\u043c\u044b\u0448\u043b\u0435\u043d\u043d\u044b\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b, Linux \u0438 \u0442\u043e\u043b\u044c\u043a\u043e C++. \u0427\u0430\u0441\u0442\u044c 2<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u044f \u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u043b \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0447\u0430\u0441\u0442\u0438, \u0442\u0443\u0442 \u043c\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043e\u0431\u043c\u0435\u043d \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 MQTT.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/c63\/59f\/98e\/c6359f98e507f1478aa43f00c7fa3102.jpg\" width=\"1280\" height=\"720\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c63\/59f\/98e\/c6359f98e507f1478aa43f00c7fa3102.jpg\" data-blurred=\"true\"\/><\/figure>\n<p>\u0427\u0442\u043e \u0436\u0435 \u0438\u0437 \u0441\u0435\u0431\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 MQTT? \u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u043e\u0431\u043c\u0435\u043d\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f\u043c\u0438. \u0414\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u043e\u043c\u0443 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0443. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043a\u0430\u043a \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0445, \u0442\u0430\u043a \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c, \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u044f\u0441\u044c \u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0433\u0440\u0443\u043f\u043f\u044b \u0432 \u0434\u0435\u0440\u0435\u0432\u0435 \u0438\u043b\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043a\u0435\u0442\u0430\u043c\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430:<\/p>\n<pre><code class=\"bash\">deb http:\/\/ppa.launchpad.net\/mosquitto-dev\/mosquitto-ppa\/ubuntu precise main deb-src http:\/\/ppa.launchpad.net\/mosquitto-dev\/mosquitto-ppa\/ubuntu precise main<\/code><\/pre>\n<p>\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0430\u043a\u0435\u0442\u043e\u0432 \u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0431\u0440\u043e\u043a\u0435\u0440 MQTT Mosquitto \u043d\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435:<\/p>\n<pre><code class=\"bash\">apt-get update apt-get install mosquitto<\/code><\/pre>\n<p>\u041d\u0430 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0444\u0430\u0439\u043b\u044b \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u043a\u0438 \u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 <code>libmosquitto-dev<\/code>:<\/p>\n<pre><code class=\"bash\">sudo apt install libmosquitto-dev apt source libmosquitto-dev<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0443\u044e \u043f\u0430\u043f\u043a\u0443 \u0441 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0430\u043c\u0438 \u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u043f\u043e\u0434 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0434\u043b\u044f <code>arm-linux-gnueabihf<\/code>:<\/p>\n<pre><code class=\"bash\">make WITH_TLS=no WITH_CJSON=no WITH_STATIC_LIBRARIES=yes<\/code><\/pre>\n<p>\u0412 \u043f\u0430\u043f\u043a\u0435 <code>.\/lib\/cpp\/<\/code>\u0432\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0432\u0441\u0435 \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u043f\u0440\u043e\u0435\u043a\u0442. \u041d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0444\u0430\u0439\u043b <code>libmosquitto.a<\/code>.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c, \u0447\u0442\u043e \u0436\u0435 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0435 \u0432\u044b\u0445\u043e\u0434\u0435? \u0420\u0430\u0437\u043b\u043e\u0436\u0438\u043c \u0432\u0441\u0435 \u043f\u043e \u043f\u0443\u043d\u043a\u0442\u0430\u043c:<\/p>\n<ul>\n<li>\n<p>\u043d\u0430\u043c \u043d\u0430\u0434\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0430\u0434\u0440\u0435\u0441\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445;<\/p>\n<\/li>\n<li>\n<p>\u0431\u0443\u0434\u0435\u043c \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u044f\u0442\u044c \u0438\u0445 \u0437\u0430 \u0442\u043e\u043f\u0438\u043a\u0430\u043c\u0438;<\/p>\n<\/li>\n<li>\n<p>\u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f, \u0430 \u043c\u043e\u0436\u0435\u043c \u0438 \u043d\u0435\u0442;<\/p>\n<\/li>\n<li>\n<p>\u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u043d\u0430 \u0431\u0440\u043e\u043a\u0435\u0440\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\u041e\u043f\u0438\u0448\u0435\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0430\u043c\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439, \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0438 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438, \u0434\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0430\u0434\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u0430 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439:<\/p>\n<pre><code class=\"cpp\">template&lt;typename T> struct Value {     Value(string topic,T old, T* current,int precision):         topic{topic},old{old},current{current},precision{precision} { }     ~Value() { }     string topic;     T old;     T* current;     uint64_t tFlag=timeSinceEpochMillisec();     int precision=2; };<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>timeSinceEpochMillisec()<\/code> \u0431\u044b\u043b\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u0430 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<p>\u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u043e\u0442 mosquittopp. \u0417\u0430\u0434\u0430\u0435\u043c Id \u0434\u043b\u044f \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u043a \u0431\u0440\u043e\u043a\u0435\u0440\u0443 \u043d\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435, \u0445\u043e\u0441\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 localhost, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0442\u043e\u043f\u0438\u043a device.<\/p>\n<pre><code class=\"cpp\">class MqttDevice:public mosquittopp::mosquittopp { public:     MqttDevice(string Id,string mqtt_host,string startTag=\"device\");     ~MqttDevice();<\/code><\/pre>\n<p>\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439\u00a0 \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u043e\u043c:<\/p>\n<pre><code class=\"cpp\">    list&lt; tuple&lt;string,string> > messages;<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0443\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0442\u043e\u043f\u0438\u043a \u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e.<\/p>\n<pre><code class=\"cpp\">    void addSubscribed(string topic, volatile bool* value);     void addSubscribed(string topic, volatile float* value);     void addSubscribed(string topic, volatile uint16_t* value);     void add(string topic,volatile bool* value);     void add(string topic,volatile uint16_t* value);     void add(string topic,volatile float* value,int precision=1);     void add(string topic,volatile double* value,int precision=1);<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u043b\u043d\u044b\u0439 \u0444\u0430\u0439\u043b <code>MqttDevice.h<\/code>:<\/p>\n<pre><code class=\"cpp\">class MqttDevice:public mosquittopp::mosquittopp { public:     MqttDevice(string Id,string mqtt_host,string startTag=\"device\");     ~MqttDevice();     void setTag(string topic,string value);     void message(string name, volatile double value);     void message(string name, volatile float value);     void message(string name, volatile bool value);     void message(string name, string value);     list&lt; tuple&lt;string,string> > messages;     void on_connect(int rc);     void on_message(const struct mosquitto_message* message);     void on_subscribe(int mid,int qos_count,const int* granted_qos);     void sendMessage(string topic, string&amp; message);     void sendMessage(string&amp; topic,char* message, int msgLength);     void addSubscribed(string topic, volatile bool* value);     void addSubscribed(string topic, volatile float* value);     void add(string topic,volatile bool* value);     void add(string topic,volatile float* value,int precision=1);     void add(string topic,volatile double* value,int precision=1); protected:     uint64_t tBool=0,tFloat=0,tDouble=0;     bool _exit; private:     string mqtt_host;     list&lt;Value&lt;volatile bool> > lwBool;     list&lt;Value&lt;volatile float> > lwFloat;     list&lt;Value&lt;volatile bool> > lrBool;     list&lt;Value&lt;volatile float> > lrFloat;     list&lt;Value&lt;volatile double> > lrDouble;     struct mosquitto *mosq;     string startTag;     void mosq_loop();     thread _threadMosquitto; }; <\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u0438 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u043c\u044b \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0443\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u043f\u043e\u0442\u043e\u043a\u0430 \u0438 \u0435\u0433\u043e \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435.<\/p>\n<pre><code class=\"cpp\">MqttDevice::MqttDevice(string Id,string mqtt_host,string startTag) : startTag{startTag},mqtt_host{mqtt_host}, mosquittopp((Id+to_string(rand())).c_str()) {     _exit=false;     int rc=connect(mqtt_host.c_str(),mqtt_port,65535);     printf(\"MqttDevice connect %d\\n\",rc);     _threadMosquitto = thread(&amp;MqttDevice::mosq_loop,this); } MqttDevice::~MqttDevice() {     _exit=true;     _threadMosquitto.join();     printf(\"MqttDevice delete\\n\"); }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 MqttDevice.cpp:<\/p>\n<pre><code class=\"cpp\">#define mqtt_port 1883  MqttDevice::MqttDevice(string Id,string mqtt_host,string startTag) : startTag{startTag},mqtt_host{mqtt_host}, mosquittopp((Id+to_string(rand())).c_str()) {     _exit=false;     int rc=connect(mqtt_host.c_str(),mqtt_port,65535);     printf(\"MqttDevice connect %d\\n\",rc);     _threadMosquitto = thread(&amp;MqttDevice::mosq_loop,this); } MqttDevice::~MqttDevice() {     _exit=true;     _threadMosquitto.join();     printf(\"MqttDevice delete\\n\"); } void MqttDevice::addSubscribed(string topic, volatile bool* value) {     lwBool.push_back({topic,false,value,0});     if(subscribe(0,topic.c_str(),0)==MOSQ_ERR_SUCCESS) printf(\"[Mosquitto] Subscribing to topic %s\\n\",topic.c_str()); } void MqttDevice::addSubscribed(string topic, volatile float* value) {     lwFloat.push_back({topic,0.0f,value,1});     if(subscribe(0,topic.c_str(),0)==MOSQ_ERR_SUCCESS) printf(\"[Mosquitto] Subscribing to topic %s\\n\",topic.c_str()); } void MqttDevice::add(string topic,volatile bool* value) {     lrBool.push_back({topic,false,value,0}); } void MqttDevice::add(string topic,volatile float* value, int precision) {     lrFloat.push_back({topic,0.0f,value,precision}); } void MqttDevice::add(string topic,volatile double* value, int precision) {     lrDouble.push_back({topic,0.0,value,precision}); } void MqttDevice::on_connect(int rc) {     int err=0;         printf(\"[Mosquitto] Connect. rc=%d\\n\",rc);     if(rc==0) {         for(auto&amp; v:lwBool) {             err=subscribe(0,v.topic.c_str(),0);             if(err==MOSQ_ERR_SUCCESS) printf(\"[Mosquitto] Subscribing to topic %s\\n\",v.topic.c_str());             else {reconnect();return;}             nsleep(10);         }         for(auto&amp; v:lwFloat) {             err=subscribe(0,v.topic.c_str(),0);             if(err==MOSQ_ERR_SUCCESS) printf(\"[Mosquitto] Subscribing to topic %s\\n\",v.topic.c_str());             else {reconnect();return;}             nsleep(10);         }     }     else         printf(\"[Mosquitto] Connection failed. Aborting subscribing.\\n\"); } void MqttDevice::on_message(const struct mosquitto_message *message) {     string tag=message->topic;     string value=string((const char*)message->payload,message->payloadlen);     setTag(tag,value); } void MqttDevice::mosq_loop() {     int rc;     lib_init();     printf(\"[Mosquitto] Start loop...\\n\");     while(!_exit) {         uint64_t now = timeSinceEpochMillisec();           rc=loop();         if(rc) {             printf(\"[Mosquitto] Disconnected. Trying to reconnect...\\n\");             nsleep(500);             reconnect();             nsleep(500);             continue;         }         while(messages.size()>0) {             rc=publish(NULL,get&lt;0>(messages.front()).c_str(),strlen(get&lt;1>(messages.front()).c_str()),(const uint8_t*)get&lt;1>(messages.front()).c_str(),0,true);             if(rc) {                 printf(\"[Mosquitto] Error send message\\n\");                 break;             }             else                 messages.pop_front();         }         if(now-tBool>300) {             tBool=now;             for(auto &amp;v:lrBool) {                 if(v.current==NULL) continue;                 if(v.old!=*v.current || now-v.tFlag>3000) {                     v.tFlag=now;                     v.old=*v.current;                     message(v.topic,*v.current);                 }             }             for(auto &amp;v:lwBool) {                 if(v.old!=*v.current || now-v.tFlag>3000) {                     v.tFlag=now;                     v.old=*v.current;                     message(v.topic,*v.current);                 }             }         }         if(now-tFloat>500) {             tFloat=now;             for(auto &amp;v:lrFloat) {                 float r=round(*v.current*pow(10,v.precision))\/pow(10,v.precision);                 if(v.old!=r || now-v.tFlag>5000) {                     v.tFlag=now;                     v.old=r;                     message(v.topic,r);                 }             }             for(auto &amp;v:lrDouble) {                 double r=round(*v.current*pow(10,v.precision))\/pow(10,v.precision);                 if(v.old!=r || now-v.tFlag>5000) {                     v.tFlag=now;                     v.old=r;                     message(v.topic,r);                 }             }             for(auto &amp;v:lwFloat) {                 float r=round(*v.current*pow(10,v.precision))\/pow(10,v.precision);                 if(v.old!=r || now-v.tFlag>5000) {                     v.tFlag=now;                     v.old=r;                     message(v.topic,r);                 }             }         }     }     lib_cleanup();     printf(\"[Mosquitto] Exit from mosquitto loop.\\n\"); } void MqttDevice::message(string name, volatile float value) {     messages.push_back(make_tuple(name,to_string(value))); } void MqttDevice::message(string name, volatile double value) {     messages.push_back(make_tuple(name,to_string(value))); } void MqttDevice::message(string name, volatile bool value) {     messages.push_back(make_tuple(name,(value?\"true\":\"false\"))); } void MqttDevice::message(string name, string value) {     messages.push_back(make_tuple(name,value)); } void MqttDevice::setTag(string topic,string value) {     try {         for(auto&amp; v:lwBool)             if(v.topic==topic) {                 *(v.current)=(value==\"true\");                 v.old=*(v.current);                 return;             }         for(auto&amp; v:lwFloat)             if(v.topic==topic) {                 *(v.current)=(float)atof(value.c_str());                 v.old=*(v.current);                 return;             }     }     catch(exception&amp; ex) { } }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"cpp\">void MqttDevice::add(ValveD* valve) {     add(startTag+\"\/\"+valve->name+\"\/SQL\",valve->b_t1);     add(startTag+\"\/\"+valve->name+\"\/SQH\",valve->b_t2);     add(startTag+\"\/\"+valve->name+\"\/out\",valve->b_out);     addSubscribed(startTag+\"\/\"+valve->name+\"\/auto\",valve->b_mode);     addSubscribed(startTag+\"\/\"+valve->name+\"\/man\",valve->b_control); }<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442 \u043e\u0442\u0441\u0435\u0447\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u043f\u0430\u043d\u0430 \u0441 \u0434\u0432\u0443\u043c\u044f \u043a\u043e\u043d\u0446\u0435\u0432\u0438\u043a\u0430\u043c\u0438 \u043a\u0440\u0430\u0439\u043d\u0438\u0445 \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u0438\u0433\u043d\u0430\u043b\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043b\u0430\u043f\u0430\u043d\u043e\u043c. \u0418\u0437 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0435\u0441\u0442\u044c \u0441\u0438\u0433\u043d\u0430\u043b \u0432 \u043a\u0430\u043a\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u043a\u043b\u0430\u043f\u0430\u043d auto. \u0415\u0441\u043b\u0438 \u0440\u0443\u0447\u043d\u043e\u0439 \u0440\u0435\u0436\u0438\u043c auto=false, \u0442\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u0441\u0438\u0433\u043d\u0430\u043b\u0430 man. \u0415\u0441\u043b\u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0440\u0435\u0436\u0438\u043c auto=true, \u0442\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e \u043f\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443.<\/p>\n<p>\u0418\u0437 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u043f\u0440\u043e\u0441\u0430 \u043c\u043e\u0434\u0443\u043b\u044f \u0432\u0432\u043e\u0434\u0430\/\u0432\u044b\u0432\u043e\u0434\u0430 \u0438 \u043a \u043d\u0435\u043c\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0431\u0440\u043e\u043a\u0435\u0440:<\/p>\n<pre><code class=\"cpp\">volatile bool b1=false,b2=false,b3=false; ModbusLine line1(modbus_new_rtu(\"\/dev\/ttyS2\", 115200, 'N', 8, 1)); line1.addMDS_DIO_16BD(1,&amp;b1,&amp;b2,&amp;b3);    MqttDevice mqtt(\"line\",\"localhost\"); mqtt.addSubscribed(\"device\/b1\",&amp;b1); mqtt.add(\"device\/b2\",&amp;b2); mqtt.add(\"device\/b3\",&amp;b3);<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043a \u0431\u0440\u043e\u043a\u0435\u0440\u0443 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 3 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445. \u041f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 b1 \u0447\u0435\u0440\u0435\u0437 \u0431\u0440\u043e\u043a\u0435\u0440 \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c6f\/80a\/a5c\/c6f80aa5ce7e13505df7e2879c303b5d.png\" alt=\"\u0421\u043c\u043e\u0442\u0440\u0438\u043c \u0432 MQTT Explorer \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b\" title=\"\u0421\u043c\u043e\u0442\u0440\u0438\u043c \u0432 MQTT Explorer \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b\" width=\"1515\" height=\"1070\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c6f\/80a\/a5c\/c6f80aa5ce7e13505df7e2879c303b5d.png\"\/><\/p>\n<div><figcaption>\u0421\u043c\u043e\u0442\u0440\u0438\u043c \u0432 MQTT Explorer \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b<\/figcaption><\/div>\n<\/figure>\n<p>\u041c\u044b \u0434\u043e\u0431\u0438\u043b\u0438\u0441\u044c \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043d\u0430\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430, \u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043d\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430.<\/p>\n<p>\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u0431\u043c\u0435\u043d \u043c\u0435\u0436\u0434\u0443 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u043c\u0438 \u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438 \u0447\u0435\u0440\u0435\u0437 RS-485 \u043f\u043e Modbus \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u042d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u043f\u0440\u0438 \u043c\u0435\u0436\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043d\u043e\u043c \u043e\u0431\u043c\u0435\u043d\u0435 \u0438 \u043e\u0431\u0449\u0435\u043d\u0438\u0438 \u0441 \u0441\u0435\u043d\u0441\u043e\u0440\u043d\u044b\u043c\u0438 \u043f\u0430\u043d\u0435\u043b\u044f\u043c\u0438.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/764116\/\"> https:\/\/habr.com\/ru\/articles\/764116\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u044f \u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u043b \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0447\u0430\u0441\u0442\u0438, \u0442\u0443\u0442 \u043c\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043e\u0431\u043c\u0435\u043d \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 MQTT.<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0427\u0442\u043e \u0436\u0435 \u0438\u0437 \u0441\u0435\u0431\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 MQTT? \u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u043e\u0431\u043c\u0435\u043d\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f\u043c\u0438. \u0414\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u043e\u043c\u0443 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0443. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043a\u0430\u043a \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0445, \u0442\u0430\u043a \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c, \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u044f\u0441\u044c \u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0433\u0440\u0443\u043f\u043f\u044b \u0432 \u0434\u0435\u0440\u0435\u0432\u0435 \u0438\u043b\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043a\u0435\u0442\u0430\u043c\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430:<\/p>\n<pre><code class=\"bash\">deb http:\/\/ppa.launchpad.net\/mosquitto-dev\/mosquitto-ppa\/ubuntu precise main deb-src http:\/\/ppa.launchpad.net\/mosquitto-dev\/mosquitto-ppa\/ubuntu precise main<\/code><\/pre>\n<p>\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0430\u043a\u0435\u0442\u043e\u0432 \u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0431\u0440\u043e\u043a\u0435\u0440 MQTT Mosquitto \u043d\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435:<\/p>\n<pre><code class=\"bash\">apt-get update apt-get install mosquitto<\/code><\/pre>\n<p>\u041d\u0430 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0444\u0430\u0439\u043b\u044b \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u043a\u0438 \u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 <code>libmosquitto-dev<\/code>:<\/p>\n<pre><code class=\"bash\">sudo apt install libmosquitto-dev apt source libmosquitto-dev<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0443\u044e \u043f\u0430\u043f\u043a\u0443 \u0441 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0430\u043c\u0438 \u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u043f\u043e\u0434 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0434\u043b\u044f <code>arm-linux-gnueabihf<\/code>:<\/p>\n<pre><code class=\"bash\">make WITH_TLS=no WITH_CJSON=no WITH_STATIC_LIBRARIES=yes<\/code><\/pre>\n<p>\u0412 \u043f\u0430\u043f\u043a\u0435 <code>.\/lib\/cpp\/<\/code>\u0432\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0432\u0441\u0435 \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u043f\u0440\u043e\u0435\u043a\u0442. \u041d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0444\u0430\u0439\u043b <code>libmosquitto.a<\/code>.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c, \u0447\u0442\u043e \u0436\u0435 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0435 \u0432\u044b\u0445\u043e\u0434\u0435? \u0420\u0430\u0437\u043b\u043e\u0436\u0438\u043c \u0432\u0441\u0435 \u043f\u043e \u043f\u0443\u043d\u043a\u0442\u0430\u043c:<\/p>\n<ul>\n<li>\n<p>\u043d\u0430\u043c \u043d\u0430\u0434\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0430\u0434\u0440\u0435\u0441\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445;<\/p>\n<\/li>\n<li>\n<p>\u0431\u0443\u0434\u0435\u043c \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u044f\u0442\u044c \u0438\u0445 \u0437\u0430 \u0442\u043e\u043f\u0438\u043a\u0430\u043c\u0438;<\/p>\n<\/li>\n<li>\n<p>\u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f, \u0430 \u043c\u043e\u0436\u0435\u043c \u0438 \u043d\u0435\u0442;<\/p>\n<\/li>\n<li>\n<p>\u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u043d\u0430 \u0431\u0440\u043e\u043a\u0435\u0440\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\u041e\u043f\u0438\u0448\u0435\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0430\u043c\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439, \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0438 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438, \u0434\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0430\u0434\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u0430 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439:<\/p>\n<pre><code class=\"cpp\">template&lt;typename T> struct Value {     Value(string topic,T old, T* current,int precision):         topic{topic},old{old},current{current},precision{precision} { }     ~Value() { }     string topic;     T old;     T* current;     uint64_t tFlag=timeSinceEpochMillisec();     int precision=2; };<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>timeSinceEpochMillisec()<\/code> \u0431\u044b\u043b\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u0430 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<p>\u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u043e\u0442 mosquittopp. \u0417\u0430\u0434\u0430\u0435\u043c Id \u0434\u043b\u044f \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u043a \u0431\u0440\u043e\u043a\u0435\u0440\u0443 \u043d\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435, \u0445\u043e\u0441\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 localhost, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0442\u043e\u043f\u0438\u043a device.<\/p>\n<pre><code class=\"cpp\">class MqttDevice:public mosquittopp::mosquittopp { public:     MqttDevice(string Id,string mqtt_host,string startTag=\"device\");     ~MqttDevice();<\/code><\/pre>\n<p>\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439\u00a0 \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u043e\u043c:<\/p>\n<pre><code class=\"cpp\">    list&lt; tuple&lt;string,string> > messages;<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0443\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0442\u043e\u043f\u0438\u043a \u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e.<\/p>\n<pre><code class=\"cpp\">    void addSubscribed(string topic, volatile bool* value);     void addSubscribed(string topic, volatile float* value);     void addSubscribed(string topic, volatile uint16_t* value);     void add(string topic,volatile bool* value);     void add(string topic,volatile uint16_t* value);     void add(string topic,volatile float* value,int precision=1);     void add(string topic,volatile double* value,int precision=1);<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u043b\u043d\u044b\u0439 \u0444\u0430\u0439\u043b <code>MqttDevice.h<\/code>:<\/p>\n<pre><code class=\"cpp\">class MqttDevice:public mosquittopp::mosquittopp { public:     MqttDevice(string Id,string mqtt_host,string startTag=\"device\");     ~MqttDevice();     void setTag(string topic,string value);     void message(string name, volatile double value);     void message(string name, volatile float value);     void message(string name, volatile bool value);     void message(string name, string value);     list&lt; tuple&lt;string,string> > messages;     void on_connect(int rc);     void on_message(const struct mosquitto_message* message);     void on_subscribe(int mid,int qos_count,const int* granted_qos);     void sendMessage(string topic, string&amp; message);     void sendMessage(string&amp; topic,char* message, int msgLength);     void addSubscribed(string topic, volatile bool* value);     void addSubscribed(string topic, volatile float* value);     void add(string topic,volatile bool* value);     void add(string topic,volatile float* value,int precision=1);     void add(string topic,volatile double* value,int precision=1); protected:     uint64_t tBool=0,tFloat=0,tDouble=0;     bool _exit; private:     string mqtt_host;     list&lt;Value&lt;volatile bool> > lwBool;     list&lt;Value&lt;volatile float> > lwFloat;     list&lt;Value&lt;volatile bool> > lrBool;     list&lt;Value&lt;volatile float> > lrFloat;     list&lt;Value&lt;volatile double> > lrDouble;     struct mosquitto *mosq;     string startTag;     void mosq_loop();     thread _threadMosquitto; }; <\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u0438 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u043c\u044b \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0443\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u043f\u043e\u0442\u043e\u043a\u0430 \u0438 \u0435\u0433\u043e \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435.<\/p>\n<pre><code class=\"cpp\">MqttDevice::MqttDevice(string Id,string mqtt_host,string startTag) : startTag{startTag},mqtt_host{mqtt_host}, mosquittopp((Id+to_string(rand())).c_str()) {     _exit=false;     int rc=connect(mqtt_host.c_str(),mqtt_port,65535);     printf(\"MqttDevice connect %d\\n\",rc);     _threadMosquitto = thread(&amp;MqttDevice::mosq_loop,this); } MqttDevice::~MqttDevice() {     _exit=true;     _threadMosquitto.join();     printf(\"MqttDevice delete\\n\"); }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 MqttDevice.cpp:<\/p>\n<pre><code class=\"cpp\">#define mqtt_port 1883  MqttDevice::MqttDevice(string Id,string mqtt_host,string startTag) : startTag{startTag},mqtt_host{mqtt_host}, mosquittopp((Id+to_string(rand())).c_str()) {     _exit=false;     int rc=connect(mqtt_host.c_str(),mqtt_port,65535);     printf(\"MqttDevice connect %d\\n\",rc);     _threadMosquitto = thread(&amp;MqttDevice::mosq_loop,this); } MqttDevice::~MqttDevice() {     _exit=true;     _threadMosquitto.join();     printf(\"MqttDevice delete\\n\"); } void MqttDevice::addSubscribed(string topic, volatile bool* value) {     lwBool.push_back({topic,false,value,0});     if(subscribe(0,topic.c_str(),0)==MOSQ_ERR_SUCCESS) printf(\"[Mosquitto] Subscribing to topic %s\\n\",topic.c_str()); } void MqttDevice::addSubscribed(string topic, volatile float* value) {     lwFloat.push_back({topic,0.0f,value,1});     if(subscribe(0,topic.c_str(),0)==MOSQ_ERR_SUCCESS) printf(\"[Mosquitto] Subscribing to topic %s\\n\",topic.c_str()); } void MqttDevice::add(string topic,volatile bool* value) {     lrBool.push_back({topic,false,value,0}); } void MqttDevice::add(string topic,volatile float* value, int precision) {     lrFloat.push_back({topic,0.0f,value,precision}); } void MqttDevice::add(string topic,volatile double* value, int precision) {     lrDouble.push_back({topic,0.0,value,precision}); } void MqttDevice::on_connect(int rc) {     int err=0;         printf(\"[Mosquitto] Connect. rc=%d\\n\",rc);     if(rc==0) {         for(auto&amp; v:lwBool) {             err=subscribe(0,v.topic.c_str(),0);             if(err==MOSQ_ERR_SUCCESS) printf(\"[Mosquitto] Subscribing to topic %s\\n\",v.topic.c_str());             else {reconnect();return;}             nsleep(10);         }         for(auto&amp; v:lwFloat) {             err=subscribe(0,v.topic.c_str(),0);             if(err==MOSQ_ERR_SUCCESS) printf(\"[Mosquitto] Subscribing to topic %s\\n\",v.topic.c_str());             else {reconnect();return;}             nsleep(10);         }     }     else         printf(\"[Mosquitto] Connection failed. Aborting subscribing.\\n\"); } void MqttDevice::on_message(const struct mosquitto_message *message) {     string tag=message->topic;     string value=string((const char*)message->payload,message->payloadlen);     setTag(tag,value); } void MqttDevice::mosq_loop() {     int rc;     lib_init();     printf(\"[Mosquitto] Start loop...\\n\");     while(!_exit) {         uint64_t now = timeSinceEpochMillisec();           rc=loop();         if(rc) {             printf(\"[Mosquitto] Disconnected. Trying to reconnect...\\n\");             nsleep(500);             reconnect();             nsleep(500);             continue;         }         while(messages.size()>0) {             rc=publish(NULL,get&lt;0>(messages.front()).c_str(),strlen(get&lt;1>(messages.front()).c_str()),(const uint8_t*)get&lt;1>(messages.front()).c_str(),0,true);             if(rc) {                 printf(\"[Mosquitto] Error send message\\n\");                 break;             }             else                 messages.pop_front();         }         if(now-tBool>300) {             tBool=now;             for(auto &amp;v:lrBool) {                 if(v.current==NULL) continue;                 if(v.old!=*v.current || now-v.tFlag>3000) {                     v.tFlag=now;                     v.old=*v.current;                     message(v.topic,*v.current);                 }             }             for(auto &amp;v:lwBool) {                 if(v.old!=*v.current || now-v.tFlag>3000) {                     v.tFlag=now;                     v.old=*v.current;                     message(v.topic,*v.current);                 }             }         }         if(now-tFloat>500) {             tFloat=now;             for(auto &amp;v:lrFloat) {                 float r=round(*v.current*pow(10,v.precision))\/pow(10,v.precision);                 if(v.old!=r || now-v.tFlag>5000) {                     v.tFlag=now;                     v.old=r;                     message(v.topic,r);                 }             }             for(auto &amp;v:lrDouble) {                 double r=round(*v.current*pow(10,v.precision))\/pow(10,v.precision);                 if(v.old!=r || now-v.tFlag>5000) {                     v.tFlag=now;                     v.old=r;                     message(v.topic,r);                 }             }             for(auto &amp;v:lwFloat) {                 float r=round(*v.current*pow(10,v.precision))\/pow(10,v.precision);                 if(v.old!=r || now-v.tFlag>5000) {                     v.tFlag=now;                     v.old=r;                     message(v.topic,r);                 }             }         }     }     lib_cleanup();     printf(\"[Mosquitto] Exit from mosquitto loop.\\n\"); } void MqttDevice::message(string name, volatile float value) {     messages.push_back(make_tuple(name,to_string(value))); } void MqttDevice::message(string name, volatile double value) {     messages.push_back(make_tuple(name,to_string(value))); } void MqttDevice::message(string name, volatile bool value) {     messages.push_back(make_tuple(name,(value?\"true\":\"false\"))); } void MqttDevice::message(string name, string value) {    <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-356145","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/356145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=356145"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/356145\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=356145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=356145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=356145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}