Example when wifi is not connected
This commit is contained in:
64
examples/client-without-wifi/client-without-wifi.ino
Normal file
64
examples/client-without-wifi/client-without-wifi.ino
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
#include <TinyMqtt.h> // https://github.com/hsaturn/TinyMqtt
|
||||||
|
#include <Streaming.h> // https://github.com/janelia-arduino/Streaming
|
||||||
|
|
||||||
|
/** TinyMQTT allows a disconnected mode:
|
||||||
|
*
|
||||||
|
* In this example, local clients A and B are talking together, no need to be connected.
|
||||||
|
* A single ESP can use this to be able to comunicate with itself with the power
|
||||||
|
* of MQTT, and once connected still continue to work with others.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
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 << "init" << endl;
|
||||||
|
|
||||||
|
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 = 5000;
|
||||||
|
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 = 3000; // 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/hsaturn/TinyMqtt.git"
|
"url": "https://github.com/hsaturn/TinyMqtt.git"
|
||||||
},
|
},
|
||||||
"version": "0.1",
|
"version": "0.2",
|
||||||
"exclude": "",
|
"exclude": "",
|
||||||
"examples": "examples/*/*.ino",
|
"examples": "examples/*/*.ino",
|
||||||
"frameworks": "arduino",
|
"frameworks": "arduino",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
name=TinyMqtt
|
name=TinyMqtt
|
||||||
version=0.1.0
|
version=0.2.0
|
||||||
author=HSaturn <hsaturn@gmail.com>
|
author=HSaturn <hsaturn@gmail.com>
|
||||||
maintainer=HSaturn <hsaturn@gmail.com>
|
maintainer=HSaturn <hsaturn@gmail.com>
|
||||||
sentence=A tiny broker and client library for MQTT messaging.
|
sentence=A tiny broker and client library for MQTT messaging.
|
||||||
|
|||||||
@@ -93,11 +93,29 @@ void MqttBroker::loop()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MqttBroker::publish(const Topic& topic, MqttMessage& msg)
|
// Should be called for inside and outside incoming publishes (all)
|
||||||
|
void MqttBroker::publish(const MqttClient* source, const Topic& topic, MqttMessage& msg)
|
||||||
{
|
{
|
||||||
Serial << " publish" << __LINE__ << endl;
|
|
||||||
for(auto client: clients)
|
for(auto client: clients)
|
||||||
client->publish(topic, msg);
|
{
|
||||||
|
bool doit = false;
|
||||||
|
if (broker && broker->connected()) // Connected: R2 R3 R5 R6
|
||||||
|
{
|
||||||
|
if (!client->isLocal()) // R2 go outside allowed
|
||||||
|
doit = true;
|
||||||
|
else // R3 any client to outside allowed
|
||||||
|
doit = true;
|
||||||
|
}
|
||||||
|
else // Disconnected: R3 R4 R5
|
||||||
|
{
|
||||||
|
if (!source->isLocal()) // R3
|
||||||
|
doit = true;
|
||||||
|
else if (client->isLocal()) // R4 local -> local
|
||||||
|
doit = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (doit) client->publish(topic, msg); // goes outside R2
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MqttBroker::compareString(
|
bool MqttBroker::compareString(
|
||||||
@@ -234,7 +252,7 @@ void MqttClient::processMessage()
|
|||||||
if (qos) payload+=2; // ignore packet identifier if any
|
if (qos) payload+=2; // ignore packet identifier if any
|
||||||
// TODO reset DUP
|
// TODO reset DUP
|
||||||
// TODO reset RETAIN
|
// TODO reset RETAIN
|
||||||
parent->publish(published, message);
|
parent->publish(this, published, message);
|
||||||
// TODO should send PUBACK
|
// TODO should send PUBACK
|
||||||
bclose = false;
|
bclose = false;
|
||||||
}
|
}
|
||||||
@@ -270,15 +288,14 @@ bool Topic::matches(const Topic& topic) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// publish from local client to a broker
|
// publish from local client
|
||||||
void MqttClient::publish(const Topic& topic, const char* payload, size_t pay_length)
|
void MqttClient::publish(const Topic& topic, const char* payload, size_t pay_length)
|
||||||
{
|
{
|
||||||
Serial << " publish" << __LINE__ << endl;
|
|
||||||
message.create(MqttMessage::Publish);
|
message.create(MqttMessage::Publish);
|
||||||
message.add(topic);
|
message.add(topic);
|
||||||
message.add(payload, pay_length);
|
message.add(payload, pay_length);
|
||||||
if (parent)
|
if (parent)
|
||||||
parent->publish(topic, message);
|
parent->publish(this, topic, message);
|
||||||
else if (client)
|
else if (client)
|
||||||
publish(topic, message);
|
publish(topic, message);
|
||||||
else
|
else
|
||||||
@@ -288,16 +305,12 @@ void MqttClient::publish(const Topic& topic, const char* payload, size_t pay_len
|
|||||||
// republish a received publish if it matches any in subscriptions
|
// republish a received publish if it matches any in subscriptions
|
||||||
void MqttClient::publish(const Topic& topic, MqttMessage& msg)
|
void MqttClient::publish(const Topic& topic, MqttMessage& msg)
|
||||||
{
|
{
|
||||||
Serial << " publish " << topic.c_str() << __LINE__ << endl;
|
|
||||||
for(const auto& subscription: subscriptions)
|
for(const auto& subscription: subscriptions)
|
||||||
{
|
{
|
||||||
Serial << " check " << subscription.c_str() << __LINE__ << endl;
|
|
||||||
if (subscription.matches(topic))
|
if (subscription.matches(topic))
|
||||||
{
|
{
|
||||||
Serial << " matche !" << endl;
|
|
||||||
if (client)
|
if (client)
|
||||||
{
|
{
|
||||||
// Serial << "Republishing " << topic.str().c_str() << " to " << clientId.c_str() << endl;
|
|
||||||
msg.sendTo(this);
|
msg.sendTo(this);
|
||||||
}
|
}
|
||||||
else if (callback)
|
else if (callback)
|
||||||
|
|||||||
@@ -125,6 +125,8 @@ class MqttClient
|
|||||||
void subscribe(Topic topic) { subscriptions.insert(topic); }
|
void subscribe(Topic topic) { subscriptions.insert(topic); }
|
||||||
void unsubscribe(Topic& topic);
|
void unsubscribe(Topic& topic);
|
||||||
|
|
||||||
|
bool isLocal() const { return client==nullptr; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class MqttBroker;
|
friend class MqttBroker;
|
||||||
MqttClient(MqttBroker* parent, WiFiClient& client);
|
MqttClient(MqttBroker* parent, WiFiClient& client);
|
||||||
@@ -146,8 +148,33 @@ class MqttClient
|
|||||||
CallBack callback;
|
CallBack callback;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/***********************************************
|
||||||
|
* R1 - accept external cnx
|
||||||
|
* R2 - allows all clients pusblish to go outside
|
||||||
|
* R3 - allows ext publish to all clients
|
||||||
|
* R4 - allows local publish to local clients
|
||||||
|
* R5 - tries to connect elsewhere (*)
|
||||||
|
* R6 - disconnect external clients
|
||||||
|
* ---------------------------------------------
|
||||||
|
* (*) single client or ip range
|
||||||
|
* ---------------------------------------------
|
||||||
|
*
|
||||||
|
* =============================================+
|
||||||
|
* | connected | not connected |
|
||||||
|
* -------------+---------------+---------------+
|
||||||
|
* proxy broker | R2 R3 R5 R6 | R3 R4 R5 |
|
||||||
|
* normal broker| R2 R3 R5 R6 | R1 R3 R4 R5 |
|
||||||
|
* -------------+---------------+---------------+
|
||||||
|
*
|
||||||
|
*/
|
||||||
class MqttBroker
|
class MqttBroker
|
||||||
{
|
{
|
||||||
|
enum State
|
||||||
|
{
|
||||||
|
Disconnected, // Also the initial state
|
||||||
|
Connecting, // connect and sends a fake publish to avoid circular cnx
|
||||||
|
Connected, // this->broker is connected and circular cnx avoided
|
||||||
|
};
|
||||||
public:
|
public:
|
||||||
MqttBroker(uint16_t port);
|
MqttBroker(uint16_t port);
|
||||||
|
|
||||||
@@ -156,6 +183,9 @@ class MqttBroker
|
|||||||
|
|
||||||
uint8_t port() const { return server.port(); }
|
uint8_t port() const { return server.port(); }
|
||||||
|
|
||||||
|
void connect(std::string host, uint32_t port=1883);
|
||||||
|
bool connected() const { return state == Connected; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class MqttClient;
|
friend class MqttClient;
|
||||||
|
|
||||||
@@ -166,7 +196,7 @@ class MqttBroker
|
|||||||
{ return compareString(auth_password, password, len); }
|
{ return compareString(auth_password, password, len); }
|
||||||
|
|
||||||
|
|
||||||
void publish(const Topic& topic, MqttMessage& msg);
|
void publish(const MqttClient* source, const Topic& topic, MqttMessage& msg);
|
||||||
|
|
||||||
// For clients that are added not by the broker itself
|
// For clients that are added not by the broker itself
|
||||||
void addClient(MqttClient* client);
|
void addClient(MqttClient* client);
|
||||||
@@ -178,4 +208,7 @@ class MqttBroker
|
|||||||
|
|
||||||
const char* auth_user = "guest";
|
const char* auth_user = "guest";
|
||||||
const char* auth_password = "guest";
|
const char* auth_password = "guest";
|
||||||
|
State state = Disconnected;
|
||||||
|
|
||||||
|
MqttClient* broker = nullptr;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user