Release 0.3.0

clients can now connect to outside.
bug fixed for broker (pings etc.)
crashes fixed when clients where removed
More examples added (the tinymqtt-test is great)
This commit is contained in:
hsaturn
2021-03-21 13:50:42 +01:00
parent 4e629bbc1e
commit 428eb51850
7 changed files with 632 additions and 71 deletions

View File

@@ -0,0 +1,90 @@
#include <TinyMqtt.h> // https://github.com/hsaturn/TinyMqtt
#include <Streaming.h> // https://github.com/janelia-arduino/Streaming
// TODO should be renamed to most-complete setup
/**
* Local broker that accept connections
*
* pros - Reduces internal latency (when publish is received by the same ESP)
* - Reduces wifi traffic
* - No need to have an external broker
* - can still report to a 'main' broker (TODO see documentation that have to be written)
* - accepts external clients
*
* cons - Takes more memory
* - 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>
std::string topic="sensor/temperature";
MqttBroker broker(1883);
MqttClient mqtt_a(&broker);
MqttClient mqtt_b(&broker);
void onPublishA(const Topic& topic, const char* payload, size_t length)
{ Serial << "--> A Received " << topic.c_str() << endl; }
void onPublishB(const Topic& topic, const char* payload, size_t length)
{ Serial << "--> B Received " << topic.c_str() << endl; }
void setup()
{
Serial.begin(115200);
delay(500);
Serial << "Clients with wifi " << endl;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { Serial << '-'; delay(500); }
Serial << "Connected to " << ssid << "IP address: " << WiFi.localIP() << endl;
broker.begin();
mqtt_a.setCallback(onPublishA);
mqtt_a.subscribe(topic);
mqtt_b.setCallback(onPublishB);
mqtt_b.subscribe(topic);
}
void loop()
{
broker.loop();
mqtt_a.loop();
mqtt_b.loop();
// ============= client A publish ================
static const int intervalA = 50000;
static uint32_t timerA = millis() + intervalA;
if (millis() > timerA)
{
Serial << "A is publishing " << topic.c_str() << endl;
timerA += intervalA;
mqtt_a.publish(topic);
}
// ============= client B publish ================
static const int intervalB = 30000; // will send topic each 5000 ms
static uint32_t timerB = millis() + intervalB;
if (millis() > timerB)
{
Serial << "B is publishing " << topic.c_str() << endl;
timerB += intervalB;
mqtt_b.publish(topic, std::string(String(15+millis()%10).c_str()));
}
}

View File

@@ -1,9 +1,7 @@
#include <ESP8266WiFi.h>
#include "TinyMqtt.h" // https://github.com/hsaturn/TinyMqtt
#include <Streaming.h> // https://github.com/janelia-arduino/Streaming
const char *ssid = ; // Put here your wifi SSID ("ssid")
const char *password = ; // Put here your Wifi password ("pwd")
#include <my_credentials.h>
#define PORT 1883
MqttBroker broker(PORT);

View File

@@ -2,31 +2,35 @@
#include "TinyMqtt.h" // https://github.com/hsaturn/TinyMqtt
#include <Streaming.h> // https://github.com/janelia-arduino/Streaming
const char *ssid = ; // Put here your wifi SSID ("ssid")
const char *password = ; // Put here your Wifi password ("pwd")
/** Simple Client
*
* This is the simplest Mqtt client configuration
*
* pro - small memory footprint (both ram and flash)
* - very simple to setup and use
*
* cons - cannot work without wifi connection
* - stop working if broker is down
* - local publishes takes more time (because they go outside)
*/
#define PORT 1883
MqttBroker broker(PORT);
#include <my_credentials.h>
void setup()
{
Serial.begin(115200);
delay(500);
Serial << "Simple clients with wifi " << endl;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial << '.';
delay(500);
}
Serial << "Connected to " << ssid << "IP address: " << WiFi.localIP() << endl;
while (WiFi.status() != WL_CONNECTED)
{ delay(500); Serial << '.'; }
broker.begin();
Serial << "Broker ready : " << WiFi.localIP() << " on port " << PORT << endl;
Serial << "Connected to " << ssid << "IP address: " << WiFi.localIP() << endl;
}
void loop()
{
broker.loop();
}

View File

@@ -0,0 +1,326 @@
#include <TinyMqtt.h> // https://github.com/hsaturn/TinyMqtt
#include <Streaming.h> // https://github.com/janelia-arduino/Streaming
#include <map>
/**
* Local broker that accept connections
*
* pros - Reduces internal latency (when publish is received by the same ESP)
* - Reduces wifi traffic
* - No need to have an external broker
* - can still report to a 'main' broker (TODO see documentation that have to be written)
* - accepts external clients
*
* cons - Takes more memory
* - 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>
std::string topic="sensor/temperature";
MqttBroker broker(1883);
void onPublish(const MqttClient* srce, const Topic& topic, const char* payload, size_t length)
{ Serial << "--> " << srce->id().c_str() << ": ======> received " << topic.c_str() << endl; }
void setup()
{
Serial.begin(115200);
delay(500);
Serial << endl << endl << endl
<< "Demo started. Type help for more..." << endl
<< "Connecting to '" << ssid << "' ";
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{ Serial << '-'; delay(500); }
Serial << "Connected to " << ssid << "IP address: " << WiFi.localIP() << endl;
broker.begin();
}
std::string getword(std::string& str, const char* if_empty=nullptr)
{
std::string sword;
while(str.length() && str[0]!=' ')
{
sword += str[0]; str.erase(0,1);
}
while(str[0]==' ') str.erase(0,1);
if (if_empty and sword.length()==0) return if_empty;
return sword;
}
class automatic
{
public:
automatic(MqttClient* clt, uint32_t intervl)
: client(clt), topic_(::topic)
{
interval(intervl);
autos[clt] = this;
}
void interval(uint32_t new_interval)
{
interval_ = new_interval;
if (interval_<1000) interval_=1000;
timer_ = millis() + interval_;
}
void loop_()
{
if (!bon) return;
if (interval_ && millis() > timer_)
{
Serial << "AUTO PUBLISH " << interval_ << endl;
timer_ += interval_;
client->publish(topic_, std::string(String(15+millis()%10).c_str()));
}
}
void topic(std::string new_topic) { topic_ = new_topic; }
static void loop()
{
for(auto it: autos)
it.second->loop_();
}
static void command(MqttClient* who, std::string cmd)
{
automatic* autop = nullptr;
if (autos.find(who) != autos.end())
{
autop=autos[who];
}
std::string s = getword(cmd);
if (compare(s, "create"))
{
std::string seconds=getword(cmd, "10000");
if (autop) delete autop;
std::string top = getword(cmd, ::topic.c_str());
autos[who] = new automatic(who, atol(seconds.c_str()));
autos[who]->topic(top);
autos[who]->bon=true;
Serial << "New auto (" << seconds.c_str() << " topic:" << top.c_str() << ')' << endl;
}
else if (autop)
{
while(s.length())
{
if (s=="on")
{
autop->bon = true;
autop->interval(autop->interval_);
}
else if (s=="off")
autop->bon=false;
else if (s=="interval")
{
int32_t i=atol(getword(cmd).c_str());
if (i)
autop->interval(atol(s.c_str()));
else
Serial << "Bad value" << endl;
}
else if (s=="view")
{
Serial << " automatic "
<< (int32_t)autop->client
<< " interval " << autop->interval_
<< (autop->bon ? " on" : " off") << endl;
}
else
{
Serial << "Unknown auto command (" << s.c_str() << ")" << endl;
break;
}
s=getword(cmd);
}
}
else if (who==nullptr)
{
for(auto it: autos)
command(it.first, s+' '+cmd);
}
else
Serial << "what ? (" << s.c_str() << ")" << endl;
}
static void help()
{
Serial << " auto [$id] on/off" << endl;
Serial << " auto [$id] view" << endl;
Serial << " auto [$id] create [millis] [topic]" << endl;
}
private:
MqttClient* client;
uint32_t interval_;
uint32_t timer_;
std::string topic_;
bool bon=false;
static std::map<MqttClient*, automatic*> autos;
};
std::map<MqttClient*, automatic*> automatic::autos;
bool compare(std::string s, const char* cmd)
{
if (s.length()==0 or s.length()>strlen(cmd)) return false;
return strncmp(cmd, s.c_str(), s.length())==0;
}
std::map<std::string, MqttClient*> clients;
using ClientFunction = void(*)(std::string& cmd, MqttClient* publish);
void clientCommand(std::string& cmd, ClientFunction func, bool canBeNull=false)
{
std::string s=getword(cmd);
bool found = clients.find(s) != clients.end();
if (canBeNull && found==false)
{
cmd += ' ' + s;
}
if (found or canBeNull)
{
MqttClient* publish = publish = clients[s];
func(cmd, publish);
}
else
{
Serial << "client not found (" << s.c_str() << ")" << endl;
cmd="";
}
}
void loop()
{
broker.loop();
for(auto it: clients)
it.second->loop();
automatic::loop();
if (Serial.available())
{
static std::string cmd;
char c=Serial.read();
if (c==10 or c==14)
{
Serial << "------------------------------------------------------" << endl;
while(cmd.length())
{
std::string s = getword(cmd);
if (compare(s,"connect"))
{
clientCommand(cmd, [](std::string& cmd, MqttClient* publish)
{ publish->connect(getword(cmd,"192.168.1.40").c_str(), 1883); });
}
else if (compare(s,"publish"))
{
clientCommand(cmd, [](std::string& cmd, MqttClient* publish)
{ publish->publish(getword(cmd, topic.c_str())); });
}
else if (compare(s,"subscribe"))
{
clientCommand(cmd, [](std::string& cmd, MqttClient* publish)
{ publish->subscribe(getword(cmd, topic.c_str())); });
}
else if (compare(s, "view"))
{
clientCommand(cmd, [](std::string& cmd, MqttClient* publish)
{ publish->dump(); });
}
else if (compare(s, "auto"))
{
clientCommand(cmd, [](std::string& cmd, MqttClient* publish)
{ automatic::command(publish, cmd);
if (publish == nullptr)
cmd.clear();
}, true);
}
else if (compare(s, "new"))
{
std::string id=getword(cmd);
if (id.length())
{
MqttClient* client = new MqttClient(&broker);
client->id(id);
clients[id]=client;
client->setCallback(onPublish);
client->subscribe(topic);
}
else
Serial << "missing id" << endl;
cmd+=" ls";
}
else if (compare(s, "delete"))
{
s = getword(cmd);
auto it=clients.find(s);
if (it != clients.end())
{
delete it->second;
clients.erase(it);
cmd+=" ls";
}
else
Serial << "Unknown client (" << s.c_str() << ")" << endl;
}
else if (compare(s, "ls"))
{
Serial << "main : " << clients.size() << " client/s." << endl;
for(auto it: clients)
{
Serial << " "; it.second->dump();
}
broker.dump();
}
else if (compare(s, "reset"))
ESP.restart();
else if (compare(s,"help"))
{
Serial << "syntax:" << endl;
Serial << " new/delete $id" << endl;
Serial << " connect $id [ip]" << endl;
Serial << " subscribe $id [topic]" << endl;
Serial << " publish $id [topic]" << endl;
Serial << " view $id " << endl;
automatic::help();
Serial << endl;
Serial << " help" << endl;
Serial << " ls" << endl;
Serial << " reset" << endl;
Serial << endl;
Serial << " $id : name of the client." << endl;
Serial << " default topic is '" << topic.c_str() << "'" << endl;
Serial << endl;
}
else
{
Serial << "Unknown command (" << s.c_str() << ")" << endl;
}
}
}
else
{
cmd=cmd+c;
}
}
}