diff --git a/examples/uMQTTBrokerSample/uMQTTBrokerSample.ino b/examples/uMQTTBrokerSample/uMQTTBrokerSample.ino index 22afffe..7431079 100644 --- a/examples/uMQTTBrokerSample/uMQTTBrokerSample.ino +++ b/examples/uMQTTBrokerSample/uMQTTBrokerSample.ino @@ -1,74 +1,25 @@ /* * uMQTTBroker demo for Arduino * - * The program starts a broker, subscribes 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. + * Minimal Demo: the program simply starts a broker and waits for any client to connect. */ #include - #include "uMQTTBroker.h" -#include "espconn.h" + +uMQTTBroker myBroker; /* * Your WiFi config here */ -char ssid[] = "ssid"; // your network SSID (name) -char pass[] = "password"; // your network password - - -unsigned int mqttPort = 1883; // the standard MQTT broker port -unsigned int max_subscriptions = 30; -unsigned int max_retained_topics = 30; +char ssid[] = "ssid"; // your network SSID (name) +char pass[] = "password"; // your network password /* - * Data callback: called on any locally subscribed topic + * WiFi init stuff */ - -void data_callback(uint32_t *client /* we can ignore this */, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh) { - char topic_str[topic_len+1]; - os_memcpy(topic_str, topic, topic_len); - topic_str[topic_len] = '\0'; - - char data_str[lengh+1]; - os_memcpy(data_str, data, lengh); - data_str[lengh] = '\0'; - - Serial.println("received topic '"+(String)topic_str+"' with data '"+(String)data_str+"'"); -} - -/* - * Authentication callback: called on any valid connction request - * Returns true, if authorized - */ - -bool auth_data_callback(const char* username, const char *password, struct espconn *pesp_conn){ - Serial.print("Connect from: "); - Serial.print((String)pesp_conn->proto.tcp->remote_ip[0]+"."); - Serial.print((String)pesp_conn->proto.tcp->remote_ip[1]+"."); - Serial.print((String)pesp_conn->proto.tcp->remote_ip[2]+"."); - Serial.println((String)pesp_conn->proto.tcp->remote_ip[3]); - - Serial.println("Username/Pasword: "+(String)username+"/"+(String)password); - -/* - * Implement this,if you want to have username/password authentication - */ -// if ((String)username == "root" && (String)password == "admin" ) { -// return true; -// }else{ -// return false; -// } - return true; -} - -void setup() +void startWiFiClient() { - Serial.begin(115200); - Serial.println(); - Serial.println(); - - // We start by connecting to a WiFi network Serial.println("Connecting to "+(String)ssid); WiFi.begin(ssid, pass); @@ -79,37 +30,36 @@ void setup() Serial.println(""); Serial.println("WiFi connected"); - Serial.println("IP address: "+WiFi.localIP().toString()); - -/* - * Register the callbacks - */ - MQTT_server_onData(data_callback); - MQTT_server_onAuth(auth_data_callback); - -/* - * Start the broker - */ - Serial.println("Starting MQTT broker"); - MQTT_server_start(mqttPort, max_subscriptions, max_retained_topics); - -/* - * Subscribe to anything - */ - MQTT_local_subscribe((unsigned char *)"#", 0); + Serial.println("IP address: " + WiFi.localIP().toString()); } -int counter = 0; +void startWiFiAP() +{ + WiFi.softAP(ssid, pass); + Serial.println("AP started"); + Serial.println("IP address: " + WiFi.softAPIP().toString()); +} + +void setup() +{ + Serial.begin(115200); + Serial.println(); + Serial.println(); + + // Connect to a WiFi network + startWiFiClient(); + + // Or start the ESP as AP +//startWiFiAP(); + + // Start the broker + Serial.println("Starting MQTT broker"); + myBroker.init(); +} void loop() -{ - String myData(counter++); - -/* - * Publish the counter value as String - */ - MQTT_local_publish((unsigned char *)"/MyBroker/count", (unsigned char *)myData.c_str(), myData.length(), 0, 0); - - // wait a second +{ + // do anything here delay(1000); } + diff --git a/examples/uMQTTBrokerSampleOOBasic/uMQTTBrokerSampleOOBasic.ino b/examples/uMQTTBrokerSampleOOBasic/uMQTTBrokerSampleOOBasic.ino new file mode 100644 index 0000000..3d9f6f4 --- /dev/null +++ b/examples/uMQTTBrokerSampleOOBasic/uMQTTBrokerSampleOOBasic.ino @@ -0,0 +1,66 @@ +/* + * uMQTTBroker demo for Arduino + * + * Minimal Demo: the program simply starts a broker and waits for any client to connect. + */ + +#include +#include "uMQTTBroker.h" + +uMQTTBroker myBroker; + +/* + * Your WiFi config here + */ +char ssid[] = "ssid"; // your network SSID (name) +char pass[] = "password"; // your network password +bool WiFiAP = false; // Do yo want the ESP as AP? + +/* + * WiFi init stuff + */ +void startWiFiClient() +{ + Serial.println("Connecting to "+(String)ssid); + 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()); +} + +void startWiFiAP() +{ + WiFi.softAP(ssid, pass); + Serial.println("AP started"); + Serial.println("IP address: " + WiFi.softAPIP().toString()); +} + +void setup() +{ + Serial.begin(115200); + Serial.println(); + Serial.println(); + + // Start WiFi + if (WiFiAP) + startWiFiAP(); + else + startWiFiClient(); + + // Start the broker + Serial.println("Starting MQTT broker"); + myBroker.init(); +} + +void loop() +{ + // do anything here + delay(1000); +} + diff --git a/examples/uMQTTBrokerSampleOOFull/uMQTTBrokerSampleOOFull.ino b/examples/uMQTTBrokerSampleOOFull/uMQTTBrokerSampleOOFull.ino new file mode 100644 index 0000000..cb2aca6 --- /dev/null +++ b/examples/uMQTTBrokerSampleOOFull/uMQTTBrokerSampleOOFull.ino @@ -0,0 +1,105 @@ +/* + * uMQTTBroker demo for Arduino (C++-style) + * + * 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 +#include "uMQTTBroker.h" + +/* + * Your WiFi config here + */ +char ssid[] = "ssid"; // your network SSID (name) +char pass[] = "password"; // your network password +bool WiFiAP = false; // Do yo want the ESP as AP? + +/* + * 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 bool onAuth(String username, String password) { + Serial.println("Username/Password: "+username+"/"+password); + return true; + } + + virtual bool 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+"'"); + } +}; + +myMQTTBroker myBroker; + +/* + * WiFi init stuff + */ +void startWiFiClient() +{ + Serial.println("Connecting to "+(String)ssid); + 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()); +} + +void startWiFiAP() +{ + WiFi.softAP(ssid, pass); + Serial.println("AP started"); + Serial.println("IP address: " + WiFi.softAPIP().toString()); +} + +void setup() +{ + Serial.begin(115200); + Serial.println(); + Serial.println(); + + // Start WiFi + if (WiFiAP) + startWiFiAP(); + else + startWiFiClient(); + + // Start the broker + Serial.println("Starting MQTT broker"); + myBroker.init(); + +/* + * Subscribe to anything + */ + myBroker.subscribe("#"); +} + +int counter = 0; + +void loop() +{ +/* + * Publish the counter value as String + */ + myBroker.publish("broker/counter", (String)counter++); + + // wait a second + delay(1000); +} + diff --git a/src/uMQTTBroker.cpp b/src/uMQTTBroker.cpp new file mode 100644 index 0000000..dfeeda2 --- /dev/null +++ b/src/uMQTTBroker.cpp @@ -0,0 +1,71 @@ + +#include "uMQTTBroker.h" +#include "espconn.h" + +uMQTTBroker *uMQTTBroker::TheBroker; + + bool uMQTTBroker::_onConnect(struct espconn *pesp_conn, uint16_t client_count) { + IPAddress connAddr(pesp_conn->proto.tcp->remote_ip[0], pesp_conn->proto.tcp->remote_ip[1], + pesp_conn->proto.tcp->remote_ip[2], pesp_conn->proto.tcp->remote_ip[3]); + + return TheBroker->onConnect(connAddr, client_count); + } + + bool uMQTTBroker::_onAuth(const char* username, const char *password, struct espconn *pesp_conn) { + return TheBroker->onAuth((String)username, (String)password); + } + + void uMQTTBroker::_onData(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t length) { + char topic_str[topic_len+1]; + + os_memcpy(topic_str, topic, topic_len); + topic_str[topic_len] = '\0'; + TheBroker->onData((String)topic_str, data, length); + } + + uMQTTBroker::uMQTTBroker(uint16_t portno, uint16_t max_subscriptions, uint16_t max_retained_topics) { + TheBroker = this; + _portno = portno; + _max_subscriptions = max_subscriptions; + _max_retained_topics = max_retained_topics; + + MQTT_server_onConnect(_onConnect); + MQTT_server_onAuth(_onAuth); + MQTT_server_onData(_onData); + } + + void uMQTTBroker::init() { + MQTT_server_start(_portno, _max_subscriptions, _max_retained_topics); + } + + bool uMQTTBroker::onConnect(IPAddress addr, uint16_t client_count) { + return true; + } + + bool uMQTTBroker::onAuth(String username, String password) { + return true; + } + + bool uMQTTBroker::onData(String topic, const char *data, uint32_t length) { + } + + bool uMQTTBroker::publish(String topic, uint8_t* data, uint16_t data_length, uint8_t qos, uint8_t retain) { + return MQTT_local_publish((uint8_t*)topic.c_str(), data, data_length, qos, retain); + } + + bool uMQTTBroker::publish(String topic, String data, uint8_t qos, uint8_t retain) { + return MQTT_local_publish((uint8_t*)topic.c_str(), (uint8_t*)data.c_str(), data.length(), qos, retain); + } + + bool uMQTTBroker::subscribe(String topic, uint8_t qos) { + return MQTT_local_subscribe((uint8_t*)topic.c_str(), qos); + } + + bool uMQTTBroker::unsubscribe(String topic) { + return MQTT_local_unsubscribe((uint8_t*)topic.c_str()); + } + + void uMQTTBroker::cleanupClientConnections() { + MQTT_server_cleanupClientCons(); + } + diff --git a/src/uMQTTBroker.h b/src/uMQTTBroker.h index e77a5fb..fde40c2 100644 --- a/src/uMQTTBroker.h +++ b/src/uMQTTBroker.h @@ -2,6 +2,9 @@ #define _MQTT_SERVER_H_ #include "user_interface.h" +#include "IPAddress.h" +#include "string.h" + extern "C" { // Interface for starting the broker @@ -37,4 +40,33 @@ int serialize_retainedtopics(char *buf, int len); bool deserialize_retainedtopics(char *buf, int len); } +class uMQTTBroker +{ +private: + static uMQTTBroker *TheBroker; + uint16_t _portno; + uint16_t _max_subscriptions; + uint16_t _max_retained_topics; + + static bool _onConnect(struct espconn *pesp_conn, uint16_t client_count); + static bool _onAuth(const char* username, const char *password, struct espconn *pesp_conn); + static void _onData(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t length); + +public: + uMQTTBroker(uint16_t portno=1883, uint16_t max_subscriptions=30, uint16_t max_retained_topics=30); + + void init(); + + virtual bool onConnect(IPAddress addr, uint16_t client_count); + virtual bool onAuth(String username, String password); + virtual bool onData(String topic, const char *data, uint32_t length); + + virtual bool publish(String topic, uint8_t* data, uint16_t data_length, uint8_t qos=0, uint8_t retain=0); + virtual bool publish(String topic, String data, uint8_t qos=0, uint8_t retain=0); + virtual bool subscribe(String topic, uint8_t qos=0); + virtual bool unsubscribe(String topic); + + void cleanupClientConnections(); +}; + #endif /* _MQTT_SERVER_H_ */