Может кому пригодится Код (C++): /* uMQTTBroker demo for Arduino (C++-style) https://github.com/martin-ger/uMQTTBroker The program defines a custom broker class with callbacks, starts it, subscribes locally to anything, and publishs a topic every second. Try to connect from a remote client and publish something - the console will show this as well. */ #include "SSD1306.h" #include <ESP8266WiFi.h> #include "uMQTTBroker.h" SSD1306 display(0x3c, D2, D1); /* Your WiFi config here */ int counter = 0; int printMin; int printHour; int printSek; int printDauy; char ssid[] = "Name WiFi"; // your network SSID (name) char pass[] = "*******"; // your network password bool WiFiAP = false; // Do yo want the ESP as AP? unsigned long previousMillis = 0; /* Custom broker class with overwritten callback functions */ class myMQTTBroker: public uMQTTBroker { public: virtual bool onConnect(IPAddress addr, uint16_t client_count) { Serial.println(addr.toString() + " connected"); return true; } virtual void onDisconnect(IPAddress addr, String client_id) { Serial.println(addr.toString() + " (" + client_id + ") disconnected"); } virtual bool onAuth(String username, String password, String client_id) { Serial.println("Username/Password/ClientId: " + username + "/" + password + "/" + client_id); return true; } virtual void onData(String topic, const char *data, uint32_t length) { char data_str[length + 1]; os_memcpy(data_str, data, length); data_str[length] = '\0'; Serial.println("received topic '" + topic + "' with data '" + (String)data_str + "'"); //printClients(); } // Sample for the usage of the client info methods virtual void printClients() { for (int i = 0; i < getClientCount(); i++) { IPAddress addr; String client_id; getClientAddr(i, addr); getClientId(i, client_id); Serial.println("Client " + client_id + " on addr: " + addr.toString()); } } }; myMQTTBroker myBroker; /* WiFi init stuff */ void startWiFiClient() { Serial.println("Connecting to " + (String)ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: " + WiFi.localIP().toString()); display.clear(); display.setTextAlignment(TEXT_ALIGN_CENTER); display.setFont(ArialMT_Plain_16); display.drawString(64, 0, "WiFi connected"); display.drawString(64, 16, "IP:" + WiFi.localIP().toString()); display.display(); } void startWiFiAP() { WiFi.mode(WIFI_AP); WiFi.softAP(ssid, pass); Serial.println("AP started"); Serial.println("IP address: " + WiFi.softAPIP().toString()); } void setup() { Serial.begin(115200); Serial.println(); Serial.println(); display.init(); display.flipScreenVertically(); // Start WiFi if (WiFiAP) startWiFiAP(); else startWiFiClient(); // Start the broker Serial.println("Starting MQTT broker"); myBroker.init(); /* Subscribe to anything */ myBroker.subscribe("#"); } void loop() { // wait a second unsigned long currentMillis = millis(); //read current time if (currentMillis - previousMillis >= 1000) { //if current time - last time > 1 sec previousMillis = currentMillis; counter++; printSek = counter % 60; printMin = (counter / 60) % 60; printHour = (counter / 3600) % 24; printDauy = (counter / 86400); /* Publish the counter value as String */ myBroker.publish("broker/counter", (String)counter); display.clear(); display.setTextAlignment(TEXT_ALIGN_CENTER); display.setFont(ArialMT_Plain_16); display.drawString(64, 0, "WiFi connected"); display.drawString(64, 16, "IP:" + WiFi.localIP().toString()); display.drawString(64, 42, (String)printDauy + "d " + (String)printHour + ":" + (String)printMin + ":" + (String)printSek); display.display(); } }
Если ты переопределяешь виртуальную функцию базового класса, никада так не пиши Код (C++): virtual void onDisconnect(IPAddress addr, String client_id) { всегда пиши так Код (C++): void onDisconnect(IPAddress addr, String client_id) override { это избавит тебя впаслецтвии от множества труднообнаруживаемых ошибок.