terrorsl / smqttbroker Goto Github PK
View Code? Open in Web Editor NEWSimple MQTT broker
License: MIT License
Simple MQTT broker
License: MIT License
Is there a way to send a message from broker to the clients or a special client? if not can you implement such solution?
As far as i see there is not client as such in the library to can do it?
Hi @terrorsl!
Faced some problem in the "advancebroker" example. I added one line to each of the event handlers:
class MyBroker: public sMQTTBroker
{
public:
bool onConnect (sMQTTClient * client, const std :: string & username, const std :: string & password)
{
// check username and password, if ok return true
Serial.println ("Somebody connected");
return true;
};
void onRemove (sMQTTClient *)
{
Serial.println ("Somebody disconnected");
};
void onPublish (const std :: string & topic, const std :: string & payload)
{
// client publish a message to the topic
// you can parse the message, save it to a database, or whatever, based on your goals
Serial.println ("Somebody posted");
}
};
I tried to write something to topics using mqtt-explorer, everything is fine, but the output contains 2 of 3 added lines:
05: 39: 40.848 -> RAM: 270876
05: 39: 41.048 -> Somebody connected
05: 39: 41.845 -> RAM: 268700
05: 40: 17.880 -> RAM: 267124
05: 40: 18.876 -> RAM: 268700
05: 40: 33.172 -> Somebody disconnected
05: 40: 33.903 -> RAM: 271060
i try to compile it but get follow errors:
In file included from .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTBroker.h:5:0, from .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:1: .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTClient.h: In constructor 'sMQTTClient::sMQTTClient(sMQTTBroker*, WiFiClient*)': .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTClient.h:44:15: error: 'sMQTTClient::_parent' will be initialized after [-Werror=reorder] sMQTTBroker *_parent; ^ .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTClient.h:39:7: error: 'bool sMQTTClient::mqtt_connected' [-Werror=reorder] bool mqtt_connected; ^ .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:3:1: error: when initialized here [-Werror=reorder] sMQTTClient::sMQTTClient(sMQTTBroker *parent, TCPClient *client):_parent(parent), mqtt_connected(false) Compiling .pio\build\m5stack-atom\FrameworkArduino\MD5Builder.cpp.o ^ .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp: In member function 'void sMQTTClient::processMessage()': .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:192:19: warning: unused variable 'msg_id' [-Wunused-variable] Compiling .pio\build\m5stack-atom\FrameworkArduino\Print.cpp.o unsigned short msg_id = (header[0] << 8) | header[1]; ^ .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:236:19: warning: unused variable 'qos' [-Wunused-variable] unsigned char qos = *payload++; ^ .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:223:19: warning: unused variable 'msg_id' [-Wunused-variable] unsigned short msg_id = (header[0] << 8) | header[1]; ^ .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:45:9: warning: enumeration value 'Unknown' not handled in switch [-Wswitch] switch (message.type()) ^ .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:45:9: warning: enumeration value 'ConnAck' not handled in switch [-Wswitch] .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:45:9: warning: enumeration value 'PubAck' not handled in switch [-Wswitch] .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:45:9: warning: enumeration value 'PubRec' not handled in switch [-Wswitch] .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:45:9: warning: enumeration value 'PubComp' not handled in switch [-Wswitch] .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:45:9: warning: enumeration value 'SubAck' not handled in switch [-Wswitch] .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:45:9: warning: enumeration value 'UnSuback' not handled in switch [-Wswitch] .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:45:9: warning: enumeration value 'PingResp' not handled in switch [-Wswitch] In file included from .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTClient.h:4:0, from .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTBroker.h:5, from .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:1: .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTMessage.h:67:32: warning: 'packeteIdent[1]' may be used uninitialized in this function [-Wmaybe-uninitialized] void add(char byte) { incoming(byte); } ^ .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:139:9: note: 'packeteIdent[1]' was declared here char packeteIdent[2]; ^ In file included from .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTClient.h:4:0, from .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTBroker.h:5, from .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:1: .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTMessage.h:67:32: warning: 'packeteIdent[0]' may be used uninitialized in this function [-Wmaybe-uninitialized] void add(char byte) { incoming(byte); } ^ .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:139:9: note: 'packeteIdent[0]' was declared here char packeteIdent[2]; ^ In file included from .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTBroker.h:4:0, from .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:1: .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTplatform.h: At global scope: .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTplatform.h:34:20: warning: 'SMQTTTAG' defined but not used [-Wunused-variable] static const char *SMQTTTAG = "sMQTTBroker"; ^ In file included from .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTClient.h:4:0, from .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTBroker.h:5, from .pio\libdeps\m5stack-atom\sMQTTBroker\src\sMQTTClient.cpp:1: .pio\libdeps\m5stack-atom\sMQTTBroker\src/sMQTTMessage.h:14:20: warning: 'debugMessageType' defined but not used [-Wunused-variable] static const char *debugMessageType[] = { ^ cc1plus.exe: some warnings being treated as errors *** [.pio\build\m5stack-atom\lib737\sMQTTBroker\sMQTTClient.cpp.o] Error 1
Hi,
This Broker looks nice, but i am getting error on while compiling on Arduino.
error: 'class WiFiServer' has no member named 'end'
_server->end();
^
exit status 1
Error compiling for board Generic ESP8266 Module.
Any help is appreciable.
thanks
Please make SSL connection possible for the broker
Hi,
This is just a question not an issue.
Is it possible to use something like mqtt.mybroker.io instead of IP address 192.168.4.1
thanks
Broker continues to send messages to the unsubscribed client and some times exception and reboot happend.
That is my code for esp32 board:
`#include"sMQTTBroker.h"
sMQTTBroker broker;
unsigned long timing;
void setup()
{
Serial.begin(115200);
const char* ssid = "My WiFi"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "12345678"; // The password of the Wi-Fi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
}
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
const unsigned short mqttPort=1883;
broker.init(mqttPort);
// all done
}
void loop()
{
if (millis() - timing > 5000){
timing = millis();
broker.publish("broker/pressure", String(760).c_str() );
broker.publish("broker/temperature", String(25).c_str() );
broker.publish("broker/humidity", String(40).c_str() );
broker.publish("broker/time", (String(12) + ":0" + String(0)).c_str() );
}
broker.update();
//delay(1000);
}`
and this is LOG message:
Connection established!
IP address: 192.168.8.112
[D][sMQTTBroker.cpp:27] update(): New Client
[D][sMQTTClient.cpp:50] processMessage(): message type:Connect(0x10)
[D][sMQTTClient.cpp:84] processMessage(): message clientId:android
[D][sMQTTClient.cpp:85] processMessage(): message keepTime:30
[D][sMQTTClient.cpp:104] processMessage(): message user:admin
[D][sMQTTClient.cpp:113] processMessage(): message password:pass
[D][sMQTTClient.cpp:50] processMessage(): message type:Subscribe(0x80)
[D][sMQTTClient.cpp:219] processMessage(): message id:1
[D][sMQTTClient.cpp:229] processMessage(): message topic:vakio/mode
[D][sMQTTClient.cpp:50] processMessage(): message type:Subscribe(0x80)
[D][sMQTTClient.cpp:219] processMessage(): message id:2
[D][sMQTTClient.cpp:229] processMessage(): message topic:broker/time
[D][sMQTTClient.cpp:50] processMessage(): message type:Subscribe(0x80)
[D][sMQTTClient.cpp:219] processMessage(): message id:3
[D][sMQTTClient.cpp:229] processMessage(): message topic:broker/temperature
[D][sMQTTClient.cpp:50] processMessage(): message type:Subscribe(0x80)
[D][sMQTTClient.cpp:219] processMessage(): message id:4
[D][sMQTTClient.cpp:229] processMessage(): message topic:broker/humidity
[D][sMQTTClient.cpp:50] processMessage(): message type:Subscribe(0x80)
[D][sMQTTClient.cpp:219] processMessage(): message id:5
[D][sMQTTClient.cpp:229] processMessage(): message topic:broker/pressure
[D][sMQTTBroker.cpp:248] publish(): topic broker/pressure Clients 1
[D][sMQTTBroker.cpp:248] publish(): topic broker/temperature Clients 1
[D][sMQTTBroker.cpp:248] publish(): topic broker/humidity Clients 1
[D][sMQTTBroker.cpp:248] publish(): topic broker/time Clients 1
[D][sMQTTClient.cpp:50] processMessage(): message type:Disconnect(0xe0)
[D][sMQTTBroker.cpp:58] update(): Clients 0
[D][sMQTTBroker.cpp:248] publish(): topic broker/humidity Clients 1
[D][sMQTTBroker.cpp:248] publish(): topic broker/time Clients 1
[D][sMQTTBroker.cpp:248] publish(): topic broker/humidity Clients 1
[D][sMQTTBroker.cpp:248] publish(): topic broker/time Clients 1
Did you have made any tests about performance? I mean how many clients can be connect on a single esp32 running the broker? Any informations available?
I am using Arduino IDE and ESP8266.
Previous version is compiling, but latest is producing the following error :
'UnSubscribe_sMQTTEventType' was not declared in this scope.
With previous version everything is working nicely only issue is the broker is able to receive short messages from cloud (though no such issue with local clients). The below message is not received properly
"Timer1": { "Enable": 0, "Mode": 0, "Time": "10:10", "Window": 0, "Days": "1000000", "Repeat": 1, "Output": 1, "Action": 0 }
but if length is reduced then no issue while receiving Timer1{"Enable":0,"Mode":0,"Time":"10:10"}
When using arduino / compile-sketches action, the default compiler settings includes some warnings being treated as errors
.
For a good library like sMQTTBroker, please amend these warnings, rather than having your users ignore them.
In file included from /home/sosandstrom/Arduino/libraries/sMQTTBroker/src/sMQTTClient.h:4,
[306](https://github.com/elmeter-cloud/device-8266/actions/runs/3128082717/jobs/5075472061#step:5:318)
from /home/sosandstrom/Arduino/libraries/sMQTTBroker/src/sMQTTBroker.h:5,
[307](https://github.com/elmeter-cloud/device-8266/actions/runs/3128082717/jobs/5075472061#step:5:319)
from /home/sosandstrom/github-runners/elmeter-cloud/_work/device-8266/device-8266/local_mqtt.h:4,
[308](https://github.com/elmeter-cloud/device-8266/actions/runs/3128082717/jobs/5075472061#step:5:320)
from /home/sosandstrom/github-runners/elmeter-cloud/_work/device-8266/device-8266/device-8266.ino:15:
[309](https://github.com/elmeter-cloud/device-8266/actions/runs/3128082717/jobs/5075472061#step:5:321)
/home/sosandstrom/Arduino/libraries/sMQTTBroker/src/sMQTTMessage.h:14:20: error: 'debugMessageType' defined but not used [-Werror=unused-variable]
[310](https://github.com/elmeter-cloud/device-8266/actions/runs/3128082717/jobs/5075472061#step:5:322)
static const char *debugMessageType[] = {
[311](https://github.com/elmeter-cloud/device-8266/actions/runs/3128082717/jobs/5075472061#step:5:323)
^~~~~~~~~~~~~~~~
[312](https://github.com/elmeter-cloud/device-8266/actions/runs/3128082717/jobs/5075472061#step:5:324)
In file included from /home/sosandstrom/Arduino/libraries/sMQTTBroker/src/sMQTTBroker.h:4,
[313](https://github.com/elmeter-cloud/device-8266/actions/runs/3128082717/jobs/5075472061#step:5:325)
from /home/sosandstrom/github-runners/elmeter-cloud/_work/device-8266/device-8266/local_mqtt.h:4,
[314](https://github.com/elmeter-cloud/device-8266/actions/runs/3128082717/jobs/5075472061#step:5:326)
from /home/sosandstrom/github-runners/elmeter-cloud/_work/device-8266/device-8266/device-8266.ino:15:
[315](https://github.com/elmeter-cloud/device-8266/actions/runs/3128082717/jobs/5075472061#step:5:327)
/home/sosandstrom/Arduino/libraries/sMQTTBroker/src/sMQTTplatform.h:39:20: error: 'SMQTTTAG' defined but not used [-Werror=unused-variable]
[316](https://github.com/elmeter-cloud/device-8266/actions/runs/3128082717/jobs/5075472061#step:5:328)
static const char *SMQTTTAG = "sMQTTBroker";
[317](https://github.com/elmeter-cloud/device-8266/actions/runs/3128082717/jobs/5075472061#step:5:329)
^~~~~~~~
[318](https://github.com/elmeter-cloud/device-8266/actions/runs/3128082717/jobs/5075472061#step:5:330)
Can you give me a fix idea to use the payload and/or topic as example from "void onPublish(const std::string &topic, const std::string &payload)" ? i have no luck to get this string and serial print it
Hello... I am new one.
It is not a issues.. Just only want to know.
I want to terminate client when client has wrong id or password.
how can i do? on broker
how can i use ethernet instead of wifi .. i want to use with WT32-ETH01
How i can use Ethernet and SmqttBroker together.
#include <ETH.h>
IPAddress local_ip(192, 168, 1, 112);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns1(8, 8, 8, 8);
IPAddress dns2 = (uint32_t)0x00000000;
ETH.config(local_ip, gateway, subnet, dns1, dns2);
Hi Terrorsl,
Very thanks for this code.
Could you give us an example, or simple description on MQTTConfig.h, wifi or BLE or other configuration, to quick start with your code?
The compilation is OK on ESP32.
Thanks
I have add this onConnect:
Serial.print("ESP32 FREE RAM: "); Serial.print(ESP.getFreeHeap() / 1024.0, 2); Serial.println("KB");
and connect and disconnect a client many times. As i can see the RAM goes smaller and smaller (even not much). May there is not correct free the memory on client disconnect? Can you double check that?
Excuse this question and thank you very much for your work first of all, but the publish event is not implemented right? Can you help me with this. I want to stop using deprecated onPublish. Again, thank you very much.
I made a build from arduino (other was platformio which can´t compile since some errors). On arduino compiling works but can´t connect from any Mqtt Client (i have try 3 different ones)
In advanced example is a event onRemove which never will fire (if client disconnected as example). Can you double check if disconnected clients really free the memory?
Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
Describe the solution you'd like
A clear and concise description of what you want to happen.
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context or screenshots about the feature request here.
I am able to connect my client to the server, but I cant receive any message from the same topic. How to solve it?
Now this lib is running at STA mode, can it use at the AP mode ?
That means I can use the ESP32 as the broker without another WLAN. thx.
Are there set anywhere into the code a max clients variable? i made some tests with several clients connected up to MQTTool where i get then Status: Dsconnected MOSQ_CONN_LOST on try to connect
Hi,
I need some help on my ESP8266 regarding two questions please:
This is how I created a filter (IF clause) with a wildcard. I tried the example below, but no sucess. Only works if I put the complete topic name such "tele/Sensor/RESULT"
String Topic_Name = "tele/+/RESULT";
if (e->Topic() == Topic_Name.c_str())
I use the code above on sMQTTPublicClientEvent event and then I filter by this unique topic. Exists any way to my MQTT boker subscribe itself this topic and then show the payload? Or the option above is the unique to achive that?
Thanks again and Best Regards
Hi, you do an amazing work. It is possible to modify the library to can receive my custom TCP Client and TCP Server? I would like to use Ethernet W5500 and ENC28J60 with ESP32.
Thanks you....
Oi. How to change the default name of ESP8266 (ESP_xxxxx, that the module generates)? For "sMQTTBroker" for exemplo. Thanks.
Hello, is it possible to limit the retained topic
As i see into code there are some debug included "SMQTT_LOGD" . How can i turn that on on platformIO to see this messages on serial monitor? may this help much more to find issues
Hi,
Latest library version with latest Arduino IDE.
Compiling I get this error:
C:\Users\XXX\Documents\Arduino\libraries\sMQTTBroker\src/sMQTTClient.h:24:35: error: 'TCPClient' has not been declared
24 | sMQTTClient(sMQTTBroker *parent, TCPClient *client);
| ^~~~~~~~~
C:\Users\XXX\Documents\Arduino\libraries\sMQTTBroker\src/sMQTTClient.h:43:2: error: 'TCPClient' does not name a type; did you mean 'DNSClient'?
43 | TCPClient *_client;
| ^~~~~~~~~
| DNSClient
Thank you!
Obviously I thought I knew more than I actually do. You would be kind to point me to some documents/answers?
Existing working system is HomeSeer 3 --> ESP8266 Broker --> TASMOTA device
Substituting the sMQTT BROKER
Can not get your ESP32 BROKER to connect to either end.
HS3 is obviously sending requests to sMQTT.... I seem to get nothing at the BROKER
TASMOTA device is sending requests to sMQTT... code detect nothing.
I assume there are some basics that I do not understand????
Thanks for reading... LZH
CODE FOLLOWS BELOW
#define IDSTRING "V 1 DEC 30 2022 CV_BROKER_ESP32TEST_Vxx "
#include<sMQTTBroker.h>
const char* zssid = "xxx"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* zpassword = "xxx"; // The password of the Wi-Fi network
// This code is LOCAL BROKER talking to DEVICE/CLIENTs and HS3
IPAddress LOCAL_BROKER_IP(10, 0, 0, 111);
IPAddress Zgateway(10, 0, 0, 1); // set gateway to match your network
IPAddress Zsubnet(255, 255, 255, 0); // set subnet mask to match your network
const unsigned short mqttPort = 1883;
class MyBroker :public sMQTTBroker
{
public:
bool onEvent(sMQTTEvent *event) override
{
Serial.println("GOT EVENT zzzz "); //
switch (event->Type())
{
case NewClient_sMQTTEventType:
{
Serial.println("NEW CLIENT EVENT "); //
sMQTTNewClientEvent *e = (sMQTTNewClientEvent*)event;
e->Login();
e->Password();
}
break;
case LostConnect_sMQTTEventType:
Serial.println("LOST WiFI Connection Try again.... "); //
WiFi.reconnect();
break;
case UnSubscribe_sMQTTEventType:
case Subscribe_sMQTTEventType:
{
Serial.println("zz subscribe "); //
sMQTTSubUnSubClientEvent *e = (sMQTTSubUnSubClientEvent*)event;
}
break;
}
return true;
}
};
MyBroker broker;
unsigned long Time;
unsigned long freeRam;
void setup()
{
Serial.begin(115200);
delay(333); // wait for port to settle
Serial.println(" "); //
//************************************************************
Serial.println(IDSTRING);
//...........................
//************************************************************
WiFi.config(LOCAL_BROKER_IP, Zgateway, Zsubnet);
WiFi.begin(zssid, zpassword);
while (WiFi.status() != WL_CONNECTED)
{ // Wait for the Wi-Fi to connect
Serial.println("Waiting for WiFi... " + String (zssid)); //
delay(1000);
}
broker.init(mqttPort);
Serial.print("WiFi Connected ...");
////Serial.print("IP address:\t");
Serial.print(WiFi.localIP());
Serial.println(" PORT is..." + String (mqttPort)); //
// all done
// your magic code
Time = millis();
freeRam = ESP.getFreeHeap();
};
// lll
void loop()
{
broker.update();
// your magic code
if (millis() - Time > 1000)
{
Time = millis();
if (ESP.getFreeHeap() != freeRam)
{
freeRam = ESP.getFreeHeap();
Serial.print("RAM:");
Serial.println(freeRam);
//zz added?? does nothing??
broker.publish("Topic", "message"); // added publish here
}
}
};
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.