Code cleaning
This commit is contained in:
@@ -16,11 +16,6 @@
|
|||||||
* cons - Takes more memory
|
* cons - Takes more memory
|
||||||
* - a bit hard to understand
|
* - a bit hard to understand
|
||||||
*
|
*
|
||||||
* This sounds crazy: a mqtt mqtt that do not need a broker !
|
|
||||||
* The use case arise when one ESP wants to publish topics and subscribe to them at the same time.
|
|
||||||
* Without broker, the ESP won't react to its own topics.
|
|
||||||
*
|
|
||||||
* TinyMqtt mqtt allows this use case to work.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <my_credentials.h>
|
#include <my_credentials.h>
|
||||||
@@ -28,18 +23,26 @@
|
|||||||
std::string topic="sensor/temperature";
|
std::string topic="sensor/temperature";
|
||||||
|
|
||||||
void onPublish(const MqttClient* srce, const Topic& topic, const char* payload, size_t length)
|
void onPublish(const MqttClient* srce, const Topic& topic, const char* payload, size_t length)
|
||||||
{ Serial << "--> " << srce->id().c_str() << ": ======> received " << topic.c_str() << endl; }
|
{
|
||||||
|
Serial << "--> " << srce->id().c_str() << ": ======> received " << topic.c_str();
|
||||||
|
if (payload) Serial << ", payload[" << length << "]=[";
|
||||||
|
while(length--)
|
||||||
|
{
|
||||||
|
const char c=*payload++;
|
||||||
|
if (c!=10 and c!=13 and c <32) Serial << '?';
|
||||||
|
Serial << *payload++;
|
||||||
|
}
|
||||||
|
Serial<< endl;
|
||||||
|
}
|
||||||
|
|
||||||
std::map<std::string, MqttClient*> clients;
|
std::map<std::string, MqttClient*> clients;
|
||||||
std::map<std::string, MqttBroker*> brokers;
|
std::map<std::string, MqttBroker*> brokers;
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial << endl << endl << endl
|
Serial << endl << endl << endl
|
||||||
<< "Demo started. Type help for more..." << endl
|
|
||||||
<< "Connecting to '" << ssid << "' ";
|
<< "Connecting to '" << ssid << "' ";
|
||||||
|
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
@@ -49,6 +52,7 @@ void setup()
|
|||||||
{ Serial << '-'; delay(500); }
|
{ Serial << '-'; delay(500); }
|
||||||
|
|
||||||
Serial << "Connected to " << ssid << "IP address: " << WiFi.localIP() << endl;
|
Serial << "Connected to " << ssid << "IP address: " << WiFi.localIP() << endl;
|
||||||
|
Serial << "Type help for more..." << endl;
|
||||||
|
|
||||||
MqttBroker* broker = new MqttBroker(1883);
|
MqttBroker* broker = new MqttBroker(1883);
|
||||||
broker->begin();
|
broker->begin();
|
||||||
@@ -92,7 +96,6 @@ std::string getip(std::string& str, const char* if_empty=nullptr, char sep=' ')
|
|||||||
std::string addr=getword(str, if_empty, sep);
|
std::string addr=getword(str, if_empty, sep);
|
||||||
std::string ip=addr;
|
std::string ip=addr;
|
||||||
std::vector<std::string> build;
|
std::vector<std::string> build;
|
||||||
bool ok=true;
|
|
||||||
while(ip.length())
|
while(ip.length())
|
||||||
{
|
{
|
||||||
std::string b=getword(ip,nullptr,'.');
|
std::string b=getword(ip,nullptr,'.');
|
||||||
|
|||||||
@@ -453,7 +453,9 @@ if (message.type() != MqttMessage::Type::PingReq && message.type() != MqttMessag
|
|||||||
{
|
{
|
||||||
callback(this, published, nullptr, 0); // TODO send the real payload
|
callback(this, published, nullptr, 0); // TODO send the real payload
|
||||||
}
|
}
|
||||||
// TODO should send PUBACK
|
message.create(MqttMessage::Type::PubAck);
|
||||||
|
// TODO re-add packet identifier if any
|
||||||
|
message.sendTo(this);
|
||||||
bclose = false;
|
bclose = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -616,7 +618,7 @@ void MqttMessage::encodeLength(char* msb, int length)
|
|||||||
|
|
||||||
MqttError MqttMessage::sendTo(MqttClient* client)
|
MqttError MqttMessage::sendTo(MqttClient* client)
|
||||||
{
|
{
|
||||||
if (buffer.size()>2)
|
if (buffer.size())
|
||||||
{
|
{
|
||||||
debug("sending " << buffer.size() << " bytes");
|
debug("sending " << buffer.size() << " bytes");
|
||||||
encodeLength(&buffer[1], buffer.size()-2);
|
encodeLength(&buffer[1], buffer.size()-2);
|
||||||
|
|||||||
Reference in New Issue
Block a user